#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e2 + 10; n const int INF = 1 << 30; n char str[MAXN][MAXN]; n int Pow(int a, int b) { n   if (b == 0) return 1; n   int res = 1; n   while (b--) res *= a; n   return res; n } n int main() { n   int a, b, n; n   while (scanf( %d%d%d , &a, &b, &n) != EOF) { n     if (a == 0) { n       if (b == 0) n         printf( 1 n ); n       else n         printf( No solution n ); n       continue; n     } n     if (b % a) { n       printf( No solution n ); n       continue; n     } n     a = b / a; n     int res = -INF; n     if (a < 0) { n       a = -a; n       if (n % 2 == 0) { n         printf( No solution n ); n         continue; n       } n       for (int i = 0; i <= 1000; i++) { n         if (Pow(i, n) == a) { n           res = i; n           break; n         } n         if (Pow(i, n) > a) break; n       } n       res = -res; n     } else { n       for (int i = 0; i <= 1000; i++) { n         if (Pow(i, n) == a) { n           res = i; n           break; n         } n         if (Pow(i, n) > a) break; n       } n     } n     if (res == -INF) { n       printf( No solution n ); n     } else { n       printf( %d n , res); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n const int maxn = 2e5 + 10; n int a[maxn]; n char s[maxn]; n const char *t =  LR ; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   scanf( %s , s); n   bool rev = s[0] ==  R ; n   int cnt = 0; n   for (int i = 1; i < n; i++) { n     cnt += s[i] == s[i - 1]; n   } n   printf( %d %c n , a[cnt], t[rev]); n   int l = cnt, r = cnt; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) n       l--, printf( %d %c n , a[l], t[((cnt - l) & 1) ^ rev]); n     else n       r++, printf( %d %c n , a[r], t[((r - cnt) & 1) ^ rev]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long po(long long x, long long n) { n   long long res = 1; n   while (n > 0) { n     if (n & 1) res = (res * x) % 998244353; n     n = n / 2; n     x = (x * x) % 998244353; n   } n   return res; n } n long long fac[500000 + 1] = {0}; n long long ifac[500000 + 1] = {0}; n void pre() { n   fac[0] = ifac[0] = 1; n   for (int i = 1; i <= 500000; i++) { n     fac[i] = (i * fac[i - 1]) % 998244353; n     ifac[i] = po(fac[i], 998244353 - 2); n   } n } n long long nCr(long long n, long long r) { n   if (r > n || r < 0 || n < 0) return 0; n   return (((fac[n] * ifac[r]) % 998244353) * ifac[n - r]) % 998244353; n } n void func() { n   long long n, k; n   cin >> n >> k; n   long long ans = 0; n   pre(); n   for (int i = 1; i <= n; i++) { n     ans = (ans + nCr(n / i - 1, k - 1)) % 998244353; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) func(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) { n     a[i] = 0; n   } n   long long int x; n   for (long long int i = 0; i < m; i++) { n     cin >> x; n     a[x - 1] += 1; n   } n   long long int min = (m > n) ? m : n; n   for (long long int i = 0; i < n; i++) { n     if (a[i] < min) min = a[i]; n   } n   cout << min << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 11111; n struct dsu { n   int f[555]; n   int c; n   void init() { n     int i; n     c = 0; n     for (i = 1; i <= 505; i++) f[i] = i; n   } n   int find(int a) { return f[a] == a ? a : f[a] = find(f[a]); } n   void Union(int x, int y) { n     x = find(x), y = find(y); n     if (x != y) f[x] = y, c++; n   } n } L[maxn], R[maxn]; n int l[maxn], r[maxn]; n int main() { n   int n, m, i, j, k, a, b; n   scanf( %d%d , &n, &m); n   L[0].init(), R[m + 1].init(); n   for (i = 1; i <= m; i++) scanf( %d%d , &l[i], &r[i]); n   for (i = 1; i <= m; i++) L[i] = L[i - 1], L[i].Union(l[i], r[i]); n   for (i = m; i >= 1; i--) R[i] = R[i + 1], R[i].Union(l[i], r[i]); n   scanf( %d , &k); n   while (k--) { n     scanf( %d%d , &a, &b); n     dsu t = L[a - 1]; n     for (i = 1; i <= n; i++) t.Union(i, R[b + 1].f[i]); n     printf( %d n , n - t.c); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 2; n vector<int> G[MAXN]; n double f[MAXN]; n int siz[MAXN]; n int pa[MAXN]; n void dfs(int u) { n   for (int i = 0; i < G[u].size(); ++i) { n     int v = G[u][i]; n     f[v] = f[u] + 1.0 + (siz[u] - siz[v] - 1.0) / 2.0; n     dfs(v); n   } n } n void solve(int n) { n   for (int i = n; i >= 1; --i) { n     ++siz[i]; n     siz[pa[i]] += siz[i]; n   } n   f[1] = 1; n   dfs(1); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 2; i <= n; ++i) { n     scanf( %d , pa + i); n     G[pa[i]].push_back(i); n   } n   solve(n); n   for (int i = 1; i <= n; ++i) printf( %.15f  , f[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n void sc(int &x) { scanf( %d , &x); } n void sc(int &x, int &y) { scanf( %d%d , &x, &y); } n void sc(int &x, int &y, int &z) { scanf( %d%d%d , &x, &y, &z); } n void sc(long long &x) { scanf( %lld , &x); } n void sc(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } n void sc(long long &x, long long &y, long long &z) { n   scanf( %lld%lld%lld , &x, &y, &z); n } n void sc(char *x) { scanf( %s , x); } n void sc(char *x, char *y) { scanf( %s%s , x, y); } n void sc(char *x, char *y, char *z) { scanf( %s%s%s , x, y, z); } n void out(int x) { printf( %d n , x); } n void out(long long x) { printf( %lld n , x); } n void out(int x, int y) { printf( %d %d n , x, y); } n void out(long long x, long long y) { printf( %lld %lld n , x, y); } n void out(int x, int y, int z) { printf( %d %d %d n , x, y, z); } n void out(long long x, long long y, long long z) { n   printf( %lld %lld %lld n , x, y, z); n } n void ast(long long x, long long l, long long r) { assert(x >= l && x <= r); } n using namespace std; n const int N = 6e3 + 5, mod = 1e9 + 7; n int n, x[N], y[N]; n int vis[2][2]; n int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } n int cal(int i, int j) { n   int xx = abs(x[i] - x[j]), yy = abs(y[i] - y[j]); n   xx = gcd(xx, yy); n   return xx * 2; n } n void sol(int cas) { n   sc(n); n   for (int i = 1; i <= n; i++) sc(x[i], y[i]); n   for (int i = 1; i <= n; i++) x[i] >>= 1, y[i] >>= 1, x[i] &= 1, y[i] &= 1; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     memset(vis, 0, sizeof(vis)); n     for (int j = n; j >= i + 1; j--) { n       for (int h = 0; h <= 1; h++) n         for (int k = 0; k <= 1; k++) { n           int s = x[i] * y[j] + x[j] * y[i] + x[i] * k + h * y[i] + x[j] * k + n                   h * y[j]; n           s &= 1; n           if (s == 0) ans += vis[h][k]; n         } n       vis[x[j]][y[j]]++; n     } n   } n   out(ans); n } n void init() {} n int main() { n   srand(time(0)); n   init(); n   int t = 1, cas = 0; n   while (t--) { n     sol(++cas); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 100009; n int arr[MX]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , arr + i); n   int add = 0; n   for (int i = 0; i < m; ++i) { n     int cmd; n     scanf( %d , &cmd); n     if (cmd == 1) { n       int idx, val; n       scanf( %d%d , &idx, &val); n       --idx; n       arr[idx] = val - add; n     } else if (cmd == 2) { n       int val; n       scanf( %d , &val); n       add += val; n     } else { n       int idx; n       scanf( %d , &idx); n       --idx; n       printf( %d n , arr[idx] + add); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p = 1000000000; n   cin >> n; n   int k[n], i; n   for (i = 0; i < n; i++) { n     cin >> k[i]; n   } n   i = 0; n   int j = 0, sum = 0; n   while (n--) { n     sum = 0; n     while (k[i]--) { n       cin >> j; n       sum = sum + j * 5 + 15; n     } n     i++; n     if (sum < p) { n       p = sum; n     } n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const double eps = 0.000001; n int q(int i, int j, vector<int> &ps) { n   if (i > j) return 0; n   if (i) return ps[j] - ps[i - 1]; n   return ps[j]; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   vector<int> ps(n + 1, 0); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     ps[i + 1] = ps[i] + (int)(s[i] ==  a ); n   } n   for (int i = 0; i < n; i++) { n     for (int j = i; j <= n; j++) { n       int cr = i - q(0, i, ps) + (n - j) - q(j + 1, n, ps) + q(i + 1, j, ps); n       ans = max(ans, n - cr); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n set<int> s; n const int nax = 5e5 + 4; n int link[nax]; n set<pair<int, int>> edge; n vector<vector<int>> group; n inline void make_set(int i) { link[i] = i; } n int Find(int a) { n   if (link[a] == a) return a; n   return (link[a] = Find(link[a])); n } n void Unite(int a, int b) { n   a = Find(a); n   b = Find(b); n   if (a == b) return; n   link[a] = b; n } n int component() { n   set<int> had; n   for (int i = 1; i <= n; ++i) { n     had.insert(Find(i)); n     group[Find(i)].push_back(i); n   } n   return ((int)had.size()); n } n void bfs(int i) { n   queue<int> q; n   q.push(i); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     s.erase(u); n     vector<int> temp; n     for (auto &c : s) { n       if (edge.find({min(u, c), max(u, c)}) == edge.end()) { n         Unite(u, c); n         q.push(c); n         temp.push_back(c); n       } n     } n     for (auto &c : temp) s.erase(c); n   } n } n void solve() { n   cin >> n >> m; n   group.resize(n + 1); n   for (int i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v; n     edge.insert({min(u, v), max(u, v)}); n   } n   for (int i = 1; i <= n; ++i) make_set(i); n   for (int i = 1; i <= n; ++i) s.insert(i); n   for (int i = 1; i <= n; ++i) { n     if (s.find(i) != s.end()) bfs(i); n   } n   cout << component() <<   n ; n   for (int i = 1; i <= n; ++i) { n     if (!group[i].empty()) { n       cout << group[i].size() <<    ; n       for (auto &c : group[i]) cout << c <<    ; n       cout <<   n ; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int n; n vector<pair<int, long long> > fa[100], fb[100]; n long long powmod(long long a, long long k) { n   long long ret = 1; n   while (k) { n     if (k & 1) { n       ret = ret * a % mod; n     } n     a = a * a % mod; n     k >>= 1; n   } n   return ret; n } n long long exgcd(long long a, long long b, long long& x, long long& y) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = exgcd(b, a % b, y, x); n   y -= x * (a / b); n   return d; n } n long long find(const vector<pair<int, long long> >& f, int p) { n   int pos = lower_bound(f.begin(), f.end(), make_pair(p, -1ll)) - f.begin(); n   if (pos != f.size() && f[pos].first == p) { n     return f[pos].second; n   } n   return 0; n } n vector<pair<int, long long> > factor(int x) { n   vector<pair<int, long long> > ret; n   for (int i = 2; i * i <= x; ++i) { n     long long cnt = 0; n     while (x % i == 0) { n       cnt++; n       x /= i; n     } n     if (cnt) { n       ret.push_back(make_pair(i, cnt)); n     } n   } n   if (x > 1) { n     ret.push_back(make_pair(x, 1)); n   } n   return ret; n } n void addfirst(vector<int>& s, const vector<pair<int, long long> >& t) { n   for (int i = 0; i < t.size(); ++i) { n     s.push_back(t[i].first); n   } n } n bool solve() { n   while (n > 1) { n     int cnt = 0; n     for (int i = 0; i < n - 1; ++i) { n       vector<int> v; n       addfirst(v, fa[i]); n       addfirst(v, fb[i]); n       addfirst(v, fa[i + 1]); n       addfirst(v, fb[i + 1]); n       sort(v.begin(), v.end()); n       v.erase(unique(v.begin(), v.end()), v.end()); n       long long px = -1, py, sx, sy; n       for (int j = 0; j < v.size(); ++j) { n         long long a1 = find(fa[i], v[j]), a2 = find(fa[i + 1], v[j]); n         long long b1 = find(fb[i], v[j]), b2 = find(fb[i + 1], v[j]); n         long long x, y, dx, dy; n         long long d = exgcd(b1, -b2, x, y); n         if (b1 == 0 && b2 == 0) { n           if (a1 != a2) { n             return false; n           } n           continue; n         } else if (b1 == 0 && a1 >= a2 && (a1 - a2) % b2 == 0) { n           x = 0; n           y = (a1 - a2) / b2; n           dx = 1; n           dy = 0; n         } else if (b2 == 0 && a2 >= a1 && (a2 - a1) % b1 == 0) { n           x = (a2 - a1) / b1; n           y = 0; n           dx = 0; n           dy = 1; n         } else if (d == 0 || (a2 - a1) % d != 0) { n           return false; n         } else { n           x *= (a2 - a1) / d; n           y *= (a2 - a1) / d; n           dx = b2 / abs(d); n           dy = b1 / abs(d); n           if (x > 0 && dx > 0) { n             long long t = (x - 1) / dx + 1; n             x -= t * dx; n             y -= t * dy; n           } n           if (y > 0 && dy > 0) { n             long long t = (y - 1) / dy + 1; n             x -= t * dx; n             y -= t * dy; n           } n           if (x < 0) { n             if (dx == 0) { n               return false; n             } n             long long t = (-x - 1) / dx + 1; n             x += t * dx; n             y += t * dy; n           } n           if (y < 0) { n             if (dy == 0) { n               return false; n             } n             long long t = (-y - 1) / dy + 1; n             x += t * dx; n             y += t * dy; n           } n         } n         if (px == -1) { n           px = x; n           py = y; n           sx = dx; n           sy = dy; n         } else { n           if (sx != 0 || sy != 0) { n             if (sx != dx || sy != dy) { n               long long k1 = n                   ((x - px) * dy - (y - py) * dx) / (sx * dy - sy * dx); n               long long nx = px + sx * k1, ny = py + sy * k1; n               if (nx < x || ny < y || (nx - x) * dy - dx * (ny - y) != 0) { n                 return false; n               } n               px = nx; n               py = ny; n               sx = sy = 0; n             } else if ((px - x) * dy - dx * (py - y) != 0) { n               return false; n             } n           } else if (px < x || py < y || (px - x) * dy - dx * (py - y) != 0) { n             return false; n           } n         } n       } n       if (px != -1) { n         vector<pair<int, long long> > nfa, nfb; n         for (int j = 0; j < v.size(); ++j) { n           long long a1 = find(fa[i], v[j]), b1 = find(fb[i], v[j]); n           if (b1 * px + a1) { n             nfa.push_back(make_pair(v[j], b1 * px + a1)); n           } n           if (b1 * sx) { n             nfb.push_back(make_pair(v[j], b1 * sx)); n           } n         } n         fa[cnt] = nfa; n         fb[cnt++] = nfb; n       } n     } n     n = cnt; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     fa[i] = factor(a); n     fb[i] = factor(b); n   } n   if (solve()) { n     long long ans = 1; n     for (int i = 0; i < fa[0].size(); ++i) { n       ans = ans * powmod(fa[0][i].first, fa[0][i].second) % mod; n     } n     printf( %I64d n , ans); n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(m); n   int an; n   cin >> an; n   for (int i = 0; i < an; ++i) { n     int t; n     cin >> t; n     a[t] = 1; n   } n   int bn; n   cin >> bn; n   for (int i = 0; i < bn; ++i) { n     int t; n     cin >> t; n     b[t] = 1; n   } n   int lim = n * m; n   while (1) { n     bool ch = false; n     for (int i = 0; i < lim; ++i) { n       if (a[i % n] != b[i % m]) { n         ch = true; n         a[i % n] = b[i % m] = 1; n       } n     } n     if (!ch) break; n   } n   bool ans = true; n   for (int i = 0; i < n; ++i) ans &= a[i]; n   for (int i = 0; i < m; ++i) ans &= b[i]; n   cout << (ans ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, out; n   cin >> s; n   for (int i = 0; s[i] !=   0 ; i++) { n     s[i] = tolower(s[i]); n     if (s[i] ==  o  || s[i] ==  u  || s[i] ==  i  || s[i] ==  e  || n         s[i] ==  a  || s[i] ==  y ) n       ; n     else { n       out +=  . ; n       out += s[i]; n     } n   } n   cout << out; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int ans = 0, flag = 1; n   for (int i = 0; i < n; i++) { n     if (s[i] !=  ? ) { n       if (s[i] == s[i + 1]) { n         flag = 0; n       } n     } else if (i == 0 || i == n - 1 || s[i + 1] == s[i - 1] || s[i + 1] ==  ? ) n       ans = 1; n   } n   if (flag && ans) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50 + 10; n int n, m, vis[maxn][maxn], need[maxn][maxn]; n struct node { n   int x, y, a, b; n } p[maxn]; n vector<node> vec; n inline void move(int id, int x, int y) { n   vec.push_back((node){p[id].x, p[id].y, p[id].x + x, p[id].y + y}); n   vis[p[id].x][p[id].y] = 0; n   p[id].x += x; n   p[id].y += y; n   vis[p[id].x][p[id].y] = id; n } n inline void work1() { n   int cnt = 0; n   for (int i = 1; i <= n; ++i) n     if (vis[1][i]) { n       ++cnt; n       int id = vis[1][i]; n       while (p[id].y > cnt) move(id, 0, -1); n     } n   for (int i = 2; i <= n; ++i) n     for (int j = n; j; --j) n       if (vis[i][j]) { n         ++cnt; n         int id = vis[i][j]; n         while (p[id].y < cnt) move(id, 0, 1); n         while (p[id].x > 1) move(id, -1, 0); n         while (p[id].y > cnt) move(id, 0, -1); n       } n } n inline void work2() { n   int cnt = 0; n   for (int i = n; i > 2; --i) n     for (int j = n; j; --j) n       if (need[i][j]) { n         int id = need[i][j]; n         move(id, 1, 0); n         while (p[id].y < j) move(id, 0, 1); n         while (p[id].y > j) move(id, 0, -1); n         while (p[id].x < i) move(id, 1, 0); n       } n   for (int i = 1; i <= n; ++i) n     if (vis[1][i]) { n       ++cnt; n       int id = vis[1][i]; n       while (p[id].y > cnt) move(id, 0, -1); n     } n   for (int i = n, cntt = cnt; i; --i) n     for (int j = 1; j <= 2; ++j) n       if (need[j][i]) { n         int id = need[j][i]; n         move(id, 1, 0); n         for (int k = p[id].y + 1; k <= cntt; ++k) move(vis[1][k], 0, -1); n         while (p[id].y < cntt) move(id, 0, 1); n         move(id, -1, 0); n         --cntt; n       } n   for (int i = 1; i <= cnt; ++i) n     if (p[vis[1][i]].a == 2) move(vis[1][i], 1, 0); n   for (int i = 1; i <= 2; ++i) { n     for (int j = 1; j <= n; ++j) n       if (vis[i][j]) { n         int id = vis[i][j]; n         while (p[id].y > p[id].b) move(id, 0, -1); n       } n     for (int j = n; j; --j) n       if (vis[i][j]) { n         int id = vis[i][j]; n         while (p[id].y < p[id].b) move(id, 0, 1); n       } n   } n } n int main() { n   cin >> n >> m; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> p[i].x >> p[i].y; n     vis[p[i].x][p[i].y] = i; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> p[i].a >> p[i].b; n     need[p[i].a][p[i].b] = i; n   } n   work1(); n   work2(); n   cout << vec.size() << endl; n   for (int i = 0; i < vec.size(); ++i) n     cout << vec[i].x <<     << vec[i].y <<     << vec[i].a <<     << vec[i].b n          << endl; n   return 0; n } n


