#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int vis[N], parent[N]; n vector<int> v[N]; n void dfs(int node) { n   vis[node] = 1; n   for (auto i : v[node]) { n     if (!vis[i]) { n       parent[i] = node; n       dfs(i); n     } n   } n } n int res[N]; n int main() { n   int n, l, r, k = 0; n   scanf( %d   , &n); n   parent[n] = n; n   for (int i = 1; i < n; ++i) { n     parent[i] = i; n     scanf( %d %d , &l, &r); n     v[l].push_back(r); n     v[r].push_back(l); n     k = max(k, max((int)v[l].size(), (int)v[r].size())); n   } n   k += 1; n   vector<int> col(k + 1); n   parent[1] = 1; n   dfs(1); n   memset(vis, 0, sizeof(vis)); n   queue<int> q; n   vis[1] = true; n   q.push(1); n   res[1] = 1; n   col[1] = true; n   while (!q.empty()) { n     int node = q.front(); n     q.pop(); n     col[res[parent[node]]] = true; n     col[res[node]] = true; n     int j = 1, x = (int)col.size(); n     for (auto i : v[node]) { n       if (!vis[i]) { n         q.push(i); n         while (j < x and col[j] == true) ++j; n         res[i] = j; n         col[j] = true; n         vis[i] = 1; n       } n     } n     for (auto i : v[node]) col[res[i]] = false; n     col[res[node]] = false; n   } n   printf( %d n , k); n   for (int i = 1; i <= n; ++i) printf( %d  , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e9 + 7; n int main() { n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int difference; n   int ans = 0; n   for (int i = 0; i < n - 1; i++) { n     if (a[i] + a[i + 1] < k) { n       difference = k - (a[i] + a[i + 1]); n       ans += difference; n       a[i + 1] = a[i + 1] + difference; n     } n   } n   cout << ans; n   cout << endl; n   ; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long int> col; n   int zero = 0, one = 0; n   col.push_back(0); n   for (int i = 1; i < s.size(); i++) { n     if (s[i] >= s[zero]) { n       col.push_back(0); n       zero = i; n     } else { n       if (s[i] < s[one] && one != 0) { n         cout <<  NO ; n         return 0; n       } else { n         col.push_back(1); n         one = i; n       } n     } n   } n   cout <<  YES  <<   n ; n   for (auto i : col) cout << i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, y; n   cin >> x >> y; n   if (y == 0) { n     cout <<  No  << endl; n     return 0; n   } n   y = y - 1; n   if (y >= 1) { n     x = x - y; n     if (x >= 0 && x % 2 == 0) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } else { n     if (x == 0) { n       cout <<  Yes  << endl; n     } else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<1000001> L, R; n vector<int> H1, H2, V1, V2, res; n multiset<int> AA, BB; n int A[1000], B[1000]; n bool solve(int *A, int s, int e, int S) { n   int m = (s + e) >> 1; n   if (s == e || S == 0) { n     if (S) res.push_back(A[m]); n     return true; n   } n   for (int i = 0; i <= S; i++) L[i] = R[i] = 0; n   L[0] = R[0] = 1; n   for (int i = s; i <= m; i++) L |= L << A[i]; n   for (int i = e; i > m; i--) R |= R << A[i]; n   for (int i = 0; i <= S; i++) n     if (L[i] && R[S - i]) { n       solve(A, s, m, i); n       solve(A, m + 1, e, S - i); n       return true; n     } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ((void)0); n   ((void)0); n   ((void)0); n   int T, N, M; n   for (cin >> T; T--;) { n     int H = 0, V = 0, x = 0, y = 0; n     cin >> N; n     AA.clear(); n     BB.clear(); n     for (int i = 0; i < N; i++) { n       cin >> A[i]; n       H += A[i]; n       AA.insert(A[i]); n     } n     cin >> M; n     for (int i = 0; i < M; i++) { n       cin >> B[i]; n       V += B[i]; n       BB.insert(B[i]); n     } n     if ((H & 1) || (V & 1) || N != M || N == 1 || M == 1) { n       cout <<  No n ; n       continue; n     } n     sort(A, A + N); n     sort(B, B + M); n     res.clear(); n     if (!solve(A, 0, N - 1, H / 2)) { n       cout <<  No n ; n       continue; n     } n     H1.clear(); n     H2.clear(); n     for (auto v : res) { n       H1.push_back(v); n       AA.erase(AA.find(v)); n     } n     for (auto v : AA) H2.push_back(v); n     res.clear(); n     if (!solve(B, 0, M - 1, V / 2)) { n       cout <<  No n ; n       continue; n     } n     V1.clear(); n     V2.clear(); n     for (auto v : res) { n       V1.push_back(v); n       BB.erase(BB.find(v)); n     } n     for (auto v : BB) V2.push_back(v); n     if (V1.size() > V2.size()) swap(V1, V2); n     if (H1.size() < H2.size()) swap(H1, H2); n     reverse(V1.begin(), V1.end()); n     reverse(V2.begin(), V2.end()); n     cout <<  Yes n ; n     for (auto &v : V1) v *= -1; n     for (auto &h : H2) H1.push_back(-h); n     for (auto v : V2) V1.push_back(v); n     for (int i = 0; i < N; i++) { n       y += V1[i]; n       cout << x <<     << y <<   n ; n       x += H1[i]; n       cout << x <<     << y <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char board[2100][2100]; n int mark[2100][2100]; n class Graph { n   int V; n   list<int> *adj; n   bool isCyclicUtil(int v, bool visited[], bool *rs); n  n  public: n   Graph(int V); n   void addEdge(int v, int w); n   bool isCyclic(); n }; n Graph::Graph(int V) { n   this->V = V; n   adj = new list<int>[V]; n } n void Graph::addEdge(int v, int w) { adj[v].push_back(w); } n bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack) { n   if (visited[v] == false) { n     visited[v] = true; n     recStack[v] = true; n     list<int>::iterator i; n     for (i = adj[v].begin(); i != adj[v].end(); ++i) { n       if (!visited[*i] && isCyclicUtil(*i, visited, recStack)) n         return true; n       else if (recStack[*i]) n         return true; n     } n   } n   recStack[v] = false; n   return false; n } n bool Graph::isCyclic() { n   bool *visited = new bool[V]; n   bool *recStack = new bool[V]; n   for (int i = 0; i < V; i++) { n     visited[i] = false; n     recStack[i] = false; n   } n   for (int i = 0; i < V; i++) n     if (isCyclicUtil(i, visited, recStack)) return true; n   return false; n } n int move(int i, int j) { n   int i0 = i, j0 = j; n   if (board[i][j] ==  # ) return 0; n   int di = 0, dj = 0; n   if (mark[i][j] != 0) { n     return mark[i][j]; n   } n   di = 0; n   dj = 0; n   if (board[i][j] ==  v ) di = 1; n   if (board[i][j] ==  ^ ) di = -1; n   if (board[i][j] ==  > ) dj = 1; n   if (board[i][j] ==  < ) dj = -1; n   i += di; n   j += dj; n   if (mark[i][j] != 0) { n     mark[i0][j0] = mark[i][j] + 1; n     return mark[i][j] + 1; n   } else { n     int d = move(i, j); n     mark[i0][j0] = d + 1; n     return d + 1; n   } n } n bool check(int i1, int j1, int i2, int j2) { n   if (board[i1][j1] ==  #  || board[i1][j2] ==  # ) return false; n   int di1 = 0, dj1 = 0, di2 = 0, dj2 = 0; n   while (board[i1][j1] !=  #  || board[i2][j2] !=  # ) { n     di1 = 0; n     dj1 = 0, di2 = 0, dj2 = 0; n     if (board[i1][j1] ==  v ) di1 = 1; n     if (board[i1][j1] ==  ^ ) di1 = -1; n     if (board[i1][j1] ==  > ) dj1 = 1; n     if (board[i1][j1] ==  < ) dj1 = -1; n     i1 += di1; n     j1 += dj1; n     if (board[i2][j2] ==  v ) di2 = 1; n     if (board[i2][j2] ==  ^ ) di2 = -1; n     if (board[i2][j2] ==  > ) dj2 = 1; n     if (board[i2][j2] ==  < ) dj2 = -1; n     i2 += di2; n     j2 += dj2; n     if (i1 == i2 && j1 == j2) n       if (board[i1][j1] !=  # ) return false; n   } n   return true; n } n Graph g(2000 * 2000); n int main() { n   int n, m, max = 0, val = 0, maxi[2000], maxj[2000], num = 0, cnt = 0; n   bool f = false; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> board[i][j]; n       if (board[i][j] ==  v  && cnt < 10000) { n         g.addEdge(m * i + j, m * (i + 1) + j); n         cnt++; n       } n       if (board[i][j] ==  ^  && cnt < 10000) { n         g.addEdge(m * i + j, m * (i - 1) + j); n         cnt++; n       } n       if (board[i][j] ==  >  && cnt < 10000) { n         g.addEdge(m * i + j, m * i + j + 1); n         cnt++; n       } n       if (board[i][j] ==  <  && cnt < 10000) { n         g.addEdge(m * i + j, m * i + j - 1); n         cnt++; n       } n     } n   } n   if (g.isCyclic()) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       val = move(i, j); n       if (val == max && !f && max != 0) { n         maxj[num] = j; n         maxi[num] = i; n         num++; n       } n       if (val > max) { n         max = val; n         maxi[0] = i; n         maxj[0] = j; n         num = 1; n         f = false; n       } n     } n   } n   if (max == 0) { n     cout << 0; n     return 0; n   } n   if (num > 1) f = check(maxi[0], maxj[0], maxi[1], maxj[1]); n   if (num > 2) { n     for (int i = 2; i < num; i++) { n       if (!f) { n         f = check(maxi[1], maxj[1], maxi[i], maxj[i]); n       } n     } n   } n   if (f) n     cout << max * 2; n   else n     cout << max * 2 - 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x; n   long long k, count = 0; n   char c; n   cin >> n >> x; n   for (int i = 1; i <= n; i++) { n     cin >> c >> k; n     if (c ==  + ) { n       x += k; n     } else { n       if (x >= k) { n         x -= k; n       } else { n         count++; n       } n     } n   } n   cout << x <<     << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long int temp, i, j, k, T; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int h[2007]; n   for (i = 0; i < n; i++) cin >> h[i]; n   sort(h, h + n); n   int val = h[b - 1]; n   if (h[b] == val) n     cout <<  0  << endl; n   else n     cout << h[b] - val << endl; n   return 0; n } n


