#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n const int mod = 1e9 + 7; n namespace DSU { n int fa[maxn], cycle[maxn], sz[maxn]; n void init() { n   for (int i = 1; i < maxn; i++) fa[i] = i, sz[i] = 1, cycle[i] = 0; n } n int find_set(int x) { n   if (x == fa[x]) n     return x; n   else n     return fa[x] = find_set(fa[x]); n } n void union_set(int x, int y) { n   int fx = find_set(x); n   int fy = find_set(y); n   if (fx == fy) { n     cycle[fx] = 1; n   } else { n     fa[fy] = fx; n     sz[fx] += sz[fy]; n     cycle[fx] |= cycle[fy]; n   } n } n }  // namespace DSU n using namespace DSU; n int main() { n   int n; n   cin >> n; n   init(); n   long long ans = 1; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     if (x == y) { n       cycle[find_set(x)] = 2; n       continue; n     } n     union_set(x, y); n   } n   for (int i = 1; i <= 2 * n; i++) { n     if (find_set(i) == i) { n       if (cycle[i] == 1) n         ans = ans * 2 % mod; n       else { n         if (cycle[i] == 0) ans = ans * sz[i] % mod; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50 + 5; n int R, C, K; n char g[maxn][maxn]; n int sx, sy, tx, ty; n bool flag[300]; n int mindist; n string ans; n const int dx[] = {0, 1, -1, 0}; n const int dy[] = {1, 0, 0, -1}; n bool mark[maxn][maxn]; n int dist[maxn][maxn]; n string board[maxn][maxn]; n set<pair<int, int> > grid[maxn * maxn]; n void calc() { n   memset(dist, -1, sizeof dist); n   memset(mark, false, sizeof mark); n   queue<pair<int, int> > q; n   dist[tx][ty] = 0; n   board[tx][ty] =   ; n   q.push(make_pair(tx, ty)); n   while (!q.empty()) { n     int x = q.front().first; n     int y = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; ++i) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (xx >= 0 && xx < R && yy >= 0 && yy < C && n           (flag[g[xx][yy]] || g[xx][yy] ==  S )) { n         if (dist[xx][yy] == -1) { n           dist[xx][yy] = dist[x][y] + 1; n           q.push(make_pair(xx, yy)); n         } n       } n     } n   } n   if (dist[sx][sy] == -1) return; n   string ret =   ; n   int curd = dist[sx][sy]; n   grid[curd].clear(); n   grid[curd].insert(make_pair(sx, sy)); n   while (curd > 0) { n     char best =  z  + 1; n     for (set<pair<int, int> >::iterator it = grid[curd].begin(); n          it != grid[curd].end(); ++it) { n       for (int i = 0; i < 4; ++i) { n         int xx = it->first + dx[i], yy = it->second + dy[i]; n         if (xx >= 0 && xx < R && yy >= 0 && yy < C && n             dist[xx][yy] + 1 == curd) { n           if (g[xx][yy] < best) { n             best = g[xx][yy]; n             grid[curd - 1].clear(); n             grid[curd - 1].insert(make_pair(xx, yy)); n           } else if (best == g[xx][yy]) { n             grid[curd - 1].insert(make_pair(xx, yy)); n           } n         } n       } n     } n     ret += best; n     curd--; n   } n   ret = ret.substr(0, (int)ret.size() - 1); n   if (mindist == -1 || dist[sx][sy] < mindist || n       dist[sx][sy] == mindist && ret < ans) { n     ans = ret; n     mindist = dist[sx][sy]; n   } n } n void dfs(int dep, int last) { n   if (last == 26) { n     if (dep == 0) { n       calc(); n     } n     return; n   } n   dfs(dep, last + 1); n   if (dep > 0) { n     flag[ a  + last] = 1; n     dfs(dep - 1, last + 1); n     flag[ a  + last] = 0; n   } n } n int main() { n   scanf( %d%d%d , &R, &C, &K); n   for (int i = 0; i < R; ++i) { n     scanf( %s , g[i]); n   } n   for (int i = 0; i < R; ++i) { n     for (int j = 0; j < C; ++j) { n       if (g[i][j] ==  S ) { n         sx = i; n         sy = j; n       } n       if (g[i][j] ==  T ) { n         tx = i; n         ty = j; n       } n     } n   } n   if (abs(sx - tx) + abs(tx - ty) == 1) { n     puts(  ); n     return 0; n   } n   ans =   ; n   mindist = -1; n   memset(flag, 0, sizeof flag); n   dfs(K, 0); n   if (mindist == -1) { n     printf( %d n , -1); n   } else { n     puts(ans.c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int board[9][9]; n int bBoard[9][9]; n int best, N, M; n void saveBoard() { n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) bBoard[i][j] = board[j][i]; n } n void printBoard() { n   for (int i = 0; i < N; i++, cout << endl) n     for (int j = 0; j < M; j++) n       cout << (char)(bBoard[i][j] +  A  ==  A  ?  .  : bBoard[i][j] +  A  - 1); n } n int dx[][5] = { n     {0, -1, -2, -1, -1}, n     {-2, 0, -1, -2, -2}, n     {-1, -1, 0, -1, -2}, n     {0, 0, -1, -2, 0}, n }; n int dy[][5] = { n     {0, 0, 0, -1, -2}, n     {0, -1, -1, -1, -2}, n     {0, -1, -2, -2, -2}, n     {0, -1, -1, -1, -2}, n }; n bool place(int x, int y, int cur, int t) { n   for (int i = 0; i < 5; i++) { n     int nx = x + dx[t][i], ny = y + dy[t][i]; n     if (nx < 0 || ny < 0 || board[nx][ny]) return false; n   } n   for (int i = 0; i < 5; i++) { n     int nx = x + dx[t][i], ny = y + dy[t][i]; n     board[nx][ny] = cur; n   } n   return true; n } n void unplace(int x, int y, int cur, int t) { n   for (int i = 0; i < 5; i++) { n     int nx = x + dx[t][i], ny = y + dy[t][i]; n     board[nx][ny] = 0; n   } n } n void search(int x, int y, int cur) { n   if (M * (N - y) + M - x <= 5 * (best - cur)) return; n   if (x == M) { n     y++; n     x = 0; n   } n   if (y == N) { n     if (cur > best) { n       best = cur; n       saveBoard(); n     } n     return; n   } n   for (int i = 0; i < 4; i++) { n     if (place(x, y, cur, i)) { n       search(x + 1, y, cur + 1); n       unplace(x, y, cur, i); n     } n   } n   search(x + 1, y, cur); n } n int main() { n   cin >> N >> M; n   search(0, 0, 1); n   cout << (best - 1) << endl; n   printBoard(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n const int mod = 1e9 + 7; n const long long INF = 2e15; n long long qpower(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = ans * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return ans; n } n long long n, k; n int main(int argc, char const *argv[]) { n   std::cin >> n >> k; n   if (k == 0) { n     std::cout << n <<   n ; n     return 0; n   } n   int cnt = 0; n   for (int i = 0; i < k; i++) { n     if (n % 10 == 0) { n       cnt++; n       n /= 10; n     } else n       break; n   } n   for (int i = cnt; i < k; i++) { n     if (n % 5 == 0) n /= 5; n     if (n % 2 == 0) n /= 2; n   } n   for (int i = 1; i <= k; i++) { n     n *= 10; n   } n   std::cout << n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ostream &operator<<(ostream &out, string str) { n   for (char c : str) out << c; n   return out; n } n template <class L, class R> n ostream &operator<<(ostream &out, pair<L, R> p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { n   out <<  { ; n   for (auto it = a.begin(); it != a.end(); it = next(it)) n     out << (it != a.begin() ?  ,   :   ) << *it; n   return out <<  } ; n } n void dump() { cerr <<   n ; } n template <class T, class... Ts> n void dump(T a, Ts... x) { n   cerr << a <<  ,  ; n   dump(x...); n } n template <class T> n int size(T &&a) { n   return a.size(); n } n using LL = long long; n using PII = pair<int, int>; n mt19937 _gen(chrono::system_clock::now().time_since_epoch().count()); n int rd(int a, int b) { return uniform_int_distribution<int>(a, b)(_gen); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   for (int _t = 0; _t < t; _t++) { n     int n, m; n     cin >> n >> m; n     vector<vector<int>> v(n, vector<int>(m)); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) cin >> v[i][j]; n     vector<int> ord(m); n     vector<int> mx(m); n     for (int j = 0; j < m; j++) { n       ord[j] = j; n       for (int i = 0; i < n; i++) mx[j] = max(mx[j], v[i][j]); n     } n     sort(ord.begin(), ord.end(), [&](int a, int b) { return mx[a] > mx[b]; }); n     vector<int> rev; n     for (int i = 0; i < min(n, m); i++) rev.emplace_back(ord[i]); n     vector<vector<int>> t(n, vector<int>(size(rev))); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < size(rev); j++) t[i][j] = v[i][rev[j]]; n     m = size(rev); n     int p = (1 << n); n     vector<vector<int>> dp(m, vector<int>(p)); n     vector<vector<int>> sum(m, vector<int>(p)); n     auto mymod = [&](int val) { n       if (val >= n) return val - n; n       return val; n     }; n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < p; k++) { n         for (int r = 0; r < n; r++) { n           int sumka = 0; n           for (int i = 0; i < n; i++) { n             if (k & (1 << mymod(i + r))) sumka += t[i][j]; n           } n           sum[j][k] = max(sum[j][k], sumka); n         } n         false; n       } n     } n     for (int j = 0; j < m; j++) { n       if (j != 0) dp[j] = dp[j - 1]; n       for (int k = 0; k < p; k++) { n         if (j == 0) n           dp[j][k] = sum[j][k]; n         else { n           for (int l = k; l > 0; l = (l - 1) & k) { n             dp[j][k] = max(dp[j][k], dp[j - 1][k ^ l] + sum[j][l]); n           } n         } n       } n       false; n     } n     cout << dp[m - 1][p - 1] <<   n ; n   } 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, m; n     cin >> n >> m; n     string s; n     cin >> s; n     vector<int> v(m); n     for (int i = 0; i < m; i++) { n       cin >> v[i]; n     } n     sort(v.begin(), v.end()); n     vector<int> ans(26); n     for (int i = 0; i < n; i++) { n       int k = v.end() - upper_bound(v.begin(), v.end(), i); n       ans[s[i] -  a ] += (k + 1); n     } n     for (auto x : ans) cout << x <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > g[100000]; n vector<pair<int, int> > t[100000]; n int n, k; n int dp[100000][2]; n bool cmp(pair<int, int> f, pair<int, int> s) { n   if (dp[f.first][0] + f.second != dp[s.first][0] + s.second) n     return dp[f.first][0] + f.second > dp[s.first][0] + s.second; n   else n     return f.first < s.first; n } n void dfs(int cur, int par = -1) { n   for (int i = 0; i < g[cur].size(); i++) { n     if (g[cur][i].first == par) continue; n     dfs(g[cur][i].first, cur); n     t[cur].push_back(g[cur][i]); n   } n   sort(t[cur].begin(), t[cur].end(), cmp); n   for (int i = 0; i < min(k - 1, (int)t[cur].size()); i++) { n     dp[cur][0] += dp[t[cur][i].first][0] + t[cur][i].second; n   } n   int add = 0; n   if (t[cur].size() >= k) { n     add = dp[t[cur][k - 1].first][0] + t[cur][k - 1].second; n   } n   for (int i = 0; i < t[cur].size(); i++) { n     if (i < k) { n       dp[cur][1] = max(dp[cur][1], dp[cur][0] + dp[t[cur][i].first][1] - n                                        dp[t[cur][i].first][0] + add); n     } else { n       dp[cur][1] = max(dp[cur][1], n                        dp[cur][0] + dp[t[cur][i].first][1] + t[cur][i].second); n     } n   } n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n - 1; i++) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     g[a].push_back(make_pair(b, c)); n     g[b].push_back(make_pair(a, c)); n   } n   dfs(0); n   cout << max(dp[0][0], dp[0][1]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, h; n int m[100001]; n int v[100001]; n int id[100001]; n int match[100001]; n int res[100001]; n double eps = 1e-9; n void qsort1(int, int); n void qsort2(int, int); n bool ok(double); n int main() { n   cin >> n >> k >> h; n   for (int i = 1; i <= n; i++) { n     cin >> m[i]; n     id[i] = i; n   } n   for (int i = 1; i <= n; i++) cin >> v[i]; n   qsort1(1, n); n   int first = 1; n   for (int i = 2; i <= n; i++) n     if (m[i] != m[i - 1]) { n       qsort2(first, i - 1); n       first = i; n     } n   if (m[n] == m[n - 1]) qsort2(first, n); n   double hi, lo; n   lo = 0; n   hi = 1e9; n   for (int j = 1; j <= 100; j++) { n     double mid = (hi + lo) / 2; n     if (ok(mid)) { n       hi = mid; n       for (int i = 1; i <= k; i++) res[i] = match[i]; n     } else n       lo = mid; n   } n   for (int i = 1; i <= k; i++) cout << res[i] <<    ; n   cout << endl; n   return 0; n } n bool ok(double mid) { n   int j = 1; n   for (int i = 1; i <= k; i++) { n     while (j <= n && double(i * h) / double(v[j]) > mid + eps) j++; n     if (j > n) return false; n     match[i] = id[j]; n     j++; n   } n   return true; n } n void qsort1(int l, int r) { n   if (l >= r) return; n   int i = l; n   int j = r; n   int mid = m[(l + r) / 2]; n   while (i < j) { n     while (m[i] < mid) i++; n     while (mid < m[j]) j--; n     if (i <= j) { n       swap(m[i], m[j]); n       swap(v[i], v[j]); n       swap(id[i], id[j]); n       i++; n       j--; n     } n   } n   qsort1(i, r); n   qsort1(l, j); n } n void qsort2(int l, int r) { n   if (l >= r) return; n   int i = l; n   int j = r; n   int mid = v[(l + r) / 2]; n   while (i < j) { n     while (v[i] < mid) i++; n     while (mid < v[j]) j--; n     if (i <= j) { n       swap(v[i], v[j]); n       swap(m[i], m[j]); n       swap(id[i], id[j]); n       i++; n       j--; n     } n   } n   qsort2(i, r); n   qsort2(l, j); n } n