#include<cmath> n #include<iostream> n #include<sstream> n #include<cstdio> n #include<cstring> n #include<string> n #include<cstdlib> n #include<istream> n #include<vector> n #include<stack> n #include<set> n //#include<unordered_map> n #include<map> n #include<algorithm> n #include<queue> n #define mmp make_pair n #define inf 0x3f3f3f3f n #define llinf 0x7fffffffffffffff n using namespace std; n typedef long long ll; n typedef pair<ll,ll> PP; n typedef double ld; n vector<int>hh[200010]; n int ID[200010]; n bool orz[200010]; n ll siz[200010]; n queue<int>q; n void topsort(int n) { n     while(!q.empty()) q.pop(); n     for(int i=1;i<=n;++i) { n         if(ID[i]==1) { n             q.push(i); n             orz[i]=true; n         } n     } n     while(!q.empty()) { n         int temp=q.front(); n         q.pop(); n         orz[temp]=true; n         for(int i=0;i<hh[temp].size();++i) { n             int uu=hh[temp][i]; n             if(orz[uu]) continue; n             ID[uu]--; n             ID[temp]--; n             if(ID[uu]==1) { n                 q.push(uu); n             } n         } n     } n } n void dfs(int u,int f) { n     siz[u]=1; n     for(int i=0;i<hh[u].size();++i) { n         int v=hh[u][i]; n         if(orz[v]==0 || v==f) continue; n         dfs(v,u); n         siz[u]+=siz[v]; n     } n } n int main() { n     int T; n     scanf( %d ,&T); n     while(T--) { n         int n; n         scanf( %d ,&n); n         for(int i=1;i<=n;++i) { n             hh[i].clear(); n             ID[i]=orz[i]=siz[i]=0; n         } n         for(int i=1;i<=n;++i) { n             int x,y; scanf( %d %d ,&x,&y); n             hh[x].push_back(y); hh[y].push_back(x); n             ++ID[x]; ++ID[y]; n         } n         topsort(n); n         ll cnt=0; n         for(int i=1;i<=n;++i) { n             if(orz[i]==0) { n                 ++cnt; n                 dfs(i,0); n             } n         } n         ll sum=0; n         ll ans=cnt*(cnt-1); n         for(int i=1;i<=n;++i) { n             if(!orz[i]) { n                 ans+=1ll*(cnt-1)*2*(siz[i]-1); n                 ans+=1ll*(siz[i]-1)*siz[i]/2; n                 sum+=siz[i]-1; n             } n         } n         for(int i=1;i<=n;++i) { n             if(!orz[i]) { n                 ans+=1ll*(sum-siz[i]+1)*(siz[i]-1); n             } n         } n         printf( %lld n ,ans); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mod = 1000000007; n const int inv3 = (mod + 1) / 3; n const int maxn = 3003003; n int fac[maxn], ifac[maxn]; n int qpow(int a, int n) { n   int ans = 1; n   for (; n; n >>= 1, a = (ll)a * a % mod) n     if (n & 1) ans = (ll)ans * a % mod; n   return ans; n } n int C(int n, int m) { n   return fac[n] * (ll)ifac[m] % mod * (ll)ifac[n - m] % mod; n } n int f[maxn], g[maxn]; n int main(void) { n   int n, q; n   scanf( %d%d , &n, &q); n   fac[0] = 1; n   for (int i = 1; i <= 3 * n + 3; i++) fac[i] = fac[i - 1] * (ll)i % mod; n   ifac[3 * n + 3] = qpow(fac[3 * n + 3], mod - 2); n   for (int i = 3 * n + 2; i >= 0; i--) n     ifac[i] = ifac[i + 1] * (ll)(i + 1) % mod; n   for (int i = 0; i < 3 * n + 3; i++) f[i] = C(3 * n + 3, i + 1); n   g[0] = n + 1; n   g[1] = 3ll * n * (n + 1) / 2 % mod; n   for (int i = 2; i <= 3 * n; i++) { n     g[i] = ((f[i] - 3ll * g[i - 1] % mod + mod) % mod - g[i - 2] + mod) % mod * n            inv3 % mod; n   } n   for (int i = 0; i < q; i++) { n     int x; n     scanf( %d , &x); n     printf( %d n , g[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string mt[1226], sc[1226], s[51], q1, q2; n int n, k1, k2, c; n map<string, int> O, Z, P; n pair<pair<pair<int, int>, int>, string> p[51]; n vector<string> v; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   for (int i = 1; i <= n * (n - 1) / 2; i++) { n     cin >> mt[i] >> sc[i]; n     c = 0; n     k1 = 0; n     k2 = 0; n     q1 =   ; n     q2 =   ; n     for (int j = 0; j < sc[i].size(); j++) { n       if (sc[i][j] ==  : ) { n         c++; n         continue; n       } n       if (c == 0) { n         k1 += sc[i][j] -  0 ; n         k1 *= 10; n       } else { n         k2 += sc[i][j] -  0 ; n         k2 *= 10; n       } n     } n     k1 /= 10; n     k2 /= 10; n     c = 0; n     for (int j = 0; j < mt[i].size(); j++) { n       if (mt[i][j] ==  - ) { n         c++; n         continue; n       } n       if (c == 0) { n         q1 += mt[i][j]; n       } n       if (c == 1) { n         q2 += mt[i][j]; n       } n     } n     if (k1 > k2) { n       O[q1] += 3; n     } else if (k1 < k2) { n       O[q2] += 3; n     } else { n       O[q1]++; n       O[q2]++; n     } n     Z[q1] += k1; n     Z[q2] += k2; n     P[q1] += k2; n     P[q2] += k1; n   } n   for (int i = 1; i <= n; i++) { n     p[i] = make_pair(make_pair(make_pair(O[s[i]], Z[s[i]] - P[s[i]]), Z[s[i]]), n                      s[i]); n   } n   sort(p + 1, p + 1 + n); n   for (int i = n; i >= n / 2 + 1; i--) { n     v.push_back(p[i].second); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n vector<int> l, r, x, y; n vector<long long> sum; n double ans(int x1, int y1, int x, int y) { n   long long a = y1 - y; n   long long b = x - x1; n   long long c = -a * x - b * y; n   double xs = -1.0 * c / a; n   int ll = 0, rr = n - 1; n   while (ll <= rr) { n     int md = (ll + rr) / 2; n     if (r[md] < xs) n       ll = md + 1; n     else n       rr = md - 1; n   } n   if (ll == n) return sum[n - 1]; n   double ans = 0; n   if (ll != 0) ans = sum[ll - 1]; n   if (l[ll] < xs) ans += xs - l[ll]; n   return ans; n } n int main() { n   int y1, a, b; n   cin >> y1 >> a >> b; n   cin >> n; n   l.resize(n); n   r.resize(n); n   for (int i = 0; i < n; ++i) cin >> l[i] >> r[i]; n   cin >> q; n   x.resize(q); n   y.resize(q); n   for (int i = 0; i < q; ++i) cin >> x[i] >> y[i]; n   sum.resize(n); n   sum[0] = r[0] - l[0]; n   for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + r[i] - l[i]; n   for (int i = 0; i < q; ++i) { n     cout << fixed << setprecision(10) n          << (ans(b, y1, x[i], y[i]) - ans(a, y1, x[i], y[i])) * (y[i] - y1) / n                 y[i] n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000009; n struct mat22 { n   long long M[2][2]; n   mat22(int a) { M[0][0] = M[1][1] = 1, M[0][1] = M[1][0] = 0; } n   mat22() { M[0][0] = M[1][1] = 0, M[0][1] = M[1][0] = 0; } n }; n mat22 pw[500000]; n mat22 Spw[500000]; n struct node { n   int z, k; n   long long val, Aup, Bup; n   int son[2]; n }; n struct intervalac { n   vector<node> T; n   void constI(int akt) { n     node n = T[akt]; n     if (n.k - n.z == 1) return; n     for (int i = 0; i < 2; i++) { n       if (i == 0) n         n.k = (n.z + n.k) / 2; n       else { n         n.z = n.k; n         n.k = T[akt].k; n       } n       T[akt].son[i] = T.size(); n       T.push_back(n); n       constI(T.size() - 1); n     } n   } n   intervalac(int N) { n     node n; n     n.son[0] = n.son[1] = -1; n     n.z = n.val = n.Aup = n.Bup = 0, n.k = N; n     T.reserve(2 * N + 47); n     T.push_back(n); n     constI(0); n   } n   void upd(int akt) { n     node n = T[akt]; n     if (n.Aup == 0 && n.Bup == 0) return; n     T[akt].val = (T[akt].val + Spw[n.k - n.z].M[1][0] * n.Bup + n                   Spw[n.k - n.z].M[1][1] * n.Aup) % n                  mod; n     if (n.son[0] != -1) { n       T[n.son[0]].Bup = (T[n.son[0]].Bup + n.Bup) % mod; n       if (T[n.son[0]].Bup < 0) T[n.son[0]].Bup += mod; n       T[n.son[0]].Aup = (T[n.son[0]].Aup + n.Aup) % mod; n       if (T[n.son[0]].Aup < 0) T[n.son[0]].Aup += mod; n     } n     if (n.son[1] != -1) { n       T[n.son[1]].Bup = n           (T[n.son[1]].Bup + pw[(n.k + n.z) / 2 - n.z].M[0][0] * n.Bup + n            pw[(n.k + n.z) / 2 - n.z].M[0][1] * n.Aup) % n           mod; n       if (T[n.son[1]].Bup < 0) T[n.son[1]].Bup += mod; n       T[n.son[1]].Aup = n           (T[n.son[1]].Aup + pw[(n.k + n.z) / 2 - n.z].M[1][0] * n.Bup + n            pw[(n.k + n.z) / 2 - n.z].M[1][1] * n.Aup) % n           mod; n       if (T[n.son[1]].Aup < 0) T[n.son[1]].Aup += mod; n     } n     T[akt].Aup = T[akt].Bup = 0; n   } n   void put(int akt, int zac, int kon, long long a, long long b) { n     node n = T[akt]; n     if (n.z >= kon || zac >= n.k) return; n     if (n.z == zac && n.k == kon) { n       T[akt].Aup = (T[akt].Aup + a) % mod; n       if (T[akt].Aup < 0) T[akt].Aup += mod; n       T[akt].Bup = (T[akt].Bup + b) % mod; n       if (T[akt].Bup < 0) T[akt].Bup += mod; n       return; n     } n     if (zac < T[n.son[0]].k) { n       put(n.son[0], zac, min(T[n.son[0]].k, kon), a, b); n       long long Bn = (pw[T[n.son[0]].k - zac].M[0][0] * b + n                       pw[T[n.son[1]].z - zac].M[0][1] * a) % n                      mod; n       if (Bn < 0) Bn += mod; n       long long An = (pw[T[n.son[0]].k - zac].M[1][0] * b + n                       pw[T[n.son[1]].z - zac].M[1][1] * a) % n                      mod; n       if (An < 0) An += mod; n       put(n.son[1], max(T[n.son[1]].z, zac), kon, An, Bn); n     } else n       put(n.son[1], max(T[n.son[1]].z, zac), kon, a, b); n     T[akt].val = n         (T[akt].val + Spw[kon - zac].M[1][0] * b + Spw[kon - zac].M[1][1] * a) % n         mod; n   } n   long long get(int akt, int zac, int kon) { n     node n = T[akt]; n     if (n.k <= zac || kon <= n.z) return 0; n     upd(akt); n     if (n.z == zac && n.k == kon) return T[akt].val; n     return (get(n.son[0], zac, min(T[n.son[0]].k, kon)) + n             get(n.son[1], max(T[n.son[1]].z, zac), kon)) % n            mod; n   } n }; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int N, Q; n   cin >> N >> Q; n   vector<int> A(N + 1, 0); n   for (int i = 0; i < N; i++) cin >> A[i + 1]; n   for (int i = 0; i < N; i++) A[i + 1] = (A[i] + A[i + 1]) % mod; n   pw[0] = mat22(1); n   pw[1] = mat22(1); n   pw[1].M[0][1] = pw[1].M[1][0] = 1, pw[1].M[1][1] = 0; n   for (int i = 2; i < 400000; i++) { n     for (int j = 0; j < 2; j++) n       for (int k = 0; k < 2; k++) { n         pw[i].M[j][k] = 0; n         for (int l = 0; l < 2; l++) n           pw[i].M[j][k] += (pw[i - 1].M[j][l] * pw[1].M[l][k]) % mod; n         pw[i].M[j][k] %= mod; n         if (pw[i].M[j][k] < 0) pw[i].M[j][k] += mod; n       } n   } n   Spw[0] = mat22(); n   for (int i = 1; i < 400000; i++) n     for (int j = 0; j < 2; j++) n       for (int k = 0; k < 2; k++) { n         Spw[i].M[j][k] = (Spw[i - 1].M[j][k] + pw[i - 1].M[j][k]) % mod; n         if (Spw[i].M[j][k] < 0) Spw[i].M[j][k] += mod; n       } n   intervalac I(N); n   for (int q = 0; q < Q; q++) { n     int t, l, r; n     cin >> t >> l >> r; n     l--; n     if (t == 2) { n       long long ans = (A[r] - A[l] + I.get(0, l, r)) % mod; n       if (ans < 0) ans += mod; n       cout << ans <<   n ; n       continue; n     } n     I.put(0, l, r, 1, 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n long long n, s, ans, ans2, ret; n struct Node { n   long long val, tmp; n   int id; n } node[N]; n bool cmp(Node A, Node B) { return A.tmp < B.tmp; } n bool check(int x) { n   for (int i = 1; i <= n; i++) { n     node[i].tmp = node[i].val + node[i].id * 1ll * x; n   } n   sort(node + 1, node + 1 + n, cmp); n   ret = 0; n   for (int i = 1; i <= x; i++) { n     ret += node[i].tmp; n   } n   if (ret <= s) { n     return 1; n   } n   return 0; n } n int main() { n   scanf( %lld%lld , &n, &s); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &node[i].val); n     node[i].id = i; n   } n   int L = 0, R = n, mid; n   while (L <= R) { n     mid = (L + R) >> 1; n     if (check(mid)) { n       ans = mid; n       ans2 = ret; n       L = mid + 1; n     } else { n       R = mid - 1; n     } n   } n   cout << ans <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int val; n   int mx[32]; n }; n int n, k, p; n vector<int> a[200005]; n node st[4 * 200005]; n int b[5]; n void build(int si, int ss, int se) { n   if (ss == se) { n     st[si].val = 0; n     for (int i = 0; i < p; i++) { n       int val = 0; n       for (int j = 0; j < k; j++) { n         if (i & (1 << j)) { n           val += a[ss][j]; n         } else { n           val -= a[ss][j]; n         } n       } n       st[si].mx[i] = val; n     } n     return; n   } n   int mid = (ss + se) >> 1; n   build(2 * si + 1, ss, mid); n   build(2 * si + 2, mid + 1, se); n   st[si].val = max(st[2 * si + 1].val, st[2 * si + 2].val); n   for (int i = 0; i < p; i++) { n     st[si].mx[i] = max(st[2 * si + 1].mx[i], st[2 * si + 2].mx[i]); n     st[si].val = n         max(st[si].val, st[2 * si + 1].mx[i] + st[2 * si + 2].mx[(p - 1) ^ i]); n   } n   return; n } n void update(int si, int ss, int se, int ind) { n   if (ind < ss || ind > se) { n     return; n   } n   if (ss == se) { n     st[si].val = 0; n     for (int i = 0; i < p; i++) { n       int val = 0; n       for (int j = 0; j < k; j++) { n         if (i & (1 << j)) { n           val += b[j]; n         } else { n           val -= b[j]; n         } n       } n       st[si].mx[i] = val; n     } n     return; n   } n   int mid = (ss + se) >> 1; n   update(2 * si + 1, ss, mid, ind); n   update(2 * si + 2, mid + 1, se, ind); n   st[si].val = max(st[2 * si + 1].val, st[2 * si + 2].val); n   for (int i = 0; i < p; i++) { n     st[si].mx[i] = max(st[2 * si + 1].mx[i], st[2 * si + 2].mx[i]); n     st[si].val = n         max(st[si].val, st[2 * si + 1].mx[i] + st[2 * si + 2].mx[(p - 1) ^ i]); n   } n   return; n } n node query(int si, int ss, int se, int l, int r) { n   if (r < ss || l > se) { n     node ans; n     ans.val = -(k * 1e6 + 5); n     for (int i = 0; i < p; i++) { n       ans.mx[i] = -k * 1e6 + 5; n     } n     return ans; n   } n   if (ss >= l && se <= r) { n     return st[si]; n   } n   int mid = (ss + se) >> 1; n   node x = query(2 * si + 1, ss, mid, l, r); n   node y = query(2 * si + 2, mid + 1, se, l, r); n   node ans; n   ans.val = max(x.val, y.val); n   for (int i = 0; i < p; i++) { n     ans.mx[i] = max(x.mx[i], y.mx[i]); n     ans.val = max(ans.val, x.mx[i] + y.mx[(p - 1) ^ i]); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   p = (1 << k); n   for (int i = 0; i < n; i++) { n     a[i].resize(k); n     for (int j = 0; j < k; j++) { n       cin >> a[i][j]; n     } n   } n   build(0, 0, n - 1); n   int q; n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int ind; n       cin >> ind; n       ind--; n       for (int i = 0; i < k; i++) { n         cin >> b[i]; n       } n       update(0, 0, n - 1, ind); n     } else { n       int l, r; n       cin >> l >> r; n       l--, r--; n       cout << query(0, 0, n - 1, l, r).val << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n struct Seg { n   const T ID = 0; n   T comb(T a, T b) { return a + b; } n   long long n; n   vector<T> seg; n   void init(long long _n) { n     n = _n; n     seg.assign(2 * n, ID); n   } n   void pull(long long p) { seg[p] = comb(seg[2 * p], seg[2 * p + 1]); } n   void upd(long long p, T val) { n     seg[p += n] = val; n     for (p /= 2; p; p /= 2) pull(p); n   } n   T query(long long l, long long r) { n     T ra = ID, rb = ID; n     for (l += n, r += n + 1; l < r; l /= 2, r /= 2) { n       if (l & 1) ra = comb(ra, seg[l++]); n       if (r & 1) rb = comb(seg[--r], rb); n     } n     return comb(ra, rb); n   } n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long m; n   cin >> m; n   string s; n   cin >> s; n   long long n = s.size(); n   long long a[n]; n   for (long long i = 0; i < n; ++i) { n     a[i] = s[i] -  a ; n   } n   vector<long long> ans; n   set<array<long long, 2>> ranges; n   Seg<long long> st; n   st.init(n); n   for (long long i = 0; i < n; ++i) { n     if (i + m - 1 >= n) break; n     ranges.insert({i, i + m - 1}); n   } n   for (long long i = 0; i < 26; ++i) { n     bool fin = 1; n     for (long long j = 0; j < n; ++j) { n       if (a[j] == i) { n         st.upd(j, 1); n       } n     } n     for (auto [l, r] : ranges) { n       fin &= st.query(l, r) > 0; n     } n     if (fin) { n       vector<long long> pos; n       for (long long j = 0; j < n; ++j) { n         if (a[j] == i) { n           st.upd(j, 0); n           pos.push_back(j); n         } n       } n       for (auto [l, r] : ranges) { n         if (st.query(l, r)) continue; n         auto lb = --(lower_bound((pos).begin(), (pos).end(), r + 1)); n         st.upd(*lb, 1); n         ans.push_back(i); n       } n       break; n     } else { n       for (long long j = 0; j < n; ++j) { n         if (a[j] == i) { n           ans.push_back(i); n         } n       } n       vector<array<long long, 2>> rem; n       for (auto [l, r] : ranges) { n         if (st.query(l, r)) { n           rem.push_back({l, r}); n         } n       } n       for (auto [l, r] : rem) { n         ranges.erase({l, r}); n       } n     } n   } n   for (long long i : ans) { n     cout << (char)(i +  a ); n   } n   cout <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <class T> n void in(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   if (f) x = -x; n } n char s[1005]; n int n, p; n char can(int x) { n   for (register int i = s[x] + 1; i <=  a  + p - 1; ++i) n     if (i != s[x - 1] && i != s[x - 2]) return i; n   return 0; n } n signed main() { n   in(n), in(p); n   scanf( %s , s + 1); n   int pos = 0; n   for (register int i = n; i >= 1; --i) { n     if (can(i)) { n       s[i] = can(i); n       pos = i; n       break; n     } n   } n   if (!pos) { n     puts( NO ); n     return 0; n   } n   for (register int i = pos + 1; i <= n; ++i) { n     for (register int j =  a ; j <=  a  + p - 1; ++j) n       if (j != s[i - 1] && j != s[i - 2]) { n         s[i] = j; n         break; n       } n   } n   for (register int i = 1; i <= n; ++i) putchar(s[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, n; n   cin >> T; n   double R1, R2, R3; n   while (T--) { n     scanf( %lf%lf%d , &R1, &R2, &n); n     R3 = R1 - R2; n     double k1 = -1 / R1; n     double k2 = 1 / R2; n     double k3 = 1 / R3; n     double k4 = k1 + k2 + k3; n     double ans; n     for (int i = 1; i <= n; i++) { n       double R4 = 1 / k4; n       ans = R4; n       double k5 = 2 * (k1 + k2 + k4) - k3; n       k3 = k4; n       k4 = k5; n     } n     printf( %0.10lf n , fabs(ans)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     set<int> s; n     for (int i = 0, x; i < n; i++) { n       cin >> x; n       s.insert(((x + i) % n + n) % n); n     } n     puts(s.size() == n ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[5]; n char s1[3], s2[3]; n void solve() { n   int i, vt, kq1, kq2; n   scanf( %d , &n); n   n = n % 4; n   a[0] =  v ; n   a[1] =  < ; n   a[2] =  ^ ; n   a[3] =  > ; n   for (i = 0; i < 4; i++) n     if (a[i] == s1[0]) vt = i; n   kq1 = 0; n   kq2 = 0; n   if (a[(vt + n) % 4] == s2[0]) kq1 = 1; n   if (a[(vt - n + 4) % 4] == s2[0]) kq2 = 1; n   if (kq1 == 1 && kq2 == 0) printf( cw n ); n   if (kq1 == 0 && kq2 == 1) printf( ccw n ); n   if ((kq1 == 0 && kq2 == 0) || (kq1 == 1 && kq2 == 1)) printf( undefined n ); n } n int main() { n   while (scanf( %s %s , &s1, &s2) > 0) { n     solve(); n   } n   return 0; n } n


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