#include <bits/stdc++.h> n void unionInit(int d[], int s) { n   int i; n   for (i = 0; i < s; i++) d[i] = i; n } n int unionGet(int d[], int n) { n   int t = n, k; n   while (d[t] != t) t = d[t]; n   while (d[n] != n) k = d[n], d[n] = t, n = k; n   return n; n } n int unionConnect(int d[], int a, int b) { n   a = unionGet(d, a); n   b = unionGet(d, b); n   if (a == b) return 0; n   d[a] = b; n   return 1; n } n int main() { n   int i, j, k, l, m, n; n   int ind[110000]; n   int res; n   while (scanf( %d%d , &n, &m) == 2) { n     unionInit(ind, n); n     res = 1; n     while (m--) { n       scanf( %d%d , &i, &j); n       i--; n       j--; n       if (unionConnect(ind, i, j) == 0) res = (res * 2) % 1000000009; n       printf( %d n , (res + 1000000009 - 1) % 1000000009); n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n struct ANS { n   int xa, ya, xb, yb, xc, yc; n }; n struct PRE { n   int p1, p2, s; n } pre[MAXN][2][2]; n int T, n, m; n int a[MAXN][MAXN]; n int f[MAXN][2][2]; n char s[MAXN]; n vector<ANS> ans; n int popcount(int v) { return v ? popcount(v - (v & -v)) + 1 : 0; } n void operate(int x, int y, int nx, int ny, bool flag = 0) { n   if (flag) ans.push_back((ANS){0, 0, 0, 0, 0, 0}); n   for (int i = 0; i <= 1; i++) n     for (int j = 0; j <= 1; j++) n       if (x + i != nx || j + y != ny) { n         if (flag) { n           if (ans.back().xa) { n             if (ans.back().xb) { n               ans.back().xc = x + i; n               ans.back().yc = y + j; n             } else { n               ans.back().xb = x + i; n               ans.back().yb = y + j; n             } n           } else { n             ans.back().xa = x + i; n             ans.back().ya = y + j; n           } n         } n         a[x + i][y + j] ^= 1; n       } n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; i++) { n       scanf( %s , s + 1); n       for (int j = 1; j <= m; j++) a[i][j] = s[j] -  0 ; n     } n     ans.clear(); n     for (int i = 1; i <= n - 2; i++) n       for (int j = 1; j <= m; j++) n         if (a[i][j]) { n           if (j < m) n             operate(i, j, i, j + 1, 1); n           else n             operate(i, j - 1, i, j - 1, 1); n         } n     for (int i = 1; i <= m; i++) n       for (int j = 0; j <= 1; j++) n         for (int k = 0; k <= 1; k++) f[i][j][k] = 1e9; n     f[1][a[n - 1][1]][a[n][1]] = 0; n     for (int i = 1; i < m; i++) n       for (int j = 0; j <= 1; j++) n         for (int k = 0; k <= 1; k++) n           if (f[i][j][k] != 1e9) { n             int t1 = a[n - 1][i], t2 = a[n][i]; n             a[n - 1][i] = j; n             a[n][i] = k; n             for (int l = 0; l < 16; l++) { n               if (l & 1) operate(n - 1, i, n - 1, i); n               if (l >> 1 & 1) operate(n - 1, i, n, i); n               if (l >> 2 & 1) operate(n - 1, i, n - 1, i + 1); n               if (l >> 3 & 1) operate(n - 1, i, n, i + 1); n               if (!a[n - 1][i] && !a[n][i] && n                   f[i + 1][a[n - 1][i + 1]][a[n][i + 1]] > n                       f[i][j][k] + popcount(l)) { n                 f[i + 1][a[n - 1][i + 1]][a[n][i + 1]] = n                     f[i][j][k] + popcount(l); n                 pre[i + 1][a[n - 1][i + 1]][a[n][i + 1]] = (PRE){j, k, l}; n               } n               if (l & 1) operate(n - 1, i, n - 1, i); n               if (l >> 1 & 1) operate(n - 1, i, n, i); n               if (l >> 2 & 1) operate(n - 1, i, n - 1, i + 1); n               if (l >> 3 & 1) operate(n - 1, i, n, i + 1); n             } n             a[n - 1][i] = t1; n             a[n][i] = t2; n           } n     int t1 = 0, t2 = 0; n     for (int i = m; i > 1; i--) { n       int l = pre[i][t1][t2].s; n       if (l & 1) operate(n - 1, i - 1, n - 1, i - 1, 1); n       if (l >> 1 & 1) operate(n - 1, i - 1, n, i - 1, 1); n       if (l >> 2 & 1) operate(n - 1, i - 1, n - 1, i, 1); n       if (l >> 3 & 1) operate(n - 1, i - 1, n, i, 1); n       int n1 = pre[i][t1][t2].p1, n2 = pre[i][t1][t2].p2; n       t1 = n1; n       t2 = n2; n     } n     printf( %d n , (int)ans.size()); n     for (int i = 0; i < ans.size(); i++) n       printf( %d %d %d %d %d %d n , ans[i].xa, ans[i].ya, ans[i].xb, ans[i].yb, n              ans[i].xc, ans[i].yc); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int man = 2e5 + 10; n const long long mod = 1e9 + 7; n struct edge { n   int v; n   int w; n   int next; n } e[man << 1]; n int head[man]; n int cnt; n void init(int n) { n   memset(head, 0, sizeof(int) * (n + 5)); n   cnt = 0; n } n void add(int u, int v, int w) { n   e[++cnt].next = head[u]; n   e[cnt].v = v; n   e[cnt].w = w; n   head[u] = cnt; n } n int vis[man]; n int dfs(int u, int fa) { n   int num = 0; n   int cnt = 0; n   for (int i = head[u]; i; i = e[i].next) { n     if (e[i].v == fa) continue; n     ++cnt; n     int tp = dfs(e[i].v, u); n     num += tp; n     vis[i] = tp; n   } n   if (!cnt) num++; n   return num; n } n struct node { n   long long w; n   int cnt, va; n   bool operator<(const node &a) const { n     return 1ll * (va / 2 + va % 2) * cnt < 1ll * (a.va / 2 + a.va % 2) * a.cnt; n   } n }; n signed main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     long long s = 0; n     long long sum = 0; n     scanf( %d%lld , &n, &s); n     for (int i = 1; i < n; ++i) { n       int u, v, w; n       scanf( %d%d%d , &u, &v, &w); n       add(u, v, w); n       add(v, u, w); n     } n     dfs(1, 1); n     priority_queue<node> q; n     for (int i = 1; i <= cnt; ++i) { n       if (vis[i] == 0) continue; n       sum += 1ll * e[i].w * vis[i]; n       q.push(node{1ll * e[i].w * vis[i], vis[i], e[i].w}); n     } n     if (sum <= s) { n       cout << 0 <<   n ; n     } else { n       int cnt = 0; n       while (q.size()) { n         node tp = q.top(); n         q.pop(); n         ++cnt; n         int tpva = tp.va / 2 + tp.va % 2; n         sum -= 1ll * tpva * tp.cnt; n         tp.va -= tpva; n         tp.w = 1ll * tp.cnt * tp.va; n         if (tp.va) q.push(tp); n         if (sum <= s) break; n       } n       cout << cnt <<   n ; n     } n     memset(vis, 0, sizeof(int) * (cnt + 5)); n     init(n); n   } n   return 0; n } n