#include <bits/stdc++.h> n using namespace std; n long long x, x2, y, y2, n, m, k, a, b, rep, koliko, koliko2, p; n long long hi, lo, mid; n bool t; n char c; n string s; n int main() { n   vector<long long> V; n   cin >> n >> k; n   k = n - k; n   for (int i = 0; i < n; i++) { n     cin >> x; n     V.push_back(x); n   } n   lo = 1; n   hi = 1000000010; n   while (lo < hi) { n     mid = (lo + hi) / 2; n     koliko = 0; n     p = 0; n     while (p < V.size()) { n       if (V[p] > mid) { n         koliko++; n         p++; n       } else { n         p += 2; n       } n     } n     koliko2 = 0; n     p = 1; n     while (p < V.size()) { n       if (V[p] > mid) { n         koliko2++; n         p++; n       } else { n         p += 2; n       } n     } n     if (min(koliko, koliko2) <= k) { n       hi = mid; n     } else { n       lo = mid + 1; n     } n   } n   cout << lo <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[26]; n int b[26]; n bool ok; n inline void go(int v, int rem, int cur, int last) { n   if (v == n) { n     ok = 1; n     for (int i = 1; i <= n && ok; ++i) n       if (b[i]) ok = 0; n     if (ok) { n       puts( YES ); n       exit(0); n     } n   } n   if (rem == 1) { n     if (cur == 1) return; n     if (v + 1 != n) ++b[a[v]]; n     go(v + 1, a[v + 1], 0, n); n     if (v + 1 != n) --b[a[v]]; n     return; n   } n   if (!cur) { n     for (int i = rem - 2; i >= 1; --i) { n       if (!b[i]) continue; n       --b[i]; n       go(v, rem - i, cur + 1, i); n       ++b[i]; n     } n   } else { n     for (int i = min(rem - 1, last); i >= 1; --i) { n       if (!b[i]) continue; n       --b[i]; n       go(v, rem - i, cur + 1, i); n       ++b[i]; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   ok = 1; n   for (int i = 0; i < n; ++i) n     if (a[i] == 2) ok = 0; n   if (a[n - 1] != n || !ok) { n     puts( NO ); n     return 0; n   } n   go(0, a[0], 0, n); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n inline pair<int, int> operator-(const pair<int, int> &a, n                                 const pair<int, int> &b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n inline int vec(const pair<int, int> &a, const pair<int, int> &b) { n   return a.first * b.second - b.first * a.second; n } n int a, b, c, d, n, m, k; n pair<int, int> mas[3005], tmp[3005], res[3005], hull[3005]; n inline int dist(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first - b.first) * (a.first - b.first) + n          (a.second - b.second) * (a.second - b.second); n } n inline bool cool(pair<int, int> *mas, int n, long double xx) { n   if (n < 2) return 0; n   int k = 0; n   for (int _n((2) - 1), iter(0); iter <= _n; iter++) { n     int h = 0; n     for (int _n((n)-1), i(0); i <= _n; i++) { n       while (h >= 2 && n              vec(mas[i] - hull[h - 1], hull[h - 1] - hull[h - 2]) <= 0) n         --h; n       hull[h++] = mas[i]; n     } n     --h; n     for (int _n((h)-1), i(0); i <= _n; i++) res[k++] = hull[i]; n     reverse(mas, mas + n); n   } n   if (k == 2) { n     if (dist(res[0], res[1]) >= xx) n       return 1; n     else n       return 0; n   } n   int a = 0; n   for (int _n((k)-1), i(0); i <= _n; i++) { n     if (a == i) { n       a = i + 1; n       if (a == k) a = 0; n     } n     while (1) { n       int t = a + 1; n       if (t >= k) t = 0; n       if (t == i) break; n       int s1 = dist(res[i], res[a]); n       int s2 = dist(res[i], res[t]); n       if (s2 >= s1) n         a = t; n       else n         break; n     } n     if (dist(res[i], res[a]) >= xx) return 1; n   } n   return 0; n } n inline bool check(long double xx) { n   for (int _n((n)-1), v(0); v <= _n; v++) { n     int k = 0; n     for (int _n(n - 1), i(v + 1); i <= _n; i++) { n       int s = (mas[v].first - mas[i].first) * (mas[v].first - mas[i].first) + n               (mas[v].second - mas[i].second) * (mas[v].second - mas[i].second); n       if (s >= xx) { n         tmp[k++] = mas[i]; n       } n     } n     if (cool(tmp, k, xx)) return 1; n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int _n((n)-1), i(0); i <= _n; i++) { n     scanf( %d%d , &mas[i].first, &mas[i].second); n   } n   sort(mas, mas + n); n   long double l = 0.0, r = 4.1e9; n   for (int _n((55) - 1), hhod(0); hhod <= _n; hhod++) { n     long double xx = (r + l) / 2.0; n     if (check(xx)) n       l = xx; n     else n       r = xx; n   } n   double ans = (double)(sqrt(l / 4)); n   printf( %.12lf n , (double)(sqrt(l / 4))); n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -fhoist-adjacent-loads ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n inline void write(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n int a[25]; n int main() { n   ios::sync_with_stdio(false); n   int n, k, l = 0, i = 1; n   n = read(); n   k = read(); n   if (k == 1) { n     write(n); n     puts(  ); n   } else { n     while (i++) { n       while (n % i == 0) { n         n /= i; n         a[++l] = i; n         if (k == l + 1) { n           if (n == 1) { n             puts( -1 ); n             break; n           } n           for (register int j = 1; j <= l; j++) { n             write(a[j]); n             putchar(   ); n           } n           write(n); n           break; n         } n       } n       if (k == l + 1) break; n       if (i > n) { n         puts( -1 ); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, N = 1e5 + 5; n int n, c[N], x; n long long dp[N][2]; n vector<int> g[N]; n void dfs(int v) { n   dp[v][0] = 1LL; n   for (int u : g[v]) { n     dfs(u); n     (dp[u][0] += dp[u][1]) %= mod; n     dp[v][1] = (dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1]) % mod; n     dp[v][0] = (dp[v][0] * dp[u][0]) % mod; n   } n   if (c[v]) { n     dp[v][1] = dp[v][0]; n     dp[v][0] = 0; n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> x; n     g[x].push_back(i); n   } n   for (int i = 0; i < n; i++) cin >> c[i]; n   dfs(0); n   cout << dp[0][1]; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-13; n const long double PI = acos(-1); n const int INF = (int)1e9; n const long long INFF = (long long)1e18; n const int mod = (int)1e9 + 7; n const int MXN = (int)1e6 + 7; n int n, k; n vector<int> edge[MXN]; n vector<int> re_edge[MXN]; n int prt[MXN]; n int dp[21][MXN]; n void init() { n   scanf( %d %d , &n, &k); n   for (int i = 2; i < n + 1; i++) { n     scanf( %d , prt + i); n     edge[prt[i]].push_back(i); n     re_edge[i].push_back(prt[i]); n     dp[0][i] = prt[i]; n   } n   return; n } n bool leaf[MXN]; n struct Scc { n   int re[MXN], cntp; n   int in[MXN]; n   int siz[MXN], gp[MXN]; n   int num[MXN]; n   int val[MXN]; n   int fd(int u) { return gp[u] == u ? u : gp[u] = fd(gp[u]); } n   void uni(int u, int v) { n     u = fd(u), v = fd(v); n     if (u == v) return; n     if (siz[u] > siz[v]) swap(u, v); n     gp[u] = v; n     siz[u] += siz[v]; n     return; n   } n   void dfs(int u) { n     in[u] = 1; n     for (int v : edge[u]) { n       if (!in[v]) dfs(v); n     } n     re[++cntp] = u; n     return; n   } n   void dfs2(int u, int la) { n     in[u] = 1; n     uni(u, la); n     for (int v : re_edge[u]) { n       if (!in[v]) dfs2(v, la); n     } n   } n   void pre() { n     for (int i = 1; i < n + 1; i++) siz[i] = 1, gp[i] = i; n     for (int i = 1; i < n + 1; i++) n       if (!in[i]) dfs(i); n     memset(in, 0, sizeof(in)); n     ; n     for (int i = n; i >= 1; i--) n       if (!in[re[i]]) dfs2(re[i], re[i]); n     for (int i = 1; i < n + 1; i++) n       if (leaf[i]) { n         num[fd(i)]++; n         val[fd(i)]++; n       } n     return; n   } n   int solve() { n     pre(); n     int ans = 0; n     for (int i = 1; i < n + 1; i++) { n       ans = max(ans, val[fd(i)]); n       for (int v : edge[re[i]]) { n         int p1 = fd(v); n         int p2 = fd(re[i]); n         if (p1 == p2) continue; n         val[p2] = max(val[p2], num[p2] + val[p1]); n         ans = max(ans, val[p2]); n       } n     } n     return ans; n   } n } scc; n int dep[MXN]; n void dfs(int u, int dpt) { n   dep[u] = dpt; n   for (int v : edge[u]) dfs(v, dpt + 1); n   return; n } n int up(int u, int d) { n   for (int i = 0; i < 21; i++) { n     if (d & (1 << i)) { n       u = dp[i][u]; n     } n   } n   return u; n } n void sol() { n   dfs(1, 1); n   for (int i = 1; i < 21; i++) n     for (int j = 1; j < n + 1; j++) { n       dp[i][j] = dp[i - 1][dp[i - 1][j]]; n     } n   for (int i = 1; i < n + 1; i++) n     if (edge[i].empty()) { n       leaf[i] = 1; n       int p = 0; n       if (dep[i] <= k) n         p = 1; n       else n         p = up(i, k); n       edge[i].push_back(p); n       re_edge[p].push_back(i); n     } n   int ans = scc.solve(); n   printf( %d n , ans); n   return; n } n int main(void) { n   init(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXL = 2000005; n string pat[100005]; n int beg[2000005]; n int bel[2000005]; n int id[4200000]; n int L = 0; n inline void modify(int a, int b, int val, int v = 1, int l = 1, int r = MAXL) { n   if (b < l || a > r) return; n   if (a <= l && r <= b) { n     id[v] = val; n     return; n   } n   int m = l + r >> 1; n   if (id[v]) id[v << 1] = id[v << 1 | 1] = id[v]; n   id[v] = 0; n   modify(a, b, val, v << 1, l, m); n   modify(a, b, val, v << 1 | 1, m + 1, r); n   return; n } n inline int query(int p, int v = 1, int l = 1, int r = MAXL) { n   if (p < l || p > r) return 0; n   if (id[v]) return id[v]; n   if (l == r) return 0; n   int m = l + r >> 1; n   return max(query(p, v << 1, l, m), query(p, v << 1 | 1, m + 1, r)); n } n int cnt = 0; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> pat[i]; n     int len = pat[i].size(); n     int m; n     cin >> m; n     while (m--) { n       cin >> beg[++cnt]; n       bel[cnt] = i; n       modify(beg[cnt], beg[cnt] + len - 1, cnt); n       L = max(L, beg[cnt] + len - 1); n     } n   } n   for (int i = 1; i <= L; ++i) { n     int q = query(i); n     if (!q) n       cout <<  a ; n     else { n       cout << pat[bel[q]][i - beg[q]]; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void optimizeIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int32_t main() { n   optimizeIO(); n   long long tt = 1; n   cin >> tt; n   while (tt--) { n     long long n; n     cin >> n; n     vector<bool> v(65, 0); n     while (n) { n       long long temp = 1; n       long long ind = 0; n       while (temp < n) { n         temp *= 3; n         ind++; n       } n       if (temp != n) { n         temp /= 3; n         ind--; n       } n       if (!v[ind]) n         v[ind] = true; n       else { n         for (long long i = ind; i < 65; i++) { n           if (v[i]) n             v[i] = !v[i]; n           else { n             v[i] = true; n             break; n           } n         } n         break; n       } n       n -= temp; n     } n     long long res = 0; n     for (long long i = (long long)(0); i < (long long)(65); i++) { n       if (v[i]) { n         long long temp = 1; n         for (long long j = (long long)(0); j < (long long)(i); j++) { n           temp *= 3; n         } n         res += temp; n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str[51]; n bool visited[51][51], flag = false; n int arrx[] = {1, -1, 0, 0}, arry[] = {0, 0, 1, -1}, n, m; n void dfs(int x, int y, int oldX, int oldY, char renk) { n   if (x < 0 || x >= n || y < 0 || y >= m) return; n   if (str[x][y] != renk) return; n   if (visited[x][y] == true) { n     flag = true; n     return; n   } n   visited[x][y] = true; n   for (int i = 0; i < 4; i++) { n     int newX = x + arrx[i], newY = y + arry[i]; n     if (newX == oldX && newY == oldY) continue; n     dfs(newX, newY, x, y, renk); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> str[i]; n   memset(visited, 0, sizeof(visited)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (!visited[i][j]) dfs(i, j, -1, -1, str[i][j]); n     } n   } n   cout << (flag ?  Yes  :  No ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:666000000 ) n using namespace std; n const int inf = (1 << 30) - 1; n const long double eps = 1e-9; n const long double pi = fabs(atan2(0.0, -1.0)); n void ML(const bool v) { n   if (v) return; n   int *ass; n   for (;;) { n     ass = new int[2500000]; n     for (int i = 0; i < 2500000; i++) ass[i] = rand(); n   } n } n void TL(const bool v) { n   if (v) return; n   int *ass; n   ass = new int[250]; n   for (;;) { n     for (int i = 0; i < 250; i++) ass[i] = rand(); n   } n } n void PE(const bool v) { n   if (v) return; n   puts( ass 1 2 3 4 5 6 fuck ); n   exit(0); n } n int n, m; n bool was[300][300]; n void LoAd() { cin >> n >> m; } n vector<pair<int, int> > res; n bool swapped; n int x = 1, y = 1; n bool can_dn() { return !was[x + 1][y]; } n bool can_up() { return !was[x - 1][y]; } n bool can_lt() { return !was[x][y - 1]; } n bool can_rt() { return !was[x][y + 1]; } n void go_dn() { n   res.push_back(make_pair(x, y)); n   was[x][y] = true; n   x++; n } n void go_up() { n   res.push_back(make_pair(x, y)); n   was[x][y] = true; n   x--; n } n void go_lt() { n   res.push_back(make_pair(x, y)); n   was[x][y] = true; n   y--; n } n void go_rt() { n   res.push_back(make_pair(x, y)); n   was[x][y] = true; n   y++; n } n void SoLvE() { n   if (min(n, m) == 1 && max(n, m) == 2) { n     puts( 0 ); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) printf( %d %d n , i, j); n     printf( 1 1 ); n     return; n   } n   if (n == 1 || m == 1) { n     puts( 1 ); n     printf( %d %d %d %d n , n, m, 1, 1); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) printf( %d %d n , i, j); n     printf( 1 1 ); n     return; n   } n   if (n % 2 == 0 && m % 2 == 1) { n     swap(n, m); n     swapped = true; n   } n   for (int i = 0; i < 300; i++) n     for (int j = 0; j < 300; j++) n       if (1 <= i && i <= n && 1 <= j && j <= m) n         was[i][j] = false; n       else n         was[i][j] = true; n   if (1 == n % 2 && 1 == m % 2) { n     puts( 1 ); n     printf( %d %d %d %d n , n, m, 1, 1); n     while (x != n || y != m) { n       while (can_dn()) go_dn(); n       if (can_rt()) go_rt(); n       while (can_up()) go_up(); n       if (can_rt()) go_rt(); n     } n     res.push_back(make_pair(n, m)); n     res.push_back(make_pair(1, 1)); n   } else { n     puts( 0 ); n     while (true) { n       while (can_dn()) go_dn(); n       if (can_rt()) go_rt(); n       if (x == n && y == m) break; n       while (can_up() && x > 2) go_up(); n       if (can_rt()) go_rt(); n     } n     while (can_up()) go_up(); n     while (can_lt()) go_lt(); n     res.push_back(make_pair(x, y)); n     res.push_back(make_pair(1, 1)); n   } n   for (int i = 0; i < (int)res.size(); i++) { n     if (swapped) swap(res[i].first, res[i].second); n     printf( %d %d n , res[i].first, res[i].second); n   } n } n int main() { n   srand((int)time(NULL)); n   LoAd(); n   SoLvE(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005], b[1000005], ans[1000005]; n int main() { n   int n; n   cin >> n; n   vector<int> v; n   int y = 1; n   while (y < n) { n     v.push_back(y); n     y *= 2; n   } n   int j = v.size() - 1, x; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i < n - 1) { n       x = a[i] + b[i]; n       if (i == 0) n         ans[i] = x; n       else n         ans[i] = ans[i - 1] + x; n       while (i + v[j] >= n && j < v.size()) { n         j--; n       } n       b[i + v[j]] += x; n     } n   } n   for (int i = 0; i < n - 1; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long PLUS = -1; n const long long MULT = -2; n string fo; n vector<long long> tmp, whereMULT; n long long i, nr, lo, hi, total, add, crt, j, p, ans; n int main() { n   cin >> fo; n   fo =  1*  + fo +  *1 ; n   for (i = 0; i < fo.size();) { n     if (!( 0  <= fo[i] && fo[i] <=  9 )) { n       (fo[i] ==  + ) ? tmp.push_back(PLUS) : tmp.push_back(MULT); n       i += 1; n     } n     long long nr = 0; n     while ( 0  <= fo[i] && fo[i] <=  9 ) { n       nr = nr * 10 + fo[i] -  0 ; n       i += 1; n     } n     tmp.push_back(nr); n   } n   for (i = 0; i < tmp.size(); ++i) n     if (tmp[i] == MULT) whereMULT.push_back(i); n   for (i = 0; i < whereMULT.size(); ++i) n     for (j = i + 1; j < whereMULT.size(); ++j) { n       lo = whereMULT[i] + 1, hi = whereMULT[j] - 1; n       crt = 0; n       add = tmp[lo]; n       lo += 1; n       p = 0; n       while (lo <= hi) { n         if (tmp[lo] == MULT) { n           lo += 1; n           add *= tmp[lo]; n           lo += 1; n         } else { n           crt += add; n           lo += 1; n           add = tmp[lo]; n           lo += 1; n         } n       } n       crt += add; n       lo = 1, hi = tmp.size() - 1; n       total = 0, add = 1; n       while (lo <= hi) { n         if (lo == whereMULT[i]) { n           add *= crt; n           lo = whereMULT[j]; n           continue; n         } n         if (tmp[lo] == MULT) { n           lo += 1; n           add *= tmp[lo]; n           lo += 1; n         } else { n           total += add; n           lo += 1; n           add = tmp[lo]; n           lo += 1; n         } n       } n       total += add; n       ans = max(ans, total); n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int su(int n) { n   int r, sum = 0; n   while (n != 0) { n     r = n % 10; n     sum = sum + r; n     n = n / 10; n   } n   return sum; n } n int main() { n   int n; n   cin >> n; n   int k = n; n   int i = 0; n   while (i != 10) { n     int d = su(k + i); n     if (d % 4 == 0) { n       cout << k + i << endl; n       break; n     } n     i++; n   } 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   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     int n, m; n     cin >> n >> m; n     string s[n]; n     for (int i = 0; i < n; ++i) { n       cin >> s[i]; n     } n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (s[i][j] ==  B ) cnt++; n       } n     } n     cnt = sqrt(cnt); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (s[i][j] ==  B ) { n           cout << i + cnt / 2 + 1 <<     << j + cnt / 2 + 1 << endl; n           return 0; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long res, d, e, k; n int N; n int h[2020]; n long long dp[2020][2020]; n long long pw(long long a, long long n) { n   long long ret = 1; n   while (n) { n     if (n % 2) ret *= a, ret %= 998244353; n     a *= a, a %= 998244353; n     n >>= 1; n   } n   return ret; n } n long long C(long long n, long long r) { n   if (r == 0 || r == n) return 1; n   if (dp[n][r]) return dp[n][r]; n   return dp[n][r] = (C(n - 1, r - 1) + C(n - 1, r)) % 998244353; n } n int main() { n   scanf( %d %d , &N, &k); n   if (k == 1) return !printf( 0 ); n   for (int i = 0; i < N; i++) scanf( %d , &h[i]); n   for (int i = 0; i < N; i++) { n     if (h[i] == h[(i + 1) % N]) n       e++; n     else n       d++; n   } n   for (long long s = 0; s < d; s++) { n     long long mul = 1; n     mul *= pw(k - 2, s); n     mul %= 998244353; n     mul *= C(d, s); n     mul %= 998244353; n     long long sum = 0; n     for (long long m = 0; m <= (d - s - 1) / 2; m++) { n       sum += C(d - s, m); n       sum %= 998244353; n     } n     mul *= sum; n     mul %= 998244353; n     res += mul; n     res %= 998244353; n   } n   res *= pw(k, e); n   res %= 998244353; n   printf( %lld , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, n     x, y, z, ans, maxi, mini, res; n string s1, s2; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> x; n   if (x == 2) n     cout << 2; n   else n     cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int on[100005]; n int prime[100005], cnt = 0; n void ola() { n   for (int i = 2; i <= 100005; i++) { n     if (!on[i]) prime[cnt++] = i; n     for (int j = 0; j < cnt && i * prime[j] <= 100005; j++) { n       on[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n int main() { n   int n, flag = 0; n   cin >> n; n   ola(); n   for (int i = 2; i <= n + 1; i++) { n     if (on[i] == 1) { n       on[i] = 2; n       flag = 1; n     } else n       on[i] = 1; n   } n   if (flag == 1) n     cout << 2 << endl; n   else n     cout << 1 << endl; n   for (int i = 2; i <= n; i++) cout << on[i] <<    ; n   cout << on[n + 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 400005; n int n, q, a[maxn]; n namespace SGT { n int Min[maxn << 2]; n void modify(int l, int r, int id, int x, int y) { n   if (l == r) { n     Min[id] = y; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     modify(l, mid, id << 1, x, y); n   else n     modify(mid + 1, r, id << 1 | 1, x, y); n   Min[id] = min(Min[id << 1], Min[id << 1 | 1]); n } n int query(int l, int r, int id, int L, int R) { n   if (L <= l && r <= R) return Min[id]; n   int mid = (l + r) >> 1, res = 1e9; n   if (L <= mid) res = min(res, query(l, mid, id << 1, L, R)); n   if (R > mid) res = min(res, query(mid + 1, r, id << 1 | 1, L, R)); n   return res; n } n }  // namespace SGT n struct query { n   int r1, c1, c2, id; n   query() {} n   query(int _r1, int _c1, int _c2, int _i) { n     r1 = _r1, c1 = _c1, c2 = _c2, id = _i; n   } n }; n int ans[maxn], now[maxn]; n vector<query> Q[2][maxn]; n void solve(vector<query> *Q) { n   static int st[maxn], num[maxn], top; n   top = 0; n   for (int i = 1; i <= n; i++) { n     while (top && num[top] >= a[i]) top--; n     st[++top] = i; n     num[top] = a[i]; n     SGT ::modify(1, n, 1, top, -2 * i + a[i]); n     for (auto u : Q[i]) { n       int r1 = u.r1, c1 = u.c1, r2 = i, c2 = u.c2, id = u.id; n       int res = r2 - r1 + c2 + 1; n       int L = lower_bound(st + 1, st + top + 1, r1) - st, R = top; n       now[id] = min(c1, num[L]); n       res = min(res, r2 - r1 + abs(c2 - now[id])); n       int pos = lower_bound(num + L, num + R + 1, c2) - num; n       if (pos <= R) res = min(res, r2 - r1 + 1 + abs(c2 - num[pos])); n       if (pos > L) n         res = min(res, r2 - r1 + 1 + abs(c2 - num[pos - 1])); n       else { n         assert(pos == L); n         pos = upper_bound(num + 1, num + R + 1, c2) - num - 1; n         if (pos && st[pos] <= r1) n           res = min(res, r1 + r2 - 2 * st[pos] + (c1 < num[pos]) + n                              abs(c2 - num[pos])); n       } n       if (pos + 1 < L) { n         R = L - 1, L = pos + 1; n         int mid = upper_bound(num + L, num + R + 1, c1) - num; n         if (mid <= R) n           res = min(res, r1 + r2 + 1 + SGT ::query(1, n, 1, mid, R) - c2); n         if (L < mid) n           res = min(res, r1 + r2 + SGT ::query(1, n, 1, L, mid - 1) - c2); n       } n       ans[id] = res; n     } n   } n   top = 0; n   for (int i = n; i >= 1; i--) { n     while (top && num[top] >= a[i]) top--; n     st[++top] = i; n     num[top] = a[i]; n     SGT ::modify(1, n, 1, top, 2 * i + a[i]); n     for (auto u : Q[i]) { n       int r1 = u.r1, r2 = i, c2 = u.c2, id = u.id; n       int pos = upper_bound(num + 1, num + top + 1, c2) - num - 1, res = 1e9; n       if (pos) n         res = -(r1 + r2) + 2 * st[pos] + (now[id] < num[pos]) + n               abs(c2 - num[pos]); n       if (pos + 1 <= top) { n         int L = pos + 1, R = top; n         int mid = upper_bound(num + L, num + R + 1, now[id]) - num; n         if (mid <= R) n           res = min(res, -(r1 + r2) + 1 + SGT ::query(1, n, 1, mid, R) - c2); n         if (L < mid) n           res = min(res, -(r1 + r2) + SGT ::query(1, n, 1, L, mid - 1) - c2); n       } n       ans[id] = min(ans[id], res); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &q); n   for (int i = 1, r1, c1, r2, c2; i <= q; i++) { n     scanf( %d%d%d%d , &r1, &c1, &r2, &c2); n     if (r1 <= r2) n       Q[0][r2].push_back(query(r1, c1, c2, i)); n     else n       Q[1][n - r2 + 1].push_back(query(n - r1 + 1, c1, c2, i)); n   } n   solve(Q[0]); n   reverse(a + 1, a + n + 1); n   solve(Q[1]); n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1E-9; n const int INF = 1000000000; n const long long INF64 = (long long)1E18; n const double PI = 3.1415926535897932384626433832795; n long long gcd(long long a, long long b) { n   if (a < b) return gcd(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   char *s = new char[2000000]; n   char *z = new char[2000000]; n   long long i, j, n, a, b, c, x = 0, y = 0, slen; n   memset(s, 0, 2000000); n   memset(z, 0, 2000000); n   cin >> s; n   slen = strlen(s); n   for (i = 0; i < slen; ++i) n     if (s[i] ==  x ) n       x++; n     else n       y++; n   if (x > y) { n     x = x - y; n     for (i = 0; i < x; ++i) z[i] =  x ; n   } else { n     y = y - x; n     for (i = 0; i < y; ++i) z[i] =  y ; n   } n   cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, T; n long long doit(long long n, long long k) { n   if (n == 1) return k * (k + 1) / 2; n   long long tp = (n - 1) / k + 1, v1 = doit(tp, k), tp2 = (n - 1) % k + 1; n   long long f1 = (tp - 1) * (k * k + 1), f2 = v1 - f1; n   if (tp2 * k < f2) n     return (f1 * 2 + tp2 * 2 * k - k + 1) * k / 2; n   else if ((tp2 - 1) * k >= f2) n     return (f1 * 2 - k + 3 + tp2 * 2 * k) * k / 2; n   else n     return (f1 * 2 - k + 1 + tp2 * 2 * k) * k / 2 + tp2 * k - f2 + 1; n } n long long solve(long long n, long long k) { n   long long tp = (n - 1) / (k * k + 1) + 1, st = doit(tp, k); n   if (n == st) n     return tp * (k + 1); n   else n     return (n - tp + (n < st) - 1) / k + n - tp + (n < st); n } n int main() { n   scanf( %d , &T); n   while (T--) scanf( %lld%lld , &n, &k), printf( %lld n , solve(n, k)); n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, a[5][200], ans; n vector<pair<int, int> > v, vec; n int getidx(int i, int j) { return (i - 1) * m + j; } n int geti(int idx) { return (idx - 1) / m; } n int getj(int idx) { return (((idx - 1) % m) + 1); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n >> m; n     v.clear(); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         cin >> a[i][j]; n         v.push_back({a[i][j], getidx(i, j)}); n       } n     } n     ans = 0; n     sort(v.rbegin(), v.rend()); n     if (n < 4 || m == 1) { n       for (int i = 0; i < n; i++) { n         ans += v[i].first; n       } n     } else { n       int ar[] = {0, 0, 0, 0, 1, 1, 1, 1}; n       do { n         int tmp = 0; n         vec.clear(); n         for (int i = 0; i < 8; i++) { n           if (ar[i] == 1) { n             int ii = geti(v[i].second); n             int jj = getj(v[i].second); n             vec.push_back({jj, ii}); n             tmp += v[i].first; n           } n         } n         sort(vec.begin(), vec.end()); n         int ct = 1; n         for (int i = 1; i < 4; i++) n           if (vec[i].first != vec[i - 1].first) ct++; n         if (ct != 2 || vec[1].first == vec[2].first || n             ((vec[1].second - vec[0].second + 4) % 2 == n              (vec[3].second - vec[2].second + 4) % 2)) { n           ans = max(ans, tmp); n         } n       } while (next_permutation(ar, ar + 8)); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n const int INF = 2147483647; n using namespace std; n unsigned char like[7][7]; n int fenzu[7]; n int n, totjy[3]; n int getnum(char l[50]) { n   switch (l[0]) { n     case  A : n       return 0; n     case  C : n       if (l[1] ==  h ) n         return 1; n       else n         return 2; n     case  T : n       return 3; n     case  D : n       return 4; n     case  S : n       return 5; n     case  H : n       return 6; n   } n } n void init_and_read() { n   int i; n   char pa[50], pb[50]; n   scanf( %d , &n); n   memset(like, 0, sizeof(like)); n   for (i = 1; i <= n; i++) { n     scanf( %s likes %s , &pa, &pb); n     like[getnum(pa)][getnum(pb)] = 1; n   } n   for (i = 0; i < 3; i++) scanf( %d , &totjy[i]); n } n void Dec_To_Three(int x) { n   int i = 0; n   while (i < 7) { n     fenzu[i] = x % 3; n     x /= 3; n     i++; n   } n } n int Abs(int x) { return (x > 0) ? x : (-x); } n int max(int a, int b, int c) { n   a = (a > b) ? a : b; n   return (a > c) ? a : c; n } n void solve_and_print() { n   int i, j, k, flag, peplo[3], jingyan[3], minidif = INF, maxlike = -INF; n   int tmp, totlike; n   for (i = 0; i <= 2186; i++) { n     Dec_To_Three(i); n     peplo[0] = peplo[1] = peplo[2] = 0; n     for (j = 0; j <= 6; j++) { n       peplo[fenzu[j]]++; n     } n     flag = 0; n     for (j = 0; j <= 2; j++) { n       if (!peplo[j]) { n         flag = 1; n         break; n       } n       jingyan[j] = totjy[j] / peplo[j]; n     } n     if (flag) continue; n     tmp = max(Abs(jingyan[0] - jingyan[1]), Abs(jingyan[0] - jingyan[2]), n               Abs(jingyan[1] - jingyan[2])); n     if (tmp < minidif) { n       minidif = tmp; n       maxlike = -INF; n     } n     if (tmp == minidif) { n       totlike = 0; n       for (j = 0; j <= 6; j++) { n         for (k = 0; k <= 6; k++) { n           if ((fenzu[j] == fenzu[k]) && (like[j][k])) { n             totlike++; n           } n         } n       } n     } n     if (totlike > maxlike) maxlike = totlike; n   } n   printf( %d %d n , minidif, maxlike); n } n int main() { n   init_and_read(); n   solve_and_print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e6 + 7; n const long double maxx = 1e11; n int tmp[MAXN]; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   k--; n   if (k >= n / 2) k = n - k - 1; n   int ans = 0, l = 0, r = 0; n   for (int i = 0, j = n - 1; i < j; i++, j--) { n     tmp[i] = min(abs(s[i] - s[j]), 26 - abs(s[i] - s[j])); n   } n   for (int i = 1; i <= n / 2; i++) tmp[i] += tmp[i - 1]; n   for (int i = 0; i < n / 2; i++) { n     if (tmp[i]) { n       l = i; n       break; n     } n   } n   for (int i = 0; i < n / 2; i++) n     if (tmp[i] == tmp[n / 2]) { n       r = i; n       break; n     } n   if (tmp[n / 2]) ans = tmp[n / 2] + min(abs(k - l), abs(k - r)) + abs(l - r); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tmp, curr = 0; n   cin >> n; n   int v[n], res[n]; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     v[tmp] = i + 1; n   } n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     if (v[tmp] > curr) { n       res[i] = v[tmp] - curr; n       curr = v[tmp]; n     } else n       res[i] = 0; n   } n   for (int i = 0; i < n; i++) cout << res[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int Time = 0; n string s; n bool dp[2][(int)1e5 + 10]; n struct Node { n   int nxt[26]; n   bool leaf; n   Node() { n     for (int i = 0; i < 26; i++) nxt[i] = -1; n     leaf = 1; n   } n } trie[(int)1e5 + 10]; n void dodaj(string s) { n   int niz[100010]; n   for (int i = 0; i < s.size(); i++) niz[i] = s[i] -  a ; n   int tr = 0; n   for (int i = 0; i < s.size(); i++) { n     if (trie[tr].nxt[niz[i]] == -1) trie[tr].nxt[niz[i]] = ++Time; n     trie[tr].leaf = 0; n     tr = trie[tr].nxt[niz[i]]; n   } n } n bool uradi0(int tr, bool minn) { n   if (trie[tr].leaf) { n     dp[0][tr] = 1; n     return 1; n   } n   for (int i = 0; i < 26; i++) { n     if (trie[tr].nxt[i] != -1) minn = min(minn, uradi0(trie[tr].nxt[i], 1)); n   } n   dp[0][tr] = 1 ^ minn; n   return dp[0][tr]; n } n bool uradi1(int tr, bool minn) { n   if (trie[tr].leaf) { n     dp[1][tr] = 0; n     return 0; n   } n   for (int i = 0; i < 26; i++) { n     if (trie[tr].nxt[i] != -1) minn = min(minn, uradi1(trie[tr].nxt[i], 1)); n   } n   dp[1][tr] = 1 ^ minn; n   return dp[1][tr]; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> s; n     dodaj(s); n   } n   uradi0(0, 1); n   uradi1(0, 1); n   if (dp[0][0] == 1 && dp[1][0] == 1) n     cout <<  First  << endl; n   else if (dp[0][0] == 1) n     cout <<  Second  << endl; n   else if (dp[1][0] == 1) n     cout << ((k % 2 == 1) ?  First  :  Second ) << endl; n   else n     cout <<  Second  << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n, t, i; n   scanf( %d%d , &n, &t); n   if (n == 1 && t == 10) n     printf( -1 ); n   else { n     if (t == 10) { n       printf( 1 ); n       for (i = 0; i < n - 1; i++) printf( 0 ); n     } else n       for (i = 0; i < n; i++) printf( %d , t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 52; n int n, m, a[N][N], b[N][N], i, j; n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) cin >> a[i][j]; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) cin >> b[i][j]; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) n       if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]); n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (a[i][j] <= a[i][j - 1] || i < n && (a[i][j] >= a[i + 1][j])) { n         cout <<  Impossible ; n         return 0; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (b[i][j] <= b[i][j - 1] || i < n && (b[i][j] >= b[i + 1][j])) { n         cout <<  Impossible ; n         return 0; n       } n     } n   } n   cout <<  Possible ; n } n


