#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t-- > 0) { n     int n; n     string s; n     cin >> n >> s; n     vector<int> a(n, 0), b(n, 0), c; n     int flag1 = 1, flag2 = 1; n     for (int i = 0; i < n - 1; i++) { n       if (s[i] ==  < ) { n         b[i] = flag1++; n       } n     } n     for (int i = n - 2; i >= 0; i--) { n       if (s[i] ==  < ) { n         while (i > 0 && s[i - 1] ==  < ) { n           i--; n         } n         int x = i; n         while (s[x] ==  < ) { n           a[x] = flag2++; n           x++; n         } n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (b[i] == 0) { n         b[i] = flag1++; n       } n       if (a[i] == 0) { n         a[i] = flag2++; n       } n     } n     for (int i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n     for (int i = 0; i < n; i++) { n       cout << b[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x[100005], h[100005]; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> x[i] >> h[i]; n   int count = 1; n   int newarr[100005]; n   newarr[1] = x[0] - h[0]; n   for (int i = 1; i < n - 1; ++i) { n     if ((x[i] - h[i] > x[i - 1]) && (x[i] - h[i] > newarr[count])) { n       count++; n       newarr[count] = x[i] - h[i]; n     } else if (x[i] + h[i] < x[i + 1]) { n       count++; n       newarr[count] = x[i] + h[i]; n     } else n       continue; n   } n   if (n == 1) n     puts( 1 ); n   else n     cout << count + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n vector<vector<pair<int, int> > > graph; n vector<pair<int, int> > edges; n class SegmentTree { n   struct node_t { n     int best[2]; n     int lazy; n     node_t() { n       best[0] = 0; n       best[1] = 0; n       lazy = 0; n     } n     node_t operator+(const node_t &other) const { n       node_t ans; n       for (int i = 0; i < 2; i++) { n         ans.best[i] = max(this->best[i], other.best[i]); n       } n       ans.lazy = 0; n       return ans; n     } n     void propag(int lazy) { n       if (lazy) { n         swap(best[0], best[1]); n         this->lazy ^= lazy; n       } n     } n   }; n   int n; n   vector<node_t> aint; n   void propag(int nod, int st, int dr) { n     if (st == dr || aint[nod].lazy == 0) { n       return; n     } n     aint[nod * 2].propag(aint[nod].lazy); n     aint[nod * 2 + 1].propag(aint[nod].lazy); n     aint[nod].lazy = 0; n   } n   void build(int nod, int st, int dr, const vector<int> &lvl, n              const vector<int> &tp) { n     if (st == dr) { n       aint[nod].best[tp[st]] = lvl[st]; n       aint[nod].best[tp[st] ^ 1] = 0; n       return; n     } n     int mid = (st + dr) / 2; n     build(nod * 2, st, mid, lvl, tp); n     build(nod * 2 + 1, mid + 1, dr, lvl, tp); n     aint[nod] = aint[nod * 2] + aint[nod * 2 + 1]; n   } n   void flip(int nod, int st, int dr, int l, int r) { n     propag(nod, st, dr); n     if (dr < l || st > r) { n       return; n     } n     if (l <= st && dr <= r) { n       aint[nod].propag(1); n       return; n     } n     int mid = (st + dr) / 2; n     flip(nod * 2, st, mid, l, r); n     flip(nod * 2 + 1, mid + 1, dr, l, r); n     aint[nod] = aint[nod * 2] + aint[nod * 2 + 1]; n   } n  n  public: n   SegmentTree(int n, const vector<int> &lvl, const vector<int> &tp) { n     this->n = n; n     aint = vector<node_t>(4 * n + 5, node_t()); n     build(1, 1, n, lvl, tp); n   } n   SegmentTree() { ; } n   void flip(int l, int r) { flip(1, 1, n, l, r); } n   int query() { return aint[1].best[0]; } n }; n class TreeWrapper { n   SegmentTree aint; n   vector<int> lvl; n   vector<int> tp; n   vector<int> l; n   vector<int> r; n   int lst = 0; n   void dfs(int nod, pair<int, int> tata, n            const vector<vector<pair<int, int> > > &graph) { n     l[nod] = ++lst; n     lvl[l[nod]] = 1 + lvl[l[tata.first]]; n     tp[l[nod]] = tp[l[tata.first]] ^ tata.second; n     for (auto it : graph[nod]) { n       if (it.first == tata.first) { n         continue; n       } n       dfs(it.first, {nod, it.second}, graph); n     } n     r[nod] = lst; n   } n  n  public: n   TreeWrapper(int root, const vector<vector<pair<int, int> > > &graph) { n     lst = 0; n     l = vector<int>(graph.size(), 0); n     r = vector<int>(graph.size(), 0); n     lvl = vector<int>(graph.size(), 0); n     tp = vector<int>(graph.size(), 0); n     dfs(root, {0, 0}, graph); n     aint = SegmentTree((int)graph.size() - 1, lvl, tp); n   } n   void flip(pair<int, int> a) { n     if (lvl[l[a.first]] > lvl[l[a.second]]) { n       swap(a.first, a.second); n     } n     aint.flip(l[a.second], r[a.second]); n   } n   int query() { return aint.query(); } n }; n pair<int, int> dfs(int nod, int tata) { n   pair<int, int> ans = {0, nod}; n   for (auto it : graph[nod]) { n     if (it.first == tata) { n       continue; n     } n     pair<int, int> tmp = dfs(it.first, nod); n     tmp.first++; n     ans = max(ans, tmp); n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   graph = vector<vector<pair<int, int> > >(n + 1, vector<pair<int, int> >()); n   edges = vector<pair<int, int> >(n, {0, 0}); n   for (int i = 1; i < n; i++) { n     int x, y, t; n     scanf( %d %d %d , &x, &y, &t); n     graph[x].push_back({y, t}); n     graph[y].push_back({x, t}); n     edges[i] = {x, y}; n   } n   int a = dfs(1, 0).second; n   int b = dfs(a, 0).second; n   TreeWrapper fst(a, graph), snd(b, graph); n   scanf( %d , &q); n   while (q--) { n     int id; n     scanf( %d , &id); n     fst.flip(edges[id]); n     snd.flip(edges[id]); n     printf( %d n , max(fst.query(), snd.query()) - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> cnt1, cnt2; n const int maxn = 1e5 * 3; n int x[maxn]; n int y[maxn]; n int main() { n   int n = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     cnt1[x[i] - y[i]]++; n     cnt2[x[i] + y[i]]++; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long c1 = cnt1[x[i] - y[i]]; n     long long c2 = cnt2[x[i] + y[i]]; n     ans += c1 + c2 - 2; n   } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   string s[n]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j <= i; j++) cout <<  ( ; n     for (long long j = 0; j <= i; j++) cout <<  ) ; n     for (long long j = 0; j < n - i - 1; j++) cout <<  () ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1, x = 0; n   cin >> t; n   while (t--) { n     x++; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 269; n const int MS = 2; n const int MOD[MS] = {(int)1e9 + 7, (int)1e9 + 9}; n const int MAXN = 1e6 + 69; n using hsh = vector<long long>; n struct hj { n   int shift = MS == 1 ? 0 : 32 / (MS - 1); n   size_t operator()(const hsh& h) const { n     long long res = 0; n     for (int M = 0; M < MS; M++) res <<= shift, res ^= h[M]; n     return res; n   } n }; n int t; n int n; n set<int> a[MAXN]; n unordered_map<hsh, int, hj> mp; n vector<string> str; n int check[MAXN]; n int cnt; n int ans; n void build(hsh& h, char c) { n   for (int M = 0; M < MS; M++) h[M] = (c + h[M] * K) % MOD[M]; n } n void init() { n   for (int i = 1; i <= cnt; i++) { n     a[i].clear(); n     check[i] = 0; n   } n   mp.clear(); n   cnt = 0; n   ans = 0; n } n bool dfs(int u) { n   check[u] = 1; n   int tookChild = 0; n   for (int v : a[u]) n     if (!check[v]) tookChild |= dfs(v); n   ans += tookChild ^ 1; n   return tookChild ^ 1; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     init(); n     cin >> n; n     str.resize(n + 1); n     for (int i = 1; i <= n; i++) cin >> str[i], str[i] =     + str[i]; n     for (int i = 1; i <= n; i++) { n       string& s = str[i]; n       hsh h(MS); n       for (int j = 1; j < (int)s.size(); j++) { n         build(h, s[j]); n         if (!mp.count(h)) mp[h] = ++cnt; n       } n     } n     for (int i = 1; i <= n; i++) { n       string& s = str[i]; n       hsh h(MS), h2(MS); n       for (int j = 1; j < (int)s.size(); j++) { n         build(h, s[j]); n         if (j >= 2) { n           build(h2, s[j]); n           if (mp.count(h2)) { n             int u = mp[h], v = mp[h2]; n             a[u].insert(v); n             a[v].insert(u); n           } n         } n       } n     } n     for (int i = 1; i <= cnt; i++) n       if (!check[i]) dfs(i); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a, b; n int dp[55][55][4]; n int f(int n, int m, int p) { n   if ((n == m && n == 0) || (n == 0 && p == 1) || (m == 0 && p == 2)) { n     return dp[n][m][p] = 0; n   } n   if (dp[n][m][p] != -1) { n     return dp[n][m][p]; n   } n   bool g = 0; n   if (p == 1) { n     for (int i = 1; i <= min(a, n); i++) { n       g = (g || (!f(n - i, m, 2))); n     } n   } else { n     for (int i = 1; i <= min(b, m); i++) { n       g = (g || (!f(n, m - i, 1))); n     } n   } n   return dp[n][m][p] = g; n } n int main() { n   int n, m; n   cin >> n >> m >> a >> b; n   memset(dp, -1, sizeof(dp)); n   if (f(n, m, 1)) { n     cout <<  First ; n   } else { n     cout <<  Second ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 40 + 2; n int n, m, q, dp[maxn][maxn][maxn][maxn], ones[maxn][maxn], a, b, c, d; n int main() { n   ios::sync_with_stdio(false); n   char ch; n   cin >> n >> m >> q; n   for (int _b = (n), i = (1); i <= _b; ++i) n     for (int _b = (m), j = (1); j <= _b; ++j) { n       cin >> ch; n       ones[i][j] = n           ones[i - 1][j] + ones[i][j - 1] - ones[i - 1][j - 1] + ch -  0 ; n     } n   for (int _b = (n - 1), h = (0); h <= _b; ++h) n     for (int _b = (m - 1), w = (0); w <= _b; ++w) n       for (int _b = (n - h), i = (1); i <= _b; ++i) n         for (int _b = (m - w), j = (1); j <= _b; ++j) { n           a = i, b = j, c = i + h, d = j + w; n           dp[a][b][c][d] = ((ones[c][d] - ones[a - 1][d] - ones[c][b - 1] + n                              ones[a - 1][b - 1]) == 0); n           for (int _b = ((1 << 4) - 1), mask = (1); mask <= _b; ++mask) { n             dp[a][b][c][d] += (-1 + 2 * (__builtin_popcount(mask) & 1)) * n                               dp[a + (mask >> 0 & 1)][b + (mask >> 1 & 1)] n                                 [c - (mask >> 2 & 1)][d - (mask >> 3 & 1)]; n           } n         } n   while (q--) { n     cin >> a >> b >> c >> d; n     cout << dp[a][b][c][d] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, mas[1000], ans[1000], anss; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> mas[i]; n   for (int i = 2; i <= n; i++) { n     if (mas[i] + mas[i - 1] < k) { n       anss += k - mas[i - 1] - mas[i]; n       mas[i] = k - mas[i - 1]; n     } n   } n   cout << anss <<   n ; n   for (int i = 1; i <= n; i++) cout << mas[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long num = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num * neg; n } n int got[21], n, m, id[1000010], id_cnt; n int stk[21], tot, f[2000010]; n bool vis[1000010]; n int cnt, to[2000010], nxt[2000010], head[1000010], in[1000010], out[1000010]; n inline void insert(int x, int y) { n   to[++cnt] = y, nxt[cnt] = head[x], head[x] = cnt; n   out[x]++, in[y]++; n } n inline void dfs(int u, int s) { n   vis[u] = s; n   if (id[u]) { n     got[s] |= (1 << id[u] - 1); n   } n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (vis[v] == s) continue; n     dfs(v, s); n   } n   return; n } n inline int getcnt(int x) { n   int res = 0; n   while (x) res++, x -= x & -x; n   return res; n } n int main() { n   n = read(), m = read(); n   for (int i = 1, x, y; i <= m; i++) x = read(), y = read(), insert(x, y); n   for (int i = 1; i <= n; i++) n     if (!out[i]) id[i] = ++id_cnt; n   for (int i = 1; i <= n; i++) n     if (!in[i]) { n       stk[++tot] = i; n       dfs(i, i); n     } n   for (int i = 1; i <= tot; i++) n     for (int j = 0; j < (1 << tot); j++) f[j | (1 << i - 1)] |= got[stk[i]]; n   for (int j = 1; j < (1 << tot) - 1; j++) { n     if (getcnt(j) == getcnt(f[j])) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n long long c[N << 2], a[N]; n void pushup(int rt) { c[rt] = c[rt << 1] | c[rt << 1 | 1]; } n void build(int rt, int l, int r) { n   if (l == r) { n     scanf( %I64d , c + rt); n     a[l] = c[rt]; n     return; n   } n   int mid = (l + r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   pushup(rt); n } n long long query(int rt, int l, int r, int x, int y) { n   if (x > y) return 0; n   if (x <= l && r <= y) { n     return c[rt]; n   } n   int mid = (l + r) >> 1; n   long long ret = 0; n   if (x <= mid) ret |= query(rt << 1, l, mid, x, y); n   if (mid < y) ret |= query(rt << 1 | 1, mid + 1, r, x, y); n   return ret; n } n int main() { n   int n, k, c; n   while (~scanf( %d%d%d , &n, &c, &k)) { n     long long tmp = 1; n     for (int i = 1; i <= c; i++) { n       tmp *= k; n     } n     build(1, 1, n); n     long long ans = (tmp * a[1]) | query(1, 1, n, 2, n); n     ans = max(ans, (tmp * a[n]) | query(1, 1, n, 1, n - 1)); n     for (int i = 2; i < n; i++) { n       ans = max(ans, query(1, 1, n, 1, i - 1) | (tmp * a[i]) | n                          query(1, 1, n, i + 1, n)); n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   set<int> col[26]; n   vector<int> ans; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     set<int> used; n     for (int j = s[i] -  a  + 1; j < 26; j++) n       for (int num : col[j]) used.insert(num); n     int newCol = 1; n     while (used.count(newCol)) newCol++; n     col[s[i] -  a ].insert(newCol); n     ans.push_back(newCol); n     cnt = max(cnt, newCol); n   } n   cout << cnt <<   n ; n   for (int i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n double ans; n int a[N]; n int n, u, p; n int main() { n   scanf( %d%d , &n, &u); n   ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n   } n   for (int i = 1; i <= n; i++) { n     if (i <= n - 2) { n       p = lower_bound(a + i + 2, a + n + 1, a[i] + u) - a; n       if (p > n || a[i] + u != a[p]) p--; n       if (p >= i + 2) { n         ans = max(ans, (a[p] - a[i + 1]) * 1.0 / (a[p] - a[i])); n       } n     } n   } n   if (ans == 0) n     printf( -1 n ); n   else n     printf( %.11lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, ans = -1, dist[2020]; n vector<int> adj[2020], vet; n int main() { n   scanf( %d%d , &n, &k); n   for (int(i) = (0); (i) < (k); (i)++) { n     int v; n     scanf( %d , &v); n     vet.push_back(v - n); n   } n   sort((vet).begin(), (vet).end()); n   vet.resize(unique((vet).begin(), (vet).end()) - vet.begin()); n   for (int(i) = (0); (i) < (2020); (i)++) n     for (int(j) = (0); (j) < (((int)(vet).size())); (j)++) n       if (i + vet[j] >= 0 && i + vet[j] < 2020) adj[i].push_back(i + vet[j]); n   queue<int> fila; n   fila.push(1010); n   while (!fila.empty()) { n     int v = fila.front(); n     fila.pop(); n     if (v == 1010 && dist[v]) { n       ans = dist[v]; n       break; n     } n     for (int(i) = (0); (i) < (((int)(adj[v]).size())); (i)++) n       if (!dist[adj[v][i]]) dist[adj[v][i]] = dist[v] + 1, fila.push(adj[v][i]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > upd; n struct query { n   int l, r, t, id, ans; n   query() {} n   query(int _l, int _r, int _t, int _id) { n     l = _l; n     r = _r; n     t = _t; n     id = _id; n   } n }; n vector<query> que; n stack<pair<int, int> > S; n vector<int> V; n int M[1005][105]; n int sz[1005]; n int cnt[300005]; n int data[100005]; n int B; n int l = 1, r = 0, t = 0, Ans = 1; n int cmp(query a, query b) { n   if (a.l / B != b.l / B) return a.l < b.l; n   if (a.r / B != b.r / B) return a.r < b.r; n   return a.t < b.t; n } n int cmpid(query a, query b) { return a.id < b.id; } n int Upd() { n   for (int i = (Ans / 100); i < (1000); i++) n     if (sz[i] < 100) { n       for (int j = (0); j < (100); j++) n         if (!M[i][j]) { n           return i * 100 + j; n         } n     } n } n void Add(int a) { n   if (!M[a / 100][a % 100]) sz[a / 100]++; n   M[a / 100][a % 100]++; n   if (Ans == a) Ans = Upd(); n } n void Del(int a) { n   M[a / 100][a % 100]--; n   if (!M[a / 100][a % 100]) { n     sz[a / 100]--; n     if (Ans > a) Ans = a; n   } n } n void add(int a) { n   Del(cnt[a]); n   Add(++cnt[a]); n } n void del(int a) { n   Del(cnt[a]); n   Add(--cnt[a]); n } n int main() { n   M[0][0] = 1e9; n   int n, m; n   scanf( %d%d , &n, &m); n   B = pow(n, 2.0 / 3); n   for (int i = (1); i < (n + 1); i++) n     scanf( %d , &data[i]), V.push_back(data[i]); n   while (m--) { n     int co, a, b; n     scanf( %d%d%d , &co, &a, &b); n     if (co == 1) n       que.push_back(query(a, b, (int)(upd).size(), (int)(que).size())); n     else n       upd.push_back(make_pair(a, b)), V.push_back(b); n   } n   sort(V.begin(), V.end()); n   V.erase(unique(V.begin(), V.end()), V.end()); n   for (int i = (1); i < (n + 1); i++) n     data[i] = lower_bound(V.begin(), V.end(), data[i]) - V.begin() + 1; n   for (int i = (0); i < ((int)(upd).size()); i++) n     upd[i].second = n         lower_bound(V.begin(), V.end(), upd[i].second) - V.begin() + 1; n   sort(que.begin(), que.end(), cmp); n   for (int i = (0); i < ((int)(que).size()); i++) { n     while (l > que[i].l) { n       l--; n       add(data[l]); n     } n     while (r < que[i].r) { n       r++; n       add(data[r]); n     } n     while (r > que[i].r) { n       del(data[r]); n       r--; n     } n     while (l < que[i].l) { n       del(data[l]); n       l++; n     } n     while (t < que[i].t) { n       int pos = upd[t].first; n       S.push(make_pair(pos, data[pos])); n       if (l <= pos && pos <= r) del(data[pos]); n       data[pos] = upd[t].second; n       if (l <= pos && pos <= r) add(data[pos]); n       t++; n     } n     while (t > que[i].t) { n       t--; n       int pos = S.top().first; n       if (l <= pos && pos <= r) del(data[pos]); n       data[pos] = S.top().second; n       if (l <= pos && pos <= r) add(data[pos]); n       S.pop(); n     } n     que[i].ans = Ans; n   } n   sort(que.begin(), que.end(), cmpid); n   for (int i = (0); i < ((int)(que).size()); i++) printf( %d n , que[i].ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long N = 1e5 + 7; n long long p[N], cnt; n bool vis[N]; n long long res1 = 0; n long long res2 = 0; n long long res3 = 0; n inline void init(long long NN) { n   res1 = res2 = res3 = 0; n   for (long long i = 2; i <= NN + res1 + res2 + res3; i++) { n     res1 = res2 = res3 = 0; n     if (vis[i]) continue; n     p[++cnt] = i + res1 + res2 + res3; n     res1 = res2 = res3 = 0; n     for (long long j = 2; i * j <= NN; j++) vis[i * j] = true; n     res1 = res2 = res3 = 0; n   } n } n void solve() { n   long long a, b; n   cin >> a >> b; n   res1 = res2 = res3 = 0; n   long long now = b; n   res1 = res2 = res3 = 0; n   if (a % b) n     printf( %lld n , a + res1 + res2 + res3); n   else { n     res1 = res2 = res3 = 0; n     long long ans = 1; n     res1 = res2 = res3 = 0; n     for (long long j = 1; j <= cnt + res1 + res2 + res3; j++) { n       res1 = res2 = res3 = 0; n       if (b % p[j] == 0) { n         res1 = res2 = res3 = 0; n         long long tmp = a; n         res1 = res2 = res3 = 0; n         while (tmp % now == 0) tmp /= p[j]; n         res1 = res2 = res3 = 0; n         ans = max(ans, tmp) + res1 + res2 + res3; n         res1 = res2 = res3 = 0; n         while (b % p[j] == 0) b /= p[j]; n         res1 = res2 = res3 = 0; n       } n     } n     long long tmp = a; n     if (b != 1) { n       res1 = res2 = res3 = 0; n       while (tmp % now == 0) tmp /= b; n       res1 = res2 = res3 = 0; n       ans = max(ans, tmp) + res1 + res2 + res3; n       res1 = res2 = res3 = 0; n     } n     printf( %lld n , ans + res1 + res2 + res3); n   } n } n signed main() { n   long long t = 1; n   res1 = res2 = res3 = 0; n   init(100000); n   cin >> t; n   res1 = res2 = res3 = 0; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fib[100], A[100], B[100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, n, najwieksza = 2; n   fib[0] = 1; n   fib[1] = 1; n   for (; fib[najwieksza - 1] + fib[najwieksza - 2] <= 1e18; najwieksza++) n     fib[najwieksza] = fib[najwieksza - 1] + fib[najwieksza - 2]; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<int> miejsca; n     for (int i = najwieksza - 1; i > 0; i--) n       if (fib[i] <= n) { n         n -= fib[i]; n         miejsca.push_back(i); n       } n     miejsca.push_back(0); n     reverse(miejsca.begin(), miejsca.end()); n     A[1] = 1; n     B[1] = (miejsca[1] - 1) / 2; n     for (int i = 2; i < (int)miejsca.size(); i++) { n       A[i] = A[i - 1] + B[i - 1]; n       B[i] = A[i - 1] * ((miejsca[i] - miejsca[i - 1] - 1) / 2) + n              B[i - 1] * ((miejsca[i] - miejsca[i - 1]) / 2); n     } n     cout << A[miejsca.size() - 1] + B[miejsca.size() - 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mediana(vector<int> &v) { n   if (int(v.size()) % 2 == 1) return v[int(v.size()) / 2]; n   return v[int(v.size()) / 2 - 1]; n } n bool esta(vector<int> &v, int x) { n   for (int i = 0; i < int(v.size()); i++) n     if (v[i] == x) return true; n   return false; n } n int main() { n   int n, x; n   cin >> n >> x; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   int amount = 0; n   while (mediana(v) != x) { n     if (not esta(v, x)) n       v.push_back(x); n     else if (mediana(v) < x) n       v.push_back(1000000001); n     else n       v.push_back(-1); n     amount++; n     sort(v.begin(), v.end()); n   } n   cout << amount << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e2 + 10; n const long long mod = 998244353; n const long long inf = (long long)4e17 + 5; n const int INF = 1e9 + 7; n const double pi = acos(-1.0); n long long inv(long long b) { n   if (b == 1) return 1; n   return (mod - mod / b) * inv(mod % b) % mod; n } n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n; n int ans[105]; n int more[maxn], les[maxn]; n int mmax, mmin; n int nex[maxn], pre[maxn]; n inline void ask_m(int x) { n   printf( ? ); n   fflush(stdout); n   for (int i = 1; i <= n; i++) { n     if (i == x) n       printf(  2 ); n     else n       printf(  1 ); n   } n   puts(  ); n   fflush(stdout); n   scanf( %d , more + x); n   if (more[x] == 0) { n     ans[x] = n; n     mmax = x; n   } n } n inline void ask_l(int x) { n   printf( ? ); n   fflush(stdout); n   for (int i = 1; i <= n; i++) { n     if (i == x) n       printf(  1 ); n     else n       printf(  2 ); n   } n   puts(  ); n   fflush(stdout); n   scanf( %d , les + x); n   if (les[x] == 0) { n     ans[x] = 1; n     mmin = x; n   } n } n void solve() { n   for (int i = 1; i <= n; i++) { n     ask_m(i); n   } n   for (int i = 1; i <= n; i++) { n     ask_l(i); n   } n   for (int i = 1; i <= n; i++) { n     if (more[i] == i || !more[i]) continue; n     nex[i] = more[i]; n     pre[more[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     if (les[i] == i || !les[i]) continue; n     nex[les[i]] = i; n     pre[i] = les[i]; n   } n   int fr; n   for (int i = 1; i <= n; i++) { n     if (!nex[i]) { n       fr = i; n       break; n     } n   } n   for (int i = n; i >= 1; i--) { n     ans[fr] = i; n     fr = pre[fr]; n   } n   printf( ! ); n   for (int i = 1; i <= n; i++) printf(  %d , ans[i]); n   puts(  ); n   fflush(stdout); n } n int main() { n   scanf( %d , &n); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5; n int n, ans; n int Dpos[N + 3]; n char str[N + 10], Nstr[N + 10]; n int main() { n   scanf( %d%s , &n, str + 1); n   int Ncnt = 0, Ned = n, Nmin = 0; n   for (int i = 1; i <= n; i++) { n     if (str[i] ==  ( ) n       Ncnt++; n     else n       Ncnt--; n     if (Nmin > Ncnt) { n       Nmin = Ncnt; n       Ned = i; n     } n   } n   if (Ncnt) printf( 0 n1 1 n ), exit(0); n   int Npos = 1; n   for (int i = Ned + 1; i <= n; i++) Dpos[Npos] = i, Nstr[Npos++] = str[i]; n   for (int i = 1; i <= Ned; i++) Dpos[Npos] = i, Nstr[Npos++] = str[i]; n   for (int i = 1, Icnt = 0; i <= n; i++) { n     if (Nstr[i] ==  ( ) n       Icnt++; n     else n       Icnt--; n     if (!Icnt) ans++; n   } n   int fans = ans; n   int ansl = 1, ansr = 1; n   for (int i = 1; i <= n;) { n     int Pcnt = 0; n     int j; n     for (j = i; j <= n; j++) { n       if (Nstr[j] ==  ( ) n         Pcnt++; n       else n         Pcnt--; n       if (!Pcnt) break; n     } n     vector<pair<int, int> > vec; n     Pcnt = 0; n     int Ptot = 0; n     for (int k = i + 1; k < j; k++) { n       if (Nstr[k] ==  ( ) n         Pcnt++; n       else n         Pcnt--; n       if (Pcnt == 0) { n         if (vec.empty()) n           vec.push_back(make_pair(i + 1, k)); n         else n           vec.push_back(make_pair(vec.back().second + 1, k)); n       } n     } n     for (auto it : vec) { n       int L = it.first, R = it.second; n       int Kcnt = 0, Ktot = 0; n       for (int k = L + 1; k <= R - 1; k++) { n         if (Nstr[k] ==  ( ) n           Kcnt++; n         else n           Kcnt--; n         if (!Kcnt) Ktot++; n       } n       if (ans < fans - 1 + 2 + Ktot) { n         ans = fans - 1 + 2 + Ktot; n         ansl = L; n         ansr = R; n       } n     } n     Ptot = vec.size(); n     if (ans < Ptot + 1) { n       ans = Ptot + 1; n       ansl = i; n       ansr = j; n     } n     i = j + 1; n   } n   ansl = Dpos[ansl]; n   ansr = Dpos[ansr]; n   if (ansl > ansr) swap(ansl, ansr); n   printf( %d n%d %d n , ans, ansl, ansr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5005] = {0}; n void ge(int a) { n   if (a == 1) n     printf( one n ); n   else if (a == 2) n     printf( two n ); n   else if (a == 3) n     printf( three n ); n   else if (a == 4) n     printf( four n ); n   else if (a == 5) n     printf( five n ); n   else if (a == 6) n     printf( six n ); n   else if (a == 7) n     printf( seven n ); n   else if (a == 8) n     printf( eight n ); n   else if (a == 9) n     printf( nine n ); n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   if (n == 0) n     printf( zero n ); n   else if (n == 1) n     printf( one n ); n   else if (n == 2) n     printf( two n ); n   else if (n == 3) n     printf( three n ); n   else if (n == 4) n     printf( four n ); n   else if (n == 5) n     printf( five n ); n   else if (n == 6) n     printf( six n ); n   else if (n == 7) n     printf( seven n ); n   else if (n == 8) n     printf( eight n ); n   else if (n == 9) n     printf( nine n ); n   else if (n == 10) n     printf( ten n ); n   else if (n == 11) n     printf( eleven n ); n   else if (n == 12) n     printf( twelve n ); n   else if (n == 13) n     printf( thirteen n ); n   else if (n == 14) n     printf( fourteen n ); n   else if (n == 15) n     printf( fifteen n ); n   else if (n == 16) n     printf( sixteen n ); n   else if (n == 17) n     printf( seventeen n ); n   else if (n == 18) n     printf( eighteen n ); n   else if (n == 19) n     printf( nineteen n ); n   else if (n == 20) n     printf( twenty n ); n   else if (n > 20 && n <= 29) { n     printf( twenty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 30) { n     printf( thirty n ); n   } else if (n > 30 && n <= 39) { n     printf( thirty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 40) n     printf( forty n ); n   else if (n > 40 && n <= 49) { n     printf( forty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 50) n     printf( fifty n ); n   else if (n > 50 && n <= 59) { n     printf( fifty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 60) n     printf( sixty n ); n   else if (n > 60 && n <= 69) { n     printf( sixty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 70) n     printf( seventy n ); n   else if (n > 70 && n <= 79) { n     printf( seventy- ); n     int k = n % 10; n     ge(k); n   } else if (n == 80) n     printf( eighty n ); n   else if (n > 80 && n <= 89) { n     printf( eighty- ); n     int k = n % 10; n     ge(k); n   } else if (n == 90) n     printf( ninety n ); n   else if (n > 90 && n <= 99) { n     printf( ninety- ); n     int k = n % 10; n     ge(k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y, alpha; n   int num; n }; n int n, c[1510] = {}, ans[1510] = {}; n vector<int> a[1510]; n point p[1510]; n int rec(int v, int pr) { n   int r = 0; n   for (int i = 0; i < a[v].size(); i++) n     if (a[v][i] != pr) r += rec(a[v][i], v); n   return (c[v] = r) + 1; n } n bool comp(point p1, point p2) { return (p1.alpha < p2.alpha); } n int rec2(int v, int t, int pr) { n   int s = t; n   for (int i = t + 1; i <= t + c[v]; i++) n     if (p[i].x < p[s].x || (p[i].x == p[s].x && p[i].y < p[s].y)) s = i; n   swap(p[s], p[t]); n   for (int i = t + 1; i <= t + c[v]; i++) n     if (p[i].x != p[t].x) n       p[i].alpha = atan((p[i].y - p[t].y) / (p[i].x - p[t].x)); n     else if (p[i].y > p[t].y) n       p[i].alpha = 3.1415926536 / 2; n     else n       p[i].alpha = -3.1415926536 / 2; n   sort(p + t + 1, p + t + c[v] + 1, comp); n   ans[p[t++].num] = v; n   for (int i = 0; i < a[v].size(); i++) n     if (a[v][i] != pr) t += rec2(a[v][i], t, v); n   return c[v] + 1; n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     a[u].push_back(v); n     a[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     cin >> p[i].x >> p[i].y; n     p[i].num = i; n   } n   rec(0, -1); n   rec2(0, 0, -1); n   for (int i = 0; i < n; i++) cout << ans[i] + 1 <<    ; n } n