#include <queue> n #include <cstdio> n #include <algorithm> n const int Maxn=200; n const int Maxl=40; n const int Mod=998244353; n int n,m; n namespace Trie{ n  struct Trie_Node{ n   int ch[26]; n   bool pos; n  }node[Maxl+5]; n  int id_tot; n  void insert(char *s,int len){ n   int root=0; n   for(int i=1;i<=len;i++){ n    if(node[root].ch[s[i]- a ]==0){ n     node[root].ch[s[i]- a ]=++id_tot; n    } n    root=node[root].ch[s[i]- a ]; n   } n   node[root].pos=1; n  } n } n struct Node{ n  int id,x,y; n  Node(int _id=0,int _x=0,int _y=0){ n   id=_id,x=_x,y=_y; n  } n }; n std::queue<Node> q; n int id[Maxl+5][Maxl+5],id_tot; n int get_id(int x,int y){ n  if(x>y){ n   std::swap(x,y); n  } n  if(id[x][y]==0){ n   id[x][y]=++id_tot; n   q.push(Node(id[x][y],x,y)); n  } n  return id[x][y]; n } n struct Matrix{ n  int a[Maxn+5][Maxn+5]; n  void init(){ n   for(int i=1;i<=id_tot;i++){ n    for(int j=1;j<=id_tot;j++){ n     a[i][j]=0; n    } n   } n  } n  friend Matrix operator *(Matrix a,Matrix b){ n   Matrix ans; n   ans.init(); n   for(int i=1;i<=id_tot;i++){ n    for(int k=1;k<=id_tot;k++){ n     for(int j=1;j<=id_tot;j++){ n      ans.a[i][j]=(ans.a[i][j]+1ll*a.a[i][k]*b.a[k][j])%Mod; n     } n    } n   } n   return ans; n  } n }g; n int main(){ n  scanf( %d%d ,&n,&m); n  for(int i=1;i<=n;i++){ n   static char s[Maxl+5]; n   scanf( %s ,s+1); n   int len=0; n   while(s[++len]!=  0 ); n   len--; n   Trie::insert(s,len); n  } n  get_id(0,0); n  while(!q.empty()){ n   Node u=q.front(); n   q.pop(); n   for(int i=0;i<26;i++){ n    int a=Trie::node[u.x].ch[i],b=Trie::node[u.y].ch[i]; n    if(a==0||b==0){ n     continue; n    } n    g.a[u.id][get_id(a,b)]++; n    if(Trie::node[a].pos){ n     g.a[u.id][get_id(0,b)]++; n    } n    if(Trie::node[b].pos){ n     g.a[u.id][get_id(a,0)]++; n    } n    if(Trie::node[a].pos&&Trie::node[b].pos){ n     g.a[u.id][get_id(0,0)]++; n    } n   } n  } n  Matrix ans; n  ans.init(); n  for(int i=1;i<=id_tot;i++){ n   ans.a[i][i]=1; n  } n  while(m){ n   if(m&1){ n    ans=ans*g; n   } n   g=g*g; n   m>>=1; n  } n  printf( %d n ,ans.a[1][1]); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, m, x, y, ans; n bool b; n int main() { n   scanf( %d%d , &n, &k); n   ans = n * 2 - k - 1; n   for (int i = 1; i <= k; i++) { n     scanf( %d , &m); n     b = 0; n     for (int j = 1; j <= m; j++) { n       scanf( %d , &x); n       if (j == 1 && x == 1) b = 1; n       if ((b) && j > 1 && x > y + 1) { n         ans -= (j - 2) * 2; n         b = 0; n       } n       if ((b) && j == m) ans -= (m - 1) * 2; n       y = x; n     } n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = -1; n const int INF = 1.01e9; n const int MOD = 1e9 + 7; n long long rev(long long a, long long m) { n   if (a == 0) return 0; n   return ((1 - rev(m % a, a) * m) / a + m) % m; n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   vector<long long> fact(n + 1); n   fact[0] = 1; n   for (int i = 1; i <= n; i++) { n     fact[i] = (fact[i - 1] * i) % MOD; n   } n   vector<long long> rfact(n + 1); n   rfact[n] = rev(fact[n], MOD); n   assert(fact[n] * rfact[n] % MOD == 1); n   for (int i = n - 1; i >= 0; i--) { n     rfact[i] = (rfact[i + 1] * (i + 1)) % MOD; n   } n   auto getC = [&](int nn, int kk) { n     assert(0 <= kk && kk <= nn); n     return fact[nn] * rfact[kk] % MOD * rfact[nn - kk] % MOD; n   }; n   vector<long long> f(n + 1); n   vector<long long> g(n + 1); n   vector<long long> prefG(n + 1); n   f[1] = 1; n   g[1] = 1; n   prefG[1] = 1; n   for (int i = 2; i <= n; i++) { n     long long tmp = prefG[i - 1]; n     if (i - k - 1 >= 1) { n       tmp = (tmp - prefG[i - k - 1] + MOD) % MOD; n     } n     tmp = (tmp * fact[i - 2]) % MOD; n     f[i] = tmp; n     g[i] = f[i] * rfact[i - 1] % MOD; n     prefG[i] = (prefG[i - 1] + g[i]) % MOD; n   } n   long long answer = 0; n   for (int i = 1; i <= n; i++) { n     answer = (answer + getC(n - 1, i - 1) * f[i] % MOD * fact[n - i]) % MOD; n   } n   cout << (fact[n] - answer + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int paths[5001][5001]; n int ct[5001], top[5001], freq[5001]; n int main() { n   for (int i = 2; i <= 5000; i++) { n     for (int j = 2; j <= 5000; j++) { n       paths[i][j] = paths[i - 1][j]; n     } n     int tmp = i; n     for (int j = 2; j <= sqrt(tmp); j++) { n       while (tmp > 0 && tmp % j == 0) { n         tmp /= j; n         paths[i][j]++; n       } n     } n     if (tmp > 1) paths[i][tmp]++; n   } n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     ct[k]++; n   } n   long long sol = 0; n   fill(top, top + 5001, 1); n   for (int i = 1; i <= 5000; i++) { n     if (!ct[i]) continue; n     for (int j = 1; j <= 5000; j++) { n       sol += 1LL * paths[i][j] * ct[i]; n       if (paths[i][j]) top[i] = j; n     } n   } n   while (true) { n     memset(freq, 0, sizeof freq); n     for (int i = 1; i <= 5000; i++) { n       if (!ct[i]) continue; n       freq[top[i]] += ct[i]; n     } n     int most = max_element(freq, freq + 5001) - freq; n     if (freq[most] * 2 <= n || most == 1) break; n     sol -= freq[most]; n     sol += n - freq[most]; n     for (int i = 1; i <= 5000; i++) { n       if (top[i] != most) { n         top[i] = 1; n       } n       if (top[i] == 1) continue; n       paths[i][top[i]]--; n       while (top[i] > 1 && paths[i][top[i]] == 0) { n         top[i]--; n       } n     } n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   long long int k, b, n, t; n   while (cin >> k >> b >> n >> t) { n     long long int s = 1; n     long long int cas = 0; n     while (s <= t && cas < n) { n       s = s * k + b; n       cas++; n     } n     if (cas == n && s <= t) n       cout << 0 << endl; n     else n       cout << n - cas + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100005; n int nbs[maxn], ev[maxn * 2], next[maxn * 2], color[maxn], d[maxn], ans[maxn], n     size[2], view[maxn], n, m, k; n void addedge(int x, int y) { n   next[++k] = nbs[x]; n   ev[k] = y; n   nbs[x] = k; n } n void init() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     addedge(x, y); n     addedge(y, x); n     ++d[x]; n     ++d[y]; n   } n } n void dfs(int x, int col) { n   color[x] = col; n   ++size[col]; n   for (int i = nbs[x]; i; i = next[i]) { n     if (color[ev[i]] == -1) dfs(ev[i], col ^ 1); n   } n } n void output(int x) { n   int y = 0; n   for (int i = 1; i <= n; ++i) { n     if (!ans[i] && color[i] == 0) { n       ans[i] = x; n       ++y; n       if (y % 3 == 0) ++x; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (!ans[i] && color[i] == 1) { n       ans[i] = x; n       ++y; n       if (y % 3 == 0) ++x; n     } n   } n   printf( YES n ); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   printf(  n ); n } n void solve(int x, int y = 1) { n   memset(view, 0, sizeof(view)); n   ans[x] = y; n   for (int i = nbs[x]; i; i = next[i]) view[ev[i]] = 1; n   int k = 0; n   for (int i = 1; i <= n && k < 2; ++i) { n     if (!ans[i] && color[i] != color[x] && !view[i]) { n       ans[i] = y; n       ++k; n     } n   } n } n void work() { n   memset(color, -1, sizeof(color)); n   for (int i = 1; i <= n; ++i) { n     if (color[i] == -1) dfs(i, 0); n   } n   if (size[0] % 3 == 0) n     output(1); n   else { n     int col = size[0] % 3 - 1, find = 0; n     for (int i = 1; i <= n && !find; ++i) { n       if (color[i] == col && d[i] + 2 <= size[col ^ 1]) { n         solve(i); n         find = 1; n       } n     } n     if (find) n       output(find + 1); n     else { n       col ^= 1; n       int t = 0; n       for (int i = 1; i <= n && find < 2; ++i) { n         if (color[i] == col && d[i] + 2 <= size[col ^ 1]) { n           solve(i, find + 1); n           ++find; n         } n       } n       if (find == 2) n         output(find + 1); n       else n         printf( NO n ); n     } n   } n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> s(n), r(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i] >> r[i]; n   } n   vector<tuple<int, int, int>> T(n); n   for (int i = 0; i < n; i++) { n     T[i] = make_tuple(s[i], r[i], i); n   } n   sort(T.begin(), T.end(), greater<tuple<int, int, int>>()); n   vector<int> id(n); n   for (int i = 0; i < n; i++) { n     s[i] = get<0>(T[i]); n     r[i] = get<1>(T[i]); n     id[i] = get<2>(T[i]); n   } n   int mx = -1, ms = -1; n   vector<int> s2, r2, id2; n   vector<vector<int>> p; n   int n2 = 0; n   for (int i = 0; i < n; i++) { n     if (r[i] > mx) { n       mx = r[i]; n       ms = s[i]; n       s2.push_back(s[i]); n       r2.push_back(r[i]); n       id2.push_back(id[i]); n       p.push_back(vector<int>(0)); n       n2++; n     } else if (r[i] == mx && s[i] == ms) { n       p.back().push_back(id[i]); n     } n   } n   stack<int> st; n   st.push(0); n   if (n2 > 1) { n     st.push(1); n   } n   for (int i = 2; i < n2; i++) { n     while (st.size() >= 2) { n       int b = st.top(); n       st.pop(); n       int a = st.top(); n       st.pop(); n       long long A = n           (long long)s2[i] * r2[a] * (r2[b] - r2[i]) * (s2[b] - s2[a]); n       long long B = n           (long long)s2[a] * r2[i] * (r2[a] - r2[b]) * (s2[i] - s2[b]); n       if (A <= B) { n         st.push(a); n         st.push(b); n         break; n       } else { n         st.push(a); n       } n     } n     st.push(i); n   } n   vector<int> ans; n   while (!st.empty()) { n     int x = st.top(); n     st.pop(); n     ans.push_back(id2[x]); n     for (int y : p[x]) { n       ans.push_back(y); n     } n   } n   sort(ans.begin(), ans.end()); n   int cnt = ans.size(); n   for (int i = 0; i < cnt; i++) { n     cout << ans[i] + 1; n     if (i < cnt - 1) { n       cout <<    ; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n  n #define MULTI_TEST false n #define int long long n #define double long double n  n //#pragma GCC optimize( Ofast ) n //#pragma GCC optimize( no-stack-protector ) n //#pragma GCC optimize( unroll-loops ) n //#pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n  n #define all(x) x.begin(), x.end() n  n using namespace std; n  n const int INF = 1e18; n  n int n, k; n vector<vector<int>> g; n vector<int> p; n vector<int> chainp, chainc; n vector<vector<int>> chains; n vector<vector<int>> g1; n vector<int> tin, tout; n vector<int> group; n vector<int> used, used1; n vector<int> ts; n  n int timer = 1; n  n int bad = 0; n  n void dfs(int v) { n     used[v] = 1; n     int x = chainc[v]; n     if (used[x] == 1) { n         bad = 1; n         return; n     } n     if (used[x] == 0) { n         dfs(x); n     } n     used[v] = 2; n } n  n void dfs1(int v) { n     used1[v] = 1; n     for (auto &x: g1[v]) { n         if (used1[x] == 1) { n             bad = 1; n             return; n         } n         if (used1[x] == 0) { n             dfs1(x); n         } n     } n     used1[v] = 2; n     ts.push_back(v); n } n  n void solve() { n     cin >> n >> k; n     p.resize(n); n     g.resize(n); n     tin.resize(n, -1); n     tout.resize(n, -1); n     chainp.resize(n, -1); n     chainc.resize(n, -1); n     int q = 0; n     for (int i = 0; i < n; i++) { n         int x; n         cin >> x; n         x--; n         p[i] = x; n     } n     for (int i = 0; i < k; i++) { n         int x, y; n         cin >> x >> y; n         x--; n         y--; n         chainp[y] = x; n         chainc[x] = y; n     } n     used.resize(n, 0); n     used1.resize(n, 0); n     group.resize(n, -1); n     for (int i = 0; i < n; i++) { n         if (used[i] == 0) n             dfs(i); n     } n     if (bad == 1) { n         cout << 0; n         return; n     } n     for (int i = 0; i < n; i++) { n         if (chainp[i] == -1) { n             chains.emplace_back(); n             int v = i; n             group[v] = chains.size() - 1; n             chains.back().push_back(v); n             while (chainc[v] != -1) { n                 v = chainc[v]; n                 group[v] = chains.size() - 1; n                 chains.back().push_back(v); n             } n         } n     } n     g1.resize(chains.size()); n     for (int i = 0; i < n; i++) { n         if (p[i] != -1) { n             int x = p[i]; n             if (group[x] != group[i]) { n                 g1[group[x]].push_back(group[i]); n             } n         } n     } n     for (int i = 0; i < g1.size(); i++) { n         if (used1[i] == 0) { n             dfs1(i); n         } n     } n     reverse(all(ts)); n     vector<int> fin; n     if (bad == 1) { n         cout << 0; n         return; n     } n     for (auto &x: ts) { n         for (auto &y: chains[x]) { n             fin.push_back(y); n         } n     } n     map<int, int> topos; n     for (int i = 0; i < n; i++) { n         topos[fin[i]] = i; n     } n     for (int i = 0; i < n; i++) { n         if (p[i] != -1) { n             int x = i; n             int y = p[i]; n             if (topos[y] > topos[x]) n                 bad = 1; n         } n     } n     if (bad == 1) { n         cout << 0; n         return; n     } n     for (auto &x: fin) { n         cout << x + 1 <<    ; n     } n } n  n signed main() { n     cin.tie(0); n     ios::sync_with_stdio(false); n  n #ifdef DEBUG n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n #endif n     int t = 1; n     if (MULTI_TEST) n         cin >> t; n     while (t--) n         solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, j; n   char c =  W ; n   cin >> t; n   for (i = 0; i < t; i++) { n     for (j = 0; j < t; j++) { n       cout << c; n       if (c ==  B ) n         c =  W ; n       else n         c =  B ; n     } n     if (t % 2 == 0) { n       if (c ==  B ) n         c =  W ; n       else n         c =  B ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000 * 1000 * 1000 + 7; n inline long long mod(long long n) { n   if (n < 0) { n     return (n % MOD + MOD) % MOD; n   } else { n     return n % MOD; n   } n } n long long fp(long long a, long long p) { n   long long ans = 1, cur = a; n   for (long long i = 0; (1ll << i) <= p; ++i) { n     if ((p >> i) & 1) ans = mod(ans * cur); n     cur = mod(cur * cur); n   } n   return ans; n } n long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); } n const long long N = 103; n long long m[N][N], ans[N][N], t[N][N]; n void add(long long a[N][N], long long b[N][N]) { n   for (long long i = 0; i < N; ++i) { n     for (long long j = 0; j < N; ++j) { n       t[i][j] = 0; n     } n   } n   for (long long i = 0; i < N; ++i) { n     for (long long j = 0; j < N; ++j) { n       for (long long k = 0; k < N; ++k) { n         t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]); n       } n     } n   } n   for (long long i = 0; i < N; ++i) { n     for (long long j = 0; j < N; ++j) { n       a[i][j] = t[i][j]; n     } n   } n } n void pw(long long p) { n   for (long long i = 0; i < N; ++i) ans[i][i] = 1; n   for (long long i = 0; (1ll << i) <= p; ++i) { n     if ((p >> i) & 1) add(ans, m); n     add(m, m); n   } n } n bool a[N]; n long long cnt[2]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n     ++cnt[a[i]]; n   } n   long long l = cnt[0]; n   long long r = n - l; n   long long op = n * (n - 1) / 2; n   for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) { n     long long l0 = l - l1; n     long long r0 = cnt[0] - l0; n     long long r1 = cnt[1] - l1; n     m[l1][l1] = op; n     if (l1) { n       m[l1][l1 - 1] = mod(l1 * r0); n       m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]); n     } n     m[l1][l1 + 1] = mod(l0 * r1); n     m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]); n   } n   pw(k); n   long long sum = 0; n   for (long long i = 0; i < l; ++i) sum += a[i]; n   cout << dv(ans[sum][0], fp(op, k)) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long double EPS = 1e11; n const long long INFL = 1e16; n const int INF = 1e9 + 7; n mt19937 rnd((unsigned)chrono::steady_clock::now().time_since_epoch().count()); n int highdig(long long x) { n   int ans = 0; n   while (x > 0LL) { n     x /= 2LL; n     ++ans; n   } n   return ans; n } n long long umen(long long x, vector<long long>& ea, vector<long long>& eb, n                vector<char>& ec) { n   long long cx = x; n   long long highdigx = 1; n   while (highdigx * 2LL <= x) { n     highdigx *= 2LL; n   } n   while (cx % highdigx != 0LL) { n     ea.push_back(cx); n     eb.push_back(cx); n     ec.push_back( + ); n     cx *= 2LL; n   } n   ea.push_back(x); n   ea.push_back(x); n   eb.push_back(cx); n   eb.push_back(cx); n   ec.push_back( + ); n   ec.push_back( ^ ); n   long long y2 = cx + x; n   long long y3 = (cx ^ x); n   ea.push_back(y2); n   eb.push_back(y3); n   ec.push_back( ^ ); n   cx = (y2 ^ y3); n   while (highdig(cx) > highdig(x)) { n     long long ccx = x; n     while (highdig(ccx) < highdig(cx)) { n       ea.push_back(ccx); n       eb.push_back(ccx); n       ec.push_back( + ); n       ccx *= 2LL; n     } n     ea.push_back(cx); n     eb.push_back(ccx); n     ec.push_back( ^ ); n     cx ^= ccx; n   } n   while (highdig(cx) < highdig(x)) { n     ea.push_back(cx); n     eb.push_back(cx); n     ec.push_back( + ); n     cx *= 2LL; n   } n   ea.push_back(x); n   eb.push_back(cx); n   ec.push_back( ^ ); n   while ((x ^ cx) % 2 == 0) { n     cx = cx; n   } n   return (x ^ cx); n } n long long trie(long long x, vector<long long>& ea, vector<long long>& eb, n                vector<char>& ec) { n   ea.push_back(x); n   eb.push_back(x); n   ec.push_back( + ); n   long long y1 = x * 2LL; n   ea.push_back(x); n   eb.push_back(y1); n   ec.push_back( + ); n   long long y2 = x + y1; n   ea.push_back(y1); n   eb.push_back(y1); n   ec.push_back( + ); n   ea.push_back(y2); n   eb.push_back(y1 * 2LL); n   ec.push_back( ^ ); n   long long y3 = (y2 ^ (2 * y1)); n   ea.push_back(y2); n   eb.push_back(y3); n   ec.push_back( + ); n   long long y4 = y2 + y3; n   ea.push_back(2 * y1); n   eb.push_back(y4); n   ec.push_back( ^ ); n   for (long long cd = 2LL; x > 1LL; cd *= 2LL) { n     ea.push_back(x); n     eb.push_back(cd); n     ec.push_back( ^ ); n     ea.push_back(cd); n     eb.push_back(cd); n     ec.push_back( + ); n     x ^= cd; n   } n   return 1LL; n } n void solve() { n   long long x; n   cin >> x; n   vector<long long> ea, eb; n   vector<char> ec; n   while (x > 1LL) { n     if ((x & (x + 1LL)) > 0LL) { n       x = umen(x, ea, eb, ec); n     } else { n       x = trie(x, ea, eb, ec); n     } n   } n   cout << ea.size() <<   n ; n   for (int i = 0; i < ea.size(); ++i) { n     cout << ea[i] <<     << ec[i] <<     << eb[i] <<   n ; n   } n } n int main() { n   int t = 1; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i; n   cin >> n; n   i = 1; n   while (n % i == 0) { n     i = i * 3; n   } n   cout << n / i + 1; n } n