#include <bits/stdc++.h> n int n; n char s[20]; n int main() { n   scanf( %d , &n); n   getchar(); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %s , s); n     if (s[0] ==  T ) { n       ans += 4; n     } else if (s[0] ==  C ) { n       ans += 6; n     } else if (s[0] ==  O ) { n       ans += 8; n     } else if (s[0] ==  D ) { n       ans += 12; n     } else { n       ans += 20; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> get_pair(int a, int b) { n   if (a > b) swap(a, b); n   return {a, b}; n } n template <class Cap, class Cost> n struct mcf_graph { n  private: n   int _n; n   struct _edge { n     int to, rev; n     Cap cap; n     Cost cost; n   }; n   vector<pair<int, int>> pos; n   vector<vector<_edge>> g; n   vector<Cost> h; n   vector<Cost> dist; n   vector<int> pv, pe; n   vector<bool> vis; n   void norm(int s, int t) { n     h.resize(_n, numeric_limits<Cost>::max()); n     h[s] = 0; n     int ch = 1; n     int it = _n; n     while (ch-- && it--) { n       for (int i = 0; i < _n; i++) { n         if (h[i] == numeric_limits<Cost>::max()) continue; n         for (auto e : g[i]) { n           if (e.cap == 0) continue; n           if (h[i] + e.cost < h[e.to]) { n             h[e.to] = h[i] + e.cost; n             ch = 1; n           } n         } n       } n       assert(it >= 0); n     } n     for (int i = 0; i < _n; i++) h[i] -= h[t]; n   } n   bool dijkstra(int s, int t) { n     pv.clear(); n     pe.clear(); n     vis.clear(); n     dist.clear(); n     dist.resize(_n, numeric_limits<Cost>::max()); n     pv.resize(_n, -1); n     pe.resize(_n, -1); n     vis.resize(_n, false); n     priority_queue<pair<Cost, int>, vector<pair<Cost, int>>, n                    greater<pair<Cost, int>>> n         pq; n     pq.push({0, s}); n     dist[s] = 0; n     while (!pq.empty()) { n       int x = pq.top().second; n       pq.pop(); n       if (vis[x]) continue; n       vis[x] = true; n       if (x == t) break; n       for (int i = 0; i < int(g[x].size()); ++i) { n         auto e = g[x][i]; n         if (vis[e.to] || e.cap == 0) continue; n         Cost cost = e.cost + h[x] - h[e.to]; n         if (dist[e.to] <= dist[x] + cost) continue; n         dist[e.to] = dist[x] + cost; n         pv[e.to] = x; n         pe[e.to] = i; n         pq.push({dist[e.to], e.to}); n       } n     } n     if (vis[t] == false) return false; n     for (int i = 0; i < _n; ++i) { n       if (!vis[i]) continue; n       h[i] -= dist[t] - dist[i]; n     } n     return true; n   } n  n  public: n   mcf_graph() {} n   mcf_graph(int n) : _n(n), g(n) {} n   int add_edge(int from, int to, Cap cap, Cost cost) { n     assert(from >= 0 && from < _n); n     assert(to >= 0 && to < _n); n     int m = int(pos.size()); n     pos.push_back({from, int(g[from].size())}); n     int from_id = int(g[from].size()); n     int to_id = int(g[to].size()); n     if (from == to) to_id++; n     g[from].push_back(_edge{to, to_id, cap, cost}); n     g[to].push_back(_edge{from, from_id, 0, -cost}); n     return m; n   } n   struct edge { n     int from, to; n     Cap cap, flow; n     Cost cost; n   }; n   edge get_edge(int i) { n     int m = int(pos.size()); n     assert(0 <= i && i < m); n     auto _e = g[pos[i].first][pos[i].second]; n     auto _re = g[_e.to][_e.rev]; n     return edge{ n         pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, n     }; n   } n   vector<edge> edges() { n     int m = int(pos.size()); n     vector<edge> result(m); n     for (int i = 0; i < m; ++i) result[i] = get_edge(i); n     return result; n   } n   pair<Cap, Cost> flow(int s, int t, bool neg = false) { n     return flow(s, t, numeric_limits<Cap>::max(), neg); n   } n   pair<Cap, Cost> flow(int s, int t, Cap flow_limit, bool neg = false) { n     return slope(s, t, flow_limit, neg).back(); n   } n   vector<pair<Cap, Cost>> slope(int s, int t, bool neg = false) { n     return slope(s, t, numeric_limits<Cap>::max(), neg); n   } n   vector<pair<Cap, Cost>> slope(int s, int t, Cap flow_limit, n                                 bool neg = false) { n     assert(s >= 0 && s < _n); n     assert(t >= 0 && t < _n); n     assert(s != t); n     h.clear(); n     if (!neg) n       h.resize(_n, 0); n     else n       norm(s, t); n     Cap flow = 0; n     Cost cost = 0, prev = -1; n     vector<pair<Cap, Cost>> result; n     result.push_back({flow, cost}); n     while (flow < flow_limit) { n       if (dijkstra(s, t) == false) break; n       Cap c = flow_limit - flow; n       for (int i = t; i != s; i = pv[i]) { n         c = min(c, g[pv[i]][pe[i]].cap); n       } n       for (int i = t; i != s; i = pv[i]) { n         auto &e = g[pv[i]][pe[i]]; n         e.cap -= c; n         g[e.to][e.rev].cap += c; n       } n       Cost d = -h[s]; n       flow += c; n       cost += c * d; n       if (prev == d) result.pop_back(); n       result.push_back({flow, cost}); n       prev = d; n     } n     return result; n   } n }; n pair<vector<int>, vector<int>> bi_color(vector<pair<int, int>> &edges, n                                         map<int, int> &id) { n   int n = int(id.size()); n   vector<vector<int>> g(n); n   for (auto e : edges) { n     int u = id[e.first]; n     int v = id[e.second]; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vector<int> x, y; n   vector<int> col(n); n   for (int i = 0; i < n; i++) { n     if (col[i] > 0) continue; n     queue<int> q; n     col[i] = 1; n     q.push(i); n     x.push_back(i); n     while (!q.empty()) { n       auto u = q.front(); n       q.pop(); n       for (auto v : g[u]) { n         if (col[v] == 0) { n           col[v] = 3 - col[u]; n           q.push(v); n           if (col[v] == 1) n             x.push_back(v); n           else n             y.push_back(v); n         } else n           assert(col[v] == 3 - col[u]); n       } n     } n   } n   return {x, y}; n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> arr(1 << n); n   for (int i = 0; i < (1 << n); i++) cin >> arr[i]; n   int max_edge = min((2 * n) * k + 12, n * (1 << (n - 1))); n   map<pair<int, int>, bool> vis; n   vector<pair<int, int>> edges; n   using node = pair<int, pair<int, int>>; n   priority_queue<node> pq; n   vector<vector<pair<int, int>>> ord(1 << n); n   for (int i = 0; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) { n       ord[i].push_back({arr[i] + arr[i ^ (1 << j)], i ^ (1 << j)}); n     } n     sort(ord[i].begin(), ord[i].end()); n     reverse(ord[i].begin(), ord[i].end()); n     pq.push({ord[i][0].first, {i, 0}}); n   } n   while (!pq.empty()) { n     auto a = pq.top(); n     pq.pop(); n     auto cost = a.first; n     int u = a.second.first, i = a.second.second; n     if (vis.find(get_pair(u, ord[u][i].second)) == vis.end()) { n       vis[get_pair(u, ord[u][i].second)] = true; n       edges.push_back({u, ord[u][i].second}); n     } n     if (int(edges.size()) == max_edge) { n       break; n     } n     if (i + 1 < n) { n       pq.push({ord[u][i + 1].first, {u, i + 1}}); n     } n   } n   assert(int(edges.size()) == max_edge); n   map<int, int> id; n   for (int i = 0, j = 0; i < max_edge; i++) { n     int a = edges[i].first; n     int b = edges[i].second; n     if (id.find(a) == id.end()) id[a] = j++; n     if (id.find(b) == id.end()) id[b] = j++; n   } n   int _n = int(id.size()); n   mcf_graph<int, long long> g(_n + 3); n   int _s = _n, _t = _n + 1; n   long long inf = 1e16; n   auto info = bi_color(edges, id); n   for (int i = 0; i < int(info.first.size()); i++) { n     g.add_edge(_s, info.first[i], 1, 0); n   } n   for (int i = 0; i < int(info.second.size()); i++) { n     g.add_edge(info.second[i], _t, 1, 0); n   } n   map<int, int> first; n   for (auto xx : info.first) first[xx] = true; n   for (int i = 0; i < max_edge; i++) { n     int a = id[edges[i].first]; n     int b = id[edges[i].second]; n     if (first[b]) swap(a, b); n     g.add_edge(a, b, 1, -(arr[edges[i].first] + arr[edges[i].second])); n   } n   auto tt = g.flow(_s, _t, k, true); n   cout << -tt.second <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const long double PI = 3.141592653589793238462643383279502884197; n long long fac[1] = {1}, inv[1] = {1}; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long mp(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = ret * a % MOD; n     a = a * a % MOD; n     b >>= 1; n   } n   return ret; n } n long long cmb(long long r, long long c) { n   return fac[r] * inv[c] % MOD * inv[r - c] % MOD; n } n int n[3]; n int vec[3][200]; n long long dp[201][201][201]; n long long go(int a, int b, int c) { n   if (dp[a][b][c] != -1) return dp[a][b][c]; n   long long mav = 0; n   int nn[3] = {a, b, c}; n   for (int i = 0; i < 3; i++) { n     int flag = 0; n     for (int j = 0; j < 3; j++) { n       if (i == j) continue; n       if (nn[j] == n[j]) flag = 1; n     } n     if (flag) continue; n     if (i == 0) mav = max(mav, go(a, b + 1, c + 1) + vec[1][b] * vec[2][c]); n     if (i == 1) mav = max(mav, go(a + 1, b, c + 1) + vec[0][a] * vec[2][c]); n     if (i == 2) mav = max(mav, go(a + 1, b + 1, c) + vec[0][a] * vec[1][b]); n   } n   return dp[a][b][c] = mav; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %d %d %d , &n[0], &n[1], &n[2]); n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < n[i]; j++) scanf( %d , &vec[i][j]); n     sort(vec[i], vec[i] + n[i], greater<long long>()); n   } n   long long ans = go(0, 0, 0); n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T maxs(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T mins(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long maxn = 2000005; n long long p[maxn]; n long long sz[maxn]; n bool s1[maxn]; n bool s2[maxn]; n void clear(long long n = maxn) { n   for (long long i = 0; i < maxn; i++) p[i] = i, sz[i] = 1; n } n long long root(long long first) { n   while (first != p[first]) { n     p[first] = p[p[first]]; n     first = p[first]; n   } n   return first; n } n void merge(long long first, long long second) { n   long long p1 = root(first); n   long long p2 = root(second); n   if (p1 == p2) return; n   if (s1[p1] && s2[p1]) n     p[p2] = p1; n   else if (s1[p2] && s2[p2]) n     p[p1] = p2; n   else if (s1[p1] || s2[p1]) n     p[p2] = p1; n   else n     p[p1] = p2; n } n long long solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> g[n + 1]; n   vector<pair<long long, long long> > edges; n   vector<pair<long long, long long> > st; n   for (long long i = 1; i < m + 1; i++) { n     long long a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n     edges.emplace_back(a, b); n   } n   long long s, t, ds, dt; n   cin >> s >> t >> ds >> dt; n   for (auto j : g[s]) { n     s1[j] = 1; n   } n   for (auto j : g[t]) { n     s2[j] = 1; n   } n   for (auto j : edges) { n     if (j.first == s || j.first == t || j.second == s || j.second == t) n       continue; n     if (root(j.first) == root(j.second)) continue; n     merge(j.first, j.second); n     st.emplace_back(j.first, j.second); n   } n   long long cnts = 0, cntt = 0, cntst = 0; n   set<long long> sts; n   vector<long long> ss(n + 1, 0), tt(n + 1, 0); n   for (long long i = 1; i < n + 1; i++) { n     if (i == s || i == t) continue; n     long long p = root(i); n     if (s1[i]) ss[p] = i; n     if (s2[i]) tt[p] = i; n     sts.insert(p); n   } n   for (auto i : sts) { n     if (ss[i] && tt[i]) n       cntst++; n     else if (ss[i]) n       cnts++; n     else n       cntt++; n   } n   if (cnts > ds || cntt > dt || cntst + cntt + cnts > ds + dt) { n     cout <<  No  n          <<   n ; n     return 0; n   } n   if (cntst == 0 && (cnts + 1 > ds || cntt + 1 > dt)) { n     cout <<  No  n          <<   n ; n     return 0; n   } n   if (cntst == 0) { n     st.emplace_back(s, t); n     merge(s, t); n     ds--, dt--; n   } n   for (auto i : sts) { n     if (ss[i] && tt[i] == 0) n       st.emplace_back(s, ss[i]), ds--, merge(s, ss[i]); n     else if (tt[i] && ss[i] == 0) n       st.emplace_back(t, tt[i]), dt--, merge(t, tt[i]); n     else { n       if (ds >= dt) n         st.emplace_back(s, ss[i]), ds--, merge(s, ss[i]); n       else n         st.emplace_back(t, tt[i]), dt--, merge(t, tt[i]); n     } n   } n   if (root(s) != root(t)) { n     bool f = 0; n     if (ds) { n       for (auto i : g[s]) { n         if (dt == 0 && i == t) continue; n         if (root(i) != root(s)) { n           f = 1; n           st.emplace_back(s, i); n           break; n         } n       } n     } n     if (f == 0 && dt) { n       for (auto i : g[t]) { n         if (ds == 0 && i == s) continue; n         if (root(i) != root(t)) { n           f = 1; n           st.emplace_back(t, i); n           break; n         } n       } n     } n     if (f == 0) { n       cout <<  No  n            <<   n ; n       return 0; n     } n   } n   cout <<  Yes  n        <<   n ; n   for (auto i : st) cout << i <<   n ; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   clear(); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ar[500009], br[500009], cr[500009], tr[500009]; n long long int fx[] = {-1, 1, 0, 0}; n long long int fy[] = {0, 0, 1, -1}; n map<long long int, long long int> mp; n vector<long long int> v; n string s; n struct evan { n   long long int l, r, pos; n } st[500009]; n bool com(evan i1, evan i2) { return (i1.r < i2.r); } n int main() { n   long long int a, b, c, d, e, i, j, k, x, y, z, f, g, h, n, m; n   cin >> a; n   while (a--) { n     cin >> b; n     c = b / 4; n     j = (b * 4) - b; n     b -= c; n     for (i = 1; i < b; i++) { n       cout <<  9 ; n     } n     if (j % 4) n       cout <<  8 ; n     else { n       cout <<  9 ; n     } n     for (i = 1; i <= c; i++) { n       cout <<  8 ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, x2, y1, y2, x, y; n   cin >> x1 >> y1 >> x2 >> y2 >> x >> y; n   int temp1 = abs(x1 - x2), temp2 = abs(y1 - y2); n   int temp3 = temp1 / x, temp4 = temp2 / y; n   if (temp1 % x == 0 && temp2 % y == 0 && temp3 % 2 == temp4 % 2) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n struct point { n   double x, y, z; n   point() {} n   point(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {} n }; n const double eps = 1e-8; n double vlen(point p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); } n point xmult(point u, point v) { n   point ret; n   ret.x = u.y * v.z - v.y * u.z; n   ret.y = u.z * v.x - u.x * v.z; n   ret.z = u.x * v.y - u.y * v.x; n   return ret; n } n point subt(point u, point v) { n   point ret; n   ret.x = u.x - v.x; n   ret.y = u.y - v.y; n   ret.z = u.z - v.z; n   return ret; n } n int dot_online_in(point p, point l1, point l2) { n   return (((vlen(xmult(subt(p, l1), subt(p, l2)))) > 0 n                ? (vlen(xmult(subt(p, l1), subt(p, l2)))) n                : -(vlen(xmult(subt(p, l1), subt(p, l2))))) < eps) && n          (l1.x - p.x) * (l2.x - p.x) < eps && n          (l1.y - p.y) * (l2.y - p.y) < eps && (l1.z - p.z) * (l2.z - p.z) < eps; n } n double x[10001], y[10001], z[10001], d[10001]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i <= n; i++) scanf( %lf %lf %lf , &x[i], &y[i], &z[i]); n   double vp, vs, px, py, pz; n   scanf( %lf %lf , &vp, &vs); n   scanf( %lf %lf %lf , &px, &py, &pz); n   double lo = 0, hi = 0; n   for (int i = 1; i <= n; i++) { n     d[i] = sqrt((x[i] - x[i - 1]) * (x[i] - x[i - 1]) + n                 (y[i] - y[i - 1]) * (y[i] - y[i - 1]) + n                 (z[i] - z[i - 1]) * (z[i] - z[i - 1])); n     hi += d[i] / vs; n   } n   if (hi * vp < sqrt((x[n] - px) * (x[n] - px) + (y[n] - py) * (y[n] - py) + n                      (z[n] - pz) * (z[n] - pz))) n     printf( NO n ); n   else { n     double X = -1, Y = -1, Z = -1; n     while ((hi - lo) >= 1e-10) { n       double mid = (lo + hi) / 2; n       double s = mid * vs; n       for (int i = 1; i <= n; i++) n         if (s > d[i]) n           s -= d[i]; n         else { n           double t = s / d[i]; n           X = x[i - 1] + (x[i] - x[i - 1]) * t; n           Y = y[i - 1] + (y[i] - y[i - 1]) * t; n           Z = z[i - 1] + (z[i] - z[i - 1]) * t; n           if (!dot_online_in(point(X, Y, Z), n                              point(x[i - 1], y[i - 1], z[i - 1]), n                              point(x[i], y[i], z[i]))) { n             X = x[i - 1] - (x[i] - x[i - 1]) * t; n             Y = y[i - 1] - (y[i] - y[i - 1]) * t; n             Z = z[i - 1] - (z[i] - z[i - 1]) * t; n           } n           break; n         } n       if (mid * vp >= n           sqrt((X - px) * (X - px) + (Y - py) * (Y - py) + (Z - pz) * (Z - pz))) n         hi = mid; n       else n         lo = mid; n     } n     printf( YES n ); n     printf( %.10lf n , lo); n     printf( %.10lf %.10lf %.10lf n , X, Y, Z); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long h, w, n; n pair<long long, long long> a[2005]; n long long fact[200005]; n long long ifact[200005]; n long long dp[2005]; n long long exp(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   if (b % 2 == 0) { n     long long r = exp(a, b / 2); n     return (r * r) % 1000000007; n   } n   long long r = exp(a, b - 1); n   return (r * a) % 1000000007; n } n long long C(long long n, long long k) { n   return (((fact[n] * ifact[k]) % 1000000007) * ifact[n - k]) % 1000000007; n } n int main() { n   scanf( %lld %lld %lld , &h, &w, &n); n   for (long long i = 0; i < n; i++) { n     scanf( %lld %lld , &a[i].first, &a[i].second); n     a[i].first--; n     a[i].second--; n   } n   sort(a, a + n); n   a[n++] = make_pair(h - 1, w - 1); n   fact[0] = 1; n   ifact[0] = 1; n   for (long long i = 1; i <= max(w, h) * 2; i++) { n     fact[i] = (fact[i - 1] * i) % 1000000007; n     ifact[i] = exp(fact[i], 1000000007 - 2); n   } n   for (long long i = 0; i < n; i++) { n     long long num = C(a[i].first + a[i].second, a[i].first); n     for (long long j = 0; j < i; j++) { n       if (a[j].first <= a[i].first && a[j].second <= a[i].second) { n         int sub = n             (dp[j] * C((a[i].first - a[j].first) + (a[i].second - a[j].second), n                        a[i].first - a[j].first)) % n             1000000007; n         num = (num - sub + 1000000007) % 1000000007; n       } n     } n     dp[i] = num; n   } n   cout << dp[n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 2; n int lis[N], where[N], length[N], link[N], a[N], b[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   a[n] = INT_MAX; n   int m; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n   } n   sort(b, b + m); n   fill(lis, lis + n + 2, INT_MAX); n   fill(where, where + n + 2, -1); n   lis[0] = 0; n   for (int i = 0; i <= n; ++i) { n     if (a[i] == -1) { n       int fill = m - 1; n       for (int index = n + 1; index > 0; --index) { n         while (fill > 0 && b[fill - 1] > lis[index - 1]) { n           --fill; n         } n         if (b[fill] > lis[index - 1] && b[fill] <= lis[index]) { n           lis[index] = b[fill], where[index] = i; n         } n       } n       link[i] = i - 1; n       while (link[i] >= 0 && a[link[i]] != -1) { n         --link[i]; n       } n       length[i] = -1; n     } else { n       int index = lower_bound(lis, lis + n + 2, a[i]) - lis; n       lis[index] = a[i], where[index] = i; n       link[i] = where[index - 1]; n       length[i] = index; n     } n   } n   int index = n, prv = -1; n   while (index != -1) { n     if (a[index] == -1) { n       length[index] = length[prv] - 1; n       int fill = m - 1; n       while (b[fill] >= a[prv]) { n         --fill; n       } n       a[index] = b[fill]; n       b[fill] = INT_MAX; n       prv = index; n       if (length[index] == 1) { n         index = -1; n       } else { n         for (int i = 0; i < index; ++i) { n           if (a[i] < a[index] && length[i] + 1 == length[index]) { n             index = i; n             break; n           } n         } n         if (index == prv) { n           index = link[index]; n         } n       } n     } else { n       prv = index, index = link[index]; n     } n   } n   int ptr = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] == -1) { n       while (b[ptr] == INT_MAX) { n         ++ptr; n       } n       a[i] = b[ptr++]; n     } n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100101; n int pre[maxn], ind[maxn], cnt[maxn], f[maxn]; n vector<int> g[maxn], q; n int main() { n   int n; n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     g[l].push_back(r); n     g[r].push_back(l); n     ind[l]++; n     ind[r]++; n   } n   q.push_back(1); n   for (int i = 0; i < q.size(); ++i) { n     int now = q[i]; n     for (int j = 0; j < g[now].size(); ++j) n       if (g[now][j] != pre[now]) { n         pre[g[now][j]] = now; n         q.push_back(g[now][j]); n       } n   } n   bool flag = 1; n   for (int i = (int)q.size() - 1; i >= 0; --i) { n     int now = q[i]; n     int k = cnt[now] + (ind[now] > 3); n     if (k) f[pre[now]]++; n     if (ind[now] > 2) cnt[now]++; n     cnt[pre[now]] += cnt[now]; n   } n   int sum = cnt[1]; n   for (int i = 0; i < q.size(); ++i) { n     int now = q[i]; n     for (int j = 0; j < g[now].size(); ++j) n       if (g[now][j] != pre[now]) { n         int k = sum - cnt[g[now][j]] - (ind[now] > 2) + (ind[now] > 3); n         if (k) f[g[now][j]]++; n       } n   } n   for (int i = 1; i <= n; ++i) n     if (f[i] > 2) flag = 0; n   if (flag) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int next, to; n } a[400001]; n int st[200001], vis[200001], fa[200001], last, cnt = 0, ans = 0, huan; n int roo[200001], roo2[200001]; n void add(int x, int y) { n   a[++cnt].to = y; n   a[cnt].next = st[x]; n   st[x] = cnt; n } n void dfs(int x, int last) { n   int i; n   if (vis[x]) { n     if (huan == 0) { n       huan = 1; n       roo[++roo[0]] = x; n       roo2[roo[0]] = 1; n     } n     return; n   } n   vis[x] = 1; n   for (i = st[x]; i; i = a[i].next) n     if (a[i].to != last) dfs(a[i].to, x); n } n int main() { n   int n, i, j; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &fa[i]); n     if (fa[i] == i) continue; n     add(fa[i], i); n     add(i, fa[i]); n   } n   for (i = 1; i <= n; i++) { n     huan = 0; n     if (vis[i] == 0) { n       dfs(i, 0); n       if (huan == 0) { n         for (j = i; fa[j] != j; j = fa[j]) n           ; n         roo[++roo[0]] = j; n       } n     } n   } n   for (i = 1; i <= roo[0]; i++) n     if (roo2[i] == 0) break; n   if (i <= roo[0]) { n     ans = roo[0] - 1; n     for (j = 1; j <= roo[0]; j++) fa[roo[j]] = roo[i]; n   } else { n     ans = roo[0]; n     for (j = 1; j <= roo[0]; j++) fa[roo[j]] = roo[1]; n   } n   printf( %d n , ans); n   for (i = 1; i <= n; i++) printf( %d  , fa[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x; n   string d; n   int t, m = 0; n   cin >> n >> x; n   for (int i = 0; i < n; i++) { n     cin >> d >> t; n     if (d[0] ==  + ) x += t; n     if (d[0] ==  - ) { n       if (x >= t) n         x -= t; n       else n         m++; n     } n   } n   cout << x <<     << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, pl; n string s; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> t; n   for (int qqw = 1; qqw <= t; qqw++) { n     cin >> n; n     cin >> s; n     pl = -1; n     for (int i = 1; i <= n; i++) n       if (s[i - 1] ==  0 ) pl = i; n     if (pl > 0) { n       if (n % 2 == 0) { n         if (pl <= n / 2) { n           cout << pl <<     << n <<     << pl + 1 <<     << n <<   n ; n           continue; n         } n         cout << 1 <<     << pl <<     << 1 <<     << pl - 1 <<   n ; n       } else { n         if (pl <= n / 2) { n           cout << pl <<     << n <<     << pl + 1 <<     << n <<   n ; n           continue; n         } n         cout << 1 <<     << pl <<     << 1 <<     << pl - 1 <<   n ; n       } n     } else { n       cout << 1 <<     << n - 1 <<     << 2 <<     << n <<   n ; n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct linearbasis { n   long long base[64], flag, cnt; n   bool add(long long x) { n     for (int i = 62; ~i; i--) { n       if (x >> i & 1) { n         if (!base[i]) { n           base[i] = x; n           return false; n         } n         x ^= base[i]; n       } n     } n     flag = 1; n     return true; n   } n   long long query_max(long long ans = 0) { n     for (int i = 62; ~i; i--) { n       if ((ans ^ base[i]) > ans) { n         ans ^= base[i]; n       } n     } n     return ans; n   } n   long long query_min() { n     for (int i = 0; i <= 62; i++) { n       if (base[i]) { n         return base[i]; n       } n     } n   } n   void rebuild() { n     cnt = 0; n     for (int i = 62; i >= 0; i--) { n       for (int j = i - 1; j >= 0; j--) { n         if (base[i] >> j & 1) { n           base[i] ^= base[j]; n         } n       } n     } n     for (int i = 0; i <= 62; i++) { n       if (base[i]) { n         long long temp = base[i]; n         base[i] = 0; n         base[cnt++] = temp; n       } n     } n   } n   long long query_k(long long k) { n     k -= flag; n     if (k == 0) return 0; n     if (k >= 1ll << cnt) return -1; n     long long ans = 0; n     for (int i = 62; ~i; i--) { n       if (k >> i & 1) { n         ans ^= base[i]; n       } n     } n     return ans; n   } n   void init() { memset(base, 0, sizeof base), flag = cnt = 0; } n } a; n int prime[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, n                31, 37, 41, 43, 47, 53, 59, 61, 67}; n const int mod = 1e9 + 7; n int main() { n   int n, ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, value = 0; n     cin >> x; n     for (int j = 0; j < 19; j++) { n       int cnt = 0; n       while (x % prime[j] == 0) { n         cnt++; n         x /= prime[j]; n       } n       if (cnt & 1) { n         value |= 1 << j; n       } n     } n     if (a.add(value)) { n       ans++; n     } n   } n   long long res = 1; n   for (int i = 1; i <= ans; i++) { n     res = res * 2 % mod; n   } n   printf( %lld n , res - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n constexpr int MAXN = 1e9 + 7; n constexpr int INF = 0x3f3f3f3f; n constexpr long long INFLL = 0x3f3f3f3f3f3f3f3f; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.tie(nullptr); n   int a, b; n   cin >> a >> b; n   if (a == b) { n     cout << a <<     << 0 <<   n ; n   } else { n     int ans = min(a, b); n     a -= ans; n     b -= ans; n     int left = max(a, b); n     int ans2 = left / 2; n     cout << ans <<     << ans2 <<   n ; n   } n } n