#include<iostream> n #include<math.h> n #include<bits/stdc++.h> n #include <string>  n #include <vector>  n using namespace std; n #define pi (2*acos(0.0)) n #define ll long long int  n #define ull unsigned long long int  n #define lp(i , start , n) for(int i = start ; i < n; i++) n  n  n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0);  n     int t; cin>>t; n     while(t--){ n         int n; cin>>n; n         if(n==2) cout<<-1<<endl; n         else{ n             int a[n][n]; n             bool odd = true; n             int oddnum= 1; n             int evennum=1; n             int cnt = 0; n  n             for(int i = 0 ; i < n ; i++){ n                 for(int j = 0 ; j < n ; j++){ n                     if(odd){ n                         a[i][j]=2*oddnum-1; n                         oddnum++; n                         cnt++; n                         if(cnt>=(n*n+1)/2){ n                             odd=false; n                         } n                     } n                     else{ n                         a[i][j]=2*evennum; n                         evennum++; n                     } n                 } n             } n  n             for(int i = 0 ; i < n ; i++){ n                 for(int j = 0 ; j < n ; j++){ n                     cout<<a[i][j]<<   ; n                 } n                 cout<<endl; n             } n  n  n         } n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int a[10000000], b[10010000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   int p = 0; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     if (a[i] != b[i]) p++; n   } n   if (p > 2) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_INT = INT32_MAX; n const long long MAX_LL = INT64_MAX; n const long long mod = 1e9 + 7; n const int N = 1; n bool primes[N + 1]; n int npr(int n, int r) { n   int res = 1; n   for (auto i = n - r + 1; i <= n; i++) res *= i; n   return res; n } n int ncr(int n, int r) { n   int res = 1; n   int rFact = 1; n   for (auto i = n - r + 1; i <= n; i++) { n     res *= i; n     if (res % rFact == 0 && rFact <= r) res /= rFact, rFact++; n   } n   return res; n } n void sieve() { n   memset(primes, true, sizeof(primes)); n   primes[0] = primes[1] = false; n   for (int i = 2; i * i <= N; i++) { n     if (primes[i]) { n       for (int j = i * i; j <= N; j += i) primes[j] = false; n     } n   } n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n == 2) return true; n   for (auto i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n set<long long> getDivisors(long long val) { n   set<long long> st; n   for (auto i = 1; i <= sqrt(val); i++) n     if (val % i == 0) st.insert(i), st.insert(val / i); n   return st; n } n long long fastPow(long long base, long long power) { n   long long res = 1; n   while (power > 0) { n     if (power % 2 != 0) res = (res * base) % mod; n     base = (base * base) % mod; n     power /= 2; n   } n   return res; n } n bool palindrome(string s) { n   string reversedS = s; n   reverse(reversedS.begin(), reversedS.end()); n   if (s == reversedS) n     return true; n   else n     return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<long long> v(n), A, B; n   int winner = -1; n   long long mxA = -1, mxB = -1; n   bool lexi = false; n   long long sum = 0; n   for (auto i = 0; i < n; i++) { n     cin >> v[i]; n     sum += v[i]; n     if (v[i] > 0) n       A.push_back(v[i]); n     else n       B.push_back(abs(v[i])); n   } n   for (auto i = 0; i < min(A.size(), B.size()); i++) { n     if (A[i] > B[i]) { n       lexi = true; n       winner = 0; n       break; n     } else if (B[i] > A[i]) { n       lexi = true; n       winner = 1; n       break; n     } n   } n   if (sum == 0) { n     if (lexi) n       cout << ((winner == 0) ?  first  :  second ); n     else n       cout << ((v.back() > 0) ?  first  :  second ); n   } else n     cout << ((sum > 0) ?  first  :  second ); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n FILE* in = stdin; n FILE* out = stdout; n const int MAX = 100001; n const int INF = 1000000001; n int n; n int dist[MAX]; n long long a[MAX]; n int main(void) { n   fscanf(in,  %d , &n); n   for (int i = 0; i < n; i++) fscanf(in,  %lld , &a[i]); n   sort(a, a + n); n   reverse(a, a + n); n   while (n > 0 && a[n - 1] == 0) n--; n   for (int bit = 0; bit < 60; bit++) { n     int cnt = 0; n     for (int i = 0; i < n; i++) n       if (a[i] & (1LL << bit)) cnt++; n     if (cnt >= 3) { n       fprintf(out,  3 n ); n       return 0; n     } n   } n   int ans = INF; n   for (int origin = 0; origin < n; origin++) { n     if (ans <= 3) break; n     memset(dist, 63, sizeof(dist)); n     queue<int> q; n     q.push(origin); n     dist[origin] = 0; n     int curDist = 0; n     while (!q.empty()) { n       curDist++; n       vector<int> nnodes; n       vector<int> cnt(n, 0); n       while (!q.empty()) { n         int node = q.front(); n         q.pop(); n         for (int i = 0; i < n; i++) { n           if (dist[i] > INF && (a[node] & a[i])) { n             dist[i] = curDist; n             nnodes.push_back(i); n           } n           if (dist[i] == curDist && (a[node] & a[i])) cnt[i]++; n         } n       } n       for (int i = 0; i < n; i++) { n         if (cnt[i] >= 2) { n           ans = min(ans, curDist + curDist); n         } n       } n       for (int i1 = 0; i1 < (int)nnodes.size(); i1++) { n         if (ans <= 3) break; n         for (int i2 = i1 + 1; i2 < (int)nnodes.size(); i2++) { n           if (a[nnodes[i1]] & a[nnodes[i2]]) { n             ans = min(ans, curDist + curDist + 1); n           } n         } n       } n       for (int i = 0; i < (int)nnodes.size(); i++) { n         q.push(nnodes[i]); n       } n     } n   } n   fprintf(out,  %d n , ans >= INF ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h << endl; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class Iterable, n           class = typename enable_if<!is_same<string, Iterable>::value>::type> n auto operator<<(ostream &os, Iterable v) -> decltype(os << *begin(v)) { n   os <<  [ ; n   for (auto vv : v) os << vv <<  ,  ; n   return os <<  ] ; n } n const int inf = 0x3f3f3f3f; n const long long infll = 0x3f3f3f3f3f3f3f3fll; n template <class T> n int sign(T x) { n   return (x > 0) - (x < 0); n } n template <class T> n T abs(const T &x) { n   return (x < T(0)) ? -x : x; n } n template <class T> n T gcd(T a, T b, T &x, T &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   T x1, y1; n   T d = gcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n template <class T> n T mod_inverse(T a, T m) { n   T x, y; n   assert(gcd(a, m, x, y) == 1); n   return (x % m + m) % m; n } n template <long long mod> n struct mod_num { n   long long x; n   explicit mod_num(long long x = 0) : x(x % mod) {} n   mod_num operator+(mod_num b) { return mod_num(x + b.x); } n   mod_num operator-(mod_num b) { return mod_num(x - b.x + mod); } n   mod_num operator*(mod_num b) { return mod_num(x * b.x); } n   mod_num operator/(mod_num b) { return mod_num(x * mod_inverse(b.x, mod)); } n   mod_num operator^(long long e) { n     mod_num ans(1); n     mod_num b = *this; n     for (; e; b = b * b, e /= 2) n       if (e & 1) ans = ans * b; n     return ans; n   } n   void operator+=(mod_num b) { x = (x + b.x) % mod; } n }; n template <long long mod> n ostream &operator<<(ostream &os, mod_num<mod> x) { n   return os << x.x; n } n vector<int> graph[512345]; n int been[512345]; n vector<int> st; n vector<vector<int>> rings; n vector<pair<int, int>> edges; n mod_num<1000000007> vertex_rings_factor[512345]; n void dfs(int a, int p) { n   been[a] = 1; n   st.push_back(a); n   for (int i = 0; i < ((int)(graph[a]).size()); i++) { n     if (graph[a][i] != p) { n       if (been[graph[a][i]] == 0) n         dfs(graph[a][i], a); n       else if (been[graph[a][i]] == 1) { n         vector<int> tmp; n         for (int j = ((int)(st).size()) - 1; st[j] != graph[a][i]; j--) n           tmp.push_back(st[j]); n         tmp.push_back(graph[a][i]); n         rings.push_back(tmp); n       } n     } n   } n   st.pop_back(); n   been[a] = 2; n } n bool intersect(const vector<int> &v1, const vector<int> &v2) { n   for (int i = 0; i < ((int)(v1).size()); i++) n     if (find(v2.begin(), v2.end(), v1[i]) != v2.end()) return true; n   return false; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n, m, a, b; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     edges.push_back({min(a, b), max(a, b)}); n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   dfs(1, 1); n   vector<mod_num<1000000007>> pot2div(max(11, n + 1)); n   pot2div[0] = mod_num<1000000007>(1); n   pot2div[1] = pot2div[0] / mod_num<1000000007>(2); n   for (int i = 2; i < ((int)(pot2div).size()); i++) n     pot2div[i] = pot2div[i - 1] * pot2div[1]; n   mod_num<1000000007> sum_rings_factors(0); n   for (int i = 0; i < ((int)(rings).size()); i++) { n     sum_rings_factors += pot2div[((int)(rings[i]).size())]; n     for (int x : rings[i]) { n       vertex_rings_factor[x] += pot2div[((int)(rings[i]).size())]; n     } n   } n   vector<mod_num<1000000007>> intersecting_rings_factors(((int)(rings).size())); n   for (int i = 0; i < ((int)(rings).size()); i++) n     for (int x : rings[i]) n       intersecting_rings_factors[i] += n           vertex_rings_factor[x] - pot2div[((int)(rings[i]).size())]; n   mod_num<1000000007> ex = (mod_num<1000000007>(2) ^ 0) * n                                mod_num<1000000007>(n) / mod_num<1000000007>(2) - n                            mod_num<1000000007>(m) / mod_num<1000000007>(4); n   for (int i = 0; i < ((int)(rings).size()); i++) n     ex += pot2div[((int)(rings[i]).size())]; n   mod_num<1000000007> ex2(0); n   mod_num<1000000007> ev2 = n       mod_num<1000000007>(n) * mod_num<1000000007>(n - 1) / n           (mod_num<1000000007>(2) ^ 2) + n       mod_num<1000000007>(n) / (mod_num<1000000007>(2) ^ 1); n   mod_num<1000000007> ee2(0); n   for (int i = 0; i < m; i++) { n     ee2 += pot2div[2]; n     ee2 += mod_num<1000000007>(((int)(graph[edges[i].first]).size()) + n                                ((int)(graph[edges[i].second]).size()) - 2) * n            pot2div[3]; n     ee2 += n         mod_num<1000000007>(m - (((int)(graph[edges[i].first]).size()) + n                                  ((int)(graph[edges[i].second]).size()) - 1)) * n         pot2div[4]; n   } n   mod_num<1000000007> er2(0); n   for (int i = 0; i < ((int)(rings).size()); i++) { n     er2 += pot2div[((int)(rings[i]).size())]; n     er2 += n         pot2div[((int)(rings[i]).size()) - 1] * intersecting_rings_factors[i]; n     er2 += pot2div[((int)(rings[i]).size())] * n            (sum_rings_factors - intersecting_rings_factors[i] - n             pot2div[((int)(rings[i]).size())]); n   } n   mod_num<1000000007> eve; n   for (int i = 1; i <= n; i++) { n     eve += mod_num<1000000007>(((int)(graph[i]).size())) * pot2div[2]; n     eve += mod_num<1000000007>(m - ((int)(graph[i]).size())) * pot2div[3]; n   } n   mod_num<1000000007> eer; n   for (int j = 0; j < ((int)(rings).size()); j++) { n     int k = ((int)(rings[j]).size()); n     eer += mod_num<1000000007>(((int)(rings[j]).size())) * n            pot2div[((int)(rings[j]).size())]; n     for (int x : rings[j]) { n       eer += mod_num<1000000007>(((int)(graph[x]).size()) - 2) * n              pot2div[((int)(rings[j]).size()) + 1]; n       k += ((int)(graph[x]).size()) - 2; n     } n     eer += mod_num<1000000007>(m - k) * pot2div[((int)(rings[j]).size()) + 2]; n   } n   mod_num<1000000007> evr; n   for (int i = 1; i <= n; i++) { n     evr += vertex_rings_factor[i]; n     evr += (sum_rings_factors - vertex_rings_factor[i]) * pot2div[1]; n   } n   ex2 = ev2 + ee2 + er2 - mod_num<1000000007>(2) * eve - n         mod_num<1000000007>(2) * eer + mod_num<1000000007>(2) * evr; n   cout << (mod_num<1000000007>(2) ^ (1000000007)) * (ex2 - ex * ex) * n               (mod_num<1000000007>(2) ^ (1000000005)) n        << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sum1, sum2; n inline void in(long long int &_8) { n   long long int _6 = 1; n   char _0; n   while (((_0 = getchar()) <  0  || _0 >  9 ) && _0 !=  - ) n     ; n   _0 ==  -  ? (_6 = -1, _8 = 0) : (_8 = _0 -  0 ); n   while ((_0 = getchar()) >=  0  && _0 <=  9 ) _8 = _8 * 10 + _0 -  0 ; n   _8 *= _6; n } n int main() { n   long long int digit, base, tmp, sum; n   in(digit); n   in(base); n   for (int i = 1; i <= digit; i++) { n     in(tmp); n     sum1 = sum1 * base + tmp; n   } n   in(digit); n   in(base); n   for (int i = 1; i <= digit; i++) { n     in(tmp); n     sum2 = sum2 * base + tmp; n   } n   sum1 == sum2 ? putchar( = ) : sum1 > sum2 ? putchar( > ) : putchar( < ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n), b(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     --a[i]; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &b[i]); n     --b[i]; n   } n   vector<int> posa(n), posb(n); n   for (int i = 0; i < n; ++i) { n     posa[a[i]] = i; n     posb[b[i]] = i; n   } n   multiset<int> left, right; n   for (int i = 0; i < n; ++i) { n     if (posb[i] < posa[i]) { n       left.insert(posa[i] - posb[i]); n     } else { n       right.insert(posb[i] - posa[i]); n     } n   } n   int x = 0; n   vector<int> r; n   while (x < n) { n     int ans = n + 10; n     if (!left.empty()) { n       ans = min(ans, *left.begin() - x); n     } n     if (!right.empty()) { n       ans = min(ans, *right.begin() + x); n     } n     r.push_back(ans); n     x++; n     if (x == n) { n       break; n     } n     while (!left.empty() && *left.begin() - x == 0) { n       left.erase(left.begin()); n       right.insert(-x); n     } n     int e = b[n - x]; n     if (posb[e] + x < posa[e]) { n       left.erase(left.find(posa[e] - posb[e])); n     } else { n       right.erase(right.find(posb[e] - posa[e])); n     } n     if ((posb[e] + x) % n < posa[e]) { n       left.insert(posa[e] - (posb[e] + x) % n + x); n     } else { n       right.insert((posb[e] + x) % n - posa[e] - x); n     } n   } n   reverse(r.begin() + 1, r.end()); n   for (int x : r) { n     printf( %d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, f[2][30], a[100005], b[100005]; n string s; n int main() { n   scanf( %d , &n); n   int now = 1, pre = 0; n   for (int i = 1; i <= n; i++, swap(now, pre)) { n     cin >> s; n     int len = s.length(); n     memset(f[now], 0, sizeof f[now]); n     for (int j = 0; j < len; j++) n       a[j] = (j == 0             ? 1 n               : s[j - 1] == s[j] ? a[j - 1] + 1 n                                  : 1), n       b[len - j - 1] = n           (len - j - 1 == len - 1                 ? 1 n            : s[len - j - 1 + 1] == s[len - j - 1] ? b[len - j - 1 + 1] + 1 n                                                   : 1), n       f[now][s[j] -  a ] = max(f[now][s[j] -  a ], a[j]); n     for (int j = 0; j < 26; j++) { n       f[now][j] = max(f[now][j], (f[pre][j] > 0) + (s[0] -  a  == j) * b[0]), n       f[now][j] = max(f[now][j], n                       (f[pre][j] > 0) + (s[len - 1] -  a  == j) * a[len - 1]); n       if (a[len - 1] == len && s[0] -  a  == j) n         f[now][j] = max(f[now][j], f[pre][j] + len * (f[pre][j] + 1)); n       else n         f[now][j] = max(f[now][j], (f[pre][j] > 0) + (s[0] -  a  == j) * b[0] + n                                        (s[len - 1] -  a  == j) * a[len - 1]); n     } n   } n   int ans = 0; n   for (int i = 0; i < 26; i++) ans = max(ans, f[pre][i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> v; n   set<long long> myset; n   set<long long>::iterator it; n   long long n, k, q, t; n   map<long long, int> mp; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     scanf( %I64d , &t); n     v.push_back(t); n     myset.insert(t); n     mp[t]++; n   } n   sort(v.begin(), v.end()); n   if (v.size() == myset.size()) { n     if (k % n != 0 && n < k) { n       long long m = k / n; n       long long p = m * n; n       long long u = v[m]; n       long long x = 0; n       for (long long i = p + 1; i <= k; i++) { n         q = v[x]; n         ++x; n       } n       cout << u <<     << q << endl; n       return 0; n     } n     if (k % n == 0 && n <= k) { n       cout << v[(k / n) - 1] <<     << v[n - 1] << endl; n       return 0; n     } n     if (k < n) { n       cout << v[0] <<     << v[k - 1] << endl; n       return 0; n     } n   } else { n     long long sum = 0, b; n     for (it = myset.begin(); it != myset.end(); ++it) { n       sum += (mp[*it] * n); n       if (k <= sum) { n         b = *it; n         sum -= (mp[*it] * n); n         break; n       } n     } n     long long cnt = sum; n     for (long long i = 0; i < n; i++) { n       cnt += mp[b]; n       if (cnt >= k) { n         cout << b <<     << v[i] << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 2e5 + 42; n int n, k; n int main() { n   cin >> n >> k; n   int d = (n - k) / 2; n   int cycle = d + 1; n   for (int i = 1; i <= n; i++) n     if (i % cycle == 0) n       cout <<  1 ; n     else n       cout <<  0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[105]; n int main() { n   int n; n   cin >> n; n   cin >> s; n   int flag = 0; n   for (int i = 1; i < n; i++) { n     if (s[i] !=  ?  && s[i] == s[i - 1]) { n       flag = 1; n       break; n     } n   } n   if (flag) n     cout <<  No  << endl; n   else { n     if (s[0] ==  ?  || s[n - 1] ==  ? ) n       flag = 1; n     else { n       for (int i = 1; i < n - 1; i++) { n         if (s[i] ==  ?  && s[i - 1] ==  ?  || n             s[i] ==  ?  && s[i + 1] == s[i - 1]) n           flag = 1; n       } n     } n     if (flag) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 6, m = 8; n   vector<string> a(n); n   vector<vector<int>> u; n   vector<int> loc(m); n   loc = {3, 3, -1, 4, 4, -1, 3, 3}; n   u.push_back(loc); n   u.push_back(loc); n   loc = {2, 2, -1, 3, 3, -1, 2, 2}; n   u.push_back(loc); n   u.push_back(loc); n   loc = {1, 1, -1, 2, 2, -1, 1, 1}; n   u.push_back(loc); n   u.push_back(loc); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int max = 0, x, y; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  .  && u[i][j] > max) { n         x = i; n         y = j; n         max = u[i][j]; n       } n     } n   } n   a[x][y] =  P ; n   for (auto i : a) { n     cout << i <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(const vector<int>& hps, const vector<int>& hero, vector<int>& soln, n            int frm, int dir) { n   soln.clear(); n   int n = hps.size(); n   if (n == 0) return true; n   vector<int> heroes; n   heroes.push_back(0); n   for (int i = 1; i < n; i++) { n     if (hero[i]) heroes.push_back(i); n   } n   int h = heroes.size(); n   if (h == 1) { n     if (hero[0]) { n       soln.push_back(frm); n     } n     return true; n   } n   vector<int> dp(h, -1); n   dp[0] = 0; n   for (int i = 1; i < h; i++) { n     int health = hero[heroes[i]]; n     assert(health > 0); n     for (int j = i - 1; j >= 0; j--) { n       bool gone = false; n       for (int k = heroes[j + 1] - 1; k >= heroes[j]; k--) { n         health += hps[k]; n         if (health < 0) { n           gone = true; n           break; n         } n       } n       if (gone) break; n       if (dp[j] != -1) { n         dp[i] = j; n         break; n       } n     } n   } n   if (dp[h - 1] == -1) return false; n   vector<int> taken(h, 0); n   int x = h - 1; n   while (1) { n     taken[x] = 1; n     if (hero[heroes[x]]) soln.push_back(x); n     if (x == 0) break; n     x = dp[x]; n   } n   reverse(soln.begin(), soln.end()); n   for (int i = 0; i < h; i++) n     if (!taken[i]) soln.push_back(i); n   for (int& x : soln) { n     x = heroes[x]; n     if (dir > 0) { n       x += frm; n     } else { n       x = frm - x; n     } n   } n   return true; n } n const int MAXN = 107; n int heroHealth[MAXN]; n int healthPnts[MAXN]; n int heroMap[MAXN]; n int n; n bool checkDirection(int rp, int dir, int bahir, vector<int>& soln) { n   vector<int> hps; n   vector<int> hero; n   for (int i = rp + bahir * dir; i <= n && i > 0; i += dir) { n     hps.push_back(healthPnts[i]); n     hero.push_back(heroHealth[i]); n   } n   return solve(hps, hero, soln, rp + bahir * dir, dir); n } n bool rally(int rp, vector<int>& soln) { n   soln.clear(); n   vector<int> bamsoln; n   vector<int> dansoln; n   bool x = checkDirection(rp, 1, 0, dansoln); n   bool y = checkDirection(rp, -1, 1, bamsoln); n   if (x && y) { n     if (healthPnts[rp] >= 0 || dansoln.size() > 0) { n       for (int g : dansoln) { n         soln.push_back(heroMap[g]); n       } n       for (int g : bamsoln) { n         soln.push_back(heroMap[g]); n       } n       return true; n     } n   } n   if (healthPnts[rp] < 0) { n     x = checkDirection(rp, 1, 1, dansoln); n     y = checkDirection(rp, -1, 0, bamsoln); n     if (x && y && bamsoln.size() > 0) { n       for (int g : bamsoln) { n         soln.push_back(heroMap[g]); n       } n       for (int g : dansoln) { n         soln.push_back(heroMap[g]); n       } n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   int m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int s, h; n     cin >> s >> h; n     heroHealth[s] = h; n     heroMap[s] = i; n   } n   for (int i = 1; i <= n; i++) { n     cin >> healthPnts[i]; n   } n   vector<int> soln; n   for (int rp = 1; rp <= n; rp++) { n     if (rally(rp, soln)) { n       cout << rp << endl; n       for (int x : soln) cout << x <<    ; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool SortbySecDesc(const pair<long long, long long> &a, n                    const pair<long long, long long> &b) { n   return a.second > b.second; n } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     long long low = 1, high = 1e18, ans; n     while (low <= high) { n       long long mid = (low + high) / 2; n       long long vag = mid / a; n       long long baki = mid - vag; n       if (baki >= b) { n         ans = mid; n         high = mid - 1; n       } else { n         low = mid + 1; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int k, tk; n   long long n, ck, a; n   scanf( %I64d %d , &n, &k); n   long long menor = n; n   scanf( %I64d , &a); n   tk = 1; n   ck = n / a; n   menor = n % a; n   for (int i = 1; i < k; i++) { n     scanf( %I64d , &a); n     if (n % a < menor) { n       tk = i + 1; n       ck = n / a; n       menor = n % a; n     } n   } n   printf( %d %I64d , tk, ck); n } n