#include <bits/stdc++.h> n const int maxn = (int)1e6; n const int mod = (int)1e9 + 7; n using namespace std; n int n; n long long dp[maxn]; n long long a[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   dp[0] = abs(a[0]); n   for (int i = 1; i < n; i++) { n     dp[i] = abs(a[i] - a[i - 1]) + dp[i - 1]; n   } n   cout << dp[n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool mycomp(vector<long long int> a, vector<long long int> b) { n   long long int acost = min(2 * a[0], a[1]) - min(a[0], a[1]); n   long long int bcost = min(2 * b[0], b[1]) - min(b[0], b[1]); n   return acost > bcost; n } n long long int fn(vector<vector<long long int> > arr, long long int n, n                  long long int f) { n   sort(arr.begin(), arr.end(), mycomp); n   long long int productSold = 0; n   for (long long int i = 0; i < f; i++) { n     productSold += min(2 * arr[i][0], arr[i][1]); n   } n   for (long long int i = f; i < n; i++) { n     productSold += min(arr[i][0], arr[i][1]); n   } n   return productSold; n } n int32_t main() { n   long long int n, f; n   cin >> n >> f; n   vector<vector<long long int> > arr(n); n   for (long long int i = 0; i < n; i++) { n     long long int k, l; n     cin >> k >> l; n     arr[i].push_back(k); n     arr[i].push_back(l); n   } n   long long int ans = fn(arr, n, f); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long f(int x, int len) { n   if (len == 0) return 0; n   if (x > len) n     return (long long)len * (2 * x - len - 1) / 2; n   else n     return (long long)x * (x - 1) / 2 + len - x + 1; n } n bool ok(int x) { n   if (f(x, k - 1) + f(x, n - k) + x <= m) n     return 1; n   else n     return 0; n } n int main() { n   cin >> n >> m >> k; n   int l = 1, r = 1e9; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (ok(mid)) { n       l = mid; n     } else { n       r = mid; n     } n   } n   int res = 0; n   if (ok(l)) res = l; n   if (ok(r)) res = r; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[310], nex[310], T, N; n bool can, isRoot[310]; n void dfs(int x, int val) { n   if (T < 0) return; n   arr[x] += val; n   if (nex[x] != -1) { n     T -= arr[x]; n     dfs(nex[x], arr[x]); n   } n } n long long int memo[310][100000 + 100]; n long long int solve(int pos, int val) { n   if (pos == 0) return val == 0; n   if (val <= 0) return val == 0; n   if (memo[pos][val] != -1) return memo[pos][val]; n   long long int ret = solve(pos - 1, val); n   if (val - arr[pos] >= 0) n     ret = (ret + solve(pos, val - arr[pos])) % 1000000007; n   return memo[pos][val] = ret; n } n long long int t[100000 + 100]; n int main() { n   int Q, x, y; n   while (cin >> N >> Q >> T) { n     for (int i = 1; i <= N; i++) scanf( %d , &arr[i]); n     memset(nex, -1, sizeof nex); n     memset(isRoot, true, sizeof isRoot); n     for (int i = 0; i < Q; i++) { n       scanf( %d %d , &x, &y); n       nex[x] = y; n       isRoot[y] = false; n     } n     bool in = false; n     for (int i = 1; i <= N; i++) n       if (isRoot[i] && nex[i] != -1) { n         in = true; n         dfs(i, 0); n       } n     if ((Q > 0 && in == false) || T < 0) { n       cout << 0 << endl; n       return 0; n     } n     memset(memo, -1, sizeof memo); n     t[0] = 1; n     for (int i = 1; i <= N; i++) n       for (int j = 0; j <= T; j++) { n         if (j - arr[i] >= 0) t[j] = (t[j] + t[j - arr[i]]) % 1000000007; n       } n     cout << t[T] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150, M = 150, OO = 0x3f3f3f3f; n int n, arr[2005], temp, ans; n void DFS(int u) { n   temp++; n   if (arr[u] == -1) return; n   DFS(arr[u]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , arr + i); n   } n   for (int i = 1; i <= n; ++i) { n     temp = 0; n     DFS(i); n     ans = max(ans, temp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int convert(int k) { n   if (k < 2) n     return k; n   else if (k < 5) n     return k - 1; n   else n     return k - 2; n } n int main() { n   string s[6]; n   int A[6][6] = {3, 3, 4, 4, 3, 3, 3, 3, 4, 4, 3, 3, 2, 2, 3, 3, 2, 2, n                  2, 2, 3, 3, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1}; n   for (int i = 0; i < 6; i++) { n     cin >> s[i]; n   } n   int best = 0, besti = 0, bestj = 0; n   for (int i = 0; i < 6; i++) { n     for (int j = 0; j < 8; j++) { n       if (s[i][j] ==  . ) { n         if (A[i][convert(j)] > best) { n           best = A[i][convert(j)]; n           besti = i; n           bestj = j; n         } n       } n     } n   } n   s[besti][bestj] =  P ; n   for (int i = 0; i < 6; i++) { n     cout << s[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void checkmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (a < b) a = b; n } n const int oo = 1 << 30; n const double eps = 1e-7; n const int N = 1 << 10; n const int M = 1; n const long long P = 1000000007; n long long dp[N][N]; n int mx; n char s[N]; n inline int is(int k) { return 4 == k || 7 == k; } n inline int to(int f, int flag) { n   flag = is(flag); n   if (0 == f) return 0; n   if (1 == f || mx + 1 == f) return flag ? f - 1 : (mx + 1); n   return flag ? 0 : f - 1; n } n long long dfs(int id, int f) { n   if (id < 0) return !f; n   long long &r = dp[id][f]; n   if (-1 != r) return r; n   r = 0; n   id--; n   r += 8 * dfs(id, to(f, 0)); n   r += 2 * dfs(id, to(f, 4)); n   return r %= P; n } n void pre() { n   (memset((dp), -1, sizeof(dp))); n   dfs(1001, mx + 1); n } n long long dfs_d(int id, int f) { n   if (id < 0) return !f; n   int k, u; n   long long r = 0; n   u = s[id]; n   id--; n   for (k = 0; k < (u); k++) r += dfs(id, to(f, k)); n   r += dfs_d(id, to(f, k)); n   return r %= P; n } n long long doit(int flag) { n   (memset((s), 0, sizeof(s))); n   scanf( %s , s); n   int i, sn; n   sn = strlen(s); n   for (i = 0; i < (sn); i++) s[i] -=  0 ; n   reverse(s, s + sn); n   s[0] -= flag; n   for (i = 0; s[i] < 0; i++) { n     s[i] = 9; n     s[i + 1]--; n   } n   return dfs_d(sn, mx + 1); n } n int main() { n   int T, _ = 0; n   long long ans = 0; n   scanf( %d %d , &T, &mx); n   pre(); n   while (_++ < T) { n     ans = -doit(1); n     ans += doit(0); n     ans = (ans % P + P) % P; n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   int b; n   int c; n   int d; n   int e; n   cin >> a; n   int aa[a]; n   for (b = 0; b < a; ++b) { n     cin >> aa[b]; n   } n   d = aa[0]; n   c = 0; n   b = 0; n   vector<int> ab; n   while (b < a) { n     if (aa[b] > d) d = aa[b]; n     if ((b + 1) == d) c = c + 1; n     b = b + 1; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q, a[100100], head, tail; n long long n; n long long solve(long long n, long long k) { n   if (k & 1) return (k + 1) / 2; n   if (n & 1) n     if (k == 2) n       return solve(n / 2, n / 2) + (n + 1) / 2; n     else n       return solve(n / 2, k / 2 - 1) + (n + 1) / 2; n   else n     return solve(n / 2, k / 2) + n / 2; n } n int main() { n   scanf( %I64d , &n); n   for (scanf( %d , &q); q--;) { n     long long x; n     scanf( %I64d , &x); n     printf( %I64d n , solve(n, x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k; n vector<long long int> v[100005]; n vector<long long int> res; n long long int ares[100005]; n long long int was[100005]; n int dfs(long long int ind, long long int c) { n   if (c > 100005) { n     return -1; n   } n   if (ares[ind]) return 1; n   if (!v[ind].size()) { n     ares[ind] = 1; n     res.push_back(ind); n     return 1; n   } n   for (auto i : v[ind]) { n     int r = dfs(i, c + 1); n     if (r < 0) { n       return -1; n     } n   } n   ares[ind] = 1; n   res.push_back(ind); n   return 1; n } n int main() { n   cin >> n >> k; n   long long int *arr = new long long int[k]; n   for (long long int i = 0; i < k; i++) { n     cin >> arr[i]; n     arr[i]--; n   } n   for (long long int i = 0; i < n; i++) { n     ares[i] = 0; n     long long int a, b; n     cin >> b; n     for (long long int j = 0; j < b; j++) { n       cin >> a; n       a--; n       v[i].push_back(a); n     } n   } n   for (long long int i = 0; i < k; i++) { n     int r = dfs(arr[i], 0); n     if (r < 0) { n       cout << -1; n       return 0; n     } n   } n   cout << res.size() << endl; n   for (auto i : res) { n     cout << (i + 1) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const double PI = acos(-1.0); n const int MOD = 1000 * 1000 * 1000 + 7; n const int INF = 2000 * 1000 * 1000; n const int MAXN = 55; n template <typename T> n inline T sqr(T n) { n   return n * n; n } n int n; n long long m; n long long weight[MAXN]; n int ans[MAXN]; n int main() { n   scanf( %d%I64d , &n, &m); n   weight[0] = 1ll << (n - 1); n   for (int i = 1; i <= n; i++) { n     weight[i] = max(weight[i - 1] >> 1, 1ll); n   } n   int l = 1, r = n; n   int num = 1; n   long long cur = 0; n   while (l <= r) { n     if (l == r || cur + weight[num] >= m) { n       ans[l] = num; n       l++; n     } else { n       cur += weight[num]; n       ans[r] = num; n       r--; n     } n     num++; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d%c , ans[i],    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 32005; n int n, m, i, j, f; n int a[N]; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   for (i = m + 1; i <= (n + 1) / 2; i++) { n     for (j = m + 1; j < 2 * i; j++) { n       f = (i * (2 * i - j) + (n + 1) * (2 * j - i)) / (i + j); n       f = max(f, m); n       if (f > n - m) continue; n       a[i] += n - m - f; n     } n   } n   for (i = m + 1; i <= (n + 1) / 2; i++) ans += 2 * a[i]; n   if (n & 1) ans -= a[i - 1]; n   printf( %I64d n , 3 * ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long mod = 1e9 + 7; n long long n, m; n struct mat { n   int a[105][105]; n   mat() { memset(a, 0, sizeof(a)); } n   mat operator*(const mat &b) const { n     mat c; n     for (int i = (1); i < (m + 1); i++) { n       for (int j = (1); j < (m + 1); j++) { n         for (int k = (1); k < (m + 1); k++) { n           c.a[i][j] = (c.a[i][j] + a[i][k] % mod * b.a[k][j] % mod) % mod; n         } n       } n     } n     return c; n   } n }; n mat mod_pow(mat x, long long n) { n   mat res; n   for (int i = (1); i < (m + 1); i++) res.a[i][i] = 1; n   while (n > 0) { n     if (n & 1) res = res * x; n     x = x * x; n     n >>= 1; n   } n   return res; n } n int main() { n   cin >> n >> m; n   mat b, x, ans; n   for (int i = (1); i < (m + 1); i++) b.a[i][1] = 1; n   for (int i = (2); i < (m + 1); i++) x.a[i][i - 1] = 1; n   x.a[1][1] = 1; n   x.a[1][m] = 1; n   ans = mod_pow(x, n - m + 1) * b; n   cout << ans.a[1][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 210000; n int n; n int oke[maxn], okp[maxn]; n struct edge { n   int y, nex; n } a[maxn << 1]; n int len, fir[maxn]; n inline void ins(const int x, const int y) { n   a[++len] = (edge){y, fir[x]}; n   fir[x] = len; n } n int siz[maxn], mxs[maxn]; n void dfs(const int x, const int fa) { n   siz[x] = 1; n   for (int k = fir[x], y = a[k].y; k; k = a[k].nex, y = a[k].y) n     if (y != fa) { n       dfs(y, x); n       siz[x] += siz[y]; n       mxs[x] = max(mxs[x], siz[y]); n     } n } n int root, t1, t2; n int d[maxn], f[maxn], f2[maxn]; n bool cmp(const int x, const int y) { n   return f[x] == f[y] ? f2[x] < f2[y] : f[x] < f[y]; n } n int t[maxn], tp; n void Hash(const int x, const int fa) { n   siz[x] = 1; n   for (int k = fir[x], y = a[k].y; k; k = a[k].nex, y = a[k].y) n     if (y != fa && oke[k]) Hash(y, x), siz[x] += siz[y]; n   tp = 0; n   for (int k = fir[x], y = a[k].y; k; k = a[k].nex, y = a[k].y) n     if (y != fa && oke[k]) t[++tp] = y; n   sort(t + 1, t + tp + 1, cmp); n   d[x] = tp + (fa != 0); n   f[x] = f2[x] = siz[x]; n   for (int i = 1; i <= tp; i++) { n     int y = t[i]; n     f[x] = f[x] * (siz[y] + 1) * f[y]; n     f2[x] = f2[x] + (siz[y] + 2) * (f2[y] + 1); n   } n } n int ans[maxn]; n void cal(const int x, const int fa) { n   if (okp[x] && d[x] < 4) ans[x]++; n   for (int k = fir[x], y = a[k].y; k; k = a[k].nex, y = a[k].y) n     if (y != fa && oke[k]) cal(y, x); n   tp = 0; n   for (int k = fir[x], y = a[k].y; k; k = a[k].nex, y = a[k].y) n     if (y != fa && oke[k]) t[++tp] = y; n   sort(t + 1, t + tp + 1, cmp); n   for (int i = 1; i <= tp; i++) { n     int y = t[i], ty = t[i - 1]; n     if (f[y] != f[ty] || f2[y] != f2[ty]) ans[x] += ans[y]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     ins(x, y), ins(y, x); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) n     if (mxs[i] * 2 <= n && (n - siz[i]) * 2 <= n) { n       if (!t1) n         t1 = i; n       else n         t2 = i; n     } n   for (int i = 1; i <= len; i++) oke[i] = 1; n   for (int i = 1; i <= n; i++) okp[i] = 1; n   if (t2) { n     for (int k = fir[t1], y = a[k].y; k; k = a[k].nex, y = a[k].y) n       if (y == t2) oke[k] = 0; n     for (int k = fir[t2], y = a[k].y; k; k = a[k].nex, y = a[k].y) n       if (y == t1) oke[k] = 0; n     okp[root = ++n] = 0; n     ins(root, t1), ins(root, t2); n     oke[len - 1] = oke[len] = 1; n   } else n     root = t1; n   Hash(root, 0); n   cal(root, 0); n   printf( %d n , ans[root]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int ts; n   cin >> ts; n   while (ts--) { n     string s, t; n     cin >> s >> t; n     int n = (int)s.size(); n     map<string, int> cnt; n     for (int i = 0; i < n; i += 2) { n       string cur =   ; n       cur += s[i]; n       cur += s[i + 1]; n       sort(cur.begin(), cur.end()); n       cnt[cur]++; n     } n     for (int i = 0; i < n; i += 2) { n       string cur =   ; n       cur += t[i]; n       cur += t[i + 1]; n       sort(cur.begin(), cur.end()); n       cnt[cur]--; n     } n     bool bad = false; n     for (auto c : cnt) { n       if (c.second != 0) { n         bad = true; n       } n     } n     if (bad) { n       cout << -1 <<   n ; n       continue; n     } n     while (true) { n       int start = rnd() % (n / 2 + 1); n       vector<int> ord; n       if (start != 0) { n         ord.push_back(start - 1); n       } n       string cur = s, me = t; n       reverse(cur.begin(), cur.begin() + 2 * start); n       int its = 0; n       bool bad = false; n       while (cur.size() > 0) { n         int ind = (int)me.size() - 2; n         vector<int> sh; n         for (int i = (int)cur.size() - 2; i >= 0; i -= 2) { n           sh.push_back(i); n         } n         shuffle(sh.begin() + 1, sh.end(), rnd); n         bool mda = false; n         for (int i : sh) { n           if (cur[i] == me[ind] && cur[i + 1] == me[ind + 1]) { n             if (i + 2 != (int)cur.size()) { n               ord.push_back(i / 2); n               reverse(cur.begin(), cur.begin() + i + 2); n               ord.push_back(ind / 2); n               reverse(cur.begin(), cur.end()); n             } n             mda = true; n             break; n           } n         } n         if (!mda) { n           vector<int> sh; n           for (int i = 0; i < (int)cur.size(); i += 2) { n             sh.push_back(i); n           } n           shuffle(sh.begin(), sh.end(), rnd); n           for (int i : sh) { n             if (cur[i] == me[ind + 1] && cur[i + 1] == me[ind]) { n               ord.push_back(i / 2); n               reverse(cur.begin(), cur.begin() + i + 2); n               ord.push_back(0); n               reverse(cur.begin(), cur.begin() + 2); n               ord.push_back(ind / 2); n               reverse(cur.begin(), cur.end()); n               break; n             } n           } n         } n         if (ord.size() > n + 1) { n           bad = true; n           break; n         } n         for (int it = 0; it < 2; it++) { n           cur.pop_back(), me.pop_back(); n         } n       } n       if (bad) { n         continue; n       } else { n         cout << ord.size() <<   n ; n         string f = s, g = t; n         for (int a : ord) { n           cout << 2 * (a + 1) <<    ; n         } n         cout <<   n ; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long n; n bool len(char x) { n   return x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u  || x ==  y ; n } n int main() { n   cin >> n; n   cin >> s; n   cout << s[0]; n   for (int i = 1; i < n; i++) { n     if (len(s[i]) && len(s[i - 1])) continue; n     cout << s[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n const long long maxn = (long long)1e6 + 5; n const long long mod = 998244353; n const long long inf = 0x3f3f3f3f; n long long T = 1; n vector<long long> v[maxn], vec[maxn]; n long long dep[maxn], pre[maxn]; n long long maxx; n void dfs(long long now) { n   maxx = max(maxx, dep[now]); n   vec[dep[now]].push_back(now); n   for (auto &to : v[now]) { n     if (dep[to]) continue; n     pre[to] = now; n     dep[to] = dep[now] + 1; n     dfs(to); n   } n } n void solve() { n   long long n, m; n   cin >> n >> m; n   maxx = 1; n   for (long long i = 1; i <= n; ++i) v[i].clear(), vec[i].clear(), dep[i] = 0; n   for (long long i = 1; i <= m; ++i) { n     long long x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dep[1] = 1; n   dfs(1); n   if (maxx > (n + 1) / 2) { n     cout <<  PATH n ; n     stack<long long> s; n     long long now = vec[maxx][0]; n     while (now) { n       s.push(now); n       now = pre[now]; n     } n     cout << s.size() <<   n ; n     while (!s.empty()) { n       cout << s.top() <<    ; n       s.pop(); n     } n     cout <<   n ; n     return; n   } n   cout <<  PAIRING n ; n   long long ans = 0; n   for (long long i = 1; i <= maxx; ++i) { n     ans += vec[i].size() / 2; n   } n   cout << ans <<   n ; n   for (long long i = 1; i <= maxx; ++i) { n     for (long long j = 0; j + 1 < vec[i].size(); j += 2) { n       cout << vec[i][j] <<     << vec[i][j + 1] <<   n ; n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> vec(n); n   for (long long i = 0; i < n; i++) { n     cin >> vec[i]; n   } n   vec.push_back(vec.back()); n   long long ans = 1; n   long long cnt = 1; n   for (long long i = 1; i < n; i++) { n     if (vec[i] > vec[i - 1] * 2) { n       ans = max(ans, cnt); n       cnt = 0; n     } n     cnt++; n   } n   ans = max(ans, cnt); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n int main() { n   int n; n   scanf( %d , &n); n   double inch = (double)n / 3; n   int ninch = (int)inch; n   if (inch - (double)ninch >= 0.5) ++ninch; n   printf( %d %d n , ninch / 12, ninch % 12); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:20000000 ) n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<string> a; n   string s; n   for (int i = (0); (i) < (n); (i)++) { n     cin >> s; n     a.push_back( #  + s +  # ); n   } n   int x = 1; n   long long r = 0; n   bool g = true; n   for (int i = 0; i < n - 1; i++) { n     if (a[i + 1][x] ==  . ) { n       r++; n       continue; n     } n     int left = x, right = x; n     int wall = 0; n     bool down = false; n     while (!down) { n       if (g) { n         if (a[i + 1][right] ==  . ) { n           r += right - x + 1; n           x = right; n           break; n         } n         for (int j = right + 1;; j++) { n           if (a[i][j] ==  + ) { n             wall = 0; n             right = j; n             r += j - x; n             x = j - 1; n             g = false; n             break; n           } else if (a[i][j] ==  # ) { n             wall++; n             right = j - 1; n             r += j - x; n             x = j - 1; n             g = false; n             break; n           } else if (a[i + 1][j] ==  . ) { n             r += j - x + 1; n             x = j; n             down = true; n             break; n           } n         } n       } else { n         if (a[i + 1][left] ==  . ) { n           r += x - left + 1; n           x = left; n           break; n         } n         for (int j = left - 1;; j--) { n           if (a[i][j] ==  + ) { n             wall = 0; n             left = j; n             r += x - j; n             x = j + 1; n             g = true; n             break; n           } else if (a[i][j] ==  # ) { n             wall++; n             left = j + 1; n             r += x - j; n             x = j + 1; n             g = true; n             break; n           } else if (a[i + 1][j] ==  . ) { n             r += x - j + 1; n             x = j; n             down = true; n             break; n           } n         } n       } n       if (wall == 2) { n         cout <<  Never ; n         return 0; n       } n     } n   } n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   node *l, *r, *p; n   long long v, s, z, w, t, k, i; n   node(long long v, long long i) n       : l(0), r(0), p(0), v(v), s(1), z(0), w(0), t(v), k(1), i(i) {} n }; n inline bool is_root(node *n) { n   return n->p == NULL || n->p->l != n && n->p->r != n; n } n inline bool left(node *n) { return n == n->p->l; } n inline long long size(node *n) { return n ? n->s + n->z : 0; } n inline long long tot(node *n) { return n ? n->t : 0; } n inline void push(node *n) { n   n->s += n->z; n   n->w += n->z; n   n->k += 2 * n->z * (n->s - n->w); n   if (n->l) n->l->z += n->z; n   if (n->r) n->r->z += n->z; n   n->z = 0; n } n inline void update(node *n) { n   push(n); n   n->t = tot(n->l) + tot(n->r) + n->v * (n->s - n->w); n } n inline void connect(node *n, node *p, bool l) { n   (l ? p->l : p->r) = n; n   if (n) n->p = p; n } n inline void rotate(node *n) { n   node *p = n->p, *g = p->p; n   bool l = left(n); n   connect(l ? n->r : n->l, p, l); n   if (!is_root(p)) n     connect(n, g, left(p)); n   else n     n->p = g; n   connect(p, n, !l); n   update(p), update(n); n } n inline void splay(node *n) { n   while (!is_root(n)) { n     node *p = n->p, *g = p->p; n     if (!is_root(p)) push(g); n     push(p), push(n); n     if (!is_root(p)) rotate(left(n) ^ left(p) ? n : p); n     rotate(n); n   } n   push(n); n } n inline node *find_root(node *n) { n   if (n == NULL) return NULL; n   while (n->l) { n     push(n); n     n = n->l; n   } n   return n; n } n inline node *expose(node *n) { n   node *last = NULL; n   for (node *m = n; m; m = m->p) { n     splay(m); n     m->w = size(find_root(last)); n     m->r = last; n     last = m; n     update(m); n   } n   splay(n); n   return last; n } n void link(node *m, node *n) { n   expose(m), expose(n); n   m->p = n; n   n->z += size(m); n } n void cut(node *n) { n   expose(n); n   n->l->z -= size(n); n   n->l->p = NULL; n   n->l = NULL; n } n node *lca(node *m, node *n) { n   expose(m); n   return expose(n); n } n const int MAXN = 50005; n long long N, M, P[MAXN], A, B, ans; n char C; n node *V[MAXN]; n int main() { n   cin >> N; n   cout << fixed << setprecision(9); n   for (int i = 1; i < N; i++) { n     cin >> P[i]; n     P[i]--; n   } n   for (int i = 0; i < N; i++) { n     int v; n     cin >> v; n     V[i] = new node(v, i); n     ans += v; n   } n   for (int i = 1; i < N; i++) { n     link(V[i], V[P[i]]); n     ans += 2 * tot(V[P[i]]) * size(V[i]); n   } n   cout << 1.0 * ans / N / N <<   n ; n   cin >> M; n   while (M--) { n     cin >> C >> A >> B; n     if (C ==  P ) { n       node *v = V[A - 1], *u = V[B - 1]; n       if (lca(u, v) == v) swap(u, v); n       expose(v); n       ans -= 2 * tot(v->l) * size(v); n       cut(v); n       link(v, u); n       ans += 2 * tot(u) * size(v); n     } else { n       node *v = V[A - 1]; n       expose(v); n       update(v); n       ans += (B - v->v) * v->k; n       v->v = B; n     } n     cout << 1.0 * ans / N / N <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 3e3; n uint16_t a[N], lst[N], nxt[N], cnxt[20], dp[N][N]; n int main() { n   cin.tie(0), ios::sync_with_stdio(0); n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i < n; ++i) { n       cin >> a[i], --a[i]; n     } n     memset(lst, 0, n * sizeof *lst); n     for (int i = 0; i < n; ++i) { n       ++lst[a[i]]; n     } n     int w = -1, r = 0, purged = 0; n     while (r < n) { n       if (w >= 0 && a[w] == a[r]) { n         if (--lst[a[r++]] == 1) { n           --w; n           ++purged; n         } n       } else if (lst[a[r]] == 1) { n         ++r; n         ++purged; n       } else { n         a[++w] = a[r++]; n       } n     } n     memset(lst, -1, n * sizeof *lst); n     n = w + 1; n     memset(nxt, 127, n * sizeof *nxt); n     for (int i = 0; i < n; ++i) { n       if (lst[a[i]] != (uint16_t)-1) { n         nxt[lst[a[i]]] = i; n       } n       lst[a[i]] = i; n     } n     for (int i = n - 1; i > -1; --i) { n       auto* cnxte = cnxt; n       for (int z = nxt[i]; z < n; z = nxt[z]) { n         *cnxte++ = z; n       } n       *cnxte = n; n       cnxte = cnxt; n       for (int j = i + 1; j < n; ++j) { n         if (a[j] == a[i]) { n           ++cnxte; n         } n         int t = dp[i + 1][j] - 1; n #pragma GCC unroll 20 n         for (const auto* z = cnxt; z < cnxte; ++z) { n           t = max(t, dp[i + 1][*z - 1] + dp[*z][j]); n         } n         dp[i][j] = t + 1; n       } n     } n     cout << (n ? n - dp[0][n - 1] : 0) - 1 + purged <<   n ; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> seg[2100000], lazy[2100000]; n pair<int, int> Merge(pair<int, int> l, pair<int, int> r) { n   if (l < r) n     return l; n   else n     return r; n } n void Propagate(int n, int l, int r) { n   if (lazy[n] != make_pair(0x3f3f3f3f, 0)) { n     if (l != r) { n       lazy[n << 1] = Merge(lazy[n << 1], lazy[n]); n       lazy[n << 1 | 1] = Merge(lazy[n << 1 | 1], lazy[n]); n     } n     seg[n] = Merge(seg[n], lazy[n]); n     lazy[n] = make_pair(0x3f3f3f3f, 0); n   } n } n pair<int, int> Update(int L, int R, pair<int, int> val, int n, int l, int r) { n   Propagate(n, l, r); n   if (r < L || R < l) return seg[n]; n   if (L <= l && r <= R) { n     lazy[n] = Merge(lazy[n], val); n     Propagate(n, l, r); n     return seg[n]; n   } n   int mid = l + r >> 1; n   return seg[n] = Merge(Update(L, R, val, n << 1, l, mid), n                         Update(L, R, val, n << 1 | 1, mid + 1, r)); n } n pair<int, int> Query(int L, int R, int n, int l, int r) { n   Propagate(n, l, r); n   if (r < L || R < l) return make_pair(0x3f3f3f3f, 0); n   if (L <= l && r <= R) return seg[n]; n   int mid = l + r >> 1; n   return Merge(Query(L, R, n << 1, l, mid), n                Query(L, R, n << 1 | 1, mid + 1, r)); n } n int N, M; n vector<pair<int, int> > v[300001]; n pair<int, int> dp[300001]; n int main() { n   for (int i = 0; i < 2100000; i++) seg[i] = lazy[i] = make_pair(0x3f3f3f3f, 0); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(ios::badbit | ios::failbit); n   cin >> N >> M; n   vector<int> com; n   for (int i = 1; i <= M; i++) { n     int n, l, r; n     cin >> n >> l >> r; n     r++; n     v[n].push_back({l, r}); n     com.push_back(l); n     com.push_back(r); n   } n   sort(com.begin(), com.end()); n   com.erase(unique(com.begin(), com.end()), com.end()); n   for (int i = 1; i <= N; i++) n     for (auto &[l, r] : v[i]) { n       l = lower_bound(com.begin(), com.end(), l) - com.begin(); n       r = lower_bound(com.begin(), com.end(), r) - com.begin(); n     } n   for (int i = 1; i <= N; i++) { n     dp[i] = {i - 1, 0}; n     for (auto [l, r] : v[i]) { n       auto it = Query(l, r - 1, 1, 0, (int)com.size() - 1); n       dp[i] = min(dp[i], make_pair(it.first + i, it.second)); n     } n     for (auto [l, r] : v[i]) { n       Update(l, r - 1, make_pair(dp[i].first - i - 1, i), 1, 0, n              (int)com.size() - 1); n     } n   } n   int res = 0x3f3f3f3f, n = 0; n   for (int i = 1; i <= N; i++) { n     int val = dp[i].first + (N - i); n     if (res > val) res = val, n = i; n   } n   set<int> s; n   for (int i = 1; i <= N; i++) s.insert(i); n   while (n) { n     s.erase(n); n     n = dp[n].second; n   } n   cout << res <<   n ; n   if (res) { n     for (int i : s) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, x; n   cin >> n >> x; n   long long count = 0; n   if (n >= x) { n     for (long long i = 1; i < n + 1; i++) { n       if (x % i == 0) { n         count++; n       } n     } n   } else { n     for (long long i = 2; i < n + 1; i++) { n       if (x % i == 0 && ((x / i) <= n)) { n         count++; n       } n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n void show_trace() { cout << endl; } n template <typename Start, typename... Rem> n void show_trace(Start S, Rem... R) { n   cout <<     << S; n   show_trace(R...); n } n const int INFi = 2139062143; n const long long INF = 1e18; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   int ct1 = count(s.begin(), s.end(),  a ) + count(t.begin(), t.end(),  a ); n   int ct2 = count(s.begin(), s.end(),  b ) + count(t.begin(), t.end(),  b ); n   if (ct1 % 2 != 0 || ct2 % 2 != 0) { n     cout << -1 <<   n ; n     return 0; n   } n   vector<int> ab, ba; n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) continue; n     if (s[i] ==  a  && t[i] ==  b ) n       ab.push_back(i); n     else n       ba.push_back(i); n   } n   int sz1 = ab.size(); n   int sz2 = ba.size(); n   vector<pair<int, int> > ans; n   for (int i = 1; i < sz1 - (sz1 % 2); i += 2) { n     ans.push_back({ab[i], ab[i - 1]}); n   } n   for (int i = 1; i < sz2 - (sz2 % 2); i += 2) { n     ans.push_back({ba[i], ba[i - 1]}); n   } n   assert(sz1 % 2 == sz2 % 2); n   if (sz1 % 2) { n     ans.push_back({ab[sz1 - 1], ab[sz1 - 1]}); n     ans.push_back({ab[sz1 - 1], ba[sz2 - 1]}); n   } n   cout << ans.size() <<   n ; n   for (auto [x, y] : ans) cout << x + 1 <<     << y + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<string> all; n   for (long long i = 0; i <= 9; i++) { n     all.push_back(s); n     for (char &c : s) n       if (c ==  9 ) n         c =  0 ; n       else n         c++; n   } n   string ans = s; n   for (string &x : all) { n     for (long long i = 0; i < n; i++) { n       string y = x; n       rotate(y.begin(), y.begin() + y.size() - i, y.end()); n       ans = min(y, ans); n     } n   } n   cout << ans <<   n ; n } n void solve(bool testcase) { n   long long t; n   cin >> t; n   while (t--) solve(); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double a[100009][2]; n long double maxi(long double a, long double b) { n   if (a > b) n     return a; n   else n     return b; n } n bool solve(long double mid, int n, long double p) { n   long double req = 0; n   for (int i = 0; i < n; i++) { n     req += maxi(0.00, mid * a[i][0] - a[i][1]); n   } n   if (req > mid * p) n     return false; n   else n     return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long double n, p, sum = 0, count = 0, m, flag = 0, ans = 0, k; n   cin >> n >> p; n   long double l = 0, r = 1e10; n   for (int i = 0; i < n; i++) { n     cin >> a[i][0] >> a[i][1]; n   } n   while (flag < 1000) { n     long double m = (l + r) / 2; n     if (solve(m, n, p)) n       ans = m, l = m; n     else n       r = m; n     flag++; n   } n   if (ans >= 1e10) { n     cout << -1; n     return 0; n   } n   cout << setprecision(8) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void FREOPEN() {} n inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; } n const int N = (int)2000, inf = (int)2e9, MOD = (int)1e9 + 7, alph = 26; n char CH[N]; n const long long INF = (long long)1e18; n const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1}; n const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1}; n const int dp[] = {8, 9, 1, 13, 3, 12, 7, 5, 0, 2, 4, 11, 6, 10, 15, 14}; n int a[N][N]; n int main() { n   FREOPEN(); n   int n; n   cin >> n; n   int num = 0; n   for (int bi = 0; bi < n; bi += 4) { n     for (int bj = 0; bj < n; bj += 4) { n       for (int i = bi; i < bi + 4; i++) { n         for (int j = bj; j < bj + 4; j++) { n           int p = (i - bi) * 4 + (j - bj); n           a[i][j] = (dp[p] ^ num); n         } n       } n       num += 16; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) printf( %d  , a[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000000 + 7; n const int M = 26; n const int mod = 999983; n const int inf = 1e9 + 7; n const double pi = acos(-1); n const int maxn = N * 2; n const double PI = acos(-1); n int n, m; n int a[N]; n int dp[22][N]; n string s; n void solve() { n   cin >> n >> m; n   for (int i = (0); i < (n); i++) { n     cin >> s; n     for (int j = (0); j < (m); j++) { n       int u = s[j] -  0 ; n       a[j] = (a[j] << 1) | u; n     } n   } n   for (int i = (0); i < (m); i++) { n     dp[0][a[i]]++; n   } n   for (int k = (1); k < (n + 1); k++) { n     for (int mask = (0); mask < (1 << n); mask++) { n       if (k >= 2) dp[k][mask] = (k - 2 - n) * dp[k - 2][mask]; n       for (int p = (0); p < (n); p++) dp[k][mask] += dp[k - 1][mask ^ (1 << p)]; n       dp[k][mask] /= k; n     } n   } n   int ans = inf; n   for (int mask = (0); mask < (1 << n); mask++) { n     int cur = 0; n     for (int k = (0); k < (n + 1); k++) cur += min(k, n - k) * dp[k][mask]; n     ans = min(ans, cur); n   } n   cout << ans; n } n int main() { n   int T = 1; n   for (int i = (1); i < (T + 1); i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[2005]; n char p[505]; n int dp[2005][2005]; n int len[2005]; n int main() { n   scanf( %s , s); n   scanf( %s , p); n   int len_s = strlen(s); n   int len_p = strlen(p); n   for (int i = 0; i < len_s; i++) { n     int tot = i; n     if (s[i] != p[0]) { n       len[i] = -1; n       continue; n     } n     for (int j = 0; j < len_p; j++) { n       while (tot < len_s && s[tot] != p[j]) tot++; n       tot++; n     } n     if (tot >= len_s + 1) n       len[i] = -1; n     else n       len[i] = tot - 1; n   } n   for (int i = 0; i <= len_s; i++) n     for (int j = 0; j <= len_s; j++) dp[i][j] = 0x3f3f3f3f; n   dp[0][0] = 0; n   int all = len_s / len_p; n   len[len_s] = -1; n   for (int i = 0; i <= len_s; i++) { n     if (i) { n       for (int j = 0; j <= all; j++) dp[i][j] = min(dp[i][j], dp[i - 1][j]); n     } n     if (len[i] != -1) { n       for (int j = 0; j <= all; j++) { n         dp[len[i] + 1][j + 1] = n             min(dp[len[i] + 1][j + 1], dp[i][j] + len[i] - i + 1 - len_p); n       } n     } n   } n   int temp = 1; n   while (dp[len_s][temp] <= 2000 && n          dp[len_s][temp] - dp[len_s][temp - 1] >= 0 && temp <= all) { n     for (int i = 0; i < dp[len_s][temp] - dp[len_s][temp - 1]; i++) n       printf( %d  , temp - 1); n     temp++; n   } n   temp -= 1; n   int a = len_s - temp * len_p - dp[len_s][temp] + 1; n   while (a > 0 && a--) printf( %d  , temp); n   for (int i = temp * len_p - 1; i >= 0; i--) { n     printf( %d  , i / len_p); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long input[100000], n, output, table[100000], a, tlg, go, high, lg; n int main(int argc, char** argv) { n   int i, j, k; n   while (scanf(  %d , &n) == 1) { n     output = 0; n     lg = 1; n     tlg = 1; n     scanf(  %d , &input[0]); n     for (i = 1; i < n; i++) { n       scanf(  %d , &a); n       if (a != input[lg - 1]) { n         input[lg] = a; n         lg++; n       } n     } n     table[0] = input[0]; n     input[lg] = 1000000000; n     for (i = 1; i < lg; i++) { n       if ((input[i] < input[i - 1] && input[i] < input[i + 1]) || n           (input[i] > input[i - 1] && input[i] > input[i + 1])) { n         table[tlg] = input[i]; n         tlg++; n       } n     } n     table[tlg] = 1000000000; n     high = table[0]; n     for (i = 0; i < tlg; i++) { n       if (table[i] > table[i + 1] && table[i + 1] < table[i + 2]) { n         if (high > table[i + 2]) { n           output += table[i + 2] - table[i + 1]; n           i++; n         } else { n           output += high - table[i + 1]; n           high = table[i + 2]; n           i++; n         } n       } else { n         high = table[i + 1]; n       } n     } n     cout << output << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 333333; n const long long mod = 1000000009ll; n int n, m; n struct node { n   int l, r; n   long long sum, f1, f2; n } t[N * 6]; n long long a[N], F[N]; n inline long long read() { n   long long x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) { n     x = x * 10ll + c -  0 ; n     c = getchar(); n   } n   return x; n } n void calc_Fibonacci(int n) { n   F[1] = 1; n   F[2] = 1; n   for (int i = (3); i <= (N); i++) F[i] = (F[i - 1] + F[i - 2]) % mod; n } n long long calc(long long a, long long b, long long n) { n   if (n == 1) n     return a; n   else if (n == 2) n     return b; n   else n     return (a * F[n - 2] % mod + b * F[n - 1] % mod) % mod; n } n long long calc_range(long long a, long long b, long long n) { n   if (n == 1) n     return a; n   else if (n == 2) n     return (a + b) % mod; n   else n     return (calc(a, b, n + 2) - b + mod) % mod; n } n void build(int x, int l, int r) { n   int mid = (l + r) / 2, lc = x * 2, rc = x * 2 + 1; n   t[x].l = l; n   t[x].r = r; n   if (l == r) { n     t[x].sum = a[l]; n     t[x].f1 = t[x].f2 = 0; n     return; n   } n   build(lc, l, mid); n   build(rc, mid + 1, r); n   t[x].sum = (t[lc].sum + t[rc].sum) % mod; n   t[x].f1 = t[x].f2 = 0; n } n void pushdown(int x) { n   int l = t[x].l, r = t[x].r; n   int mid = (l + r) / 2, lc = x * 2, rc = x * 2 + 1; n   t[lc].f1 = (t[lc].f1 + t[x].f1) % mod; n   t[lc].f2 = (t[lc].f2 + t[x].f2) % mod; n   t[lc].sum += calc_range(t[x].f1, t[x].f2, mid - l + 1); n   t[lc].sum %= mod; n   t[rc].f1 = (t[rc].f1 + calc(t[x].f1, t[x].f2, mid - l + 2)) % mod; n   t[rc].f2 = (t[rc].f2 + calc(t[x].f1, t[x].f2, mid - l + 3)) % mod; n   t[rc].sum += calc_range(t[x].f1, t[x].f2, r - l + 1) - n                calc_range(t[x].f1, t[x].f2, mid - l + 1); n   t[rc].sum = (t[rc].sum + mod) % mod; n   t[x].f1 = t[x].f2 = 0; n } n void update(int x, int l, int r, int ql, int qr) { n   int mid = (l + r) / 2, lc = x * 2, rc = x * 2 + 1; n   if (l >= ql && r <= qr) { n     t[x].f1 = (t[x].f1 + F[l - ql + 1]) % mod; n     t[x].f2 = (t[x].f2 + F[l - ql + 2]) % mod; n     t[x].sum += calc_range(F[l - ql + 1], F[l - ql + 2], r - l + 1); n     t[x].sum %= mod; n     return; n   } n   pushdown(x); n   if (ql <= mid) update(lc, l, mid, ql, qr); n   if (qr > mid) update(rc, mid + 1, r, ql, qr); n   t[x].sum = (t[lc].sum + t[rc].sum) % mod; n   return; n } n long long query(int x, int l, int r, int ql, int qr) { n   int mid = (l + r) / 2, lc = x * 2, rc = x * 2 + 1; n   if (l >= ql && r <= qr) return t[x].sum; n   pushdown(x); n   long long ans_left = 0, ans_right = 0; n   if (ql <= mid) ans_left = query(lc, l, mid, ql, qr); n   if (qr > mid) ans_right = query(rc, mid + 1, r, ql, qr); n   return (ans_left + ans_right) % mod; n } n int main() { n   n = read(); n   m = read(); n   calc_Fibonacci(n); n   for (int i = (1); i <= (n); i++) a[i] = read(); n   build(1, 1, n); n   for (int i = (1); i <= (m); i++) { n     int tag, l, r; n     tag = read(); n     l = read(); n     r = read(); n     if (tag == 1) n       update(1, 1, n, l, r); n     else n       printf( %lld n , query(1, 1, n, l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 10; n int a[M]; n vector<int> ans[M]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int num = 0; n   while (num < k) { n     for (int i = n; i >= 0 && num < k; i--) { n       for (int j = 0; j < i; j++) { n         for (int x = n - 1; x >= i; x--) { n           ans[num].push_back(a[x]); n         } n         ans[num].push_back(a[j]); n         num++; n         if (num == k) { n           break; n         } n       } n     } n   } n   for (int i = 0; i < num; i++) { n     cout << ans[i].size() <<    ; n     for (int j = 0; j < ans[i].size(); j++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Pow(int x, int y, int mod) { n   long long t = x, r = 1; n   for (; y; y >>= 1, t = t * t % mod) y& 1 ? r = r * t % mod : 0; n   return r; n } n inline int Get() { n   int res = 0, q = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) q = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch -  0 , ch = getchar(); n   return res * q; n } n const double eps = 1e-12, pi = M_PI; n const int oo = (int)2.1e9, mod = (int)1e9 + 7; n const long long INF = (long long)1e17; n const int N = 110; n int n; n int dp[N][3]; n int a[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   dp[1][0] = 1; n   dp[1][1] = a[1] == 1 || a[1] == 3 ? 0 : oo; n   dp[1][2] = a[1] == 2 || a[1] == 3 ? 0 : oo; n   for (int i = 2; i <= n; ++i) { n     dp[i][0] = min(min(dp[i - 1][0], dp[i - 1][1]), dp[i - 1][2]) + 1; n     if (a[i] == 1 || a[i] == 3) n       dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]); n     else n       dp[i][1] = oo; n     if (a[i] == 2 || a[i] == 3) n       dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]); n     else n       dp[i][2] = oo; n   } n   printf( %d n , min(dp[n][0], min(dp[n][1], dp[n][2]))); n   return 0; n } n