// precompute lookup table for each digit?  n  n #include <iostream> n #include <unordered_map> n #include <utility> n #include <algorithm> n using namespace std; n  n #define MP make_pair<int, int> n  n int main() n { n     unordered_map<int, int> lookup[10]; n     lookup[1].insert(MP(0, 1)); n     lookup[2].insert(MP(0, 2)); n     lookup[2].insert(MP(1, 21)); n     lookup[3].insert(MP(0, 3)); n     lookup[3].insert(MP(1, 13)); n     lookup[3].insert(MP(2, 23)); n     lookup[4].insert(MP(0, 4)); n     lookup[4].insert(MP(2, 14)); n     lookup[4].insert(MP(1, 41)); n     lookup[4].insert(MP(3, 43)); n     lookup[5].insert(MP(0, 5)); n     lookup[5].insert(MP(1, 51)); n     lookup[5].insert(MP(2, 52)); n     lookup[5].insert(MP(3, 53)); n     lookup[5].insert(MP(4, 54)); n     lookup[6].insert(MP(0, 6)); n     lookup[6].insert(MP(4, 16)); n     lookup[6].insert(MP(2, 26)); n     lookup[6].insert(MP(1, 61)); n     lookup[6].insert(MP(3, 63)); n     lookup[6].insert(MP(5, 65)); n     lookup[7].insert(MP(0, 7)); n     lookup[7].insert(MP(3, 17)); n     lookup[7].insert(MP(6, 27)); n     lookup[7].insert(MP(2, 37)); n     lookup[7].insert(MP(5, 47)); n     lookup[7].insert(MP(1, 57)); n     lookup[7].insert(MP(4, 67)); n     lookup[8].insert(MP(0, 8)); n     lookup[8].insert(MP(2, 18)); n     lookup[8].insert(MP(4, 28)); n     lookup[8].insert(MP(6, 38)); n     lookup[8].insert(MP(1, 81)); n     lookup[8].insert(MP(3, 83)); n     lookup[8].insert(MP(5, 85)); n     lookup[8].insert(MP(7, 87)); n     lookup[9].insert(MP(0, 9)); n     lookup[9].insert(MP(1, 19)); n     lookup[9].insert(MP(2, 29)); n     lookup[9].insert(MP(3, 39)); n     lookup[9].insert(MP(4, 49)); n     lookup[9].insert(MP(5, 59)); n     lookup[9].insert(MP(6, 69)); n     lookup[9].insert(MP(7, 79)); n     lookup[9].insert(MP(8, 89)); n     int t; cin >> t; n     while (t--) { n         int q, d; cin >> q >> d; n         for (int i = 0; i < q; ++i) { n             int x; cin >> x; n             int mod = x % d; n             if (lookup[d].at(mod) <= x) cout <<  YES  << endl; n             else cout <<  NO  << endl; n         } n     } n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   float h, l, ans; n   cin >> h >> l; n   ans = (l * l - h * h) / (2 * h); n   cout << setprecision(13) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 50; n const int mod = 1e9 + 7; n const int inf = (1 << 30); n vector<int> v[maxn][5], tpl; n int ted[maxn], com[maxn], tmp = 1; n bool visited[maxn]; n bool dfs(int u, int d) { n   visited[u] = 1; n   bool rz = (ted[com[u]] == 1); n   if (d == 1) { n     com[u] = tmp; n     ted[tmp]++; n   } n   for (auto w : v[u][d]) { n     if (!visited[w]) { n       rz &= dfs(w, d); n     } n   } n   if (d == 0) tpl.push_back(u); n   return rz; n } n bool dfs1(int u) { n   visited[u] = 1; n   bool rz = (ted[com[u]] == 1); n   for (auto w : v[u][0]) n     if (!visited[w]) rz &= dfs1(w); n   for (auto w : v[u][1]) n     if (!visited[w]) rz &= dfs1(w); n   return rz; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     v[a][0].push_back(b); n     v[b][1].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     if (!visited[i]) dfs(i, 0); n   memset(visited, 0, sizeof visited); n   for (int i = n - 1; i >= 0; i--) { n     int u = tpl[i]; n     if (!visited[u]) { n       dfs(u, 1); n       tmp++; n     } n   } n   memset(visited, 0, sizeof visited); n   long long ans = n; n   for (int i = 1; i <= n; i++) { n     if (!visited[i]) { n       bool k = dfs1(i); n       ans -= k; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long am[20]; 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> a(n); n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) { n     cin >> a[i]; n     for (int j = 0, ThxDem = 20; j < ThxDem; ++j) n       if (a[i] & (1 << j)) am[j]++; n     a[i] = 0; n   } n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) { n     for (int j = 0, ThxDem = 20; j < ThxDem; ++j) n       if (am[j]) a[i] |= 1 << j, am[j]--; n   } n   long long ans = 0; n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) ans += a[i] * a[i]; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:400000000 ) n int n, r; n string ans; n pair<int, int> f(int a, int b) { n   if (a == 1 && b == 0) return pair<int, int>(0, 0); n   if (a > 1 && b == 0) return pair<int, int>(1000000007LL, 1000000007LL); n   if (a == 1 && b == 1) return pair<int, int>(1, 0); n   if (a > 1 && b == 1) return pair<int, int>(2, a - 2); n   pair<int, int> p = f(b, a % b); n   return pair<int, int>(p.first + 1, p.second + a / b - 1); n } n void g(int a, int b) { n   if (a == 1 && b == 0) return; n   if (a == 1 && b == 1) { n     ans +=  T ; n     return; n   } n   if (a > 1 && b == 1) { n     ans +=  TB ; n     for (int i = 0; i < a - 2; i++) ans +=  B ; n     return; n   } n   g(b, a % b); n   if (ans[ans.size() - 1] ==  T ) { n     ans +=  B ; n     for (int i = 0; i < a / b - 1; i++) ans +=  B ; n   } else { n     ans +=  T ; n     for (int i = 0; i < a / b - 1; i++) ans +=  T ; n   } n } n int main() { n   cin >> n >> r; n   int mi = 1000000007LL, best = -1; n   for (int i = 1; i < r + 1; i++) { n     if (r > 1 && r == i) continue; n     pair<int, int> p = f(r, i); n     if (p.first + p.second == n && p.second < mi) mi = p.second, best = i; n   } n   if (best == -1) n     cout <<  IMPOSSIBLE ; n   else { n     g(r, best); n     cout << mi << endl << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int U[5005], V[5005]; n struct emm { n   int a, b, x; n } a[5005]; n bool cmp(emm x, emm y) { return x.x < y.x; } n struct Edge { n   int v; n   Edge *next; n } pool[5005 << 1], *h[5005]; n int fa[5005], dep[5005], ans[5005]; n int tot; n void addEdge(int u, int v) { n   Edge *p = &pool[tot++]; n   p->v = v; n   p->next = h[u]; n   h[u] = p; n } n void dfs(int u) { n   for (Edge *p = h[u]; p; p = p->next) { n     int v = p->v; n     if (v == fa[u]) continue; n     dep[v] = dep[u] + 1; n     fa[v] = u; n     dfs(v); n   } n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   while (dep[u] > dep[v]) { n     u = fa[u]; n   } n   if (u == v) return u; n   while (fa[u] != fa[v]) { n     u = fa[u]; n     v = fa[v]; n   } n   return fa[u]; n } n void update(int u, int v, int x) { n   if (u == v) return; n   while (u != v) { n     ans[u] = x; n     u = fa[u]; n   } n } n int check(int u, int v) { n   int res = 0x3f3f3f3f; n   if (u == v) return res; n   while (u != v) { n     res = min(res, ans[u]); n     u = fa[u]; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &U[i], &V[i]); n     addEdge(U[i], V[i]); n     addEdge(V[i], U[i]); n   } n   dep[1] = 1; n   dfs(1); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d%d%d , &a[i].a, &a[i].b, &a[i].x); n   sort(a + 1, a + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     int t = LCA(a[i].a, a[i].b); n     update(a[i].a, t, a[i].x); n     update(a[i].b, t, a[i].x); n   } n   for (int i = 1; i <= m; i++) { n     int t = LCA(a[i].a, a[i].b); n     if (min(check(a[i].a, t), check(a[i].b, t)) != a[i].x) { n       printf( -1 n ); n       return 0; n     } n   } n   for (int i = 1; i < n; i++) { n     if (dep[U[i]] < dep[V[i]]) swap(U[i], V[i]); n     printf( %d  , max(ans[U[i]], 1)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[1010]; n   cin >> a; n   int l = strlen(a), dot; n   for (int i = 0; i < l; i++) { n     if (a[i] ==  . ) { n       dot = i; n     } n   } n   if (a[dot - 1] ==  9 ) { n     cout <<  GOTO Vasilisa.  << endl; n   } else { n     if (a[dot + 1] >=  5 ) { n       a[dot - 1]++; n     } n     for (int i = 0; i <= dot - 2; i++) { n       cout << a[i]; n     } n     cout << a[dot - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 11; n int use[N], a[N], b[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   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     use[a[i]] = 1; n   } n   for (int j = 1; j <= n; j++) { n     cin >> b[j]; n     use[b[j]] = 1; n   } n   int kol = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       int d = a[i] ^ b[j]; n       if (d <= 2000000 && use[d] == 1) kol++; n     } n   if (kol % 2 == 0) n     cout <<  Karen  << endl; n   else n     cout <<  Koyomi  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int xans[200005]; n bool pos; n vector<pair<int, int> > xoc[200005]; n vector<pair<int, int> > V[200005]; n int erased[200005]; n queue<int> toErase; n int sgn(int v) { return abs(v) / v; } n vector<pair<int, int> > E[200005]; n int vis[200005]; n int cycleRoot; n bool assigned[200005]; n bool arcUsed[200005]; n void dfs(int u) { n   vis[u] = 2; n   for (int i = 0, v, x, i1, i2; i < E[u].size(); i++) { n     v = E[u][i].first; n     x = E[u][i].second; n     if (!assigned[x]) { n       i1 = xoc[x][1].first == u; n       i2 = 1 - i1; n       xans[x] = (xoc[x][i2].second == 1); n       assigned[x] = true; n       if (vis[v] != 2) dfs(v); n     } n   } n } n bool findCycle(int u) { n   vis[u] = 1; n   for (int i = 0, v, x; i < E[u].size(); i++) { n     v = E[u][i].first; n     x = E[u][i].second; n     if (arcUsed[x]) continue; n     arcUsed[x] = true; n     if (vis[v]) { n       cycleRoot = v; n       return true; n     } else if (findCycle(v)) { n       return true; n     } n   } n   return false; n } n void makeE(int c1) { n   for (int i = 0, x, i1, i2, c2; i < V[c1].size(); i++) { n     x = V[c1][i].first; n     i1 = xoc[x][1].first == c1; n     i2 = 1 - i1; n     c2 = xoc[x][i2].first; n     if (c1 < c2) { n       E[c1].push_back(make_pair(c2, x)); n       E[c2].push_back(make_pair(c1, x)); n     } n   } n } n void erase(int c) { n   int x, s, i1, i2; n   for (int i = 0; i < V[c].size(); i++) { n     x = V[c][i].first; n     s = V[c][i].second; n     if (xoc[x].size() == 1) { n       xans[x] = (xoc[x][0].second == 1); n     } else { n       i1 = (xoc[x][1].first == c); n       i2 = 1 - i1; n       if (xoc[x][i1].second == xoc[x][i2].second) { n         xans[x] = xoc[x][i1].second == 1; n         if (!erased[xoc[x][i2].first]) { n           erased[xoc[x][i2].first] = 1; n           erase(xoc[x][i2].first); n         } n       } else { n         if (erased[xoc[x][i2].first]) { n           xans[x] = xoc[x][i1].second == 1; n         } else { n           erased[xoc[x][i2].first] = 1; n           erase(xoc[x][i2].first); n         } n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   int cs, x, s; n   for (int i = 0; i < n; i++) { n     scanf( %d , &cs); n     for (int j = 0; j < cs; j++) { n       scanf( %d , &x); n       s = sgn(x); n       x = abs(x) - 1; n       V[i].push_back(make_pair(x, s)); n       xoc[x].push_back(make_pair(i, s)); n     } n   } n   for (int x = 0, c1, c2; x < m; x++) { n     if (xoc[x].size() == 1) { n       c1 = xoc[x][0].first; n       if (!erased[c1]) { n         erased[c1] = 1; n         erase(c1); n       } n     } else if (xoc[x].size() == 2 && xoc[x][0].second == xoc[x][1].second) { n       c1 = xoc[x][0].first; n       if (!erased[c1]) { n         erased[c1] = 1; n         erase(c1); n       } n       c2 = xoc[x][1].first; n       if (!erased[c2]) { n         erased[c2] = 1; n         erase(c2); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (!erased[i]) { n       makeE(i); n     } n   } n   pos = true; n   for (int u = 0; u < n; u++) { n     if (!erased[u] && !vis[u]) { n       if (findCycle(u)) { n         dfs(cycleRoot); n       } else { n         pos = false; n         break; n       } n     } n   } n   if (!pos) n     printf( NO n ); n   else { n     printf( YES n ); n     for (int i = 0; i < m; i++) printf( %d , xans[i]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1234567; n const double PI = acos(-1.0), EPS = 1e-9; n long long N, arr[MX]; n long long gcd(long long a, long long b) { n   while (a > 0 && b > 0) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return a + b; n } n int main() { n   cin >> N; n   long long g = 0; n   for (int i = 0; i < N; i++) cin >> arr[i]; n   for (int i = 0; i < N; i++) { n     g = gcd(g, arr[i]); n   } n   if (g != 1) { n     cout <<  YES n ; n     cout << 0 <<   n ; n     return 0; n   } n   int oper = 0; n   for (int i = 0; i < N; i++) { n     if (arr[i] % 2 && arr[i + 1] % 2) { n       i++; n       oper++; n     } else if (arr[i] % 2 && arr[i + 1] % 2 == 0) { n       i++; n       oper += 2; n     } n   } n   cout <<  YES n ; n   cout << oper <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     f = ch ==  -  ? -1 : 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n const int N = 105; n int n, p[N], c[N]; n int main() { n   int T = read(); n   while (T--) { n     n = read(); n     for (int i = 1; i <= n; i++) p[i] = read(), c[i] = read(); n     p[0] = 0; n     c[0] = 0; n     int fl = 1; n     for (int i = 1; i <= n; i++) { n       if (c[i] < c[i - 1] || p[i] < p[i - 1]) { n         fl = 0; n         break; n       } n       if (c[i] - c[i - 1] > p[i] - p[i - 1]) { n         fl = 0; n         break; n       } n     } n     if (fl) n       puts( YES ); n     else n       puts( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char arr[100][100]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  - ) { n         arr[i][j] =  - ; n       } else { n         if (i % 2 == 0 && j % 2 == 0) { n           arr[i][j] =  B ; n         } else if (i % 2 == 0 && j % 2 != 0) { n           arr[i][j] =  W ; n         } else if (i % 2 != 0 && j % 2 == 0) { n           arr[i][j] =  W ; n         } else if (i % 2 != 0 && j % 2 != 0) { n           arr[i][j] =  B ; n         } n       } n       cout << arr[i][j]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   int l = v.size(); n   for (int i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n const int N = 10000001; n int lp[N], primes[N], pcnt; n void getSieve() { n   for (int i = 2; i <= N; i++) { n     if (lp[i] == 0) { n       lp[i] = i; n       primes[pcnt++] = i; n     } n     for (int j = 0; j < pcnt and primes[j] <= lp[i] and i * primes[j] <= N; j++) n       lp[i * primes[j]] = primes[j]; n   } n   return; n } n const int M = 1000100; n int k, a, cnt[N], mx; n long long cp[N]; n int f(long long val) { n   long long p, d, tot; n   for (int i = 0; i < pcnt and primes[i] <= val; i++) { n     d = p = primes[i], tot = 0; n     while (d <= val and tot < cp[p]) { n       tot += val / d; n       d *= p; n     } n     if (tot < cp[p]) return 0; n   } n   return 1; n } n int main() { n   getSieve(); n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d , &a); n     mx = max(mx, a); n     cnt[1]++; n     cnt[a + 1]--; n   } n   int p, num; n   for (int i = 1; i <= mx; i++) { n     cnt[i] += cnt[i - 1]; n     num = i; n     while (num > 1) { n       p = lp[num]; n       while (lp[num] == p) { n         cp[p] += cnt[i]; n         num /= p; n       } n     } n   } n   long long lo = mx, hi = (1LL << 45), mid; n   while (lo < hi) { n     mid = (lo / 2) + (hi / 2); n     if (f(mid)) n       hi = mid; n     else n       lo = mid + 1; n   } n   cout << hi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double r, x, y, z, t; n   cin >> r >> x >> y >> z >> t; n   double d = (x - z) * (x - z) + (y - t) * (y - t); n   d = sqrt(d); n   double i; n   for (i = 0; i < 10000000; i++) { n     if ((i * r) >= d / 2) break; n   } n   cout << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int w1, w2, h1, h2; n int main() { n   scanf( %d%d%d%d , &w1, &h1, &w2, &h2); n   printf( %d n , (h1 + h2) * 2 + 4 + max(w1, w2) * 2); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O500 ) n #pragma comment(linker,  /STACK:1677777216 ) n #pragma warning(default : 4) n using namespace std; n const double eps = 1e-12; n const int oo = 0x3F3F3F3F; n const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; n const int MOD = 1000000007; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n void debpr(const T &); n template <typename T1, typename T2> n void debpr(const pair<T1, T2> &); n template <typename T1, typename T2> n void debpr(const vector<T1, T2> &); n template <typename T> n void debpr(const set<T> &); n template <typename T1, typename T2> n void debpr(const map<T1, T2> &); n template <typename T> n void prcont(T be, T en, const string &st, const string &fi, const string &mi) { n   debpr(st); n   bool ft = 0; n   while (be != en) { n     if (ft) debpr(mi); n     ft = 1; n     debpr(*be); n     ++be; n   } n   debpr(fi); n } n template <typename T> n void debpr(const T &a) {} n template <typename T1, typename T2> n void debpr(const pair<T1, T2> &p) { n   debpr( ( ); n   debpr(p.first); n   debpr( ,  ); n   debpr(p.second); n   debpr( ) ); n } n template <typename T1, typename T2> n void debpr(const vector<T1, T2> &a) { n   prcont(a.begin(), a.end(),  [ ,  ] ,  ,  ); n } n template <typename T> n void debpr(const set<T> &a) { n   prcont(a.begin(), a.end(),  { ,  } ,  ,  ); n } n template <typename T1, typename T2> n void debpr(const map<T1, T2> &a) { n   prcont(a.begin(), a.end(),  { ,  } ,  ,  ); n } n void deb(){}; n template <typename T1> n void deb(const T1 &t1) { n   debpr(t1); n   debpr(  n ); n } n template <typename T1, typename T2> n void deb(const T1 &t1, const T2 &t2) { n   debpr(t1); n   debpr(   ); n   debpr(t2); n   debpr(  n ); n } n template <typename T1, typename T2, typename T3> n void deb(const T1 &t1, const T2 &t2, const T3 &t3) { n   debpr(t1); n   debpr(   ); n   debpr(t2); n   debpr(   ); n   debpr(t3); n   debpr(  n ); n } n template <typename T1, typename T2, typename T3, typename T4> n void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { n   debpr(t1); n   debpr(   ); n   debpr(t2); n   debpr(   ); n   debpr(t3); n   debpr(   ); n   debpr(t4); n   debpr(  n ); n } n const double PI = acos(-1.); n long long Round(double x) { return x < 0 ? x - .5 : x + .5; } n template <typename T> n void ass(bool v, const T &x, string m =  Fail ) { n   if (!v) { n     deb(m); n     deb(x); n     throw; n   } n } n int main() { n   void run(); n   run(); n   return 0; n } n long long a[1 << 20]; n int n; n long long gcd(long long a, long long b) { n   long long t; n   while (a) b %= a, t = a, a = b, b = t; n   return b; n } n long long mrand() { n   long long rs = 0; n   for (int i = (0), _b(60); i < _b; ++i) n     if (rand() % 2) rs += 1LL << i; n   return rs; n } n void run() { n   scanf( %d , &n); n   for (int i = (0), _b(n); i < _b; ++i) scanf( %lld , &a[i]); n   long long rs = 1; n   srand(2); n   for (int it = (0), _b(12); it < _b; ++it) { n     long long t = a[mrand() % n]; n     unordered_map<long long, int> m; n     m.reserve(1 << 15); n     for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; n     vector<pair<long long, long long> > v((m).begin(), (m).end()); n     for (int i = (0), _b(v.size()); i < _b; ++i) { n       int z = 0; n       for (int j = (0), _b(v.size()); j < _b; ++j) n         if (v[j].first % v[i].first == 0) z += v[j].second; n       if (z >= (n + 1) / 2) rs = max(rs, v[i].first); n     } n   } n   cout << rs << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ispalin(string a) { n   string b = a; n   reverse(b.begin(), b.end()); n   return a == b; n } n int main() { n   string str; n   cin >> str; n   int N = str.size(); n   for (int i = 0; i <= N; i++) { n     for (char xc =  a ; xc <=  z ; xc++) { n       string str1; n       for (int j = 0; j < i; j++) str1 += str[j]; n       str1 += xc; n       for (int j = i; j < N; j++) str1 += str[j]; n       if (ispalin(str1)) { n         cout << str1; n         return 0; n       } n     } n   } n   cout <<  NA ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int h = n / 2; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i <= h) { n         if (j >= h - i && j <= h + i) { n           cout <<  D ; n         } else { n           cout <<  * ; n         } n       } else { n         if (j >= i - h && j <= 3 * n / 2 - 1 - i) { n           cout <<  D ; n         } else { n           cout <<  * ; n         } n       } n     } n     cout <<   n ; n   } n   return 0; n } n