#include <bits/stdc++.h> n int temp[500000], a[500000], indices[500000], increments[500000], n     previous_appearance[500000], numbers_equal_c[500000]; n void mergesort(int Array[], int indices[], int first, int last) { n   if (first == last) return; n   int middle = (first + last) / 2; n   mergesort(Array, indices, first, middle); n   mergesort(Array, indices, middle + 1, last); n   int i, j, k; n   for (i = first, j = first, k = middle + 1; i <= last; i++) { n     if (j == middle + 1) n       temp[i] = indices[k++]; n     else if (k == last + 1) n       temp[i] = indices[j++]; n     else if (Array[indices[k]] < Array[indices[j]]) n       temp[i] = indices[k++]; n     else n       temp[i] = indices[j++]; n   } n   for (i = first; i <= last; i++) { n     indices[i] = temp[i]; n   } n } n int main() { n   int n, i, one_counter = 0, c, maximum_increament = 0, number_equal_to_c = 0; n   scanf( %d %d , &n, &c); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     indices[i] = i; n     previous_appearance[i] = -1; n     numbers_equal_c[i] = 0; n     increments[i] = 0; n   } n   mergesort(a, indices, 0, n - 1); n   for (i = 1; i < n; i++) { n     if (a[indices[i]] == a[indices[i - 1]]) { n       previous_appearance[indices[i]] = indices[i - 1]; n     } n   } n   for (i = 0; i < n; i++) { n     if (a[i] == c) { n       number_equal_to_c++; n     } else { n       if (previous_appearance[i] == -1 || n           increments[previous_appearance[i]] - number_equal_to_c + n                   numbers_equal_c[previous_appearance[i]] + 1 <= n               0) n         increments[i] = 1; n       else n         increments[i] = increments[previous_appearance[i]] - number_equal_to_c + n                         numbers_equal_c[previous_appearance[i]] + 1; n       if (maximum_increament < increments[i]) n         maximum_increament = increments[i]; n     } n     numbers_equal_c[i] = number_equal_to_c; n   } n   printf( %d , number_equal_to_c + maximum_increament); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     int ans = n; n     for (int i = 1; i * i <= n; i++) n       if (n % i == 0) { n         if (i <= k) ans = min(ans, n / i); n         if (n / i <= k) ans = min(ans, i); n       } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, s, x; n   scanf( %d%d%d , &t, &s, &x); n   if (x < t) { n     puts( NO ); n     return 0; n   } n   int temp = (x - t) % s; n   if (temp == 0 || temp == 1) { n     if (x == t + 1) n       puts( NO ); n     else n       puts( YES ); n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long int> s; n int main() { n   long long int n, m, i, ans = 0; n   cin >> n >> m; n   long long int a[n], c[200005] = {0}; n   for (i = 0; i < m; i++) s.insert(i); n   for (i = 0; i < n; i++) { n     long long int d, x; n     cin >> a[i]; n     d = a[i] % m; n     if (d > *s.rbegin()) n       x = *s.begin(); n     else n       x = *s.lower_bound(d); n     if (++c[x] == n / m) s.erase(x); n     ans += (x - d + m) % m; n     a[i] += (x - d + m) % m; n   } n   cout << ans << endl; n   for (i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n const long long mod = 1e9 + 7; n inline long long add(long long a, long long b) { n   return (a % mod + b % mod) % mod; n } n inline long long sub(long long a, long long b) { n   return (a % mod - b % mod + mod) % mod; n } n inline long long mul(long long a, long long b) { n   return (a % mod * b % mod) % mod; n } n inline long long power(long long a, long long b, long long MOD) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res *= a; n       res %= MOD; n     } n     a = a * a; n     a %= MOD; n     b >>= 1; n   } n   return res; n } n clock_t time_p = clock(); n void time() { n   time_p = clock() - time_p; n   cerr <<  Time elapsed :   << (long double)(time_p) / CLOCKS_PER_SEC <<   n ; n } n bool go(string a, string b) { n   if (a == b) return true; n   long long n1 = a.length(); n   long long n2 = b.length(); n   if (n1 & 1 || n2 & 1) return false; n   string a1, a2, b1, b2; n   for (long long i = 0; i < n1 / 2; i++) a1 += a[i]; n   for (long long i = n1 / 2; i < n1; i++) a2 += a[i]; n   for (long long i = 0; i < n2 / 2; i++) b1 += b[i]; n   for (long long i = n2 / 2; i < n2; i++) b2 += b[i]; n   if (go(a1, b2) && go(a2, b1)) return true; n   if (go(a1, b1) && go(a2, b2)) return true; n   return false; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   string a, b; n   cin >> a >> b; n   cout << (go(a, b) ?  YES  :  NO ) <<   n ; n   time(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005]; n int main() { n   int l, t; n   scanf( %d %d , &l, &t); n   scanf( %s , s + 1); n   s[0] =  0 ; n   for (int i = 0; i <= l; i++) { n     if (s[i] ==  . ) { n       for (int j = i + 1; j <= l; j++) n         if (s[j] >=  5 ) { n           s[j] = 0; n           bool roundup = true; n           t--; n           for (int k = j - 1; k > i; k--) { n             if (roundup) { n               s[k]++; n               if (s[k] >=  5  && t) { n                 s[k] = 0; n                 t--; n                 roundup = true; n               } else n                 roundup = false; n             } n           } n           if (roundup) { n             s[i] = 0; n             for (int k = i - 1; k >= 0; k--) { n               s[k]++; n               if (s[k] <=  9 ) n                 break; n               else n                 s[k] =  0 ; n             } n           } n           break; n         } n     } n   } n   if (s[0] !=  0 ) { n     printf( %s n , s); n   } else { n     printf( %s n , s + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string rep(char c, int x) { n   string ret =   ; n   for (int i = 0; i < x; i++) { n     ret += c; n   } n   return ret; n } n int main() { n   int n, k; n   cin >> n >> k; n   if (n == k) { n     string ans =   ; n     for (int i = 0; i < n; i++) { n       ans +=  0 ; n     } n     cout << ans << endl; n     return 0; n   } else if (k == 1) { n     string ans =  1  + rep( 0 , n - 1); n     cout << ans << endl; n     return 0; n   } else { n     int diff = n - k; n     string repl =  1  + rep( 0 , diff / 2); n     string ans =   ; n     while (true) { n       if (ans.size() + repl.size() > n) { n         break; n       } n       ans += repl; n     } n     int rem = n - ans.size(); n     for (int i = 0; i < rem; i++) { n       ans += repl[i]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int x, y, i; n   if (b * c > a * d) { n     x = b * c - a * d; n     y = b * c; n   } else { n     x = a * d - b * c; n     y = a * d; n   } n   while (1) { n     int k = 0; n     for (i = 2; i <= x; i++) { n       if (x % i == 0 && y % i == 0) { n         x = x / i; n         y = y / i; n         k++; n       } n     } n     if (k == 0) break; n   } n   if (x == 0) n     cout << x <<  /  n          <<  1 ; n   else n     cout << x <<  /  << y; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1000000007; n int N; n struct M { n   int a[250][250]; n   M() { memset(a, 0, sizeof(a)); } n }; n M I() { n   M res; n   for (int i = 0; i < N; i++) res.a[i][i] = 1; n   return res; n } n M operator*(const M& a, const M& b) { n   M ans; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) n       for (int k = 0; k < N; k++) n         ans.a[i][j] = (ans.a[i][j] + 1ll * a.a[i][k] * b.a[k][j]) % Mod; n   return ans; n } n int k, m; n int id(int x, int y) { return x * (1 << m) + y; } n M fpow(M a, int b) { n   M ans = I(), t = a; n   while (b) { n     if (b & 1) ans = ans * t; n     t = t * t; n     b >>= 1; n   } n   return ans; n } n int cnt(int x) { n   int ans = 0; n   while (x) { n     ans += (x & 1); n     x >>= 1; n   } n   return ans; n } n int main() { n   int n; n   scanf( %d %d %d , &n, &k, &m); n   N = (k + 1) * (1 << m); n   M S, T; n   S.a[0][0] = 1; n   for (int c = 0; c <= k; c++) n     for (int s = 0; s < (1 << m); s++) { n       T.a[id(c, s)][id(c, s >> 1)]++; n       if (c < k) n         T.a[id(c, s)][id(c + 1, (s >> 1) | (1 << (m - 1)))] += cnt(s) + 1; n     } n   S = S * fpow(T, n); n   int ans = 0; n   for (int s = 0; s < (1 << m); s++) ans = (ans + S.a[0][id(k, s)]) % Mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, v; n   cin >> n >> v; n   vector<vector<int> > mat(n + 1, vector<int>(v + 1, INT_MAX)); n   for (int i = 1, j = 0; j <= v; j++) { n     mat[i][j] = j; n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= v; j++) { n       if (j < v) { n         mat[i][j] = mat[i - 1][j + 1]; n       } else { n         mat[i][j] = mat[i][j - 1] + i; n       } n     } n   } n   cout << mat[n][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int n; n double sum, tmp, ans; n int main() { n   scanf( %d , &n); n   for (int div = 1; div <= n; div++) { n     scanf( %lf , &tmp); n     sum += tmp; n     if (sum / div > ans) ans = sum / div; n   } n   printf( %.20lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(0); n   long long n, p = 0, q = 0, l = 0, i, flag = 0, k, m, t, j, r = 0, curr = 0, d; n   cin >> n >> m; n   long long a[n], b[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n   } n   if (a[0] == 0) { n     cout <<  NO ; n     return 0; n   } n   if (a[0] != 0) { n     if (a[m - 1] != 0) { n       cout <<  YES ; n       return 0; n     } n   } n   if (a[m - 1] == 0) { n     for (long long i = m; i < n; i++) { n       if (a[i] == 1 && b[i] == 1) { n         if (b[m - 1] == 1) { n           flag = 1; n           break; n         } n       } n     } n   } n   if (flag == 1) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Mo = 1000000007LL; n long long f[60][60], g[60][60]; n long long fac[70], inv[70]; n int n, m; n void add(long long &a, long long b) { n   a += b; n   a %= Mo; n } n long long fpm(long long a, long long b) { n   long long ans = 1LL; n   for (; b; b >>= 1) { n     if (b & 1LL) ans *= a, ans %= Mo; n     a *= a; n     a %= Mo; n   } n   return ans; n } n long long C(long long a, long long b) { n   a += b - 1; n   a %= Mo; n   if (a < b) return 0LL; n   long long ans = 1LL; n   for (int i = a; i > a - b; i--) ans = ans * i % Mo; n   ans *= inv[b]; n   return ans % Mo; n } n void solve() { n   f[2][1] = 1; n   for (int i = 1; i <= 52; i++) { n     for (int x = 2; x <= i + 1; x++) { n       int y = i + 3 - x; n       for (int a = 1; a <= x; a++) n         for (int b = 1; b <= y; b++) add(g[i][min(a, b)], f[x][a] * f[y][b]); n     } n     for (int x = 1; x <= i; x++) n       for (int j = 52; j >= 2; j--) { n         for (int k = 1; k * i + 2 <= j; k++) { n           for (int y = x * k; y <= j; y++) n             add(f[j][y], f[j - i * k][y - x * k] * C(g[i][x], k)); n         } n       } n   } n } n int main() { n   fac[0] = 1; n   for (int i = 1; i <= 60; i++) fac[i] = (fac[i - 1] * (long long)i) % Mo; n   for (int i = 0; i <= 60; i++) inv[i] = fpm(fac[i], Mo - 2LL); n   scanf( %d %d , &n, &m); n   solve(); n   cout << f[n + 2][m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x20202020; n const int mod = 1000000007; n inline void read(int &x) { n   char ch; n   bool f = 0; n   int a = 0; n   while (!((((ch = getchar()) >=  0 ) && (ch <=  9 )) || (ch ==  - ))) n     ; n   if (ch !=  - ) n     a *= 10, a += ch -  0 ; n   else n     f = 1; n   while (((ch = getchar()) >=  0 ) && (ch <=  9 )) a *= 10, a += ch -  0 ; n   if (f) a = -a; n   x = a; n } n const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long powmod(long long a, long long b, long long mod) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n const int N = 111111, M = 111111; n int l, m, n, t, tot; n struct zcc { n   int x, v; n } a[N]; n inline bool cmp(const zcc &a, const zcc &b) { return a.v < b.v; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i].v), a[i].x = i, tot += a[i].v; n   sort(a + 1, a + 1 + n, cmp); n   if (n == 1 && m == tot) { n     puts( Yes ); n     for (int i = 1; i <= 2 * a[1].v - 1; ++i) printf( %d  , a[1].x); n     printf( %d n , a[1].x); n   } else if (n >= 2 && m >= 2 && m <= tot) { n     puts( Yes ); n     while (m > 2) { n       if (a[1].v > 1) n         a[1].v--, printf( %d %d  , a[1].x, a[1].x), m--; n       else if (a[n].v) n         a[n].v--, printf( %d %d  , a[n].x, a[n].x), m--; n       else n         n--; n     } n     if (a[2].v) { n       a[2].v--; n       a[1].v--; n       printf( %d  , a[1].x); n       for (int i = 2; i <= n; ++i) n         for (int j = 1; j <= a[i].v; ++j) printf( %d %d  , a[i].x, a[i].x); n       printf( %d  , a[1].x); n       printf( %d  , a[2].x); n       for (int i = 1; i <= a[1].v; ++i) printf( %d %d  , a[1].x, a[1].x); n       printf( %d n , a[2].x); n     } else { n       printf( %d %d n , a[1].x, a[1].x); n     } n   } else if (n >= 2 && m == 1 && a[1].v == 1) { n     puts( Yes ); n     printf( %d  , a[1].x); n     for (int i = 2; i <= n; ++i) n       for (int j = 1; j <= a[i].v; ++j) printf( %d %d  , a[i].x, a[i].x); n     printf( %d n , a[1].x); n   } else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, x; n long long sum; n char s[30]; n vector<int> a; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %s , s); n     if (s[0] ==  s ) { n       sum = 0; n       for (j = 2; j < a.size(); j += 5) { n         sum += a[j]; n       } n       cout << sum; n       printf(  n ); n     } else { n       scanf( %d , &x); n       if (s[0] ==  a ) n         a.insert(lower_bound(a.begin(), a.end(), x), x); n       else n         a.erase(lower_bound(a.begin(), a.end(), x)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n const unsigned long long bigN = 1e18; n const long long mod9 = 1e9 + 9; n const long long mod7 = 1e9 + 7; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   vector<int> a, b; n   int n, l, r, x; n   cin >> n >> l >> r; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (i >= l && i <= r) { n       continue; n     } else { n       a.push_back(x); n     } n   } n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (i >= l && i <= r) { n       continue; n     } else { n       b.push_back(x); n     } n   } n   if (a == b) { n     cout <<  TRUTH ; n   } else { n     cout <<  LIE ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> M; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     string name, ip; n     cin >> name >> ip; n     M[ip] = name; n   } n   for (int i = 0; i < m; i++) { n     string cmd, ip; n     cin >> cmd >> ip; n     ip.pop_back(); n     cout << cmd <<     << ip <<  ; #  << M[ip] << endl; n   } n } n