#include <bits/stdc++.h> n long long n, i, j, h, k, t, d; n long double a, m, b; n using namespace std; n long double pow(long double a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long double t = pow(a, b / 2); n   t = t * t; n   if (b % 2 == 0) n     return t; n   else n     return t * a; n } n int main() { n   cin >> m >> n; n   a = m; n   for (i = 1; i < m; i++) { n     b = i / m; n     a -= pow(b, n); n   } n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int main() { n   ios::sync_with_stdio(false); n   int _; n   cin >> _; n   while (_--) { n     int n; n     cin >> n; n     int ans = 1e9 + 10; n     for (int i = 1; i * i <= n; i++) { n       int res = i - 1 + ceil(1.0 * n / (1.0 * i)) - 1; n       ans = min(res, ans); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cycle; n vector<int> adj[26], topo; n int color[26]; n void dfs(int i) { n   color[i] = 1; n   for (auto nx : adj[i]) n     if (!color[nx]) n       dfs(nx); n     else if (color[nx] == 1) n       cycle = 1; n   topo.push_back(i); n   color[i] = 2; n } n int main() { n   int n; n   scanf( %d , &n); n   string a[110]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     for (char c =  a ; c <=  z ; c++) { n       cout << c; n     } n     return 0; n   } n   for (int i = 1; i < n; i++) { n     int flag = 0; n     for (int j = 0; j < (int)min(a[i - 1].size(), a[i].size()); j++) { n       if (a[i - 1][j] != a[i][j]) { n         adj[a[i - 1][j] -  a ].push_back(a[i][j] -  a ); n         flag = 1; n         break; n       } n     } n     if (!flag && a[i - 1].size() > a[i].size()) { n       puts( Impossible ); n       return 0; n     } n   } n   for (int i = 0; i < 26; i++) { n     if (!color[i]) dfs(i); n   } n   if (cycle) { n     puts( Impossible ); n     return 0; n   } n   reverse(topo.begin(), topo.end()); n   for (auto x : topo) cout << char(x +  a ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, col[1000005], flag[1000005], tot, res[1000005]; n int k = 1, la[1000005], ff[1000005 * 2], check[1000005], pre[1000005], pos, n     del[1000005]; n int cnt, f[1000005], g[1000005]; n struct node { n   int a, b; n } e[1000005 * 2]; n map<pair<int, int>, int> MAP; n void add(int a, int b) { n   e[++k] = (node){a, b}; n   ff[k] = la[a]; n   la[a] = k; n   e[++k] = (node){b, a}; n   ff[k] = la[b]; n   la[b] = k; n } n void dfs(int x, int fa) { n   check[x] = flag[x] = 1; n   col[x] = -col[fa]; n   for (int a = la[x]; a; a = ff[a]) { n     if (e[a].b == fa) continue; n     if (!check[e[a].b]) { n       pre[e[a].b] = a; n       dfs(e[a].b, x); n       f[x] += f[e[a].b]; n       g[x] += g[e[a].b]; n       continue; n     } n     if (col[e[a].b] == col[x]) { n       if (flag[e[a].b]) { n         if (!cnt) pos = a; n         cnt++; n         f[x]++; n       } else n         f[x]--; n     } else { n       if (flag[e[a].b]) n         g[x]++; n       else n         g[x]--; n     } n   } n   flag[x] = 0; n } n int main() { n   int a, b; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a, &b), add(a, b); n     int num = MAP[make_pair(a, b)]; n     if (num) del[i] = 1, del[num] = 1; n     MAP[make_pair(a, b)] = i; n   } n   col[0] = 1; n   for (int i = 1; i <= n; i++) n     if (!check[i]) dfs(i, 0); n   if (!cnt) n     for (int i = 1; i <= m; i++) res[++tot] = i; n   else { n     if (cnt == 1 && !del[pos >> 1]) res[++tot] = (pos >> 1); n     for (int i = 1; i <= n; i++) { n       if (f[i] == cnt && !g[i] && !del[pre[i] >> 1]) n         if (!pre[i]) { n           for (int a = la[i]; a; a = ff[a]) n             if (e[a].b == i && !del[a >> 1]) res[++tot] = (a >> 1); n         } else n           res[++tot] = (pre[i] >> 1); n     } n   } n   sort(res + 1, res + tot + 1); n   tot = unique(res + 1, res + tot + 1) - res - 1; n   printf( %d n , tot); n   for (int i = 1; i < tot; i++) printf( %d  , res[i]); n   if (tot) printf( %d n , res[tot]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int who(int n) { n   if (n < 5) { n     return n; n   } else { n     n -= 5; n     return who(n / 2); n   } n } n int main(int argc, char** argv) { n   int n, m; n   cin >> n >> m; n   vector<vector<char> > snake(n, vector<char>(m,  # )); n   for (int i = 1; i < n; i += 2) { n     for (int j = (i / 2) % 2; j < m - 1 + (i / 2) % 2; ++j) { n       snake[i][j] =  . ; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cout << snake[i][j]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5, K = 4; n vector<int> g[N]; n set<int> s; n int a[N]; n int get_index(const vector<int> x) { n   static map<vector<int>, int> m; n   static int cnt; n   if (m.find(x) == m.end()) m[x] = cnt++; n   return m[x]; n } n void dfs_pre(int u, int fa) { n   vector<int> x; n   for (int v : g[u]) n     if (v != fa) { n       dfs_pre(v, u); n       x.push_back(a[v]); n     } n   sort(x.begin(), x.end()); n   a[u] = get_index(x); n } n void dfs(int u, int fa) { n   vector<int> x; n   if (g[u].size() < K) { n     for (int v : g[u]) x.push_back(a[v]); n     sort(x.begin(), x.end()); n     s.insert(get_index(x)); n   } n   sort(g[u].begin(), g[u].end(), [](int x, int y) { return a[x] < a[y]; }); n   x.clear(); n   for (int i = 1; i < g[u].size(); i++) x.push_back(a[g[u][i]]); n   int t = a[u]; n   for (int i = 0; i < g[u].size(); i++) { n     if (g[u][i] != fa) { n       a[u] = get_index(x); n       dfs(g[u][i], u); n     } n     if (i < g[u].size() - 1) x[i] = a[g[u][i]]; n   } n   a[u] = t; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs_pre(0, -1); n   dfs(0, -1); n   cout << s.size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   int a[] = {1, 2, 3, 4, 6, 12}; n   int b[] = {12, 6, 4, 3, 2, 1}; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int ans[20]; n     int count = 0, n = 0, no; n     for (int i = 0; i < 6; i++) { n       for (int j = 0; j < b[i]; j++) { n         no = 0; n         for (int k = 0; k < a[i]; k++) { n           if (s[j + k * b[i]] !=  X ) { n             no = 1; n             break; n           } n         } n         if (!no) { n           ans[count++] = i; n           n++; n           break; n         } n       } n     } n     cout << n <<    ; n     for (int i = 0; i < n; i++) { n       cout << a[ans[i]] <<  x  << b[ans[i]] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const int arr = 2e5 + 10; n const int ar = 2e3 + 10; n const long double pi = acos(-1); n const long double eps = 1e-10; n const long long md = 1e9 + 7; n vector<int> reb1[arr]; n vector<int> reb2[arr]; n bool use[arr]; n int comp[arr]; n const int M = 70 / 4 + 1; n int dist[1ll << M][71]; n void dfs(int now, int c) { n   comp[now] = c; n   use[now] = 1; n   for (auto wh : reb1[now]) { n     if (!use[wh]) { n       dfs(wh, c); n     } n   } n } n int main() { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   for (int i = 0; i < m; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     if (c == a) { n       reb1[u].push_back(v); n       reb1[v].push_back(u); n     } n     if (c == b) { n       reb2[u].push_back(v); n       reb2[v].push_back(u); n     } n   } n   int c = 0; n   int cc = 1e5; n   for (int i = 1; i <= n; i++) { n     if (!use[i]) { n       dfs(i, ++c); n       int cnt = 0; n       for (int j = 1; j <= n; j++) { n         if (comp[j] == c) { n           cnt++; n         } n       } n       if (cnt <= 3) { n         for (int j = 1; j <= n; j++) { n           if (comp[j] == c) { n             comp[j] = cc; n           } n         } n         cc--; n         c--; n       } n     } n   } n   set<pair<int, pair<int, int>>> s; n   int start_mask = 0; n   if (comp[1] <= c) { n     start_mask |= (1ll << (comp[1] - 1)); n   } n   for (int m = 0; m < (1ll << c); m++) { n     for (int j = 1; j <= n; j++) { n       if (j == 1 && m == start_mask) { n         dist[m][j] = 0; n       } else { n         dist[m][j] = 2e9 + 1; n       } n       s.insert(make_pair(dist[m][j], make_pair(m, j))); n     } n   } n   function<bool(int, int)> have_bit = [&](int m, int bit) { n     return m & (1ll << bit); n   }; n   while (!s.empty()) { n     pair<int, int> now = s.begin()->second; n     s.erase(s.begin()); n     for (auto wh : reb1[now.second]) { n       if (comp[wh] > c || comp[now.second] == comp[wh] || n           !have_bit(now.first, comp[wh] - 1)) { n         int to_m = now.first | (comp[wh] > c ? 0 : (1ll << (comp[wh] - 1))); n         if (dist[to_m][wh] > dist[now.first][now.second] + a) { n           s.erase(make_pair(dist[to_m][wh], make_pair(to_m, wh))); n           dist[to_m][wh] = dist[now.first][now.second] + a; n           s.insert(make_pair(dist[to_m][wh], make_pair(to_m, wh))); n         } n       } n     } n     for (auto wh : reb2[now.second]) { n       if ((comp[wh] > c || !have_bit(now.first, comp[wh] - 1)) && n           (comp[wh] != comp[now.second])) { n         int to_m = now.first | (comp[wh] > c ? 0 : (1ll << (comp[wh] - 1))); n         if (dist[to_m][wh] > dist[now.first][now.second] + b) { n           s.erase(make_pair(dist[to_m][wh], make_pair(to_m, wh))); n           dist[to_m][wh] = dist[now.first][now.second] + b; n           s.insert(make_pair(dist[to_m][wh], make_pair(to_m, wh))); n         } n       } n     } n   } n   for (int j = 1; j <= n; j++) { n     int ans = 2e9 + 1; n     for (int m = 0; m < (1ll << c); m++) { n       ans = min(ans, dist[m][j]); n     } n     cout << ans <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first == b.first) a.second < b.second; n   return a.first > b.first; n } n int main() { n   long long t, n, arr[200005], brr[200005], i; n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = 0; i < n; i++) cin >> arr[i]; n     for (i = 0; i < n; i++) cin >> brr[i]; n     for (i = 0; i < n; i++) v.push_back({arr[i], brr[i]}); n     sort(v.begin(), v.end(), cmp); n     long long self = 0; n     long long deli = v[0].first; n     long long mx = max(self, deli); n     long long mn = deli; n     for (i = 1; i < n; i++) { n       self += v[i - 1].second; n       deli = v[i].first; n       mx = max(self, deli); n       mn = min(mn, mx); n     } n     mn = min(mn, self + v[n - 1].second); n     cout << mn << endl; n     v.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n >> s; n   int a[n + 1]; n   int b[n + 1]; n   bool isCrossable = false; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   if (a[1] == 0 || (a[s] == 0 && b[s] == 0)) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (i >= s && a[i] == 1 && b[i] == 1) isCrossable = true; n   } n   if (a[s]) { n     cout <<  YES ; n     return 0; n   } n   if (b[s] && isCrossable) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct interval { n   int l, r; n   bool operator<(const interval &t) const { return r < t.r; } n }; n set<interval> itree[20]; n set<interval>::iterator l, r; n long long ans; n long long f(int type, int num) { n   return (1LL << type) * ((num + 1LL) * num) / 2; n } n void add(int type, int pos, int val) { n   interval unit = {pos, pos}; n   l = r = itree[type].lower_bound(unit); n   if (val == 0) { n     if (r->l <= pos && pos <= r->r) { n       interval new_left = {r->l, pos - 1}, new_right = {pos + 1, r->r}; n       ans -= f(type, (r->r) - (r->l) + 1); n       ans += f(type, new_left.r - new_left.l + 1); n       ans += f(type, new_right.r - new_right.l + 1); n       itree[type].erase(r); n       if (new_left.l <= new_left.r) itree[type].insert(new_left); n       if (new_right.l <= new_right.r) itree[type].insert(new_right); n     } n   } else { n     if (r->l <= pos && pos <= r->r) { n       return; n     } n     if (!itree[type].empty()) { n       if (l != itree[type].begin() && (--l)->r == pos - 1) { n         unit.l = l->l; n         ans -= f(type, l->r - l->l + 1); n         itree[type].erase(l); n       } n       if (r != itree[type].end() && r->l == pos + 1) { n         unit.r = r->r; n         ans -= f(type, r->r - r->l + 1); n         itree[type].erase(r); n       } n     } n     itree[type].insert(unit); n     ans += f(type, unit.r - unit.l + 1); n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     for (int j = 0; j < 20; ++j) { n       if (a % 2 != 0) add(j, i, a % 2); n       a /= 2; n     } n   } n   for (int i = 0; i < m; ++i) { n     int pos, val; n     cin >> pos >> val; n     for (int j = 0; j < 20; ++j) { n       add(j, pos, val % 2); n       val /= 2; n     } n     cout << ans <<   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     long long n, a, b; n     cin >> a >> b >> n; n     if (n % 2 == 0) n       cout << (n / 2) * (a - b) << endl; n     else n       cout << ((n / 2) * (a - b)) + a << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000005; n long long n, L, t; n long long K; n long long a[maxn]; n double ans; n int main() { n   scanf( %I64d%I64d%I64d , &n, &L, &t); n   t += t; n   K = t / L; n   t = t - L * K; n   ans += 0.25 * (double)K * n * (n - 1); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]), a[i + n] = a[i]; n   for (int i = 1; i <= n; i++) { n     int l = i, r = i + n - 1; n     while (l < r) { n       int mid = (l + r + 1) / 2; n       if (mid > n) { n         if (L - a[i] + a[mid] <= t) n           l = mid; n         else n           r = mid - 1; n       } else { n         if (a[mid] - a[i] <= t) n           l = mid; n         else n           r = mid - 1; n       } n     } n     ans += 0.25 * (l - i); n   } n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(n); n   set<int> s; n   int g = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     g ^= a[i]; n     s.insert(a[i]); n   } n   if (n == 3) { n     printf( YES n ); n     printf( 1 n ); n     printf( 1 2 3 n ); n     return 0; n   } n   if (n % 2 == 0 and g > 0) { n     printf( NO n ); n     return 0; n   } n   if (n % 2 == 0) { n     printf( YES n ); n     vector<pair<int, pair<int, int>>> v; n     for (int i = 1;; i += 2) { n       if (i + 2 > n) break; n       v.push_back(make_pair(i, make_pair(i + 1, i + 2))); n     } n     for (int i = 1;; i += 2) { n       if (i + 3 == n) break; n       v.push_back(make_pair(i, make_pair(i + 1, n))); n     } n     printf( %d n , v.size()); n     for (auto p : v) { n       printf( %d %d %d n , p.first, p.second.first, p.second.second); n     } n     return 0; n   } else { n     printf( YES n ); n     vector<pair<int, pair<int, int>>> v; n     for (int i = 1;; i += 2) { n       if (i + 2 > n) break; n       v.push_back(make_pair(i, make_pair(i + 1, i + 2))); n     } n     for (int i = 1;; i += 2) { n       if (i + 2 == n) break; n       v.push_back(make_pair(i, make_pair(i + 1, n))); n     } n     printf( %d n , v.size()); n     for (auto p : v) { n       printf( %d %d %d n , p.first, p.second.first, p.second.second); n     } n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1100; n int N, M; n int WX, WY, BX, BY; n int wdist[MAXN][MAXN]; n int wprev[MAXN][MAXN]; n int bdist[MAXN][MAXN]; n int bprev[MAXN][MAXN]; n int xc[8] = {-2, 2, -1, 1, -2, 2, -1, 1}; n int yc[8] = {-1, -1, -2, -2, 1, 1, 2, 2}; n bool is_white; n void await() { n   int nx, ny; n   cin >> nx >> ny; n   if (nx == -1) exit(0); n   if (is_white) { n     BX = nx; n     BY = ny; n   } else { n     WX = nx; n     WY = ny; n   } n } n void wsolve(int sx, int sy) { n   wdist[sx][sy] = 0; n   wprev[sx][sy] = -1; n   vector<pair<int, int> > q; n   q.push_back(make_pair(sx, sy)); n   for (int i = 0; i < q.size(); i++) { n     int cx = q[i].first, cy = q[i].second; n     int cdist = wdist[cx][cy]; n     for (int j = 0; j < 8; j++) { n       int x = cx + xc[j], y = cy + yc[j]; n       if (x < 1 || y < 1 || x > N || y > M) continue; n       if (wdist[x][y] <= cdist + 1) continue; n       wdist[x][y] = cdist + 1; n       wprev[x][y] = j; n       q.push_back(make_pair(x, y)); n     } n   } n } n void bsolve(int sx, int sy) { n   bdist[sx][sy] = 0; n   bprev[sx][sy] = -1; n   vector<pair<int, int> > q; n   q.push_back(make_pair(sx, sy)); n   for (int i = 0; i < q.size(); i++) { n     int cx = q[i].first, cy = q[i].second; n     int cdist = bdist[cx][cy]; n     for (int j = 0; j < 8; j++) { n       int x = cx + xc[j], y = cy + yc[j]; n       if (x < 1 || y < 1 || x > N || y > M) continue; n       if (bdist[x][y] <= cdist + 1) continue; n       bdist[x][y] = cdist + 1; n       bprev[x][y] = j; n       q.push_back(make_pair(x, y)); n     } n   } n } n bool wwin(int bmove) { n   int wturns = wdist[WX][WY]; n   int wblock = bdist[WX][WY]; n   int bturns = bdist[BX][BY]; n   int bblock = wdist[BX][BY]; n   if (bmove) { n     if (bturns <= wturns) { n       if (bturns < wblock) return false; n       if (bturns % 2 == wblock % 2) return true; n       return false; n     } else { n       if (wturns < bblock - 1) return true; n       if (wturns % 2 != bblock % 2) return false; n       return true; n     } n   } else { n     if (wturns <= bturns) { n       if (wturns < bblock) return true; n       if (wturns % 2 == bblock % 2) return false; n       return true; n     } else { n       if (bturns < wblock - 1) return false; n       if (bturns % 2 != wblock % 2) return true; n       return false; n     } n   } n } n int main() { n   cin >> N >> M >> WX >> WY >> BX >> BY; n   for (int i = 0; i < MAXN; i++) n     for (int j = 0; j < MAXN; j++) wdist[i][j] = bdist[i][j] = 1e9; n   wsolve(N / 2, M / 2); n   bsolve(N / 2 + 1, M / 2); n   if (wwin(0)) { n     is_white = true; n     cout <<  WHITE n ; n   } else { n     is_white = false; n     cout <<  BLACK n ; n     await(); n   } n   while (true) { n     int bloc = -1, bturns = 1e9; n     for (int i = 0; i < 8; i++) { n       if (is_white) { n         WX += xc[i]; n         WY += yc[i]; n         if (WX >= 1 && WX <= N && WY >= 1 && WY <= M) { n           if (wwin(1)) { n             if (wdist[WX][WY] < bturns) { n               bturns = wdist[WX][WY]; n               bloc = i; n             } n           } n         } n         WX -= xc[i]; n         WY -= yc[i]; n       } else { n         BX += xc[i]; n         BY += yc[i]; n         if (BX >= 1 && BX <= N && BY >= 1 && BY <= M) { n           if (!wwin(0)) { n             if (bdist[BX][BY] < bturns) { n               bturns = bdist[BX][BY]; n               bloc = i; n             } n           } n         } n         BX -= xc[i]; n         BY -= yc[i]; n       } n     } n     if (is_white) { n       WX += xc[bloc]; n       WY += yc[bloc]; n       cout << WX <<     << WY <<   n  << flush; n     } else { n       BX += xc[bloc]; n       BY += yc[bloc]; n       cout << BX <<     << BY <<   n  << flush; n     } n     if (is_white && WX == N / 2 && WY == M / 2) break; n     if (!is_white && BX == N / 2 + 1 && BY == M / 2) break; n     if (WX == BX && WY == BY) break; n     await(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &x) { n   char c; n   while (c = getchar(), c != EOF) n     if (c >=  0  && c <=  9 ) { n       x = c -  0 ; n       while (c = getchar(), c >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n       ungetc(c, stdin); n       return; n     } n } n int n, a, b; n stack<int> s; n bool dfs(long long now) { n   if (now > b) return 0; n   if (now == b) { n     s.push(b); n     return 1; n   } n   if (dfs(now << 1)) { n     s.push(now); n     return 1; n   } n   if (dfs(now * 10 + 1)) { n     s.push(now); n     return 1; n   } n   return 0; n } n int main() { n   Read(a), Read(b); n   if (dfs(a)) { n     puts( YES ); n     printf( %u n , s.size()); n     while (!s.empty()) { n       printf( %d  , s.top()); n       s.pop(); n     } n   } else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     long long l, r; n     cin >> l >> r; n     cout << l <<     << l * 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, int> ar[200003 + 2]; n long long DP[200003 + 2][2]; n long long n; n long long dp(int i, bool fl) { n   if (i == n + 1) { n     if (fl == 0) return 0; n     return 2e18; n   } n   if (i > n + 1) return 2e18; n   long long &ret = DP[i][fl]; n   if (ret != -1) return ret; n   ret = 2e18; n   if (fl == 0) n     ret = min(ret, -ar[i].first + dp(i + 2, 1)); n   else { n     ret = min(ret, dp(i + 1, 1)); n     ret = min(ret, +ar[i].first + dp(i + 1, 0)); n   } n   return ret; n } n int team[200003 + 2]; n long long res; n int cnt = 0; n void print(int i, bool fl) { n   if (i == n + 1) return; n   if (fl == 0) { n     res += ar[i].first; n     team[ar[i].second] = ++cnt; n     team[ar[i + 1].second] = cnt; n     print(i + 2, 1); n   } else { n     long long kop = dp(i + 1, 1); n     if (kop == res) { n       team[ar[i].second] = cnt; n       print(i + 1, 1); n     } else { n       team[ar[i].second] = cnt; n       res -= ar[i].first; n       print(i + 1, 0); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ar[i].first; n     ar[i].second = i; n   } n   sort(ar + 1, ar + n + 1); n   memset(DP, -1, sizeof DP); n   res = dp(1, 0); n   long long ans = res; n   print(1, 0); n   cout << ans <<     << cnt << endl; n   for (int i = 1; i <= n; i++) { n     if (i > 1) cout <<    ; n     cout << team[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h; n   cin >> n >> h; n   double s = 1.0 * h / n; n   double t = s; n   for (int i = 0; i < n - 1; i++) { n     double ans = sqrt(1.0 * s * h); n     s += t; n     printf( %.12llf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   map<char, int> m; n   for (int i = 0; i < s.size(); i++) { n     m[s[i]]++; n   } n   char a =  A ; n   bool flag1 = true; n   for (int i = 1; i < k; i++) { n     if (m[a] == 0) { n       flag1 = false; n       break; n     } n     a++; n   } n   bool flag = false; n   for (int i = 0; i < s.size(); i++) { n     if (a == s[i]) { n       flag = true; n       break; n     } n   } n   int c = 999999; n   if (flag == true && flag1 == true) { n     for (auto x : m) { n       if (x.second < c) { n         c = x.second; n       } n     } n     cout << c * k; n   } else { n     cout <<  0 ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> inT; n set<int>::iterator it; n int ans[300000]; n vector<int> pak; n int main() { n   int n, m, l, r, x, i; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     inT.insert(i); n   } n   for (i = 0; i < m; i++) { n     cin >> l >> r >> x; n     l--; n     r--; n     x--; n     it = inT.lower_bound(l); n     while (it != inT.end()) { n       if (*it > r) break; n       if (*it != x) { n         pak.push_back(*it); n         ans[*it] = x + 1; n       } n       it++; n     } n     for (x = 0; x < pak.size(); x++) inT.erase(pak[x]); n     pak.clear(); n   } n   for (i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n