#include <bits/stdc++.h> n std::vector<std::vector<int>> ad; n std::vector<int> fm; n std::vector<bool> tmp; n int f(int x) { n   if (fm[x] >= 0) return fm[x]; n   for (int y : ad[x]) f(y); n   for (int y : ad[x]) tmp[fm[y]] = true; n   fm[x] = int(std::find(begin(tmp), end(tmp), false) - begin(tmp)); n   for (int y : ad[x]) { n     tmp[fm[y]] = false; n   } n   return fm[x]; n } n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   int n, m; n   std::cin >> n >> m; n   std::vector<int> h(n); n   for (int& x : h) std::cin >> x; n   ad.resize(n); n   for (int z = m; z--;) { n     int u, v; n     std::cin >> u >> v; n     --u; n     --v; n     ad[u].push_back(v); n   } n   std::vector<int> val(n); n   fm.assign(n, -1); n   tmp.resize(n); n   for (int i = n; i--;) val[f(i)] ^= h[i]; n   while (!val.empty() && val.back() == 0) val.pop_back(); n   if (val.empty()) { n     std::cout <<  LOSE n ; n     return 0; n   } n   std::cout <<  WIN n ; n   int i = 0; n   while (fm[i] != val.size() - 1 || (h[i] ^ val.back()) > h[i]) { n     ++i; n   } n   h[i] ^= val.back(); n   std::vector<bool> ok(val.size() - 1); n   for (int j : ad[i]) n     if ((unsigned)fm[j] < ok.size() && !ok[fm[j]]) { n       ok[fm[j]] = true; n       h[j] ^= val[fm[j]]; n     } n   for (auto x : h) std::cout << x <<    ; n   std::cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, i; n   long long h, k, a[100005], cnt = 0; n   cin >> n >> h >> k; n   for (i = 0; i < n; i++) cin >> a[i]; n   long long carry = 0; n   for (i = 0; i < n; i++) { n     if (carry + a[i] <= h) { n       carry += a[i]; n     } else { n       cnt += 1; n       carry = a[i]; n     } n     cnt += (carry / k); n     carry %= k; n   } n   cnt += carry / k; n   carry %= k; n   cnt += (carry > 0); n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 50500, C = 27, N = 11, root = 0, None = -987958798; n const char END =  { ; n struct node { n   int s[C], b[N], par, val; n } t[L * 22] = {}; n char ch[L * 22] = {}; n int n, l = 0, tot = 0, x[N] = {}, y[N] = {}, d[N] = {}, in[L * 22] = {}, n        q[L * 22] = {}, head = 0, tail = 0; n long long ans = 0; n void add(int p, int c) { n   int np = ++tot; n   t[np].val = t[p].val + 1; n   while (p != None && t[p].s[c] == 0) t[p].s[c] = np, p = t[p].par; n   if (p == None) n     t[np].par = root; n   else { n     int q = t[p].s[c]; n     if (t[p].val + 1 == t[q].val) n       t[np].par = q; n     else { n       int nq = ++tot; n       t[nq] = t[q]; n       t[nq].val = t[p].val + 1; n       t[q].par = t[np].par = nq; n       for (int i = p; i != None && t[i].s[c] == q; i = t[i].par) t[i].s[c] = nq; n     } n   } n } n int main() { n   t[root].par = None; n   d[0] = ++l; n   cin >> (ch + d[0]) >> n; n   l += strlen(ch + d[0]); n   for (int i = 1; i <= n; ++i) { n     ch[l] = END; n     d[i] = ++l; n     cin >> (ch + d[i]) >> x[i] >> y[i]; n     l += strlen(ch + d[i]); n   } n   ch[l] = END; n   for (int i = 1, p = root; i < l; p = t[p].s[ch[i++] -  a ]) n     add(p, ch[i] -  a ); n   for (int i = 0; i <= n; ++i) n     for (int j = d[i], p = t[root].s[ch[j] -  a ]; ch[j] != END; n          p = t[p].s[ch[++j] -  a ]) n       ++t[p].b[i]; n   for (int i = 1; i <= tot; ++i) ++in[t[i].par]; n   for (int i = 1; i <= tot; ++i) n     if (!in[i]) q[++tail] = i; n   for (int tmp = 1; tmp <= tot; ++tmp) { n     int s = q[++head]; n     bool flag = t[s].b[0]; n     for (int i = 1; i <= n; ++i) flag &= x[i] <= t[s].b[i] && t[s].b[i] <= y[i]; n     if (flag) ans += t[s].val - t[t[s].par].val; n     for (int i = 0; i <= n; ++i) t[t[s].par].b[i] += t[s].b[i]; n     if ((--in[t[s].par]) == 0) q[++tail] = t[s].par; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int co, key; n } num[1010]; n int seven; n char str[1010 * 100]; n int init; n bool cmp(node x, node y) { return x.co < y.co; } n int main() { n   scanf( %d , &init); n   scanf( %s , str); n   seven = 1; n   int cnt = 1; n   int length = strlen(str); n   int now = 0; n   int pre = -1; n   for (int i = 1; i <= 1000; i++) num[i].co = 1; n   while (now < length) { n     if (str[now] ==  - ) { n       seven = -1; n       now++; n     } else if (str[now] ==  *  || str[now] ==  + ) n       now++; n     else if (str[now] ==  a ) { n       if (now - 2 > pre && str[now - 1] ==  +  && str[now - 2] ==  + ) { n         pre = now; n         num[cnt].co *= seven; n         num[cnt].key = 0; n         cnt++; n         now++; n       } else { n         pre = now + 2; n         num[cnt].co *= seven; n         num[cnt].key = 1; n         cnt++; n         now += 3; n       } n       seven = 1; n     } else { n       int sum = 0; n       while (str[now] >=  0  && str[now] <=  9 ) { n         sum = sum * 10 + (str[now] -  0 ); n         now++; n       } n       num[cnt].co = sum; n     } n   } n   cnt--; n   sort(num + 1, num + 1 + cnt, cmp); n   int ans = 0; n   for (int i = 1; i <= cnt; i++) { n     if (!num[i].key) { n       init++; n       ans += (num[i].co * init); n     } else { n       ans += (num[i].co * init); n       init++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const long long p1 = 737; n const long long p2 = 3079; n const long long m1 = 1000000123; n const long long m2 = 1000000321; n vector<long long> p_pow1(N), p_pow2(N); n pair<vector<long long>, vector<long long> > H[3]; n void init() { n   p_pow1[0] = 1; n   p_pow2[0] = 1; n   for (int i = 1; i < N; i++) { n     p_pow1[i] = (p_pow1[i - 1] * p1) % m1; n     p_pow2[i] = (p_pow2[i - 1] * p2) % m2; n   } n } n pair<vector<long long>, vector<long long> > Hash(string const& s) { n   int n = s.size(); n   vector<long long> h1(n + 1), h2(n + 1); n   for (int i = 0; i < n; i++) { n     h1[i + 1] = ((h1[i] * p1) % m1 + (s[i] -  a  + 1)) % m1; n     h2[i + 1] = ((h2[i] * p2) % m2 + (s[i] -  a  + 1)) % m2; n   } n   return {h1, h2}; n } n pair<long long, long long> get_Hash(int idx, int l, int r) { n   long long x = n       (H[idx].first[r] - (H[idx].first[l - 1] * p_pow1[r - l + 1]) % m1 + m1) % n       m1; n   long long y = (H[idx].second[r] - n                  (H[idx].second[l - 1] * p_pow2[r - l + 1]) % m2 + m2) % n                 m2; n   return {x, y}; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   init(); n   string s, t1, t2; n   char ch[2005]; n   scanf( %s , ch); n   s = ch; n   scanf( %s , ch); n   t1 = ch; n   scanf( %s , ch); n   t2 = ch; n   int n = s.size(); n   int a = t1.size(); n   int b = t2.size(); n   H[0] = Hash(s); n   H[1] = Hash(t1); n   H[2] = Hash(t2); n   vector<pair<long long, long long> > v; n   for (int i = 1; i <= (n - a + 1); i++) { n     for (int j = i; j <= (n - b + 1); j++) { n       int len1 = i + a - 1; n       int len2 = j + b - 1; n       if (get_Hash(0, i, len1) == get_Hash(1, 1, a) && n           get_Hash(0, j, len2) == get_Hash(2, 1, b)) { n         if (len1 <= len2) v.push_back(get_Hash(0, i, len2)); n       } n     } n   } n   sort(v.begin(), v.end()); n   int res = 0; n   if (v.size()) res = 1; n   for (int i = 1; i < v.size(); i++) { n     if (v[i - 1] != v[i]) res++; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, c, d, p, nl, np; n   { n     cin >> n >> k >> l >> c >> d >> p >> nl >> np; n     int D = k * l; n     D /= nl; n     cout << min(D, min((c * d), (p / np))) / n << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, 1, 0, -1}; n int dx2[] = {1, -1, -1, 1, 0, 0, -1, 1}; n int dy2[] = {1, -1, 1, -1, 1, -1, 0, 0}; n int kmx[] = {-1, -1, 1, 1, 2, -2, 2, -2}; n int kmy[] = {2, -2, 2, -2, -1, -1, 1, 1}; n int bishopX[] = {-1, 1, -1, 1}; n int bishopY[] = {-1, -1, 1, 1}; n class Timer { n  public: n   clock_t T; n   Timer() { T = clock(); } n   ~Timer() { n     fprintf(stderr,   n%.3f n , double(clock() - T) / CLOCKS_PER_SEC); n   } n }; n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n long long readL() { n   long long x; n   scanf( %lld , &x); n   return x; n } n const int N = 1e5 + 5; n int visX[N][4], visY[N][4]; n vector<int> v1[4 * N], v2[4 * N]; n int n, m, k; n long long ans[N]; n pair<int, int> a[N]; n int main() { n   n = read(); n   m = read(); n   k = read(); n   for (int i = 0; i < k; i++) { n     int x = read(); n     int y = read(); n     ans[i] = -1; n     v1[x - y + N].push_back(i); n     v2[x + y].push_back(i); n     a[i] = make_pair(x, y); n   } n   int x = 0; n   int y = 0; n   int d = 0; n   long long gt = 0; n   int mn; n   while (1) { n     switch (d) { n       case 0: n         mn = min(n - x, m - y); n         x = x + mn; n         y = y + mn; n         gt += mn; n         break; n       case 1: n         mn = min(x, y); n         x = x - mn; n         y = y - mn; n         gt += mn; n         break; n       case 2: n         mn = min(x, m - y); n         x = x - mn; n         y = y + mn; n         gt += mn; n         break; n       case 3: n         mn = min(n - x, y); n         x = x + mn; n         y = y - mn; n         gt += mn; n         break; n     } n     int dif = x - y + N; n     int sum = x + y; n     if (d == 0 || d == 1) n       for (int i = 0; i < v1[dif].size(); i++) { n         int j = v1[dif][i]; n         if (ans[j] == -1) { n           int dddd = abs(x - a[j].first); n           ans[j] = gt - dddd; n         } n       } n     else n       for (int i = 0; i < v2[sum].size(); i++) { n         int j = v2[sum][i]; n         if (ans[j] == -1) { n           int dddd = abs(x - a[j].first); n           ans[j] = gt - dddd; n         } n       } n     if (x == 0 && y == 0 || x == 0 && y == m || x == n && y == 0 || n         x == n && y == m) { n       break; n     } n     if (x == n) { n       if (d == 0) n         d = 2; n       else if (d == 3) n         d = 1; n       continue; n     } n     if (y == m) { n       if (d == 2) n         d = 1; n       else if (d == 0) n         d = 3; n       continue; n     } n     if (x == 0) { n       if (d == 2) n         d = 0; n       else if (d == 1) n         d = 3; n       continue; n     } n     if (y == 0) { n       if (d == 1) n         d = 2; n       else if (d == 3) n         d = 0; n       continue; n     } n   } n   for (int i = 0; i < k; i++) { n     printf( %lld n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n, i, j, k, c, v, l = 1, r = 1, maxm = 0, minm; n   cin >> n; n   int A[n]; n   string s; n   cin >> s; n   for (i = 0; i < n; i++) A[i] = 0; n   for (i = 0; i < n; i++) { n     for (j = i; j < n; j++) { n       swap(s[i], s[j]); n       v = 0; n       c = 0; n       for (k = 0; k < n; k++) { n         if (s[k] ==  ( ) n           v++; n         else n           v--; n         A[k] = v; n       } n       if (v == 0) { n         minm = 0; n         for (k = 0; k < n; k++) { n           if (A[k] < minm) { n             minm = A[k]; n             c = 1; n           } else if (A[k] == minm) n             c++; n         } n         if (c > maxm) { n           maxm = c; n           l = i + 1; n           r = j + 1; n         } n       } n       swap(s[i], s[j]); n     } n   } n   cout << maxm << endl; n   cout << l <<     << r << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int div1(int a, int b) { n   if (a % b == 0) n     return a / b; n   else n     return a / b + 1; n } n int main() { n   int k, n, s, p; n   while (scanf( %d%d%d%d , &k, &n, &s, &p) != EOF) { n     int ever_need = div1(n, s); n     int ans = div1(ever_need * k, p); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const int oo = 1e9; n int g[N][N], n, m; n int col[N]; n vector<pair<int, int> > ans; n pair<int, int> get(int val) { n   for (int i = 1; i <= m; ++i) { n     if (g[1][i] < val) return {oo, val}; n   } n   memset(col, 0, sizeof(col)); n   ans.clear(); n   int ret = val; n   for (int i = 1; i <= m; ++i) col[i] = g[1][i] - val; n   ans.push_back({1, val}); n   for (int i = 2; i <= n; ++i) { n     int mn = oo, mx = -oo; n     for (int j = 1; j <= m; ++j) { n       if (g[i][j] < col[j]) return {oo, val}; n       mn = min(mn, g[i][j] - col[j]); n       mx = max(mx, g[i][j] - col[j]); n     } n     if (mn != mx) return {oo, val}; n     ans.push_back({i, mn}); n     ret += mn; n   } n   for (int i = 1; i <= m; ++i) { n     ans.push_back({-i, col[i]}); n     ret += col[i]; n   } n   return {ret, val}; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       scanf( %d , &g[i][j]); n     } n   } n   pair<int, int> ret = {oo, -1}; n   for (int i = 0; i <= 500; ++i) ret = min(ret, get(i)); n   if (ret.first == oo) { n     printf( -1 n ); n     return 0; n   } n   get(ret.second); n   printf( %d n , ret.first); n   for (auto u : ans) { n     if (u.first < 0) { n       for (int i = 1; i <= u.second; ++i) printf( col %d n , -u.first); n     } else { n       for (int i = 1; i <= u.second; ++i) printf( row %d n , u.first); n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long  n #define pb push_back n #define inf 1e18 n #define nn    n  n #define vll vector<long long int> n #define vl vector<long int> n #define ff first n #define ss second n  n  n  n  n  n  n int main() n { n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n     #endif n  n     int t; n     cin>>t; n  n     while(t--) n     { n      ll int hero_attack,hero_health,n; n      cin>>hero_attack>>hero_health>>n; n  n      vector<pair<ll int,ll int>> v; n      ll int attack[n]; n      for(int i=0;i<n;i++) n      { n       cin>>attack[i];       n      } n  n      ll int health[n]; n       n      for(int i=0;i<n;i++) n      { n       cin>>health[i];       n      } n  n      for(int i=0;i<n;i++) n      { n       v.pb({attack[i],health[i]}); n      } n  n      sort(v.begin(),v.end()); n   n         int flag=1; n         for(auto &it:v) n         {  n  n          ll int x,y; n          if(hero_health<=0) n          { n           flag=0; n           cout<< NO <<nn; n           break; n          } n  n          if(it.second%hero_attack!=0) n          { n          x=(it.second/hero_attack)+1; n          } n          if(it.second%hero_attack==0) n          { n          x=it.second/hero_attack; n          } n          if(hero_health%it.first!=0) n          { n          y=(hero_health/it.first)+1; n          } n          if(hero_health%it.first==0) n          { n          y=hero_health/it.first; n          } n  n  n          if(x<=y) n          { n           hero_health=hero_health-x*it.first; n  n          } n          else n          { n           cout<< NO <<nn; n           flag=0; n           break; n  n          } n  n  n  n         } n  n         if(flag==1) n          cout<< YES <<nn; n  n  n  n       n  n  n     } n  n     return 0; n  n }   n  n