#include <bits/stdc++.h> n #define ll long long n using namespace std; n const ll N = 5005, MOD = 998244353; n vector<vector<ll>> dp(N, vector<ll>(N)); n vector<vector<int>> edges(N); n vector<int> h(N); n int n, k; n void add(ll &x, ll y) { n  if((x += y) >= MOD) { n   x -= MOD; n  } n } n void dfs(int x, int p) { n  dp[x][0] = 1;  n  for(int to : edges[x]) { n   if(to != p) { n    dfs(to, x); n    vector<ll> temp(N); n    for(int L1 = 0; L1 <= min(h[x], k); ++L1) { n     for(int L2 = 0; L2 <= h[to]; ++L2) { n      ll ways = dp[x][L1] * dp[to][L2] % MOD; n      if(L1 + L2 < k) add(temp[max(L1, L2+1)], ways); n      add(temp[L1], ways); n     } n    } n    dp[x] = temp; n    h[x] = max(h[x], h[to]+1); n   } n  } n } n int main() { n  ios::sync_with_stdio(0); cin.tie(0); n  cin >> n >> k; n  for(int i = 0; i < n - 1; ++i) { n   int u, v; cin >> u >> v; n   edges[u].push_back(v); n   edges[v].push_back(u); n  } n  dfs(1, 0); n  ll ans = 0; n  for(int i = 0; i <= k; ++i) { n   add(ans, dp[1][i]); n  } n  cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int maxn = 2e5 + 9; n int a[maxn], b[maxn]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &b[i]); n   } n   long long ans = 1; n   int l = n, r = n; n   for (int i = m; i >= 1; i--) { n     while (a[r] > b[i]) r--; n     if (a[r] != b[i] || r == 0) { n       ans = 0; n       break; n     } n     l = r; n     while (a[l] >= b[i]) l--; n     if (i != 1) n       ans = ans * (r - l) % mod; n     else if (l != 0) n       ans = 0; n     r = l; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[5] = {}, b[5] = {}; n   for (int k = 0; k < n; k++) { n     int x; n     cin >> x; n     a[x - 1]++; n   } n   for (int k = 0; k < n; k++) { n     int x; n     cin >> x; n     b[x - 1]++; n   } n   int cnt = 0; n   for (int k = 0; k < 5; k++) { n     if ((a[k] + b[k]) % 2 != 0) { n       cout << -1; n       return 0; n     } n     cnt += (max(a[k], b[k]) - min(a[k], b[k])) / 2; n   } n   cout << (cnt % 2 == 0 ? cnt / 2 : -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   unordered_map<int, int> m; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     m[x]++; n   } n   int ans = 0; n   for (pair<int, int> x : m) ans += x.second / 2; n   printf( %d n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 16; n int from[MAXN], to[MAXN], p[MAXN], q[MAXN]; n bool w[MAXN][MAXN]; n long long dp[1 << MAXN]; n int main() { n   int n, m, a, b; n   long long y; n   cin >> n >> y >> m; n   fill(from, from + n, 0); n   fill(to, to + n, 0); n   fill(w[0], w[m], false); n   for (int i = 0; i < m; ++i) { n     cin >> a >> b; n     --a; n     --b; n     w[a][b] = true; n   } n   for (int k = 0; k < n; ++k) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         w[i][j] |= w[i][k] && w[k][j]; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (w[i][j]) { n         from[j] |= 1 << i; n         to[i] |= 1 << j; n       } n     } n   } n   fill(dp, dp + (1 << n), 0); n   dp[0] = 1; n   for (int i = 1; i < (1 << n); ++i) { n     for (int j = 0; j < n; ++j) { n       if ((i & (1 << j)) != 0 && (i & to[j]) == 0) { n         dp[i] += dp[i ^ (1 << j)]; n       } n     } n   } n   y -= 2001; n   m = (1 << n) - 1; n   if (y >= dp[m]) { n     cout <<  The times have changed  << endl; n     return 0; n   } n   fill(p, p + n, -1); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (p[j] != -1) { n         continue; n       } n       p[j] = i; n       { n         fill(dp, dp + (1 << n), 0); n         dp[0] = 1; n         for (int i = 1; i < (1 << n); ++i) { n           int k = n - __builtin_popcount(i); n           if (p[k] != -1) { n             int j = p[k]; n             if ((i & (1 << j)) != 0 && (i & from[j]) == 0) { n               dp[i] += dp[i ^ (1 << j)]; n             } n           } else { n             for (int j = 0; j < n; ++j) { n               if ((i & (1 << j)) != 0 && (i & from[j]) == 0) { n                 dp[i] += dp[i ^ (1 << j)]; n               } n             } n           } n         } n       } n       if (y < dp[m]) { n         q[i] = j; n         break; n       } else { n         p[j] = -1; n         y -= dp[m]; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     printf( %d%c , q[i] + 1, i == n - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1000000 + 5; n const long long inf = 1e9; n const long long mod = 1e9 + 7; n int a[MX], k; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, p, x, y, tc; n   cin >> tc; n   while (tc--) { n     cin >> n >> p >> k; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     sort(a + 1, a + n + 1); n     int rs = 0, pre = 0; n     for (int i = 1; i <= k; i++) { n       int sm = pre; n       if (pre > p) break; n       rs = max(rs, i - 1); n       for (int j = i + k - 1; j <= n; j += k) { n         sm += a[j]; n         if (sm > p) break; n         rs = max(rs, j); n       } n       pre += a[i]; n     } n     cout << rs << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   unordered_map<string, long long> m; n   m[ Tetrahedron ] = 4; n   m[ Cube ] = 6; n   m[ Octahedron ] = 8; n   m[ Dodecahedron ] = 12; n   m[ Icosahedron ] = 20; n   long long n; n   cin >> n; n   long long count = 0; n   while (n--) { n     string str; n     cin >> str; n     count += m[str]; n   } n   cout << count << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n namespace Acc{ n  const int N = 1e2+10; n  string a,b; n     int n; n     void work(){ n         queue<pair<string,int> >q; n         cin>>n>>a,q.push({b,0}); n         while(1){ n             auto[u,pos]=q.front();q.pop(); n             u.resize(pos+1); n             for(int i=0;i<26;i++){ n                 u[pos]=(char)( a +i); n                 if(a.find(u)==string::npos)return (void)(cout<<u); n                 q.push({u,pos+1}); n             } n         } n     } n } n int main(){ n  int T; n  for(cin>>T;T--;puts(  ))Acc::work(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long par[27]; n long long parent(long long x) { n   while (x != par[x]) { n     par[x] = par[par[x]]; n     x = par[x]; n   } n   return x; n } n void uni(long long x, long long y) { n   long long px = parent(x); n   long long py = parent(y); n   par[y] = px; n } n int main() { n   long long t = 1; n   while (t--) { n     long long n; n     cin >> n; n     set<long long> st; n     for (long long i = 0; i < 27; i++) par[i] = i; n     for (long long i = 0; i < n; i++) { n       string s; n       cin >> s; n       st.insert(s[0] -  a ); n       for (long long i = 1; i < s.length(); i++) { n         st.insert(s[i] -  a ); n         uni(s[0] -  a , s[i] -  a ); n       } n     } n     long long ans = 0; n     for (long long i = 0; i < 26; i++) n       if (parent(i) == i && st.find(i) != st.end()) ans++; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n using namespace std; n const long long INF(0x3f3f3f3f3f3f3f3fll); n const long long inf(0x3f3f3f3f); n template <typename T> n void read(T &res) { n   bool flag = false; n   char ch; n   while (!isdigit(ch = getchar())) (ch ==  - ) && (flag = true); n   for (res = ch - 48; isdigit(ch = getchar()); n        res = (res << 1) + (res << 3) + ch - 48) n     ; n   flag && (res = -res); n } n template <typename T> n void Out(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) Out(x / 10); n   putchar(x % 10 +  0 ); n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long pow_mod(long long x, long long n, long long mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n long long fact_pow(long long n, long long p) { n   long long res = 0; n   while (n) { n     n /= p; n     res += n; n   } n   return res; n } n long long mult(long long a, long long b, long long p) { n   a %= p; n   b %= p; n   long long r = 0, v = a; n   while (b) { n     if (b & 1) { n       r += v; n       if (r > p) r -= p; n     } n     v <<= 1; n     if (v > p) v -= p; n     b >>= 1; n   } n   return r; n } n long long quick_pow(long long a, long long b, long long p) { n   long long r = 1, v = a % p; n   while (b) { n     if (b & 1) r = mult(r, v, p); n     v = mult(v, v, p); n     b >>= 1; n   } n   return r; n } n bool CH(long long a, long long n, long long x, long long t) { n   long long r = quick_pow(a, x, n); n   long long z = r; n   for (long long i = 1; i <= t; i++) { n     r = mult(r, r, n); n     if (r == 1 && z != 1 && z != n - 1) return true; n     z = r; n   } n   return r != 1; n } n bool Miller_Rabin(long long n) { n   if (n < 2) return false; n   if (n == 2) return true; n   if (!(n & 1)) return false; n   long long x = n - 1, t = 0; n   while (!(x & 1)) { n     x >>= 1; n     t++; n   } n   srand(time(NULL)); n   long long o = 8; n   for (long long i = 0; i < o; i++) { n     long long a = rand() % (n - 1) + 1; n     if (CH(a, n, x, t)) return false; n   } n   return true; n } n long long exgcd1(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   long long t = exgcd1(b, a % b, y, x); n   y -= a / b * x; n   return t; n } n long long get_inv(long long a, long long mod) { n   long long x, y; n   long long d = exgcd1(a, mod, x, y); n   return d == 1 ? (x % mod + mod) % mod : -1; n } n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return; n   } n   exgcd(b, a % b, x, y); n   long long t = x; n   x = y, y = t - (a / b) * y; n } n long long INV(long long a, long long b) { n   long long x, y; n   return exgcd(a, b, x, y), (x % b + b) % b; n } n long long crt(long long x, long long p, long long mod) { n   return INV(p / mod, mod) * (p / mod) * x; n } n long long FAC(long long x, long long a, long long b) { n   if (!x) return 1; n   long long ans = 1; n   for (long long i = 1; i <= b; i++) n     if (i % a) ans *= i, ans %= b; n   ans = pow_mod(ans, x / b, b); n   for (long long i = 1; i <= x % b; i++) n     if (i % a) ans *= i, ans %= b; n   return ans * FAC(x / a, a, b) % b; n } n long long C(long long n, long long m, long long a, long long b) { n   long long N = FAC(n, a, b), M = FAC(m, a, b), Z = FAC(n - m, a, b), sum = 0, n             i; n   for (i = n; i; i = i / a) sum += i / a; n   for (i = m; i; i = i / a) sum -= i / a; n   for (i = n - m; i; i = i / a) sum -= i / a; n   return N * pow_mod(a, sum, b) % b * INV(M, b) % b * INV(Z, b) % b; n } n long long exlucas(long long n, long long m, long long p) { n   long long t = p, ans = 0, i; n   for (i = 2; i * i <= p; i++) { n     long long k = 1; n     while (t % i == 0) { n       k *= i, t /= i; n     } n     ans += crt(C(n, m, i, k), p, k), ans %= p; n   } n   if (t > 1) ans += crt(C(n, m, t, t), p, t), ans %= p; n   return ans % p; n } n const long long N = 1e6 + 10; n long long tree[N << 2]; n long long n, m; n long long a[N], sum[N]; n map<long long, long long> pos; n long long lowbit(long long x) { return x & (-x); } n void add(long long pos, long long val) { n   while (pos <= n) { n     tree[pos] ^= val; n     pos += lowbit(pos); n   } n } n long long get(long long pos) { n   long long res = 0; n   while (pos) { n     res ^= tree[pos]; n     pos -= lowbit(pos); n   } n   return res; n } n long long get_sum(long long l, long long r) { return get(r) ^ get(l - 1); } n struct Q { n   long long l, r, ans, id; n   bool operator<(const Q &a) const { return r < a.r; } n } q[N]; n bool cmp(Q a, Q b) { return a.id < b.id; } n signed main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] ^ a[i]; n   cin >> m; n   for (long long i = 1; i <= m; i++) cin >> q[i].l >> q[i].r, q[i].id = i; n   sort(q + 1, q + 1 + m); n   long long now = 1; n   for (long long i = 1; i <= m; i++) { n     long long l = q[i].l, r = q[i].r, id = q[i].id; n     while (now <= r) { n       if (pos[a[now]]) add(pos[a[now]], a[now]); n       pos[a[now]] = now; n       add(pos[a[now]], a[now]); n       now++; n     } n     q[i].ans = (sum[r] ^ sum[l - 1]) ^ get_sum(l, r); n   } n   sort(q + 1, q + 1 + m, cmp); n   for (long long i = 1; i <= m; i++) cout << q[i].ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n long long int fast_pow(long long int base, long long int exp, n                        long long int M = 1000000007) { n   base = base % M; n   long long int result = 1; n   while (exp > 0) { n     if (exp % 2 == 1) result = (result * base) % M; n     base = (base * base) % M; n     exp = exp / 2; n   } n   return result; n } n long long int modInverse(long long int A, long long int M = 1000000007) { n   return fast_pow(A, M - 2, M); n } n long long int maxSubArraySum(long long int a[], long long int size) { n   long long int max_so_far = INT_MIN, max_ending_here = 0; n   for (long long int i = 0; i < size; i++) { n     max_ending_here = max_ending_here + a[i]; n     if (max_so_far < max_ending_here) max_so_far = max_ending_here; n     if (max_ending_here < 0) max_ending_here = 0; n   } n   return max_so_far; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     long long int yass = 0; n     cin >> n; n     long long int v[n]; n     for (long long int i = 0; i <= n - 1; i++) { n       cin >> v[i]; n       yass += v[i]; n     } n     long long int m1 = maxSubArraySum(v, n - 1); n     long long int m2 = maxSubArraySum(v + 1, n - 1); n     ; n     long long int adel = max(m1, m2); n     if (yass > adel) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (k % 3 == 0) { n       if (n >= k) { n         n = n % (k + 1); n         if (n == k) n           cout <<  Alice n ; n         else { n           if ((n) % 3 != 0) { n             cout <<  Alice n ; n           } else { n             cout <<  Bob n ; n           } n         } n       } else { n         if ((n) % 3 != 0) { n           cout <<  Alice n ; n         } else { n           cout <<  Bob n ; n         } n       } n     } else { n       if ((n) % 3 != 0) { n         cout <<  Alice n ; n       } else { n         cout <<  Bob n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define mod 1000000007 n typedef long long ll; n #define MAXN 300005 n #define mod 1000000007 n  n  n int main(){ n     int n; n     cin>>n; n     vector<int>ar(n+1); n     for(int i=1;i<=n;i++)cin>>ar[i]; n     vector<pair<int,pair<int,int> > >supp; n     for(int i=1;i<=n;i++){ n         for(int j=i+1;j<=n;j++){ n             supp.push_back({abs(ar[i]-ar[j]),{i,j}}); n         } n     } n     sort(supp.rbegin(),supp.rend()); n     for(int i=0;i<(int)supp.size();i++){ n         pair<int,int>tmp=supp[i].second; n         if(ar[tmp.first]<ar[tmp.second])swap(tmp.first,tmp.second); n         cout<< ?  <<tmp.first<<   <<tmp.second<<endl; n         string ok; n         cin>>ok; n         if(ok== Yes ){ n             cout<< !  <<tmp.first<<   <<tmp.second<<endl; n             return 0; n         } n     } n     cout<< ! 0 0 <<endl; n  n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   int a[m + 1]; n   for (int i = 0; i < m + 1; i++) { n     cin >> a[i]; n   } n   int ans = 0; n   for (int i = 0; i < m; i++) { n     int count = 0; n     for (int j = 0; j < n; j++) { n       if (((a[m] >> j) & 1) != ((a[i] >> j) & 1)) { n         count++; n       } n     } n     if (count <= k) ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long mod = 1e9 + 7; n int n, m; n vector<int> depth; n vector<bool> visited; n vector<vector<int>> adj; n void dfs(int curr, int dt) { n   depth[curr] = (dt) % 2; n   visited[curr] = true; n   for (int i = 0; i < adj[curr].size(); i++) { n     if (!visited[adj[curr][i]]) { n       dfs(adj[curr][i], dt + 1); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     adj = vector<vector<int>>(n + 1); n     depth = vector<int>(n + 1); n     visited = vector<bool>(n + 1, false); n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     dfs(1, 0); n     vector<int> v1; n     vector<int> v2; n     for (int i = 1; i <= n; i++) { n       if (depth[i] % 2) { n         v1.push_back(i); n       } else { n         v2.push_back(i); n       } n     } n     if (v1.size() < v2.size()) { n       cout << v1.size() << endl; n       for (int i = 0; i < v1.size(); i++) { n         cout << v1[i] <<    ; n       } n     } else { n       cout << v2.size() << endl; n       for (int i = 0; i < v2.size(); i++) { n         cout << v2[i] <<    ; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n void self_add(long long &x, long long y) { x = (x + y) % mod; } n long long binpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * a) % mod; n     n >>= 1; n     a = (a * a) % mod; n   } n   return res; n } n int main() { n   long long n; n   cin >> n; n   long long fact = 1; n   for (long long i = 2; i <= n; i++) { n     fact = (fact * (i)) % mod; n   } n   long long power = binpow(2, n - 1); n   long long ans = (fact - power) % mod; n   if (ans < 0) ans += mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int v; n   cin >> v; n   int a[9]; n   int t = 100000, x; n   for (long long i = 0; i < 9; ++i) { n     cin >> a[i]; n     t = min(t, a[i]); n   } n   if (t > v) { n     cout << -1; n     return; n   } n   x = v / t; n   while (x--) { n     for (int i = 8; i >= 0; i--) { n       if (a[i] <= v && (v - a[i]) / t == x) { n         cout << i + 1; n         v -= a[i]; n         break; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y, f; n   char na; n } f[30]; n bool cmp(node a, node b) { return a.na < b.na; } n char s[1005][1005]; n int s1[1005][1005]; n int s2[1005][1005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   memset(f, 0, sizeof(f)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       s1[i][j] = s1[i][j - 1] + ((s[i][j] ==  # ) ? 1 : 0); n     } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       s2[j][i] = s2[j - 1][i] + ((s[j][i] ==  # ) ? 1 : 0); n     } n   int ns = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (s[i][j] >=  A  && s[i][j] <=  Z ) { n         f[ns].x = i; n         f[ns].y = j; n         f[ns].na = s[i][j]; n         ns++; n       } n     } n   sort(f, f + ns, cmp); n   int nq; n   scanf( %d , &nq); n   for (int i = 0; i < nq; i++) { n     char dir[9]; n     int len; n     scanf( %s %d , dir, &len); n     for (int j = 0; j < ns; j++) { n       if (!f[j].f) { n         if (dir[0] ==  W ) { n           if (f[j].y - len < 1) n             f[j].f = 1; n           else if (s1[f[j].x][f[j].y] - s1[f[j].x][f[j].y - len - 1] > 0) n             f[j].f = 1; n           f[j].y = f[j].y - len; n         } n         if (dir[0] ==  E ) { n           if (f[j].y + len > m) n             f[j].f = 1; n           else if (s1[f[j].x][f[j].y + len] - s1[f[j].x][f[j].y - 1] > 0) n             f[j].f = 1; n           f[j].y = f[j].y + len; n         } n         if (dir[0] ==  S ) { n           if (f[j].x + len > n) n             f[j].f = 1; n           else if (s2[f[j].x + len][f[j].y] - s2[f[j].x - 1][f[j].y] > 0) n             f[j].f = 1; n           f[j].x = f[j].x + len; n         } n         if (dir[0] ==  N ) { n           if (f[j].x - len < 1) n             f[j].f = 1; n           else if (s2[f[j].x][f[j].y] - s2[f[j].x - len - 1][f[j].y] > 0) n             f[j].f = 1; n           f[j].x = f[j].x - len; n         } n       } n     } n   } n   int flag = 0; n   for (int i = 0; i < ns; i++) { n     if (!f[i].f) { n       printf( %c , f[i].na); n       flag = 1; n     } n   } n   if (!flag) printf( no solution ); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, head[100005], tot, d[100005], fa[100005], vis[100005], lf[100005], cnt; n struct node { n   int v, nxt; n } edg[100005 << 1]; n inline void addedg(int u, int v) { n   edg[tot].v = v; n   edg[tot].nxt = head[u]; n   head[u] = tot++; n } n inline void init() { n   memset(head, -1, sizeof(int) * (n + 1)); n   tot = cnt = 0; n } n inline void dfs1(int u, int f) { n   int v; n   d[u] = 0; n   for (int i = head[u]; i != -1; i = edg[i].nxt) { n     v = edg[i].v; n     ++d[u]; n     if (v == f) continue; n     dfs1(v, u); n   } n } n inline void dfs2(int u, int f, int dis) { n   int v; n   fa[u] = f; n   d[u] = dis; n   for (int i = head[u]; i != -1; i = edg[i].nxt) { n     v = edg[i].v; n     if (f == v) continue; n     dfs2(v, u, dis + 1); n   } n } n inline bool cmp(int u, int v) { return d[u] > d[v]; } n set<int> se; n int main() { n   scanf( %d , &n); n   init(); n   int u, v; n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &u, &v); n     addedg(u, v); n     addedg(v, u); n   } n   dfs1(1, 1); n   for (int i = 1; i <= n; ++i) { n     if (d[i] == 1) lf[cnt++] = i; n   } n   int root = lf[0]; n   dfs2(root, root, 0); n   vis[root] = 1; n   int maxn = 0, minn = 1; n   for (int i = 1; i < cnt; ++i) { n     if (d[lf[i]] & 1) minn = 3; n   } n   fa[root] = edg[head[root]].v; n   maxn = n - 1 - cnt; n   for (int i = 0; i < cnt; ++i) se.insert(fa[lf[i]]); n   maxn += se.size(); n   printf( %d %d n , minn, maxn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n struct Edge { n   int to; n   int nxt; n } edge[MAXN << 1]; n int n, k, id; n int first[MAXN]; n int a[MAXN]; n int fa[MAXN]; n int f[MAXN]; n int g[MAXN]; n int sz[MAXN]; n void addE(int u, int v) { n   edge[++id] = (Edge){v, first[u]}; n   first[u] = id; n } n void dfsinfo(int u) { n   sz[u] = 1; n   for (int i = first[u]; i; i = edge[i].nxt) n     if (edge[i].to != fa[u]) { n       fa[edge[i].to] = u; n       dfsinfo(edge[i].to); n       sz[u] += sz[edge[i].to]; n     } n } n void dfsf(int u, int lim) { n   int MX = 0, sum = 1; n   for (int i = first[u]; i; i = edge[i].nxt) n     if (edge[i].to != fa[u] && a[edge[i].to] >= lim) { n       dfsf(edge[i].to, lim); n       if (sz[edge[i].to] == f[edge[i].to]) n         sum += f[edge[i].to]; n       else n         MX = max(MX, f[edge[i].to]); n     } n   f[u] = MX + sum; n } n void dfsg(int u, int lim) { n   int MX1 = 0, MX2 = 0, sum = 1; n   for (int i = first[u]; i; i = edge[i].nxt) n     if (edge[i].to != fa[u] && a[edge[i].to] >= lim) { n       if (sz[edge[i].to] == f[edge[i].to]) n         sum += f[edge[i].to]; n       else if (f[edge[i].to] >= MX1) { n         MX2 = MX1; n         MX1 = f[edge[i].to]; n       } else if (f[edge[i].to] > MX2) n         MX2 = f[edge[i].to]; n     } n   if (n - sz[u] == g[u]) n     sum += g[u]; n   else if (g[u] >= MX1) { n     MX2 = MX1; n     MX1 = g[u]; n   } else if (g[u] > MX2) n     MX2 = g[u]; n   for (int i = first[u]; i; i = edge[i].nxt) n     if (edge[i].to != fa[u] && a[edge[i].to] >= lim) { n       if (sz[edge[i].to] == f[edge[i].to]) n         g[edge[i].to] = sum - f[edge[i].to] + MX1; n       else if (f[edge[i].to] == MX1) n         g[edge[i].to] = sum + MX2; n       else n         g[edge[i].to] = sum + MX1; n       dfsg(edge[i].to, lim); n     } n } n int getans(int lim) { n   memset(f, 0, sizeof(f)); n   memset(g, 0, sizeof(g)); n   if (a[1] >= lim) { n     dfsf(1, lim); n     dfsg(1, lim); n   } n   for (int i = 1; i <= n; i++) n     if (a[i] < lim) n       for (int j = first[i]; j; j = edge[j].nxt) n         if (edge[j].to != fa[i] && a[edge[j].to] >= lim) { n           dfsf(edge[j].to, lim); n           dfsg(edge[j].to, lim); n         } n   int res = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] < lim) continue; n     int MX = 0, sum = 1; n     for (int j = first[i]; j; j = edge[j].nxt) n       if (a[edge[j].to] >= lim) { n         int SZ = 0, FG = 0; n         if (edge[j].to == fa[i]) { n           SZ = n - sz[i]; n           FG = g[i]; n         } else { n           SZ = sz[edge[j].to]; n           FG = f[edge[j].to]; n         } n         if (FG == SZ) n           sum += FG; n         else n           MX = max(MX, FG); n       } n     res = max(res, MX + sum); n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int u, v, i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     addE(u, v); n     addE(v, u); n   } n   dfsinfo(1); n   int low = 1, high = 1000000; n   while (low < high) { n     int mid = low + high + 1 >> 1; n     if (getans(mid) >= k) n       low = mid; n     else n       high = mid - 1; n   } n   printf( %d n , low); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 510; n const int inf = 0x3f3f3f3f; n int n; n long long d[maxn][maxn]; n int w(int i, int j, int k) { return i * j * k; } n int main() { n   int n; n   scanf( %d , &n); n   memset(d, 0x3f, sizeof(d)); n   for (int i = 1; i <= n; i++) { n     d[i][i + 2] = w(i, i + 1, i + 2); n     d[i][i] = 0; n     d[i][i + 1] = 0; n   } n   for (int i = n - 2; i >= 1; i--) { n     for (int j = i + 2; j <= n; j++) { n       for (int k = i + 1; k < j; k++) { n         d[i][j] = min(d[i][j], d[i][k] + d[k][j] + w(i, j, k)); n       } n     } n   } n   printf( %I64d , d[1][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int l, MOD; n map<long long, int> mp; n int fib(long long n) { n   if (mp.count(n)) return mp[n]; n   if (n & 1) n     return mp[n] = 1LL * fib(n / 2) * (fib(n / 2 - 1) + fib(n / 2 + 1)) % MOD; n   return mp[n] = (1LL * fib(n / 2) * fib(n / 2) + n                   1LL * fib(n / 2 - 1) * fib(n / 2 - 1)) % n                  MOD; n } n int bpow(int a, long long b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % MOD; n     b >>= 1; n     a = 1LL * a * a % MOD; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k >> l >> MOD; n   if (k >= (1LL << min(l, 62))) { n     cout <<  0 n ; n     return 0; n   } n   mp[0] = 1 % MOD; n   mp[1] = 1 % MOD; n   int zero = fib(n + 1); n   int one = (bpow(2, n) - zero + MOD) % MOD; n   int ans = 1 % MOD; n   for (int i = 0; i < min(l, 62); i++) n     if ((1LL << i) & k) n       ans = 1LL * ans * one % MOD; n     else n       ans = 1LL * ans * zero % MOD; n   for (int i = 62; i < l; i++) ans = 1LL * ans * zero % MOD; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sol() { n   long long int n; n   cin >> n; n   vector<int> ar[6]; n   long long int a; n   vector<int> v = {4, 8, 15, 16, 23, 42}; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     ar[lower_bound(v.begin(), v.end(), a) - v.begin()].push_back(i); n   } n   int p[6] = {0, 0, 0, 0, 0, 0}; n   int sit = 0; n   int cnt = 0; n   while (1) { n     int prev = -1; n     for (long long int i = 0; i < 6; i++) { n       while (p[i] < ar[i].size() && ar[i][p[i]] < prev) { n         p[i]++; n       } n       if (p[i] >= ar[i].size()) { n         sit = 1; n         break; n       } n       prev = ar[i][p[i]]; n       p[i]++; n     } n     if (sit == 1) n       break; n     else n       cnt++; n   } n   cout << n - 6 * cnt <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     sol(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int bound = 400; n struct qq { n   int id, vl; n   bool operator<(const qq &temp) const { return vl < temp.vl; } n }; n qq bk[210000 / bound][bound], ns; n int nk[210000 / bound]; n struct pp { n   int vl, id, min_value, value; n   int flag; n   bool operator<(const pp &temp) const { return vl < temp.vl; } n }; n pp bl[210000 / bound][2 * bound], now; n int num[210000 / bound], num_bl, cnt_list, np[2 * bound]; n int m, n, a[220000], num_q, ans, nw; n int lower_bound(int left, int right, qq bl[], int x) { n   int mid; n   while (left <= right) { n     mid = (left + right) >> 1; n     if (bl[mid].vl < x) n       left = mid + 1; n     else n       right = mid - 1; n   } n   return right; n } n int lower_bound(int left, int right, pp bl[], int x) { n   int mid; n   while (left <= right) { n     mid = (left + right) >> 1; n     if (bl[mid].vl < x) n       left = mid + 1; n     else n       right = mid - 1; n   } n   return right; n } n int main() { n   int i, j, s, p, q, ip, id, l, r, x, fir; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   memset(num, 0, sizeof(num)); n   memset(nk, 0, sizeof(nk)); n   num_bl = 0; n   for (i = 0; i < n; i++) { n     if (i % bound == 0) { n       for (j = i; j < min(n, i + bound); j++) { n         if (j + m < n) { n           now.id = j; n           now.vl = a[j]; n           now.flag = -1; n           bl[num_bl][num[num_bl]++] = now; n           now.id = j + m; n           now.vl = a[j + m]; n           now.flag = 1; n           bl[num_bl][num[num_bl]++] = now; n         } n         ns.id = j; n         ns.vl = a[j]; n         bk[num_bl][nk[num_bl]++] = ns; n       } n       num_bl++; n     } n   } n   for (i = 0; i < num_bl; i++) { n     sort(bl[i], bl[i] + num[i]); n     sort(bk[i], bk[i] + nk[i]); n     for (j = i * bound; j < min((i + 1) * bound, n); j++) np[j - i * bound] = 0; n     for (j = 0; j < num[i]; j++) { n       bl[i][j].value = bl[i][j].flag; n       ip = bl[i][j].id; n       if (bl[i][j].flag > 0) ip -= m; n       np[ip - i * bound] += bl[i][j].flag; n       if (bl[i][j].flag < 0) { n         if (bl[i][j].id + m >= n) { n           bl[i][j].value = 0; n           np[ip - i * bound]++; n         } n       } n       if (j) { n         bl[i][j].value += bl[i][j - 1].value; n       } n       bl[i][j].min_value = 0; n       nw = 0; n       for (s = i * bound; s < min((i + 1) * bound, n); s++) { n         nw += np[s - i * bound]; n         if (bl[i][j].min_value > nw) bl[i][j].min_value = nw; n       } n     } n   } n   scanf( %d , &num_q); n   ans = 0; n   while (num_q--) { n     scanf( %d%d%d , &l, &r, &x); n     l--; n     r--; n     x ^= ans; n     ans = 0; n     id = l / bound; n     nw = fir = 0; n     for (i = l; i <= min(l + m - 1, (l / bound + 1) * bound - 1); i++) { n       if (a[i] < x) fir++; n     } n     if (l + m - 1 >= (l / bound + 1) * bound) { n       for (i = (l + m - 1) / bound * bound; i <= l + m - 1; i++) { n         if (a[i] < x) fir++; n       } n       for (i = l / bound + 1; i < (l + m - 1) / bound; i++) { n         id = lower_bound(0, nk[i] - 1, bk[i], x); n         fir += (id + 1); n       } n     } n     for (i = l; i <= min(r - 1, (l / bound + 1) * bound - 1); i++) { n       if (i + m < n) { n         if (a[i] < x) nw--; n         if (a[i + m] < x) nw++; n         if (ans > nw) ans = nw; n       } n     } n     if (r - 1 >= (l / bound + 1) * bound) { n       for (i = l / bound + 1; i < (r - 1) / bound; i++) { n         id = lower_bound(0, num[i] - 1, bl[i], x); n         if (id >= 0) { n           if (nw + bl[i][id].min_value < ans) ans = nw + bl[i][id].min_value; n           nw += bl[i][id].value; n         } n       } n       for (i = ((r - 1) / bound) * bound; i <= r - 1; i++) { n         if (i + m < n) { n           if (a[i] < x) nw--; n           if (a[i + m] < x) nw++; n           if (ans > nw) ans = nw; n         } n       } n     } n     ans += fir; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void readi(int &x); n const int maxn = 300005; n int n, b[maxn], id[maxn], rk[maxn]; n double fl[maxn], fr[maxn]; n bool cmpb(const int &i, const int &j) { return b[i] < b[j]; } n struct node { n   double div, dta; n   node *lc, *rc; n   inline void downdate() { n     if (div != 1) n       lc->div *= div, lc->dta *= div, rc->div *= div, rc->dta *= div, div = 1; n     if (dta != 0) lc->dta += dta, rc->dta += dta, dta = 0; n   } n   inline void modify(int l, int r, const int &a, const int &b, const double &c, n                      const double &d) { n     if (l >= a && r <= b) n       div *= c, dta *= c, dta += d; n     else { n       int mid = l + r >> 1; n       downdate(); n       if (a <= mid) lc->modify(l, mid, a, b, c, d); n       if (b > mid) rc->modify(mid + 1, r, a, b, c, d); n     } n   } n   inline double get(int l, int r, const int &p) { n     if (l == r) return dta; n     int mid = l + r >> 1; n     downdate(); n     if (p <= mid) n       return lc->get(l, mid, p); n     else n       return rc->get(mid + 1, r, p); n   } n } ndl[maxn * 3], *root; n int ns; n node *build(int l, int r) { n   node *c = ndl + ns++; n   c->dta = 0; n   c->div = 1; n   if (l == r) n     c->lc = c->rc = NULL; n   else { n     int mid = l + r >> 1; n     c->lc = build(l, mid); n     c->rc = build(mid + 1, r); n   } n   return c; n } n void build() { ns = 1, root = build(1, n); } n void init() { n   readi(n); n   for (int i = 1; i <= n; i++) readi(b[i]), id[i] = i; n   sort(id + 1, id + n + 1, cmpb); n   for (int i = 1; i <= n; i++) rk[id[i]] = i; n } n void work() { n   build(); n   for (int i = 1; i <= n; i++) { n     root->modify(1, n, 1, n, 1, 1); n     if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0); n     fl[i] = root->get(1, n, rk[i]) / 2; n   } n   build(); n   for (int i = n; i >= 1; i--) { n     root->modify(1, n, 1, n, 1, 1); n     if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0); n     fr[i] = root->get(1, n, rk[i]) / 2; n   } n   double ans = 0; n   for (int i = 1; i <= n; i++) ans += fl[i] * fr[i] * b[i]; n   ans /= (double)n * n; n   printf( %lf , (double)ans * 2); n } n int main() { n   init(); n   work(); n   return 0; n } n inline void readi(int &x) { n   char c; n   for (c = getchar(); c >  9  || c <  0 ; c = getchar()) n     ; n   x = c ^  0 ; n   for (c = getchar(); c >=  0  && c <=  9 ; c = getchar()) n     x = x * 10 + (c ^  0 ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > seg[2]; n int n, m, k, x, y, dX, dY; n bool flag = 0; n long long ans; n void add(int tx, int ty) { n   seg[0].push_back(pair<int, int>(tx - ty, tx)); n   seg[1].push_back(pair<int, int>(tx + ty, tx)); n } n void reflect() { n   int fc; n   if (dX == dY) n     fc = 0; n   else n     fc = 1; n   pair<int, int> p = (fc) ? pair<int, int>(x + y, x) : pair<int, int>(x - y, x); n   vector<pair<int, int> >::iterator it = n       upper_bound(seg[fc].begin(), seg[fc].end(), p); n   while (it->first != p.first) it--; n   if (dX < 0) n     while (it->second >= x) it--; n   ans += abs(x - it->second) - 1; n   x = it->second; n   y = (fc) ? (it->first - x) : (x - it->first); n   bool A = binary_search(seg[0].begin(), seg[0].end(), n                          pair<int, int>(x - y - dX, x - dX)); n   bool B = binary_search(seg[0].begin(), seg[0].end(), n                          pair<int, int>(x - y + dY, x)); n   if (A == B) n     flag = 1, dX = -dX, dY = -dY; n   else { n     if (A) n       x -= dX, dY = -dY; n     else if (B) n       y -= dY, dX = -dX; n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= m; i++) add(0, i), add(n + 1, i); n   for (int i = 0; i <= n + 1; i++) add(i, 0), add(i, m + 1); n   for (int i = 1; i <= k; i++) { n     scanf( %d %d , &x, &y); n     add(x, y); n   } n   scanf( %d %d , &x, &y); n   char c = getchar(); n   while (c !=  N  && c !=  S  && c !=  W  && c !=  E ) c = getchar(); n   if (c ==  N ) n     dX = -1; n   else n     dX = 1; n   c = getchar(); n   if (c ==  W ) n     dY = -1; n   else n     dY = 1; n   sort(seg[0].begin(), seg[0].end()); n   sort(seg[1].begin(), seg[1].end()); n   reflect(); n   int sx = x, sy = y, sdx = dX, sdy = dY; n   ans = 0; n   do reflect(); n   while (!(x == sx && y == sy && dX == sdx && dY == sdy)); n   printf( %lld n , (flag) ? (ans >> 1) : ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7; n const double pi = acos(-1), eps = 1e-9; n int n, x, c = 1; n vector<int> v; n long long ans; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x); n     if (x >= c) { n       while (c < x) { n         ans += i - v.back(); n         ++c; n         v.pop_back(); n       } n       ++c; n     } else { n       v.push_back(i); n       ans += c - x - 1; n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int a[N]; n long long dp[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     a[x]++; n   } n   long long ans = 0; n   dp[N - 1] = dp[N - 2] = 0; n   for (int i = N - 3; i >= 1; --i) { n     dp[i] = max(dp[i + 1], dp[i + 2] + 1LL * a[i] * i); n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7, MAXL = 25; n void OUT(long double o, int x) { n   cout << fixed << setprecision(x) << o; n   return; n } n long long a[MAX], t = 1; n vector<int> adj[MAX]; n void dfs(int v, int p) { n   long long c = 0; n   for (int u : adj[v]) { n     if (u != p) { n       dfs(u, v); n       if (adj[u].size() == 1) c++; n     } n   } n   if (adj[v].size() > 1 && c < 3) t = 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     long long x; n     cin >> x; n     adj[x].push_back(i); n     adj[i].push_back(x); n   } n   dfs(1, 1); n   if (t) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e6 + 1, mod = 1e9 + 7; n int n, m, k, cnt[maxn], pow2[maxn], ans; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   ++k; n   while (m--) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u + 1 != v && u + k != v) { n       puts( 0 ); n       return 0; n     } n     if (u + k == v) cnt[u] = 1; n   } n   for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1]; n   for (int i = k + 1; i <= n; ++i) n     if (cnt[i] - cnt[i - 1] && cnt[i - k]) { n       puts( 0 ); n       return 0; n     } n   pow2[0] = 1; n   for (int i = 1; i <= n; ++i) { n     pow2[i] = pow2[i - 1] << 1; n     if (pow2[i] >= mod) pow2[i] -= mod; n   } n   ans = !cnt[n]; n   for (int i = 1; i <= n - k; ++i) n     if (cnt[i] == cnt[n]) { n       if (i <= k) n         ans += pow2[i - cnt[i - 1] - 1]; n       else if (!cnt[i - k]) n         ans += pow2[k - cnt[i - 1] - 1]; n       if (ans >= mod) ans -= mod; n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using v2d = vector<vector<T> >; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n const int maxN = 2e3 + 10; n int n, k, a[maxN], dp[maxN][maxN]; n void solve() { n   cin >> n >> k; n   for (int i = 1; i <= (int)(n); ++i) { n     cin >> a[i]; n   } n   for (int i = (int)(0); i <= (int)(n); ++i) { n     for (int j = (int)(0); j <= (int)(n); ++j) { n       dp[i][j] = -1e9; n     } n   } n   dp[0][0] = 0; n   for (int i = 1; i <= (int)(n); ++i) { n     for (int j = (int)(0); j <= (int)(i); ++j) { n       if (j > 0) { n         uax(dp[i][j], dp[i - 1][j - 1]); n       } n       uax(dp[i][j], dp[i - 1][j] + (a[i] == i - j)); n     } n   } n   for (int i = 0; i < (int)(n); ++i) { n     if (dp[n][i] >= k) { n       cout << i <<   n ; n       return; n     } n   } n   cout << -1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int T = 1; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, h, k, i, j, s2, H[2][3000], H2[2][3000], s, a, x, y, z, l[2], top; n vector<pair<int, int> > v[2]; n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> x >> y >> z; n     v[x].push_back(make_pair(z, y)); n   } n   h = m; n   sort(v[0].begin(), v[0].end()); n   sort(v[1].begin(), v[1].end()); n   l[0] = v[0].size(); n   l[1] = v[1].size(); n   while (1) { n     a = 0; n     for (i = l[k] - 1; i >= 0; i--) n       if (v[k][i].second <= m && !H[k][i]) { n         s++; n         H[k][i] = 1; n         m += v[k][i].first; n         a = 1; n         break; n       } n     if (!a) break; n     k = !k; n   } n   m = h; n   k = 1; n   while (1) { n     a = 0; n     for (i = l[k] - 1; i >= 0; i--) n       if (v[k][i].second <= m && !H2[k][i]) { n         s2++; n         H2[k][i] = 1; n         m += v[k][i].first; n         a = 1; n         break; n       } n     if (!a) break; n     k = !k; n   } n   cout << max(s, s2); n } n


#include <bits/stdc++.h> n using namespace std; n bool adj[200][200]; n int bfs(int source, int dest) { n   bool vis[200]; n   for (int i = 0; i < 200; i++) vis[i] = 0; n   queue<pair<int, int> > qe; n   qe.push({source, 0}); n   while (!qe.empty()) { n     int node = qe.front().first; n     int d = qe.front().second; n     qe.pop(); n     if (node == dest) return d; n     vis[node] = 1; n     for (int i = 0; i < 200; i++) { n       if (adj[node][i] and !vis[i]) qe.push({i, d + 1}); n     } n   } n   return INT_MAX; n } n int main() { n   int n; n   cin >> n; n   vector<long long> arr; n   while (n--) { n     long long x; n     cin >> x; n     if (x != 0) arr.push_back(x); n   } n   n = arr.size(); n   if (n > 128) { n     cout << 3 << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (arr[i] & arr[j]) adj[i][j] = 1; n     } n   } n   int min_cycle = INT_MAX; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (adj[i][j]) { n         adj[i][j] = 0; n         adj[j][i] = 0; n         int m = bfs(i, j); n         min_cycle = min(m, min_cycle); n         adj[i][j] = 1; n         adj[j][i] = 1; n       } n     } n   } n   if (min_cycle > 10000000) n     cout << -1 << endl; n   else n     cout << min_cycle + 1 << endl; n } n