#include <bits/stdc++.h> n using namespace std; n const int size = 400007; n const long long modulo = 1000000007; n const int INF = 2e9; n const double EPS = 1e-8; n const double PI = acos(-1.0); n int d[2][size]; n char board[2][size]; n int compo[size]; n int l[size], r[size]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   cin.ignore(); n   gets(board[0]); n   gets(board[1]); n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < size; j++) d[i][j] = 2e9; n   int j = 0, com = -1; n   vector<bool> start(n, false); n   while (j < n) { n     while (j < n && board[0][j] ==  X  && board[1][j] ==  X ) ++j; n     if (j < n) { n       start[j] = true; n       ++com; n       compo[j] = com; n     } n     d[0][j] = (board[0][j] ==  X  ? 2e9 : 0); n     d[0][j + n] = (board[1][j] ==  X  ? 2e9 : 1); n     d[1][j] = (board[0][j] ==  X  ? 2e9 : 1); n     d[1][j + n] = (board[1][j] ==  X  ? 2e9 : 0); n     int col = j + 1; n     while (col < n) { n       bool ok = false; n       for (int st = 0; st < 2; st++) n         for (int cell = col; cell < 2 * n; cell += n) n           if (board[cell / n][col] !=  X  && n               d[st][cell] > d[st][cell - 1] + 1) { n             ok = true; n             d[st][cell] = d[st][cell - 1] + 1; n           } n       for (int st = 0; st < 2; st++) n         for (int cell = col; cell < 2 * n; cell += n) n           if (board[cell / n][col] !=  X  && n               d[st][cell] > d[st][(cell + n) % (2 * n)] + 1) { n             d[st][cell] = d[st][(cell + n) % (2 * n)] + 1; n           } n       if (ok) { n         compo[col] = com; n         ++col; n       } else { n         break; n       } n     } n     j = col; n   } n   if (board[0][0] ==  .  && board[1][0] ==  . ) n     l[0] = -1; n   else n     l[0] = 0; n   for (int i = 1; i < n; i++) { n     if (board[0][i] ==  .  && board[1][i] ==  . ) n       l[i] = l[i - 1]; n     else n       l[i] = i; n   } n   if (board[0][n - 1] ==  .  && board[1][n - 1] ==  . ) n     r[n - 1] = n; n   else n     r[n - 1] = n - 1; n   for (int i = n - 2; i >= 0; i--) { n     if (board[0][i] ==  .  && board[1][i] ==  . ) n       r[i] = r[i + 1]; n     else n       r[i] = i; n   } n   while (m--) { n     int a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     if (a + n == b) n       cout << 1 << endl; n     else { n       --a; n       --b; n       int a_row = a / n, a_col = a % n, b_row = b / n, b_col = b % n; n       if (a_col > b_col) { n         swap(a_row, b_row); n         swap(a_col, b_col); n         swap(a, b); n       } n       if (compo[a_col] != compo[b_col]) { n         cout << -1 << endl; n         continue; n       } n       int ans = min(d[0][b], d[1][b]); n       if (l[a_col] == a_col) n         ans -= min(d[0][a], d[1][a]); n       else if (start[a_col]) n         ans = d[a_row][b]; n       else { n         if (r[a_col] > b_col) n           ans = b_col - a_col + (a_row != b_row); n         else { n           int right_exit = (board[0][r[a_col]] ==  .  ? 0 : 1); n           ans = min(d[0][b] - d[0][a], d[1][b] - d[1][a]); n           int pref = (d[0][b] - d[0][a] < d[1][b] - d[1][a] ? 0 : 1); n           int lind = l[a_col]; n           int left_exit = -1; n           if (lind < 0 || start[lind + 1]) n             left_exit = pref; n           else if (board[0][lind] ==  . ) n             left_exit = 0; n           else n             left_exit = 1; n           if (right_exit != a_row && left_exit != a_row) ans += 2; n         } n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3f; n static const double EPS = 1e-10; n static const double PI = acos(-1.0); n inline int read() { n   int X = 0; n   bool flag = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) flag = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     X = (X << 1) + (X << 3) + ch -  0 ; n     ch = getchar(); n   } n   if (flag) return X; n   return ~(X - 1); n } n inline void write(int X) { n   if (X < 0) { n     X = ~(X - 1); n     putchar( - ); n   } n   if (X > 9) write(X / 10); n   putchar(X % 10 +  0 ); n } n static const int MAXN = 500 + 10; n vector<int> g[MAXN]; n int d[1 << 19], pre[1 << 19]; n int n, m, u, v; n int main() { n   scanf( %d%d , &n, &m); n   while (m--) { n     scanf( %d%d , &u, &v); n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   memset(d, -1, sizeof(d)); n   d[n - 1] = 0; n   queue<int> Q; n   Q.push(n - 1); n   while (!Q.empty()) { n     int t = Q.front(); n     Q.pop(); n     bool flag = false; n     if (t & (1 << 18)) { n       flag = true; n       t ^= (1 << 18); n     } n     int u = t >> 9, v = t & 511; n     if (flag) t ^= (1 << 18); n     if (flag) { n       for (int i = 0; i < g[v].size(); i++) n         if (g[v][i] != u && d[(u << 9) | g[v][i]] == -1) { n           d[(u << 9) | g[v][i]] = d[t] + 1; n           pre[(u << 9) | g[v][i]] = t; n           Q.push((u << 9) | g[v][i]); n         } n     } else { n       for (int i = 0; i < g[u].size(); i++) n         if (d[(1 << 18) | (g[u][i] << 9) | v] == -1) { n           d[(1 << 18) | (g[u][i] << 9) | v] = d[t]; n           pre[(1 << 18) | (g[u][i] << 9) | v] = t; n           Q.push((1 << 18) | (g[u][i] << 9) | v); n         } n     } n   } n   int pos = (n - 1) << 9; n   if (d[pos] == -1) n     puts( -1 ); n   else { n     printf( %d n , d[pos]); n     vector<int> ans1, ans2; n     ans1.push_back(n - 1); n     ans2.push_back(0); n     while (pos != n - 1) { n       pos = pre[pre[pos]]; n       ans1.push_back(pos >> 9); n       ans2.push_back(pos & 511); n     } n     reverse(ans1.begin(), ans1.end()); n     reverse(ans2.begin(), ans2.end()); n     for (auto it : ans1) printf( %d  , it + 1); n     puts(  ); n     for (auto it : ans2) printf( %d  , it + 1); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n pair<int, int> joker[2] = {{-1, -1}, {-1, -1}}; n pair<int, int> s1 = {-1, -1}, s2 = {-1, -1}; n string ranks =  23456789TJQKA , suits =  CDHS , a[64][64]; n set<string> deck; n vector<string> left_cards; n void gen_deck() { n   for (char r : ranks) n     for (char s : suits) { n       string res =   ; n       res = res + r; n       res = res + s; n       deck.insert(res); n     } n } n bool can(int ii, int jj) { n   set<char> cur_ranks; n   set<char> cur_suits; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) { n       cur_ranks.insert(a[ii + i][jj + j][0]); n       cur_suits.insert(a[ii + i][jj + j][1]); n     } n   return ((((int)(cur_ranks).size()) == 9) || (((int)(cur_suits).size()) == 1)); n } n bool check() { n   for (int lr = 0; lr < n - 2; lr++) n     for (int lc = 0; lc < m - 2; lc++) n       for (int rr = 0; rr < n - 2; rr++) n         for (int rc = 0; rc < m - 2; rc++) { n           if (rr < lr + 3 && rc < lc + 3) continue; n           if (can(lr, lc) && can(rr, rc)) { n             s1 = {lr, lc}; n             s2 = {rr, rc}; n             return 1; n           } n         } n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   gen_deck(); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j][0] ==  J ) joker[a[i][j][1] -  1 ] = {i, j}; n       deck.erase(a[i][j]); n     } n   for (auto it : deck) left_cards.push_back(it); n   for (int i = 0; i < ((int)(left_cards).size()); i++) n     for (int j = 0; j < ((int)(left_cards).size()); j++) { n       if (i == j) continue; n       if (joker[0].first != -1) n         a[joker[0].first][joker[0].second] = left_cards[i]; n       if (joker[1].first != -1) n         a[joker[1].first][joker[1].second] = left_cards[j]; n       if (check()) { n         cout <<  Solution exists.  << endl; n         if (joker[0].first < 0 && joker[1].first < 0) { n           cout <<  There are no jokers.  << endl; n         } else if (!((joker[0].first < 0) || (joker[1].first < 0))) { n           cout <<  Replace J1 with   << left_cards[i] <<   and J2 with   n                << left_cards[j] <<  .  << endl; n         } else { n           if (joker[0].first < 0) { n             cout <<  Replace J2 with   << left_cards[j] <<  .  << endl; n           } else { n             cout <<  Replace J1 with   << left_cards[i] <<  .  << endl; n           } n         } n         cout <<  Put the first square to (  << s1.first + 1 <<  ,   n              << s1.second + 1 <<  ).  << endl; n         cout <<  Put the second square to (  << s2.first + 1 <<  ,   n              << s2.second + 1 <<  ).  << endl; n         return 0; n       } n     } n   cout <<  No solution.  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 25; n string str[maxn]; n int a[maxn][maxn]; n long long dp[1 << 20]; n int mask[26], tot[26], big[26], cnt[26], candi[26]; n int Min[maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> str[i]; n   } n   memset(Min, 0x3f, sizeof(Min)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       Min[i] = min(Min[i], a[i][j]); n     } n   } n   for (int i = 0; i < (1 << n); i++) { n     int tmp = 0; n     for (int j = 0; j < n; j++) { n       if (i >> j & 1) tmp += Min[j + 1]; n     } n     dp[i] = tmp; n   } n   for (int i = 1; i <= m; i++) { n     memset(mask, 0, sizeof(mask)); n     memset(tot, 0, sizeof(tot)); n     memset(big, 0, sizeof(big)); n     memset(cnt, 0, sizeof(cnt)); n     for (int j = 1; j <= n; j++) { n       int c = str[j][i - 1] -  a ; n       cnt[c]++; n       mask[c] |= (1 << (j - 1)); n       tot[c] += a[j][i]; n       big[c] = max(big[c], a[j][i]); n     } n     int idx = 0; n     for (int j = 0; j < 26; j++) { n       if (cnt[j]) candi[idx++] = j; n     } n     for (int j = 0; j < (1 << n); j++) { n       for (int k = 0; k < idx; k++) { n         int u = candi[k]; n         dp[j | mask[u]] = min(dp[j | mask[u]], dp[j] + (tot[u] - big[u])); n       } n     } n   } n   cout << dp[(1 << n) - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000006; n int t, n, m, x, y; n stack<int> s; n int rg[N], rs[N], ls[N], lg[N], a[N]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   a[0] = a[n + 1] = 0; n   for (int i = 1; i <= n + 1; i++) { n     while (!s.empty() && a[s.top()] > a[i]) { n       t = s.top(); n       s.pop(); n       rs[t] = i - 1; n     } n     s.push(i); n   } n   s.pop(); n   for (int i = n; i >= 0; i--) { n     while (!s.empty() && a[s.top()] >= a[i]) { n       t = s.top(); n       s.pop(); n       ls[t] = i + 1; n     } n     s.push(i); n   } n   s.pop(); n   a[0] = a[n + 1] = INT_MAX; n   for (int i = 1; i <= n + 1; i++) { n     while (!s.empty() && a[s.top()] < a[i]) { n       t = s.top(); n       s.pop(); n       rg[t] = i - 1; n     } n     s.push(i); n   } n   s.pop(); n   for (int i = n; i >= 0; i--) { n     while (!s.empty() && a[s.top()] <= a[i]) { n       t = s.top(); n       s.pop(); n       lg[t] = i + 1; n     } n     s.push(i); n   } n   s.pop(); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += 1LL * ((rg[i] - lg[i]) + 1LL * (rg[i] - i) * (i - lg[i])) * a[i]; n     ans -= 1LL * ((rs[i] - ls[i]) + 1LL * (rs[i] - i) * (i - ls[i])) * a[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3FFFFFFF; n char arr[1001][1001]; n int main() { n   int n, m, x, y, z, p; n   scanf( %d%d%d%d%d%d , &n, &m, &x, &y, &z, &p); n   x %= 4; n   y %= 2; n   z %= 4; n   vector<int> a(p), b(p); n   for (int i = 0; i < p; i++) { n     scanf( %d%d , &a[i], &b[i]); n   } n   if (x == 1) { n     for (int i = 0; i < p; i++) { n       int at = b[i]; n       int bt = n - a[i] + 1; n       a[i] = at; n       b[i] = bt; n     } n     swap(n, m); n   } else if (x == 2) { n     for (int i = 0; i < p; i++) { n       int at = n - a[i] + 1; n       int bt = m - b[i] + 1; n       a[i] = at; n       b[i] = bt; n     } n   } else if (x == 3) { n     for (int i = 0; i < p; i++) { n       int at = m - b[i] + 1; n       int bt = a[i]; n       a[i] = at; n       b[i] = bt; n     } n     swap(n, m); n   } n   if (y == 1) { n     for (int i = 0; i < p; i++) { n       int at = a[i]; n       int bt = m - b[i] + 1; n       a[i] = at; n       b[i] = bt; n     } n   } n   if (z == 3) { n     for (int i = 0; i < p; i++) { n       int at = b[i]; n       int bt = n - a[i] + 1; n       a[i] = at; n       b[i] = bt; n     } n     swap(n, m); n   } else if (z == 2) { n     for (int i = 0; i < p; i++) { n       int at = n - a[i] + 1; n       int bt = m - b[i] + 1; n       a[i] = at; n       b[i] = bt; n     } n   } else if (z == 1) { n     for (int i = 0; i < p; i++) { n       int at = m - b[i] + 1; n       int bt = a[i]; n       a[i] = at; n       b[i] = bt; n     } n     swap(n, m); n   } n   for (int i = 0; i < p; i++) { n     printf( %d %d n , a[i], b[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   if (a < b) return gcd(b, a); n   while (a > b) { n     a %= b; n   } n   return gcd(b, a); n } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n const double EPS = 10e-9; n const double PI = acos(-1.0); n const double INF = (1LL << 29); n using namespace std; n template <typename T> n string toString(T t) { n   stringstream ss; n   ss << t; n   return ss.str(); n } n const long long prime = 1000000007; n long long power(long long n, long long a, long long prime) { n   long long res = 1LL; n   while (a > 0) { n     if (a & 1LL) { n       res *= n; n       res %= prime; n     } n     n *= n; n     n %= prime; n     a >>= 1; n   } n   return res; n } n int main() { n   string x; n   cin >> x; n   long long total = 0; n   for (int i = 0; i < (x.size()); ++i) { n     if (x[i] ==  1 ) { n       int len = i, left = x.size() - len - 1; n       long long part = power(2, len, prime) * power(4, left, prime); n       total += part % prime; n       total %= prime; n     } n   } n   cout << total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, cnt[21][21], pos[21][400001], dp[1 << 20]; n vector<long long> v[21]; n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     a--; n     v[a].push_back(i); n     pos[a][i]++; n   } n   for (long long i = 0; i < 20; i++) { n     for (long long j = 1; j <= n; j++) { n       pos[i][j] += pos[i][j - 1]; n     } n   } n   for (long long i = 0; i < 20; i++) { n     for (long long j = 0; j < 20; j++) { n       if (i == j) continue; n       for (auto x : v[i]) { n         cnt[i][j] += pos[j][x]; n       } n     } n   } n   dp[0] = 0; n   for (long long i = 1; i < (1 << 20); i++) { n     dp[i] = 1000000000000000000; n     for (long long j = 0; j < 20; j++) { n       if (i >> j & 1) { n         long long ans1 = 0; n         for (long long k = 0; k < 20; k++) { n           if (k == j) continue; n           if (i >> k & 1) ans1 += cnt[j][k]; n         } n         dp[i] = min(dp[i], dp[i ^ (1 << j)] + ans1); n       } n     } n   } n   cout << dp[(1 << 20) - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n void _fill_int(int* p, int val, int rep) { n   int i; n   for (i = 0; i < rep; i++) p[i] = val; n } n int N, M, K; n signed long long mo = 1000000007; n int L, R; n string LL, RR; n vector<string> S; n int V[201]; n vector<vector<int> > TR; n vector<int> AC; n signed long long dpdp[2][201][501][2][2]; n int find(string s) { n   int cur = 0; n   for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++) n     if ((cur = TR[cur][*it]) == 0) return -1; n   return cur; n } n void create(vector<string> S) { n   int i; n   TR.push_back(vector<int>(256)); n   for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++) { n     int cur = 0; n     for (__typeof((*it).begin()) c = (*it).begin(); c != (*it).end(); c++) { n       if (TR[cur][*c] == 0) n         TR.push_back(vector<int>(256)), TR[cur][*c] = TR.size() - 1; n       cur = TR[cur][*c]; n     } n     AC.resize(TR.size()); n     AC[cur] += V[it - S.begin()]; n   } n   queue<int> Q; n   for (i = 0; i < 255; i++) n     if (TR[0][i]) TR[TR[0][i]][255] = 0, Q.push(TR[0][i]); n   while (!Q.empty()) { n     int k = Q.front(); n     Q.pop(); n     for (i = 0; i < 255; i++) n       if (TR[k][i]) { n         Q.push(TR[k][i]); n         int pre = TR[k][255]; n         while (pre != 0 && TR[pre][i] == 0) pre = TR[pre][255]; n         TR[TR[k][i]][255] = TR[pre][i]; n         AC[TR[k][i]] += AC[TR[pre][i]]; n       } n   } n } n signed long long dodo(string SS) { n   signed long long ret = 0, rr; n   int i, j, k, x, y, pos, val, b1, b2; n   memset(dpdp, 0, sizeof(dpdp)); n   dpdp[0][0][0][1][1] = 1; n   for (i = 0; i < SS.size(); i++) { n     int cur = i % 2, tar = cur ^ 1; n     memset(dpdp[tar], 0, sizeof(dpdp[tar])); n     for (b1 = 0; b1 < 2; b1++) n       for (b2 = 0; b2 < 2; b2++) n         for (val = 0; val < K + 1; val++) n           for (pos = 0; pos < TR.size(); pos++) n             if ((rr = dpdp[cur][pos][val][b1][b2])) { n               for (j = 0; j < M; j++) { n                 if (b1 && j > SS[i]) break; n                 y = (b1 && j == SS[i]); n                 if (b2 && (j == 0)) { n                   dpdp[tar][pos][val][y][1] += rr; n                   dpdp[tar][pos][val][y][1] %= mo; n                   continue; n                 } n                 x = pos; n                 while (x && TR[x][j] == 0) x = TR[x][255]; n                 x = TR[x][j]; n                 if (val + AC[x] <= K) { n                   dpdp[tar][x][val + AC[x]][y][0] += rr; n                   dpdp[tar][x][val + AC[x]][y][0] %= mo; n                 } n               } n             } n   } n   x = SS.size() % 2; n   for (i = 0; i < 201; i++) n     for (j = 0; j < K + 1; j++) n       ret += dpdp[x][i][j][0][0] + dpdp[x][i][j][0][1] + dpdp[x][i][j][1][0] + n              dpdp[x][i][j][1][1]; n   return ret % mo; n } n void solve() { n   int f, i, j, k, l, x, y; n   cin >> N >> M >> K; n   cin >> L; n   for (i = 0; i < L; i++) cin >> x, LL += x; n   cin >> R; n   for (i = 0; i < R; i++) cin >> x, RR += x; n   for (i = 0; i < N; i++) { n     cin >> j; n     string s; n     while (j--) cin >> x, s += x; n     S.push_back(s); n     cin >> V[i]; n   } n   create(S); n   i = LL.size() - 1; n   LL[i] -= 1; n   while (LL[i] < 0) LL[i] += M, LL[--i] -= 1; n   while (LL.size() > 0 && LL[0] == 0) LL.erase(LL.begin()); n   cout << (dodo(RR) - dodo(LL) + mo) % mo << endl; n } n int main(int argc, char** argv) { n   string s; n   if (argc == 1) ios::sync_with_stdio(false); n   for (int i = 1; i < argc; i++) s += argv[i], s +=   n ; n   for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin); n   solve(); n   return 0; n } n