#include <bits/stdc++.h> n using namespace std; n long long int tree[6000005], a[3000005], minp[3000005], upd[3000005]; n long long int cun[3000005]; n long long int d[3000005]; n long long int n; n void sieve() { n   a[0] = 1; n   a[1] = 1; n   for (long long int i = 1; i < 1e6 + 1; i++) { n     for (long long int j = i; j < 1e6 + 1; j += i) { n       d[j]++; n     } n   } n } n long long int lazy[3000005], tree2[3000005]; n long long int b[3000005]; n void build_tree(long long int node, long long int c, long long int en) { n   if (c > en) return; n   if (c == en) { n     tree[node] = b[c]; n     tree2[node] = (tree[node] != 2 && tree[node] != 1); n     return; n   } n   build_tree(node * 2, c, (c + en) / 2); n   build_tree(node * 2 + 1, 1 + (c + en) / 2, en); n   tree[node] = tree[2 * node] + tree[2 * node + 1]; n   tree2[node] = tree2[node * 2] | tree2[node * 2 + 1]; n } n long long int query(long long int node, long long int st, long long int en, n                     long long int l, long long int r) { n   if (st > r || en < l || st > en) return 0; n   if (st >= l && en <= r) { n     return tree[node]; n   } n   long long int r1 = query(2 * node, st, (st + en) / 2, l, r); n   long long int r2 = query(2 * node + 1, (st + en) / 2 + 1, en, l, r); n   return r1 + r2; n } n void update(long long int node, long long int st, long long int en, n             long long int l, long long int r) { n   if (st > r || en < l || st > en) return; n   if (st == en) { n     int p = d[tree[node]]; n     tree[node] = p; n     tree2[node] = (tree[node] != 2 && tree[node] != 1); n     return; n   } n   if (tree2[2 * node]) update(2 * node, st, (st + en) / 2, l, r); n   if (tree2[2 * node + 1]) update(2 * node + 1, (st + en) / 2 + 1, en, l, r); n   tree[node] = tree[2 * node] + tree[2 * node + 1]; n   tree2[node] = tree2[node * 2] | tree2[node * 2 + 1]; n } n int main() { n   sieve(); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int q; n   cin >> n >> q; n   for (long long int i = 0; i < n; i++) cin >> b[i]; n   build_tree(1, 0, n - 1); n   for (long long int i = 0; i < q; i++) { n     long long int ty, l, r; n     cin >> ty >> l >> r; n     l--; n     r--; n     if (ty == 1) n       update(1, 0, n - 1, l, r); n     else n       cout << query(1, 0, n - 1, l, r) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> g[50005]; n vector<int> t[50005]; n int T; n int col[1000005]; n int team[50005]; n mt19937 mt(1235577); n int x[100005], y[100005]; n int ans[50005]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(m); ++i) { n     int from, to; n     scanf( %d%d , &from, &to), --from, --to; n     x[i] = from; n     y[i] = to; n     g[from].push_back(to); n     g[to].push_back(from); n   } n   for (int i = 0; i < (int)(n); ++i) { n     int cnt; n     scanf( %d , &cnt); n     for (int j = 0; j < (int)(cnt); ++j) { n       int x; n       scanf( %d , &x), --x; n       T = max(T, x + 1); n       t[i].push_back(x); n     } n   } n   int times = 0; n   while (true) { n     assert(times++ < 100); n     for (int i = 0; i < (int)(T); ++i) { n       col[i] = mt() & 1; n     } n     bool ok = true; n     bool was[2]; n     for (int i = 0; i < (int)(n); ++i) { n       was[0] = was[1] = false; n       for (int x : t[i]) { n         was[col[x]] = true; n       } n       if (!was[0] || !was[1]) { n         ok = false; n         break; n       } n     } n     if (ok) { n       break; n     } n   } n   while (true) { n     for (int i = 0; i < (int)(n); ++i) team[i] = mt() & 1; n     int cnt = 0; n     for (int i = 0; i < (int)(m); ++i) n       if (team[x[i]] != team[y[i]]) { n         ++cnt; n       } n     if (2 * cnt < m) { n       continue; n     } n     for (int i = 0; i < (int)(n); ++i) { n       for (int x : t[i]) n         if (col[x] == team[i]) { n           ans[i] = x; n           break; n         } n     } n     bool firstTime = true; n     for (int i = 0; i < (int)(n); ++i) { n       if (!firstTime) { n         printf(   ); n       } n       firstTime = false; n       printf( %d , ans[i] + 1); n     } n     printf(  n ); n     firstTime = true; n     for (int i = 0; i < (int)(T); ++i) { n       if (!firstTime) { n         printf(   ); n       } n       firstTime = false; n       printf( %d , col[i] + 1); n     } n     printf(  n ); n     break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> num; n void solve() { n   int n; n   cin >> n; n   char arr[2][n]; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < n; j++) { n       cin >> arr[i][j]; n     } n   } n   int row = 0, col = 0; n   int visited[2][n]; n   memset(visited, false, sizeof(visited)); n   while (col < n - 1) { n     visited[row][col] = true; n     if (!visited[row][col + 1] && arr[row][col + 1] !=  1 ) n       col++; n     else if (!visited[1 - row][col] && arr[1 - row][col] !=  1 ) { n       row = 1 - row; n     } else if (!visited[1 - row][col + 1] && arr[1 - row][col + 1] !=  1 ) { n       row = 1 - row; n       col++; n     } else { n       break; n     } n   } n   if (col == n - 1) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, x0, y, y0; n   cin >> x >> y >> x0 >> y0; n   char s[100013]; n   cin >> s; n   int a[513][513] = {}; n   int num = 0; n   for (int i = 0; s[i] !=   0 ; i++) { n     if (a[x0][y0] != 1) { n       cout <<  1  ; n       num++; n     } else { n       cout <<  0  ; n     } n     a[x0][y0] = 1; n     if (s[i] ==  U  && x0 > 1) { n       x0--; n     } n     if (s[i] ==  D  && x0 < x) { n       x0++; n     } n     if (s[i] ==  L  && y0 > 1) { n       y0--; n     } n     if (s[i] ==  R  && y0 < y) { n       y0++; n     } n   } n   cout << (x * y) - num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[40000005]; n void oku() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) a[i] = i; n   int l, k; n   for (k = 2, l = 0; k <= n; k++, l++) { n     int r = (n - 1) / k; n     a[n + l] = a[r * k + l]; n     for (; r > 0; r--) { n       a[r * k + l] = a[(r - 1) * k + l]; n     } n   } n   for (int i = l; i < l + n; i++) printf( %d  , a[i] + 1); n   cout << endl; n } n void oku2() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) a[i] = i; n   for (int i = 2; i <= n; i++) { n     for (int k = 0; i * k < n; k++) n       for (int j = i * k; j + 1 < min(n, i * (k + 1)); j++) n         swap(a[j], a[j + 1]); n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout << endl; n } n int main() { n   oku(); n   return 0; n } n


#include <bits/stdc++.h> n const int maxN = 1000000; n char in[maxN + 1]; n int occ[10], instanceOfRest[7] = {8169, 8961, 8619, 6891, 8691, 9168, 8196}; n int main() { n   scanf( %s , in); n   int s = std::strlen(in); n   for (int i = 0; i < s; i++) occ[in[i] -  0 ]++; n   occ[1]--, occ[6]--, occ[8]--, occ[9]--; n   int powerOf10 = 1, res = 0, occOfZero = occ[0]; n   occ[0] = 4; n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < occ[i]; j++) { n       res = (res + (i % 7) * powerOf10) % 7; n       powerOf10 = (powerOf10 * 3) % 7; n     } n   } n   for (int i = 9; i > 0; i--) n     for (int j = 0; j < occ[i]; j++) printf( %d , i); n   printf( %d , instanceOfRest[(7 - res) % 7]); n   while (occOfZero--) printf( 0 ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int b[200100]; n  n int l[200100], r[200100]; n  n int main(){ n     int T; scanf( %d , &T); n     int tot = T; n     int num = 0; n     while (T--){ n         num++; n         int N, K, M; scanf( %d%d%d , &N, &K, &M); n         for (int i = 1; i <= M; i++) scanf( %d , &b[i]); n         int ind = 1; n         int cnt = 0; n         for (int i = 1; i <= N; i++){ n             l[i] = 0; n             if (ind == M+1 || b[ind] != i){ n                 cnt++; n             } else { n                 ind++; n                 l[i] = cnt; n             } n         } n         if (cnt%(K-1) != 0){ n             printf( NO n ); n             continue; n         } n         bool work = false; n         for (int i = 1; i <= N; i++){ n             if (l[i] >= K/2 && cnt-l[i] >= K/2){ n                 work = true; n                 break; n             } n         } n         if (work) printf( YES n ); n         else printf( NO n ); n     } n }


#include <bits/stdc++.h> n using namespace std; n enum faces { n   Tetrahedron = 4, n   Cube = 6, n   Octahedron = 8, n   Dodecahedron = 12, n   Icosahedron = 20 n }; n int main() { n   int n; n   cin >> n; n   long long total = 0; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     if (s ==  Tetrahedron ) n       total += Tetrahedron; n     else if (s ==  Cube ) n       total += Cube; n     else if (s ==  Octahedron ) n       total += Octahedron; n     else if (s ==  Dodecahedron ) n       total += Dodecahedron; n     else if (s ==  Icosahedron ) n       total += Icosahedron; n   } n   cout << total; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long even[222222] = {0}, a[222222], odd[222222] = {0}, backEven[222222], n           cur, best = numeric_limits<int>::min(), besteven = 0; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) cin >> a[i]; n   even[1] = 0; n   odd[1] = 0; n   for (int i = 2; i <= n; i++) { n     even[i] = even[i - 1] + (a[i] / 2) * 2; n     if (a[i] < 2) even[i] = 0; n     odd[i] = odd[i - 1] + a[i]; n     if (a[i] % 2 == 0) odd[i]--; n   } n   backEven[n] = 0; n   for (int i = n - 1; i >= 1; i--) { n     backEven[i] = backEven[i + 1] + (a[i + 1] / 2) * 2; n     if (a[i + 1] < 2) backEven[i] = 0; n   } n   for (int j = 1; j <= n; j++) { n     cur = besteven + backEven[j] + odd[j]; n     best = max(cur, best); n     besteven = max(besteven, even[j + 1] - odd[j + 1]); n   } n   cout << best; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const int mod = 998244353; n const int inf = 0x3f3f3f3f; n vector<int> G[maxn]; n int id[maxn]; n struct SEG { n   int ma[maxn << 2]; n   SEG() { memset(ma, -1, sizeof ma); } n   void update(int rt, int pos, int l, int r, int val) { n     if (l == r) { n       ma[rt] = val; n       return; n     } n     int m = (l + r) >> 1; n     if (m >= pos) n       update(rt << 1, pos, l, m, val); n     else n       update(rt << 1 | 1, pos, m + 1, r, val); n     ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]); n   } n   int query(int rt, int L, int R, int l, int r) { n     if (L <= l && r <= R) return ma[rt]; n     int m = (l + r) >> 1; n     int ret = -1; n     if (m >= L) ret = max(ret, query(rt << 1, L, R, l, m)); n     if (m < R) ret = max(ret, query(rt << 1 | 1, L, R, m + 1, r)); n     return ret; n   } n } seg; n struct TREE { n   int dfn[maxn], siz[maxn], son[maxn], top[maxn], fa[maxn], o = 0; n   void dfs(int rt, int p) { n     fa[rt] = p; n     siz[rt] = 0; n     son[rt] = -1; n     for (int x : G[rt]) { n       if (x == p) continue; n       dfs(x, rt); n       siz[rt] += siz[x] + 1; n       if (son[rt] == -1 || siz[son[rt]] < siz[x]) son[rt] = x; n     } n   } n   void dfs2(int rt, int p, int tp) { n     dfn[rt] = ++o; n     top[rt] = tp; n     if (son[rt] != -1) dfs2(son[rt], rt, tp); n     for (int x : G[rt]) { n       if (x == p || x == son[rt]) continue; n       dfs2(x, rt, x); n     } n   } n   int query(int rt) { n     int ret = -1; n     while (rt != -1) { n       int tp = top[rt]; n       ret = max(ret, seg.query(1, dfn[tp], dfn[rt], 1, maxn - 1)); n       rt = fa[tp]; n     } n     return ret; n   } n } tree; n multiset<int> S[maxn]; n struct ACAM { n   int nxt[maxn][26], fail[maxn], o = 0; n   void insert(char* s, int x) { n     int rt = 0; n     for (int i = 0; s[i]; i++) { n       int c = s[i] -  a ; n       if (!nxt[rt][c]) nxt[rt][c] = ++o; n       rt = nxt[rt][c]; n     } n     id[x] = rt; n   } n   void build() { n     queue<int> Q; n     for (int i = 0; i < 26; i++) n       if (nxt[0][i]) { n         G[0].push_back(nxt[0][i]); n         Q.push(nxt[0][i]); n       } n     while (!Q.empty()) { n       int x = Q.front(); n       Q.pop(); n       for (int i = 0; i < 26; i++) { n         if (nxt[x][i]) { n           fail[nxt[x][i]] = nxt[fail[x]][i]; n           Q.push(nxt[x][i]); n           G[nxt[fail[x]][i]].push_back(nxt[x][i]); n         } else n           nxt[x][i] = nxt[fail[x]][i]; n       } n     } n   } n   int query(char* s) { n     int rt = 0; n     int ret = -1; n     for (int i = 0; s[i]; i++) { n       rt = nxt[rt][s[i] -  a ]; n       int tmp = tree.query(rt); n       ret = max(ret, tmp); n     } n     return ret; n   } n } ac; n char s[maxn]; n int raw[maxn]; n int main(void) { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s); n     ac.insert(s, i); n   } n   ac.build(); n   tree.dfs(0, -1); n   tree.dfs2(0, -1, 0); n   for (int i = 1; i <= n; i++) { n     int rt = id[i]; n     S[rt].insert(0); n     seg.update(1, tree.dfn[rt], 1, maxn - 1, 0); n   } n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int pos, val; n       scanf( %d%d , &pos, &val); n       int rt = id[pos]; n       S[rt].erase(S[rt].find(raw[pos])); n       raw[pos] = val; n       S[rt].insert(val); n       seg.update(1, tree.dfn[rt], 1, maxn - 1, *S[rt].rbegin()); n     } else { n       scanf( %s , s); n       int ans = ac.query(s); n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n void solve() { n   int n, m; n   cin >> n >> m; n   int mex = INT_MAX, curr = 0; n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     mex = min(mex, r - l + 1); n   } n   cout << mex <<   n ; n   for (int i = 0; i < n; i++) { n     cout << curr <<    ; n     curr++; n     if (curr == mex) curr = 0; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   ; n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int dp[100010]; n int n, m, maxx; n char str[100010]; n int main() { n   cin >> n; n   if (n & 1) { n     cout << 0 << endl; n     return 0; n   } n   scanf( %s , str + 1); n   dp[0] = 1; n   for (int i = 1; i <= n; i++) n     if (str[i] ==  ? ) { n       m++; n       maxx = min(i / 2, m); n       for (int j = maxx; j; j--) dp[j] += dp[j - 1]; n     } n   if (m < n / 2) { n     cout << 0 << endl; n     return 0; n   } n   m -= n / 2; n   while (m--) dp[n >> 1] *= 25; n   cout << dp[n >> 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> a >> b; n   int p1 = -1, p2 = -1; n   for (int i = 0; i < n; i++) { n     if (a[i] != b[i]) { n       p1 = i; n       break; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] != b[i]) { n       p2 = i; n       break; n     } n   } n   string x =   , y =   ; n   for (int i = p1; i <= p2; i++) { n     x += a[i]; n     y += b[i]; n   } n   int sol = 0; n   if (x + y.back() == x[0] + y) sol++; n   if (y[0] + x == y + x.back()) sol++; n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n pair<int, int> pa[maxn]; n int main() { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &pa[i].first); n     pa[i].second = i + 1; n   } n   sort(pa, pa + n); n   for (int i = 0; i < n; ++i) { n     int c = (a + pa[i].first - 1) / pa[i].first + i; n     if (c < n && (b + pa[c].first - 1) / pa[c].first <= n - c) { n       int d = (b + pa[c].first - 1) / pa[c].first; n       puts( Yes ); n       printf( %d %d n , c - i, d); n       printf( %d , pa[i].second); n       for (int j = i + 1; j < c; ++j) printf(  %d , pa[j].second); n       printf(  n ); n       printf( %d , pa[c].second); n       for (int j = 1; j < d; ++j) printf(  %d , pa[c + j].second); n       printf(  n ); n       return 0; n     } n   } n   swap(a, b); n   for (int i = 0; i < n; ++i) { n     int c = (a + pa[i].first - 1) / pa[i].first + i; n     if (c < n && (b + pa[c].first - 1) / pa[c].first <= n - c) { n       int d = (b + pa[c].first - 1) / pa[c].first; n       puts( Yes ); n       printf( %d %d n , d, c - i); n       printf( %d , pa[c].second); n       for (int j = 1; j < d; ++j) printf(  %d , pa[c + j].second); n       printf(  n ); n       printf( %d , pa[i].second); n       for (int j = i + 1; j < c; ++j) printf(  %d , pa[j].second); n       printf(  n ); n       return 0; n     } n   } n   puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 100005; n vector<pair<long long, char>> adj[maxn]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long u, v; n     char c; n     cin >> u >> v >> c; n     adj[u].push_back({v, c}); n     adj[v].push_back({u, c}); n   } n   vector<long long> res(2e5, 0); n   for (long long c : { R ,  B }) { n     vector<long long> vec; n     bool vis[maxn], color[maxn]; n     for (long long i = 0; i <= n; i++) vis[i] = color[i] = false; n     for (long long i = 1; i <= n; i++) { n       if (vis[i]) continue; n       vector<long long> s, t; n       queue<long long> q; n       q.push(i); n       vis[i] = true; n       while (!q.empty()) { n         long long u = q.front(); n         q.pop(); n         if (color[u]) n           s.push_back(u); n         else n           t.push_back(u); n         for (pair<long long, char> e : adj[u]) { n           if (vis[e.first]) continue; n           if (e.second == c) n             color[e.first] = !color[u]; n           else n             color[e.first] = color[u]; n           vis[e.first] = true; n           q.push(e.first); n         } n       } n       if (s.size() < t.size()) n         for (long long u : s) vec.push_back(u); n       else n         for (long long u : t) vec.push_back(u); n     } n     bool ok = true; n     for (long long u = 1; u <= n; u++) { n       for (pair<long long, char> e : adj[u]) { n         if (e.second == c) n           ok &= color[e.first] != color[u]; n         else n           ok &= color[e.first] == color[u]; n       } n     } n     if (!ok) continue; n     if (vec.size() < res.size()) res = vec; n   } n   if (res.size() == 2e5) n     cout <<  -1 n ; n   else { n     cout << res.size() <<   n ; n     for (long long i : res) cout << i <<    ; n     cout <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> ch(26); n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     ch[c -  a ]++; n   } n   if (k >= *max_element(ch.begin(), ch.end())) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


/* n #pragma GCC optimize( O2 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( avx,avx2,sse,sse2,fma ) n */ n #include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef pair<int , int> pii; n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n const int maxn = 3e6; n const ll mod = 1e9+7; n const ld PI = acos((ld)-1); n  n #define pb push_back n #define endl   n  n #define dokme(x) cout << x , exit(0) n #define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) n #define ms(x , y) memset(x , y , sizeof x) n ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);} n  n ll n , q , k; n  n  n int32_t main(){ n  migmig; n  cin >> q; n  while(q --){ n   cin >> n >> k; n   if(n - (k - 1) <= n/2){ n    for(int i = 1 ; i < k ; i ++) n     cout << 1 <<    ; n    cout << n - (k - 1) << endl; n    continue; n   } n   if(n%k == 0){ n    for(int i = 1 ; i <= k ; i ++) n     cout << n / k <<    ; n    cout << endl; n    continue; n   } n   for(int i = 1 ; i <= k - 3 ; i ++){ n    n--; n    cout << 1 <<    ; n   } n   if(n % 3 == 0){ n    cout << n/3 <<     << n/3 <<     << n/3 << endl; n    continue; n   } n   if(n % 2){ n    cout << 1 <<     << (n - 1)/2 <<     << (n - 1)/2 << endl; n    continue; n   } n   if((n - 2)%4 == 0){ n    cout << 2 <<     << (n - 2) / 2 <<     << (n - 2) / 2 << endl; n    continue; n   }   n   cout << n / 2 <<     << n/4 <<      << n/4 << endl; n  } n  return(0); n } n  n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; n int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n bool debug = false; n int main() { n   int n, L, a; n   cin >> n >> L >> a; n   int ans = 0, now = 0; n   for (int i = 0; i < n; i++) { n     int t, l; n     cin >> t >> l; n     ans += (t - now) / a; n     now = t + l; n   } n   ans += (L - now) / a; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 7; n bool pri(int a) { n   if (a == 1) return 0; n   for (int i = 2; i * i <= a; i++) { n     if (a % i == 0) { n       return 0; n     } n   } n   return 1; n } n long long n, m, t, a[200000]; n string s; n int main() { n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     if (x > y) { n       cout << x + y <<   n ; n       continue; n     } n     if (x == y) { n       cout << x <<   n ; n       continue; n     } n     if (2 * x >= y) { n       cout << (x + y) / 2 <<   n ; n       continue; n     } n     x = x * int(y / x); n     cout << (x + y) / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned short log2(unsigned short x) { n   unsigned short log = 0; n   while (x > 1) { n     log++; n     x /= 2; n   } n   return log; n } n int main() { n   unsigned short a, b, n, r; n   cin >> n >> a >> b; n   a -= 1; n   b -= 1; n   n /= 2; n   if (a / n != b / n) n     cout <<  Final!  << endl; n   else { n     r = log2(n); n     n /= 2; n     while (a / n == b / n) { n       n /= 2; n       r -= 1; n     } n     cout << r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class flow_t, const flow_t inf> n struct Dinic { n   struct edge_t { n     long long to; n     flow_t cap, flow; n     edge_t(long long to = 0, flow_t cap = 0, flow_t flow = 0) n         : to(to), cap(cap), flow(flow) {} n   }; n   long long n, s, t; n   vector<vector<long long>> g; n   vector<long long> d, ptr; n   vector<edge_t> ed; n   Dinic(long long n = 0, long long s = 0, long long t = 0) : n(n), s(s), t(t) { n     g.assign(n + 5, vector<long long>()); n     ed.clear(); n   } n   void add(long long u, long long v, long long cap) { n     g[u].push_back(ed.size()); n     ed.push_back(edge_t(v, cap, 0)); n     g[v].push_back(ed.size()); n     ed.push_back(edge_t(u, 0, 0)); n   } n   bool bfs() { n     d.assign(n + 5, -1); n     ptr.assign(n + 5, 0); n     queue<long long> q; n     d[s] = 0; n     q.push(s); n     while (!q.empty()) { n       long long u = q.front(); n       q.pop(); n       for (long long id : g[u]) { n         long long v = ed[id].to; n         if (d[v] == -1 && ed[id].cap > ed[id].flow) { n           d[v] = d[u] + 1; n           q.push(v); n         } n       } n     } n     return d[t] != -1; n   } n   flow_t dfs(long long u, flow_t flow) { n     if (u == t || !flow) return flow; n     for (long long& i = ptr[u]; i < (long long)g[u].size(); ++i) { n       long long id = g[u][i]; n       long long v = ed[id].to; n       if (d[v] != d[u] + 1 || ed[id].cap <= ed[id].flow) continue; n       flow_t nxt = dfs(v, min(flow, ed[id].cap - ed[id].flow)); n       if (nxt) { n         ed[id].flow += nxt; n         ed[id ^ 1].flow -= nxt; n         return nxt; n       } n     } n     return 0; n   } n   flow_t max_flow() { n     while (bfs()) n       while (dfs(s, inf)) n         ; n     flow_t ret = 0; n     for (long long id : g[s]) ret += ed[id].flow; n     return ret; n   } n   vector<long long> path; n   flow_t trace(long long u) { n     path.push_back(u); n     if (u == t) return 1; n     for (long long id : g[u]) { n       long long v = ed[id].to; n       if (ed[id].flow > 0) { n         ed[id].flow--; n         trace(v); n         return 1; n       } n     } n     return 0; n   } n   vector<vector<long long>> solve() { n     vector<vector<long long>> ret; n     while (true) { n       path.clear(); n       if (trace(s)) n         ret.push_back(path); n       else n         break; n     } n     return ret; n   } n }; n signed main() { n   ios_base::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   long long s = 0, t = n + m + 1; n   Dinic<long long, (long long)1e18> dn(n + m, s, t); n   for (long long i = 1; i <= n; ++i) { n     long long x; n     cin >> x; n     dn.add(i, t, x); n   } n   long long sum = 0; n   for (long long i = 1; i <= m; ++i) { n     long long u, v; n     long long w; n     cin >> u >> v >> w; n     dn.add(n + i, u, 1e18); n     dn.add(n + i, v, 1e18); n     dn.add(s, n + i, w); n     sum += w; n   } n   cout << sum - dn.max_flow() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class _Tp> n _Tp gcd(_Tp a, _Tp b) { n   return (b == 0) ? (a) : (gcd(b, a % b)); n } n const long long Inf = 1000000000000000000ll; n const int inf = 1000000000; n char buf[1 << 25], *p1 = buf, *p2 = buf; n inline int getc() { n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 25, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int x = 0, f = 0; n   char c = getc(); n   while (!isdigit(c)) (c ==  - ) && (f = 1), c = getc(); n   while (isdigit(c)) x = x * 10 + (c & 15), c = getc(); n   return f ? -x : x; n } n const int N = 200005; n const int Lg = 19; n int dfn[N], fa[N][Lg], dep[N], siz[N]; n int sum[N * 4], t[N * 4], cnt; n vector<int> e[N]; n map<int, int> added[N]; n void dfs(int u, int f) { n   dfn[u] = ++cnt; n   fa[u][0] = f; n   dep[u] = dep[f] + 1; n   siz[u] = 1; n   for (int i = 0; i < e[u].size(); ++i) { n     int to = e[u][i]; n     if (to == f) continue; n     dfs(to, u); n     siz[u] += siz[to]; n   } n } n void pushup(int o, int len) { n   if (t[o]) n     sum[o] = len; n   else if (len == 1) n     sum[o] = 0; n   else n     sum[o] = sum[o << 1] + sum[o << 1 | 1]; n } n void update(int o, int l, int r, int L, int R, int val) { n   if (L <= l && r <= R) { n     t[o] += val; n     pushup(o, r - l + 1); n     return; n   } n   int mid = l + r >> 1; n   if (L <= mid) update(o << 1, l, mid, L, R, val); n   if (R > mid) update(o << 1 | 1, mid + 1, r, L, R, val); n   pushup(o, r - l + 1); n } n int n, q; n int main() { n   n = read(); n   q = read(); n   for (int i = 1; i < n; ++i) { n     int x = read(), y = read(); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs(1, 0); n   for (int w = 1; w < Lg; ++w) n     for (int i = 1; i <= n; ++i) fa[i][w] = fa[fa[i][w - 1]][w - 1]; n   while (q--) { n     int u = read(), v = read(), k; n     if (dfn[u] > dfn[v]) swap(u, v); n     if (added[u][v]) n       added[u][v] = 0, k = -1; n     else n       added[u][v] = 1, k = 1; n     if (dfn[u] + siz[u] - 1 >= dfn[v] + siz[v] - 1) { n       int x = v; n       for (int w = Lg - 1; ~w; --w) n         if (dep[fa[x][w]] > dep[u]) x = fa[x][w]; n       update(1, 1, n, dfn[x], dfn[v] - 1, k); n       update(1, 1, n, dfn[v] + siz[v], dfn[x] + siz[x] - 1, k); n     } else { n       update(1, 1, n, 1, dfn[u] - 1, k); n       update(1, 1, n, dfn[u] + siz[u], dfn[v] - 1, k); n       update(1, 1, n, dfn[v] + siz[v], n, k); n     } n     printf( %d n , n - sum[1]); 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 lim = (int)sqrt(n); n   int val; n   for (int i = 1; i <= lim; i++) { n     if (n % i == 0) val = i; n   } n   cout << val <<     << n / val <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-13; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   int k = n / m; n   vector<int> v(n); n   vector<vector<int>> d(m); n   for (int i = 0; i < (int)(n); ++i) { n     cin >> v[i]; n     d[v[i] % m].push_back(i); n   } n   long long ans = 0; n   vector<pair<int, int>> free; n   for (int i = 0; i < (int)(2 * m); ++i) { n     int cur = i % m; n     while (d[cur].size() > k) { n       free.push_back(make_pair(d[cur].back(), i)); n       d[cur].pop_back(); n     } n     while (d[cur].size() < k && !free.empty()) { n       d[cur].push_back(free.back().first); n       v[free.back().first] += i - free.back().second; n       ans += i - free.back().second; n       free.pop_back(); n     } n   } n   cout << ans <<   n ; n   for (auto x : v) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { cin >> x; } n void _R(int64_t &x) { cin >> x; } n void _R(double &x) { cin >> x; } n void _R(long double &x) { cin >> x; } n void _R(char &x) { cin >> x; } n void _R(char *x) { cin >> x; } n void _R(string &x) { cin >> x; } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { cout << x; } n void _W(const int64_t &x) { cout << x; } n void _W(const double &x) { cout << fixed << setprecision(8) << x; } n void _W(const long double &x) { cout << fixed << setprecision(16) << x; } n void _W(const char &x) { cout << x; } n void _W(const char *x) { cout << x; } n void _W(const string &x) { cout << x; } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   cout <<    ; n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) cout <<    ; n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   cout << (sizeof...(tail) ?     :   n ); n   W(tail...); n } n int n, k; n vector<int> a; n bool ok_even(int x) { n   int cnt = 0; n   for (int i = (0); i < (n); i++) { n     if ((cnt & 1)) { n       if (a[i] <= x) cnt++; n     } else { n       cnt++; n     } n     if (cnt >= k) return true; n   } n   return false; n } n bool ok_odd(int x) { n   int cnt = 0; n   for (int i = (0); i < (n); i++) { n     if ((!(cnt & 1))) { n       if (a[i] <= x) cnt++; n     } else { n       cnt++; n     } n     if (cnt >= k) return true; n   } n   return false; n } n bool ok(int x) { n   if (ok_even(x)) { n     return true; n   } else if (ok_odd(x)) { n     return true; n   } n   return false; n } n void solve() { n   R(n, k); n   a.resize(n); n   for (int i = (0); i < (n); i++) { n     R(a[i]); n   } n   int l = 0, r = 1e9 + 10; n   while (r - l > 1) { n     int mid = (r + l) / 2; n     if (ok(mid)) { n       r = mid; n     } else { n       l = mid; n     } n   } n   W(r); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int testcase = 1; testcase <= t; testcase++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9 + 7, maxn = (int)1e5 + 5; n int a[maxn], d[maxn]; n int mpow(int A, int N) { n   int ret = 1; n   while (N) { n     if (N & 1) ret = (ret * 1ll * A) % mod; n     A = (A * 1ll * A) % mod; n     N >>= 1; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.clear(); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int ans = 0; n   for (int i = maxn - 1; i > 0; i--) { n     int sz = 0; n     for (int j = 1; j * j <= i; j++) { n       if (i % j) continue; n       if (j != i) d[sz++] = j; n       if (j != 1 && j * j != i) d[sz++] = i / j; n     } n     sort(d, d + sz); n     int lim = lower_bound(a, a + n, i) - a; n     int cur = (mpow(sz + 1, n - lim) - mpow(sz, n - lim) + mod) % mod; n     for (int j = sz - 1; j >= 0; j--) { n       int idx = lower_bound(a, a + lim, d[j]) - a; n       cur = (cur * 1ll * mpow(j + 1, lim - idx)) % mod; n       lim = idx; n     } n     ans = (ans + cur) % mod; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n int cnt[1009]; n int vec[1009]; n vector<int> ans; n int main() { n   int a, b, c, d, e, i, j, k, l, n, m, x, y, t, p; n   scanf( %d , &t); n   for (p = 1; p <= t; p++) { n     scanf( %d , &n); n     s.clear(); n     ans.clear(); n     for (i = 0; i <= n; i++) { n       s.insert(i); n       cnt[i] = 0; n     } n     for (i = 1; i <= n; i++) { n       scanf( %d , &vec[i]); n       s.erase(vec[i]); n       cnt[vec[i]]++; n     } n     c = 0; n     for (i = 2; i <= n; i++) { n       if (vec[i] < vec[i - 1]) { n         c = 1; n         break; n       } n     } n     if (c) { n       while (c) { n         auto it = s.begin(); n         a = *it; n         i = a; n         if (i == 0) { n           i = 1; n           for (j = 2; j <= n; j++) { n             if (vec[j] < vec[j - 1]) { n               if (vec[j] != j) { n                 i = j; n               } else { n                 i = j - 1; n               } n               break; n             } n           } n         } n         s.erase(it); n         ans.push_back(i); n         cnt[a]++; n         cnt[vec[i]]--; n         if (cnt[vec[i]] == 0) { n           s.insert(vec[i]); n         } n         vec[i] = a; n         c = 0; n         for (i = 2; i <= n; i++) { n           if (vec[i] < vec[i - 1]) { n             c = 1; n             break; n           } n         } n       } n     } n     l = ans.size(); n     printf( %d n , l); n     for (i = 0; i < l; i++) { n       printf( %d  , ans[i]); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct abc { n   int p, q, idx; n }; n bool compareTwoStudents(abc a, abc b) { n   if (a.p != b.p) return a.p < b.p; n   return (a.q < b.q); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s, s1, s2; n   int a, b, c, x, y, z, p, q, u, v, idx; n   int i, j, k, m, n, t, flag = 0, count = 0; n   cin >> q; n   while (q--) { n     cin >> n; n     cin >> s; n     if (s.length() == 2) { n       if (s[1] <= s[0]) { n         cout <<  NO n ; n       } else { n         cout <<  YES n2 n ; n         for (i = 0; i < s.length(); i++) { n           cout << s[i]; n           if (i == 0) cout <<    ; n         } n         cout <<   n ; n       } n     } else { n       cout <<  YES n2 n ; n       for (i = 0; i < s.length(); i++) { n         cout << s[i]; n         if (i == 0) cout <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, d, e, i; n   scanf( %d %d %d %d %d , &a, &b, &c, &d, &e); n   int arr[e]; n   if (a == 1 || b == 1 || c == 1 || d == 1) n     printf( %d n , e); n   else { n     for (i = 1; i <= e; i++) { n       if (i % a == 0) n         arr[i - 1] = 1; n       else if (i % b == 0) n         arr[i - 1] = 1; n       else if (i % c == 0) n         arr[i - 1] = 1; n       else if (i % d == 0) n         arr[i - 1] = 1; n       else n         arr[i - 1] = 0; n     } n     int count = 0; n     for (i = 0; i < e; i++) { n       if (arr[i] == 1) count++; n     } n     printf( %d n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 107; n const long long mod = 1e9 + 7; n const int rx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int ry[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n const long double pi = acos(-1.0); n const int B = (int)sqrt(N); n int d[16 / 2][2 * 4]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string lada1, lada2, korol, black; n   cin >> lada1 >> lada2 >> korol >> black; n   int l1x = lada1[0] -  a , l1y = lada1[1] -  1 ; n   int l2x = lada2[0] -  a , l2y = lada2[1] -  1 ; n   int kx = korol[0] -  a , ky = korol[1] -  1 ; n   int bx = black[0] -  a , by = black[1] -  1 ; n   d[kx][ky] = 2; n   for (int i = 0; i < 4; ++i) { n     int x = l1x + rx[i], y = l1y + ry[i]; n     while (x > -1 && y > -1 && x < 16 / 2 && y < 2 * 4 && d[x][y] != 2) { n       d[x][y] = 1; n       x += rx[i], y += ry[i]; n     } n   } n   for (int i = 0; i < 4; ++i) { n     int x = l2x + rx[i], y = l2y + ry[i]; n     while (x > -1 && y > -1 && x < 16 / 2 && y < 2 * 4 && d[x][y] != 2) { n       d[x][y] = 1; n       x += rx[i], y += ry[i]; n     } n   } n   for (int i = max(0, kx - 1); i < min(8, kx + 2); ++i) n     for (int j = max(0, ky - 1); j < min(8, ky + 2); ++j) d[i][j] = 1; n   for (int i = max(0, bx - 1); i < min(8, bx + 2); ++i) n     for (int j = max(0, by - 1); j < min(8, by + 2); ++j) n       if (!d[i][j]) { n         cout <<  OTHER  <<   n ; n         return 0; n       } n   cout <<  CHECKMATE  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 30; n int num[MAX], x[MAX]; n char s[1000 + 10]; n int main() { n   int n; n   while (cin >> s >> n) { n     int m = n; n     string a; n     memset(num, 0, sizeof num); n     int len = strlen(s), sum = 0; n     for (int i = 0; i < len; ++i) { n       if (!num[s[i] -  a ]) ++sum; n       ++num[s[i] -  a ]; n     } n     if (sum > n) n       cout <<  -1 ; n     else { n       for (int i = 0; i < 26; ++i) { n         if (num[i]) { n           a += (i +  a ); n           --m; n         } n         x[i] = 1; n       } n       while (m) { n         int j = 0; n         for (int i = 0; i < 26; ++i) { n           if ((num[i] - 1) / x[i] + 1 > (num[j] - 1) / x[j] + 1) j = i; n         } n         a += (j +  a ); n         ++x[j]; n         --m; n       } n       int j = 0; n       for (int i = 0; i < 26; ++i) { n         if ((num[i] - 1) / x[i] + 1 > (num[j] - 1) / x[j] + 1) j = i; n       } n       cout << (num[j] - 1) / x[j] + 1 << endl; n       cout << a; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print_ret(const std::vector<long long>& ret) { n   long long r = 0; n   for (typeof((ret).end()) it = (ret).begin(); it != (ret).end(); ++it) n     r ^= *it; n   cout << r << endl; n   cout << ret.size() << endl; n   for (typeof((ret).end()) it = (ret).begin(); it != (ret).end(); ++it) n     cout << *it <<    ; n   cout << endl; n } n int main(int argc, const char* argv[]) { n   long long l, r, k; n   std::vector<long long> ret; n   cin >> l >> r >> k; n   if (k == 1) { n     ret.push_back(l); n   } else if (k == 2) { n   k2: n     if (r - l >= 2) { n       ret.push_back(l + 1); n       if (l & 1) n         ret.push_back(l + 2); n       else n         ret.push_back(l); n     } else { n       ret.push_back(l); n       if ((l ^ (l + 1)) < l) ret.push_back(l + 1); n     } n   } else if (k == 3) { n     long long t = l; n     while (t & (t - 1)) t &= (t - 1); n     t |= (t << 1); n     if (t <= r) { n       ret.push_back(l); n       ret.push_back(t); n       ret.push_back(l ^ t); n     } else { n       goto k2; n     } n   } else { n     for (long long t = l; t <= r; t++) { n       if (!(t & 3)) { n         if (t + 3 <= r) { n           ret.push_back(t); n           ret.push_back(t + 1); n           ret.push_back(t + 2); n           ret.push_back(t + 3); n           print_ret(ret); n           return 0; n         } n         break; n       } n     } n     std::vector<long long> vals; n     for (long long t = l; t <= r; t++) vals.push_back(t); n     long long bestval = l; n     long long bestmask = 1; n     for (int mask = 1; mask < (1 << vals.size()); mask++) { n       long long cur = 0; n       for (int i = 0; i < vals.size(); i++) { n         if (mask & (1 << i)) cur ^= vals[i]; n       } n       if (cur < bestval) { n         bestval = cur; n         bestmask = mask; n       } n     } n     for (int i = 0; i < vals.size(); i++) { n       if (bestmask & (1 << i)) ret.push_back(vals[i]); n     } n   } n   print_ret(ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(vector<pair<int, int> > &v) { n   for (int i = 0; i < v.size(); i++) cout << v[i].second <<    ; n   cout << endl; n } n int main() { n   int n; n   cin >> n; n   vector<pair<int, int> > input; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     input.push_back(make_pair(x, i + 1)); n   } n   sort(input.begin(), input.end()); n   vector<pair<int, int> > swaps; n   for (int i = 1; i < n && swaps.size() < 2; i++) { n     if (input[i - 1].first == input[i].first) { n       swaps.push_back(make_pair(i - 1, i)); n     } n   } n   if (swaps.size() < 2) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   print(input); n   swap(input[swaps[0].first], input[swaps[0].second]); n   print(input); n   swap(input[swaps[1].first], input[swaps[1].second]); n   print(input); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 200000; n struct el { n   int l, r, id; n } a[MAX_N], b[MAX_N]; n long k[MAX_N]; n class compare { n  public: n   bool operator()(el a, el b) { return a.r < b.r; } n }; n int main() { n   int n, m; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].l >> a[i].r; n     a[i].id = i; n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> b[i].l >> b[i].r >> k[i]; n     b[i].id = i; n   } n   compare cmp; n   sort(a, a + n, cmp); n   sort(b, b + m, cmp); n   int uk = 0; n   vector<int> ans(n); n   multimap<int, int> mp; n   for (int i = 0; i < m; i++) { n     while (uk < n && a[uk].r <= b[i].r) n       mp.insert(make_pair(a[uk].l, a[uk].id)), uk++; n     map<int, int>::iterator it; n     for (it = mp.lower_bound(b[i].l); it != mp.end() && k[b[i].id]; n          it = mp.lower_bound(b[i].l)) { n       ans[it->second] = b[i].id + 1; n       k[b[i].id]--; n       mp.erase(it); n     } n   } n   if (mp.empty() && uk == n) { n     cout <<  YES n ; n     copy(ans.begin(), ans.end(), ostream_iterator<int>(cout,    )); n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n int a[18] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0}; n long long n; n int main() { n   cin >> n; n   if (n == 0) { n     cout << 1 << endl; n     return 0; n   } n   int res = 0; n   while (n != 0) { n     int m = n % 16; n     n /= 16; n     res += a[m]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   cout << (n + 1) / 36 <<     << ((n + 1) / 3) % 12; 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   string s; n   cin >> s; n   int l = 0; n   int r = 0; n   vector<int> a(3); n   vector<int> b(3); n   for (int i = 0; i < 3; i++) { n     a[i] = (int)(s[i] -  0 ); n     b[i] = (int)(s[i + 3] -  0 ); n     l += a[i]; n     r += b[i]; n   } n   if (l == r) { n     cout <<  0 ; n     return 0; n   } n   int dif = abs(l - r); n   if (l > r) { n     sort(a.rbegin(), a.rend()); n     sort(b.begin(), b.end()); n     if (a[0] >= dif || (9 - b[0]) >= dif) { n       cout <<  1 ; n       return 0; n     } n     int mx = max({a[0] + (9 - b[0]), a[0] + a[1], 18 - b[0] - b[1]}); n     if (mx >= dif) { n       cout <<  2 ; n       return 0; n     } n   } else { n     sort(a.begin(), a.end()); n     sort(b.rbegin(), b.rend()); n     if (b[0] >= dif || (9 - a[0]) >= dif) { n       cout <<  1 ; n       return 0; n     } n     int mx = max({b[0] + (9 - a[0]), b[0] + b[1], 18 - a[0] - a[1]}); n     if (mx >= dif) { n       cout <<  2 ; n       return 0; n     } n   } n   cout <<  3 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1100000; n const int M = 21; n vector<int> adj[N]; n int par[N][M]; n int vis[N]; n void dfs(int u, int p) { n   par[u][0] = p; n   for (int v : adj[u]) { n     if (v == p) continue; n     dfs(v, u); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(n, 0); n   for (int i = 1; i < M; i++) n     for (int j = 1; j <= n; j++) par[j][i] = par[par[j][i - 1]][i - 1]; n   vis[n] = 1; n   int rest = n - k - 1; n   for (int i = n - 1; i >= 0; i--) { n     if (vis[i]) continue; n     int now = i; n     int cnt = 0; n     for (int j = M - 1; j >= 0; j--) { n       int cur = par[now][j]; n       if (cur == 0 || vis[cur]) continue; n       cnt += 1 << j; n       now = cur; n     } n     if (cnt + 1 > rest) continue; n     rest -= cnt + 1; n     vis[i] = 1; n     now = i; n     for (int times = 0; times < cnt; times++) { n       now = par[now][0]; n       vis[now] = 1; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pct(int x) { return __builtin_popcount(x); } n int pct(long long x) { return __builtin_popcountll(x); } n int bt(int x) { return 31 - __builtin_clz(x); } n int bt(long long x) { return 63 - __builtin_clzll(x); } n int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } n long long cdiv(long long a, long long b) { n   return a / b + !(a < 0 || a % b == 0); n } n int nxt_C(int x) { n   int c = x & -x, r = x + c; n   return (((r ^ x) >> 2) / c) | r; n } n long long nxt_C(long long x) { n   long long c = x & -x, r = x + c; n   return (((r ^ x) >> 2) / c) | r; n } n vector<int> get_bits(int mask) { n   vector<int> bb; n   while (mask) { n     int b = bt(mask); n     bb.push_back(b); n     mask ^= (1 << b); n   } n   reverse(bb.begin(), bb.end()); n   return bb; n } n int get_mask(vector<int> v) { n   int mask = 0; n   for (int x : v) { n     mask ^= (1 << x); n   } n   return mask; n } n template <typename T> n void uniq(vector<T> &v) { n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long rand(long long l, long long r) { n   uniform_int_distribution<long long> uid(l, r); n   return uid(rng); n } n void sc() {} n template <typename Head, typename... Tail> n void sc(Head &H, Tail &...T) { n   cin >> H; n   sc(T...); n } n const int mod = 1e9 + 7; n int pwr(int a, long long b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * 1LL * a) % mod; n     a = (a * 1LL * a) % mod; n     b >>= 1; n   } n   return ans; n } n const int N = 2e5 + 5; n int a[N]; n int dp[N][2]; n int n, k; n bool f(int x) { n   dp[0][0] = 0; n   dp[0][1] = 0; n   for (int i = 1; i <= n; i++) { n     dp[i][0] = dp[i][1] = 0; n   } n   for (int i = 0; i <= n - 1; i++) { n     for (int j = 0; j <= 1; j++) { n       dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       if (a[i + 1] > x && !j) continue; n       dp[i + 1][j ^ 1] = max(dp[i + 1][j ^ 1], dp[i][j] + 1); n     } n   } n   return max(dp[n][0], dp[n][1]) >= k; n } n void solve() { n   sc(n, k); n   for (int i = 1; i <= n; i++) { n     sc(a[i]); n   } n   int l = 0, r = 1e9 + 5; n   while (r - l > 1) { n     int m = (l + r) >> 1; n     if (f(m)) n       r = m; n     else n       l = m; n   } n   cout << r; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int tt = 1; tt <= t; tt++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c; n   while (cin >> n >> a >> b >> c) { n     int s = a + b + c; n     double ans = (1.0 * n / s); n     if (a == 0 && b == 0 && c == 0) n       printf( %.1lf %.1lf %.1lf n , 0.0, 0.0, 0.0); n     else n       printf( %.16f %.16f %.16f n , ans * a, ans * b, ans * c); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, dp, q, c, b; n string ans =   ; n void update(int &x) { n   if (x == 0) n     x = 1; n   else n     x = 0; n } n int main() { n   scanf( %d , &n); n   int a[n][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) scanf( %d , &a[i][j]); n   } n   for (int i = 0; i < n; i++) dp = (dp + a[i][i]) % 2; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &b); n     if (b == 3) { n       printf( %d , dp); n     } else { n       scanf( %d , &c); n       update(dp); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string DIRECTION =  NEWS ; n string s[2]; n int n, a[2][1000100], pre[1000100]; n void calcPre(int p[]) { n   int i = 0, j = -1; n   pre[0] = -1; n   while (i < n) { n     while (j >= 0 && p[i] != p[j]) j = pre[j]; n     i++; n     j++; n     pre[i] = (j >= n || p[i] != p[j] ? j : pre[j]); n   } n } n int suffixMatch(int s[], int p[]) { n   int i = 0, j = 0; n   while (i < n) { n     while (j >= 0 && s[i] != p[j]) j = pre[j]; n     i++; n     j++; n   } n   return j; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> s[0] >> s[1]; n   n--; n   for (int z = 0; z < 2; z++) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < 4; j++) n         if (s[z][i] == DIRECTION[j]) a[z][i] = j; n   reverse(a[0], a[0] + n); n   for (int i = 0; i < n; i++) a[0][i] = 3 - a[0][i]; n   calcPre(a[0]); n   cout << (suffixMatch(a[1], a[0]) ?  NO  :  YES ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int nsz = 3e5; n int n; n long long k, mod, a[nsz + 5], b[nsz + 5], ans; n map<long long, int> mp; n long long inline qpow(long long a, int p) { n   long long res = 1; n   for (; p; p >>= 1) { n     if (p & 1) { n       res *= a; n       res %= mod; n     } n     a *= a; n     a %= mod; n   } n   return res % mod; n } n long long inline modify(long long a) { n   a %= mod; n   return a < 0 ? a + mod : a; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> mod >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     ++mp[modify(qpow(a[i], 4) - k * a[i] % mod)]; n   } n   for (__typeof(mp.begin()) it = mp.begin(); it != mp.end(); ++it) { n     ans += it->second * (it->second - 1) / 2; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   int fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= fh; n } n struct Po { n   long long x, y, t; n } po[200010], b[200010], c[200010]; n struct shj { n   long long t, x, zh; n } a[200010]; n int n, l; n string an, no; n bool vi[2000010][2]; n bool c1(const shj &a, const shj &b) { return a.t < b.t; } n bool c2(const Po &a, const Po &b) { return a.t < b.t; } n void check(long long x) { n   for (int i = 1; i <= n; i++) { n     long long no = b[i].t / l; n     c[i] = b[i]; n     c[i].x -= x * no; n     c[i].t %= l; n   } n   sort(c + 1, c + n + 1, c2); n   c[n + 1].t = l; n   c[n + 1].x = x; n   no =   ; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= c[i + 1].t - c[i].t - c[i + 1].x + c[i].x; j++) n       no +=  0 ; n     for (int j = 1; j <= c[i + 1].x - c[i].x; j++) no +=  1 ; n   } n   if (an ==   ) n     an = no; n   else if (no < an) n     an = no; n } n int main() { n   read(n); n   read(l); n   for (int i = 1; i <= n; i++) { n     read(po[i].t); n     read(po[i].x); n     read(po[i].y); n   } n   po[++n] = (Po){0, 0, 0}; n   for (int i = 1; i <= n; i++) { n     a[i].t = po[i].t % l; n     a[i].x = po[i].x + po[i].y + po[i].t; n     b[i].x = (po[i].x + po[i].y + po[i].t) / 2; n     b[i].t = po[i].t; n     if (a[i].x % 2 == 1 || a[i].x < 0) { n       puts( NO ); n       return 0; n     } else n       a[i].x /= 2; n     a[i].zh = po[i].t / l; n   } n   a[n + 1] = (shj){l, 0, -1}; n   sort(a + 1, a + n + 2, c1); n   long long nl = 0, nr = l; n   for (int i = 1; i <= n; i++) { n     long long x = a[i + 1].t - a[i].t, y = a[i + 1].zh - a[i].zh, n               z = a[i + 1].x - a[i].x; n     if (y > 0) { n       if ((z - x) % y == 0) n         nl = max(nl, (z - x) / y); n       else n         nl = max(nl, (z - x) / y + 1); n       nr = min(nr, z / y); n     } n     if (y < 0) { n       z = -z; n       y = -y; n       if (z % y == 0) n         nl = max(nl, z / y); n       else n         nl = max(nl, z / y + 1); n       nr = min(nr, (z + x) / y); n     } n   } n   if (nl > nr) { n     puts( NO ); n     return 0; n   } n   an =   ; n   check(nl); n   check(nr); n   for (int i = 1; i <= l; i++) vi[i][0] = an[i - 1] -  0 ; n   for (int i = 1; i <= n; i++) { n     a[i].t = po[i].t % l; n     a[i].x = po[i].y - po[i].x + po[i].t; n     b[i].x = (po[i].y - po[i].x + po[i].t) / 2; n     b[i].t = po[i].t; n     if (a[i].x % 2 == 1 || a[i].x < 0) { n       puts( NO ); n       return 0; n     } else n       a[i].x /= 2; n     a[i].zh = po[i].t / l; n   } n   a[n + 1] = (shj){l, 0, -1}; n   sort(a + 1, a + n + 2, c1); n   nl = 0, nr = l; n   for (int i = 1; i <= n; i++) { n     long long x = a[i + 1].t - a[i].t, y = a[i + 1].zh - a[i].zh, n               z = a[i + 1].x - a[i].x; n     if (y > 0) { n       if ((z - x) % y == 0) n         nl = max(nl, (z - x) / y); n       else n         nl = max(nl, (z - x) / y + 1); n       nr = min(nr, z / y); n     } n     if (y < 0) { n       z = -z; n       y = -y; n       if (z % y == 0) n         nl = max(nl, z / y); n       else n         nl = max(nl, z / y + 1); n       nr = min(nr, (z + x) / y); n     } n   } n   if (nl > nr) { n     puts( NO ); n     return 0; n   } n   an =   ; n   check(nl); n   check(nr); n   for (int i = 1; i <= l; i++) vi[i][1] = an[i - 1] -  0 ; n   for (int i = 1; i <= l; i++) printf( %c ,  DLRU [2 * vi[i][0] + vi[i][1]]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 255; n int cnt; n int n, a, b, k, f; n map<string, int> M; n map<pair<int, int>, int> Edge; n int main() { n   scanf( %d %d %d %d %d , &n, &a, &b, &k, &f); n   int last = 0; n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     string aa, bb; n     cin >> aa >> bb; n     if (!M.count(aa)) M[aa] = ++cnt; n     if (!M.count(bb)) M[bb] = ++cnt; n     int x = M[aa], y = M[bb]; n     Edge[{min(x, y), max(x, y)}] += last == x ? b : a; n     res += last == x ? b : a; n     last = y; n   } n   priority_queue<int> Q; n   for (auto v : Edge) Q.push(v.second); n   while (!Q.empty() && k > 0) { n     int xd = Q.top(); n     Q.pop(); n     if (xd <= f) break; n     --k; n     res += f - xd; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1; n int n, sol, counter; n bool mark[20]; n void check(long long x) { n   if (x > n) return; n   if (x != 0) sol++; n   for (int i = 0; i <= 9; i++) { n     if (mark[i]) { n       if (i != 0 || (i == 0 && x != 0)) check(x * 10 + i); n     } else { n       if (counter < 2) { n         counter++; n         mark[i] = true; n         if (i != 0 || (i == 0 && x != 0)) check(x * 10 + i); n         mark[i] = false; n         counter--; n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   sol = 0; n   counter = 0; n   for (int i = 0; i <= 9; i++) mark[i] = false; n   long long x = 0; n   check(x); n   printf( %d n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using ii = pair<int, int>; n using ld = long double; n const int N = 5e5; n bool flip[N], still[N]; n int par[N], asdjashsgggg[N], x[N]; n int find(int v, bool& b) { n   b = flip[v]; n   while (par[v] != v) { n     v = par[v]; n     b ^= flip[v]; n   } n   return v; n } n tuple<int, int, bool, bool> merge(int u, int v) { n   bool bu, bv; n   u = find(u, bu); n   v = find(v, bv); n   if (u == v) { n     if (bu == bv) n       throw true; n     else n       throw false; n   } else { n     int from = u, to = v; n     bool flipped = bu == bv, delta = 0; n     if (asdjashsgggg[u] == asdjashsgggg[v]) n       delta = 1; n     else if (asdjashsgggg[to] < asdjashsgggg[from]) n       swap(from, to); n     par[from] = to; n     flip[from] ^= flipped; n     asdjashsgggg[to] += delta; n     return {from, to, flipped, delta}; n   } n } n int main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(false); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n     x[i]--; n   } n   iota(par, par + n, 0); n   fill(still, still + k, true); n   vector<ii> e; n   while (m--) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     if (x[u] == x[v] && still[x[u]]) { n       try { n         merge(u, v); n       } catch (bool oc) { n         if (oc) still[x[u]] = false; n       } n     } n     e.emplace_back(u, v); n   } n   int cnt = accumulate(still, still + k, 0); n   sort(e.begin(), e.end(), [&](ii a, ii b) { n     a = minmax(x[a.first], x[a.second]); n     b = minmax(x[b.first], x[b.second]); n     return a < b; n   }); n   ii pre = {-1, -1}; n   stack<tuple<int, int, bool, bool>> s; n   bool bad = false; n   long long ans = (long long)cnt * (cnt - 1) >> 1; n   for (auto [u, v] : e) { n     ii cur = minmax(x[u], x[v]); n     if (cur.first == cur.second || !still[cur.first] || !still[cur.second]) n       continue; n     if (pre < cur) { n       while (!s.empty()) { n         auto [from, to, flipped, delta] = s.top(); n         s.pop(); n         par[from] = from; n         flip[from] ^= flipped; n         asdjashsgggg[to] -= delta; n       } n       pre = cur; n       ans -= bad; n       bad = false; n     } n     if (bad) continue; n     try { n       s.push(merge(u, v)); n     } catch (bool oc) { n       if (oc) bad = true; n     } n   } n   ans -= bad; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dfn[200002], low[200002], tot, head[200002], num, f[200002]; n int p[20][200002]; n int deep[200002], n, m, q, st[200002], top, dep[200002]; n bool vis[200002]; n inline long long rd() { n   long long x = 0; n   char c = getchar(); n   bool f = 0; n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   return f ? -x : x; n } n struct edge { n   int n, to; n } e[200002]; n inline void add(int u, int v) { n   e[++tot].n = head[u]; n   e[tot].to = v; n   head[u] = tot; n } n vector<int> vec[200002]; n void tarjan(int u, int fa) { n   dfn[u] = low[u] = ++dfn[0]; n   st[++top] = u; n   vis[u] = 1; n   for (vector<int>::iterator it = vec[u].begin(); it != vec[u].end(); ++it) { n     int v = *it; n     if (v == fa) continue; n     if (!dfn[v]) { n       tarjan(v, u); n       low[u] = min(low[u], low[v]); n       if (low[v] == dfn[u]) { n         ++num; n         add(u, num); n         int x = 0; n         while (x != v) { n           x = st[top]; n           add(num, x); n           vis[x] = 0; n           top--; n         } n       } n       if (low[v] > dfn[u]) { n         add(u, v); n         vis[st[top]] = 0; n         top--; n       } n     } else if (vis[v]) n       low[u] = min(low[u], dfn[v]); n   } n } n void dfs(int u, int fa) { n   for (int i = 1; (1 << i) <= deep[u]; ++i) p[i][u] = p[i - 1][p[i - 1][u]]; n   for (int i = head[u]; i; i = e[i].n) n     if (e[i].to != fa) { n       int v = e[i].to; n       f[v] = u; n       deep[v] = deep[u] + 1; n       p[0][v] = u; n       dep[v] = dep[u] + (v > n); n       dfs(v, u); n     } n } n inline int getlca(int u, int v) { n   if (deep[u] < deep[v]) swap(u, v); n   for (int i = 19; i >= 0; --i) n     if (deep[u] - (1 << i) >= deep[v]) u = p[i][u]; n   if (u == v) return u; n   for (int i = 19; i >= 0; --i) n     if (p[i][u] != p[i][v]) u = p[i][u], v = p[i][v]; n   return p[0][u]; n } n inline int dist(int u, int v) { n   int lca = getlca(u, v); n   return deep[u] + deep[v] - 2 * deep[lca] - n          (dep[u] + dep[v] - dep[lca] - dep[f[lca]]); n } n int main() { n   n = rd(); n   m = rd(); n   q = rd(); n   int u, v; n   for (int i = 1; i <= m; ++i) { n     u = rd(); n     v = rd(); n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   num = n; n   tarjan(1, 0); n   dfs(1, 0); n   while (q--) { n     u = rd(); n     v = rd(); n     printf( %d n , dist(u, v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt_list, pos[20200], pa[20100], len[20100], fs[20100], pc[20100], n     ls[20100], cnt_ls, tmp[20100], cnt, n, num_q; n struct pp { n   int id; n   char ch; n }; n vector<pp> adj[20201]; n pp now; n void dfs(int id, int pr) { n   int i, j, s, p, q, ip; n   pos[id] = cnt_list++; n   pa[pos[id]] = pr; n   for (i = 0; i < adj[id].size(); i++) { n     ip = adj[id][i].id; n     if (pos[ip] >= 0) continue; n     pc[cnt_list] = adj[id][i].ch -  a ; n     dfs(ip, pos[id]); n   } n } n int main() { n   int id1, id2, i, j, s, p, q, x, y, fg; n   char ch; n   scanf( %d%d , &n, &num_q); n   for (i = 0; i < n - 1; i++) { n     scanf( %d%d , &id1, &id2); n     ch = getchar(); n     while (ch <  a  || ch >  z ) ch = getchar(); n     id1--; n     id2--; n     now.id = id2; n     now.ch = ch; n     adj[id1].push_back(now); n     now.id = id1; n     now.ch = ch; n     adj[id2].push_back(now); n   } n   memset(pos, -1, sizeof(pos)); n   dfs(0, -1); n   while (num_q--) { n     scanf( %d%d , &x, &y); n     x--; n     y--; n     x = pos[x]; n     y = pos[y]; n     int u, v; n     u = x; n     v = y; n     cnt_ls = cnt = 0; n     while (u != v) { n       if (u > v) { n         ls[cnt_ls++] = pc[u]; n         u = pa[u]; n       } else { n         tmp[cnt++] = pc[v]; n         v = pa[v]; n       } n     } n     while (cnt > 0) ls[cnt_ls++] = tmp[--cnt]; n     for (i = 0; i < n; i++) len[i] = -1; n     u = x; n     fs[u] = len[u] = 0; n     int ans = 0; n     while (u != 0) { n       len[pa[u]] = len[u] + 1; n       if (fs[u] == 0) { n         if (len[u] < cnt_ls) { n           if (ls[len[u]] > pc[u]) n             fs[pa[u]] = -1; n           else if (ls[len[u]] < pc[u]) n             fs[pa[u]] = 1; n           else n             fs[pa[u]] = 0; n         } else n           fs[pa[u]] = 1; n       } else n         fs[pa[u]] = fs[u]; n       if (fs[pa[u]] < 0 || (fs[pa[u]] == 0 && len[pa[u]] != cnt_ls)) ans++; n       u = pa[u]; n     } n     for (i = 1; i < n; i++) { n       if (len[pa[i]] >= 0 && len[i] < 0) { n         len[i] = len[pa[i]] + 1; n         if (fs[pa[i]] == 0) { n           if (len[pa[i]] < cnt_ls) { n             if (ls[len[pa[i]]] > pc[i]) n               fs[i] = -1; n             else if (ls[len[pa[i]]] < pc[i]) n               fs[i] = 1; n             else n               fs[i] = 0; n           } else n             fs[i] = 1; n         } else n           fs[i] = fs[pa[i]]; n         if (fs[i] < 0 || (fs[i] == 0 && len[i] != cnt_ls)) ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n #pragma GCC optimize( -ffloat-store ) n using namespace std; n const auto start_time = std::chrono::high_resolution_clock::now(); n void aryanc403() {} n const long long int INF = 0xFFFFFFFFFFFFFFFL; n long long int seed; n mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rnd(long long int l = 0, long long int r = INF) { n   return uniform_int_distribution<long long int>(l, r)(rng); n } n class CMP { n  public: n   bool operator()(pair<long long int, long long int> a, n                   pair<long long int, long long int> b) { n     return !(a.first < b.first || (a.first == b.first && a.second <= b.second)); n   } n }; n void add(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt == m.end()) n     m.insert({x, cnt}); n   else n     jt->second += cnt; n } n void del(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt->second <= cnt) n     m.erase(jt); n   else n     jt->second -= cnt; n } n bool cmp(const pair<long long int, long long int> &a, n          const pair<long long int, long long int> &b) { n   return a.first < b.first || (a.first == b.first && a.second < b.second); n } n const long long int mod = 1000000007L; n const long long int L = 38; n long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y; n long long int m; n string s; n vector<long long int> a; n vector<pair<long long int, pair<long long int, long long int>>> hashes, n     prefHash; n long long int getHash() { n   long long int a = 0; n   for (long long int i = 0; i < L; ++i) a = a * 3 + rnd(0, 2); n   return a; n } n void getVec(vector<long long int> &a, long long int n) { n   for (long long int i = 0; i < L; ++i) { n     a.push_back(n % 3); n     n /= 3; n   } n } n long long int getXOR(const long long int &a1, const long long int &b1) { n   vector<long long int> a, b; n   getVec(a, a1); n   reverse(begin(a), end(a)); n   getVec(b, b1); n   reverse(begin(b), end(b)); n   long long int ans = 0; n   for (long long int i = 0; i < L; ++i) ans = ans * 3 + ((a[i] + b[i]) % 3); n   return ans; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   { n     cin >> n; n     hashes.resize(n + 1); n     for (auto &v : hashes) { n       v.first = getHash(); n       v.second.first = getHash(); n       v.second.second = getHash(); n     } n     a.clear(); n     a.reserve(n + 1); n     prefHash.resize(n + 1); n     a.push_back(0); n     prefHash[0] = hashes[0]; n     for (i = (1); i <= (n); ++i) { n       cin >> in; n       a.push_back(in); n       prefHash[i].first = getXOR(prefHash[i - 1].first, hashes[in].first); n       prefHash[i].second.first = n           getXOR(prefHash[i - 1].second.first, hashes[in].second.first); n       prefHash[i].second.second = n           getXOR(prefHash[i - 1].second.second, hashes[in].second.second); n     } n     vector<long long int> freq(n + 1, 0); n     map<pair<long long int, pair<long long int, long long int>>, long long int> n         hval; n     long long int l = 0; n     long long int ans = 0; n     hval[prefHash[0]]++; n     for (long long int i = 1; i <= n; ++i) { n       freq[a[i]]++; n       while (l < i && freq[a[i]] > 3) { n         hval[prefHash[l]]--; n         l++; n         freq[a[l]]--; n       } n       ans += hval[prefHash[i]]; n       hval[prefHash[i]]++; n     } n     cout << ans <<   n ; n   } n   aryanc403(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const string nl =   n ; n long long n, k, hld = 0, sum, hld1 = 0; n vector<long long> a; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   cin >> n >> k; n   a.resize(n); n   for (auto& i : a) { n     cin >> i; n   } n   for (long long i = 0; i < n; ++i) { n     sum += a[i]; n     hld = max(hld, sum); n     hld1 = min(hld1, sum); n   } n   cout << max(0LL, k - (hld - hld1 - 1)) << nl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, i; n   cin >> t; n   for (i = 1; i <= t; i++) { n     cin >> n; n     if (n % 4 == 0) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int ms = 200200; n const int eps =  z  -  a  + 1; n void print(int x) { n   for (int i = 0; i < eps; i++) { n     std::cout << ((x & (1 << i)) ? 1 : 0); n   } n   std::cout <<   n ; n } n char t[200200]; n std::string str[200200]; n int main() { n   int n; n   std::cin >> n; n   int isnt = 0; n   int know = (1 << eps) - 1; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     std::cin >> t[i] >> str[i]; n   } n   for (int i = 0; i < n; i++) { n     int tot = (~isnt & know); n     bool hmm = tot - (tot & -tot) == 0; n     int mask = 0; n     for (auto a : str[i]) { n       mask |= 1 << (a -  a ); n     } n     if (t[i] ==  ! ) { n       if (hmm) { n         ans++; n       } n       know &= mask; n     } else if (t[i] ==  . ) { n       isnt |= mask; n     } else { n       if (hmm && (1 << (str[i][0] -  a )) != tot) { n         ans++; n       } n       if (!hmm && str[i][0] != str[n - 1][0]) { n         isnt |= 1 << (str[i][0] -  a ); n       } n     } n   } n   std::cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n int N, M; n int a[110]; n int b[110]; n int sum[110]; n int num[110][30]; n char ans[110]; n void solve(int n) { n   string temp; n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   memset(sum, 0, sizeof(sum)); n   cout <<  ? 1   << n << endl; n   cout.flush(); n   for (int i = n * (n + 1) / 2; i > 0; i--) { n     cin >> temp; n     for (int j = 0; j < temp.size(); j++) { n       a[temp.size()] += temp[j] -  a ; n     } n   } n   cout <<  ? 1   << n - 1 << endl; n   cout.flush(); n   for (int i = n * (n - 1) / 2; i > 0; i--) { n     cin >> temp; n     for (int j = 0; j < temp.size(); j++) { n       b[temp.size()] += temp[j] -  a ; n     } n   } n   for (int i = 1; i <= n; i++) { n     sum[i] = a[i] - b[i]; n     ans[n - i] = sum[i] - sum[i - 1] +  a ; n   } n } n int main() { n   string temp; n   int limit; n   int now; n   while (~scanf( %d , &N)) { n     if (N <= 3) { n       char c; n       for (int i = 1; i <= N; i++) { n         cout <<  ?   << i <<     << i << endl; n         cout.flush(); n         cin >> c; n         ans[i - 1] = c; n       } n     } else { n       memset(num, 0, sizeof(num)); n       limit = (N + 1) >> 1; n       solve(limit); n       cout <<  ?   << 1 <<     << N << endl; n       cout.flush(); n       for (int i = N * (N + 1) / 2; i > 0; i--) { n         cin >> temp; n         for (int j = 0; j < temp.size(); j++) { n           num[temp.size()][temp[j] -  a ]++; n         } n       } n       for (int i = N; i >= limit; i--) { n         for (int j = 1; j <= min(N - i + 1, limit); j++) { n           now = j; n           num[i][ans[j - 1] -  a ] -= now; n         } n         for (int j = now + 1; j <= limit; j++) { n           num[i][ans[j - 1] -  a ] -= now; n         } n       } n       for (int i = N; i > limit; i--) { n         for (int j = 0; j < 30; j++) { n           if (num[i][j] / (N - i + 1) * (N - i + 2) != num[i - 1][j]) { n             ans[i - 1] = j +  a ; n             for (int k = i - 1; k >= limit; k--) { n               num[k][j] -= N - i + 1; n             } n             break; n           } n         } n       } n     } n     ans[N] =   0 ; n     printf( ! %s n , ans); n     cout.flush(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n vector<string> getWords(string s) { n   string cur =   ; n   vector<string> vec; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==    ) { n       vec.push_back(cur); n       cur =   ; n     } else n       cur += s[i]; n   } n   vec.push_back(cur); n   return vec; n } n int dist[213456], sz[213456]; n vector<vector<int> > vec; n void dfs(int n, int p) { n   sz[n] = 1; n   for (auto x : vec[n]) { n     if (x != p) { n       dist[x] = dist[n] + 1; n       dfs(x, n); n       sz[n] += sz[x]; n     } n   } n } n int main() { n   int n, k; n   cin >> n >> k; n   vec.resize(n + 1); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   dfs(1, 0); n   vector<long long> vec1; n   for (int i = 2; i <= n; i++) { n     int val = dist[i] - (sz[i] - 1); n     vec1.push_back(val); n   } n   sort(vec1.begin(), vec1.end()); n   reverse(vec1.begin(), vec1.end()); n   long long ans = 0; n   for (int i = 0; i < k; i++) ans += vec1[i]; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, r, c; n vector<string> grip; n int main() { n   grip.clear(); n   cin >> r >> c >> n >> k; n   string ful =   ; n   for (int i = 0; i < c; i++) ful +=  . ; n   for (int i = 0; i < r; i++) grip.push_back(ful); n   for (int i = 0; i < n; i++) { n     int y, x; n     cin >> y >> x; n     y--, x--; n     grip[y][x] =  # ; n   } n   int ans = 0; n   for (int il = 0; il < r; il++) { n     for (int ir = il; ir < r; ir++) { n       for (int jl = 0; jl < c; jl++) { n         for (int jr = jl; jr < c; jr++) { n           int con = 0; n           for (int p = il; p <= ir; p++) { n             for (int q = jl; q <= jr; q++) n               if (grip[p][q] ==  # ) con++; n           } n           if (con >= k) ans++; n         } n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int h[N], nex[N << 1], to[N << 1], tot, dis[N]; n void add(int x, int y) { n   to[++tot] = y; n   nex[tot] = h[x]; n   h[x] = tot; n } n int fa[N][30], dep[N], lg[N]; n void dfs(int u, int fath, int d) { n   dis[u] = d; n   fa[u][0] = fath; n   dep[u] = dep[fath] + 1; n   for (int i = 1; i <= lg[dep[u]]; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   for (int j = h[u]; j; j = nex[j]) n     if (to[j] != fath) dfs(to[j], u, d + 1); n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   while (dep[x] > dep[y]) x = fa[x][lg[dep[x] - dep[y]] - 1]; n   if (x == y) return x; n   for (int i = lg[dep[x] - 1]; i >= 0; i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n   } n   for (int i = 1; i <= n; i++) lg[i] = lg[i - 1] + (1 << lg[i - 1] == i); n   dfs(1, 0, 0); n   int q; n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int x, y, a, b, k; n     scanf( %d%d%d%d%d , &x, &y, &a, &b, &k); n     int lcab = lca(a, b); n     int d1 = dis[a] + dis[b] - 2 * dis[lcab]; n     if (k >= d1 && (k % 2) == (d1 % 2)) { n       printf( Yes n ); n       continue; n     } n     int lcax = lca(a, x); n     int lcay = lca(a, y); n     int lcby = lca(b, y); n     int lcbx = lca(b, x); n     int d2 = dis[x] + dis[a] - 2 * dis[lcax]; n     int d3 = dis[a] + dis[y] - 2 * dis[lcay]; n     int d4 = dis[b] + dis[y] - 2 * dis[lcby]; n     int d5 = dis[b] + dis[x] - 2 * dis[lcbx]; n     if (k >= (d2 + 1 + d4) && (k % 2) == ((d2 + 1 + d4) % 2)) { n       printf( Yes n ); n     } else if (k >= (d3 + 1 + d5) && (k % 2) == ((d3 + 1 + d5) % 2)) { n       printf( Yes n ); n     } else { n       printf( No n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const long long M = 1000000007; n long long a; n int main() { n   scanf( %I64d , &a); n   a %= 360; n   a += 360; n   long long ans = 0, mx = -1; n   for (int i = 0; i < 4; i++) { n     if (mx < abs((a + i * 270) % 360 - 180)) { n       mx = abs((a + i * 270) % 360 - 180); n       ans = i; n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int pos[100005]; n pair<int, int> tree[100005]; n bool func(pair<int, int> x, pair<int, int> y) { return (x.first) < (y.first); } n int main() { n   int n; n   for (int i = 1; i < 100005; i++) { n     pos[i] = 2e9 + 1000; n   } n   pos[0] = -2e9; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &tree[i].first, &tree[i].second); n   } n   sort(tree, tree + n, func); n   int mx = 0, g; n   for (int i = 0; i < n; i++) { n     if (i != 0 && tree[i].first - tree[i].second <= tree[i - 1].first) { n     } else { n       g = lower_bound(pos, pos + n, tree[i].first - tree[i].second) - pos; n       pos[g] = min(pos[g], tree[i].first); n       mx = max(mx, g); n     } n     if (i != n - 1 && tree[i].first + tree[i].second >= tree[i + 1].first) { n     } else { n       g = lower_bound(pos, pos + n, tree[i].first) - pos; n       pos[g] = min(pos[g], tree[i].first + tree[i].second); n       mx = max(mx, g); n     } n   } n   printf( %d , mx); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<char> s(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   vector<char> t(n); n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) { n       continue; n     } else { n       char target = t[i]; n       for (int j = n - 2; j >= i; j--) { n         if (s[j + 1] == target) { n           swap(s[j], s[j + 1]); n           ans.push_back(j + 1); n         } n       } n       if (s[i] != t[i]) { n         cout << -1; n         return 0; n       } n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[200], n, c = 0; n   double sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   sort(a, a + n); n   while (double(sum / n) < 4.5) { n     sum += (5 - a[c]); n     c++; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buffer[2000020], *p1, *p2; n template <class T> n void read(T& x) { n   char ch = ((p1 == p2 && n               (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n                  ? EOF n                  : *p1++); n   x = 0; n   bool f = 1; n   while (!( 0  <= ch && ch <=  9 ) && ch !=  - ) n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   if (ch ==  - ) n     f = 0, n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   while ( 0  <= ch && ch <=  9 ) n     x = (x << 1) + (x << 3) + ch -  0 , n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   x = (f) ? x : -x; n } n int _num[20]; n template <class T> n void write(T x) { n   if (!x) { n     putchar( 0 ), putchar(  n ); n     return; n   } n   if (x < 0) putchar( - ), x = -x; n   int c = 0; n   while (x) _num[++c] = x % 10, x /= 10; n   while (c) putchar( 0  + _num[c--]); n   putchar(  n ); n } n int bc[128]; n int masks[] = {119, 18, 93, 91, 58, 107, 111, 82, 127, 123}; n int a[2020], res[2020]; n int n, k; n bool vis[2020][2020], dp[2020][2020]; n bool DP(int pos, int rest) { n   if (pos > n) return rest == 0; n   if (vis[pos][rest]) return dp[pos][rest]; n   vis[pos][rest] = 1; n   bool& ans = dp[pos][rest]; n   ans = false; n   for (register int i = 9; i >= 0; i--) { n     if ((a[pos] | masks[i]) != masks[i]) continue; n     if (rest < bc[masks[i] ^ a[pos]]) continue; n     if (DP(pos + 1, rest - bc[masks[i] ^ a[pos]])) { n       res[pos] = i; n       return ans = true; n     } n   } n   return ans; n } n int main() { n   bc[0] = 0; n   for (register int i = 1; i <= 127; i++) bc[i] = bc[i >> 1] + (i & 1); n   read(n), read(k); n   for (register int i = 1; i <= n; i++) { n     char ch = n         ((p1 == p2 && n           (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n              ? EOF n              : *p1++); n     while (ch !=  0  && ch !=  1 ) n       ch = ((p1 == p2 && n              (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n                 ? EOF n                 : *p1++); n     while (ch ==  0  || ch ==  1 ) n       a[i] = (a[i] << 1) + ch -  0 , n       ch = ((p1 == p2 && n              (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) n                 ? EOF n                 : *p1++); n   } n   if (!DP(1, k)) n     printf( -1 ); n   else n     for (register 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 maxN = 100010; n int n, parent[maxN], child[maxN]; n long long f[maxN], g[maxN], res; n struct ioi { n   int v, w; n }; n vector<ioi> adj[maxN]; n void dfs(int u) { n   child[u] = 1; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i].v; n     if (parent[u] == v) continue; n     parent[v] = u; n     dfs(v); n     child[u] = child[u] + child[v]; n     f[u] += ((adj[u][i].w) ? child[v] : f[v]); n   } n } n void dfs_g(int u) { n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i].v; n     if (parent[u] == v) continue; n     int tmp = ((adj[u][i].w) ? child[v] : f[v]); n     g[v] = (adj[u][i].w) ? (n - child[v]) : (f[u] + g[u] - tmp); n     dfs_g(v); n   } n } n bool check(int x) { n   while (x) { n     if (((x % 10) != 4) && ((x % 10) != 7)) return false; n     x /= 10; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n - 1; ++i) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     w = check(w); n     ioi tmp; n     tmp.v = v; n     tmp.w = w; n     adj[u].push_back(tmp); n     tmp.v = u; n     tmp.w = w; n     adj[v].push_back(tmp); n   } n   dfs(1); n   dfs_g(1); n   for (int i = 1; i <= n; ++i) res += ((f[i] + g[i]) * (f[i] + g[i] - 1)); n   cout << res; n   fclose(stdin); n   fclose(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ans = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     ans = (ans << 1) + (ans << 3) + (ch ^ 48), ch = getchar(); n   return ans; n } n inline long long L_read() { n   long long ans = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     ans = (ans << 1) + (ans << 3) + (ch ^ 48), ch = getchar(); n   return ans; n } n int dx[5] = {0, -1, 1, 0, 0}; n int dy[5] = {0, 0, 0, -1, 1}; n struct cz { n   long long Time; n   bool Initial; n }; n cz state[1050][1050]; n bool mp[1050][1050]; n long long p; n string s; n struct pos { n   int x; n   int y; n }; n pos ww; n inline bool check(int x, int y, int n, int m) { n   for (int i = 1; i <= 4; i++) { n     int nowx = x + dx[i], nowy = y + dy[i]; n     if (nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m) n       if (mp[nowx][nowy] == mp[x][y]) return true; n   } n   return false; n } n int n, m, k, x, y; n bool vis[1050][1050]; n queue<pos> qwq; n inline void bfs() { n   while (!qwq.empty()) { n     int nowx = qwq.front().x; n     int nowy = qwq.front().y; n     qwq.pop(); n     for (int i = 1; i <= 4; i++) { n       if (nowx + dx[i] >= 1 && nowx + dx[i] <= n && nowy + dy[i] >= 1 && n           nowy + dy[i] <= m) { n         if (!vis[nowx + dx[i]][nowy + dy[i]]) { n           vis[nowx + dx[i]][nowy + dy[i]] = 1; n           state[nowx + dx[i]][nowy + dy[i]].Time = state[nowx][nowy].Time + 1; n           ww.x = nowx + dx[i]; n           ww.y = nowy + dy[i]; n           qwq.push(ww); n         } n       } n     } n   } n } n int Odd(long long i) { return (i & 1) != 0; } n int main() { n   bool ff = 0; n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; i++) { n     cin >> s; n     for (int j = 0; j < m; j++) { n       if (s[j] ==  1 ) state[i][j + 1].Initial = 1, mp[i][j + 1] = 1; n     } n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (check(i, j, n, m)) { n         ww.x = i, ww.y = j; n         vis[i][j] = 1; n         qwq.push(ww); n         ff = 1; n       } n   if (!ff) { n     for (int i = 1; i <= k; i++) { n       x = read(), y = read(), p = L_read(); n       cout << mp[x][y] <<   n ; n     } n     return 0; n   } n   bfs(); n   for (int i = 1; i <= k; i++) { n     x = read(), y = read(); n     p = L_read(); n     if (p <= state[x][y].Time) { n       cout << state[x][y].Initial <<   n ; n       continue; n     } n     if (!Odd(p - state[x][y].Time)) n       cout << state[x][y].Initial <<   n ; n     else { n       if (state[x][y].Initial == 0) n         cout << 1 <<   n ; n       else n         cout <<  0 n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static const long long L = 233333; n   static char sxd[L], *sss = sxd, *ttt = sxd; n   if (sss == ttt) { n     ttt = (sss = sxd) + fread(sxd, 1, L, stdin); n     if (sss == ttt) { n       return EOF; n     } n   } n   return *sss++; n } n inline char readalpha() { n   char c = gc(); n   for (; !isalpha(c); c = gc()) n     ; n   return c; n } n inline char readchar() { n   char c = gc(); n   for (; c ==    ; c = gc()) n     ; n   return c; n } n template <class T> n inline bool read(T& x) { n   bool flg = false; n   char c = gc(); n   x = 0; n   for (; !isdigit(c); c = gc()) { n     if (c ==  - ) { n       flg = true; n     } else if (c == EOF) { n       return false; n     } n   } n   for (; isdigit(c); c = gc()) { n     x = (x << 1) + (x << 3) + (c ^ 48); n   } n   if (flg) { n     x = -x; n   } n   return true; n } n template <class T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x < 10) { n     putchar(x | 48); n     return; n   } n   write(x / 10); n   putchar((x % 10) | 48); n } n const int maxn = 20; n const int maxl = 1000005; n int n; n int f[1 << maxn]; n int g[1 << maxn]; n char s[maxl]; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; ++i) { n     int now = i, nowzt = 0; n     while (now) { n       if (nowzt & (1 << (s[now] -  a ))) { n         break; n       } n       nowzt |= (1 << (s[now] -  a )); n       now--; n       f[nowzt] = i - now; n     } n   } n   int ans = 0; n   for (int i = 0; i < 1 << 20; ++i) { n     g[i] = f[i]; n   } n   for (int i = 0; i < 1 << 20; ++i) { n     for (int j = 0; j < 20; ++j) { n       g[i | (1 << j)] = max(g[i], g[i | (1 << j)]); n     } n   } n   for (int i = 0; i < 1 << 20; ++i) { n     ans = max(ans, f[i] + g[((1 << 20) - 1) ^ i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, sum = 0; n   cin >> n >> m; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int ff; n     cin >> ff; n     v.push_back(ff); n   } n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     if (x == 1) { n       int l, k; n       cin >> l >> k; n       v[l - 1] = k - sum; n     } n     if (x == 2) { n       int y; n       cin >> y; n       { sum += y; } n     } n     if (x == 3) { n       int ans; n       cin >> ans; n       cout << v[ans - 1] + sum << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> used; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int a[n]; n   long long ans = 0; n   int p = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     used[a[i]]++; n     if (used[a[i]] == k) { n       ans += (long long)(n - i); n       while (1) { n         used[a[p]]--; n         if (a[p++] == a[i]) break; n         ans += (long long)(n - i); n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 9, inf = 0x3f3f3f3f; n void Chkmax(int &x, int y) { n   if (y > x) x = y; n } n int n, ans, mx, root; n int a[maxn], sum[maxn]; n void Solve(int v) { n   static int b[maxn], last[maxn << 1]; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == root) { n       b[i] = 1; n     } else if (a[i] == v) { n       b[i] = -1; n     } else n       b[i] = 0; n   } n   int V(n); n   for (int i = 0; i <= 2 * n; ++i) last[i] = inf; n   last[V] = 0; n   int nw(0); n   for (int i = 1; i <= n; ++i) { n     nw += b[i]; n     if (last[nw + V] <= i) { n       Chkmax(ans, i - last[nw + V]); n     } else n       last[nw + V] = i; n   } n } n int main() { n   scanf( %d , &n); n   mx = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     sum[a[i]]++; n     Chkmax(mx, a[i]); n   } n   root = 0; n   for (int i = 1; i <= mx; ++i) { n     if (sum[i] > sum[root]) { n       root = i; n     } n   } n   for (int i = 1; i <= mx; ++i) n     if (i ^ root) { n       Solve(i); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     vector<pair<int, int> > v; n     for (int i = 0; i < n; i++) { n       int k, l; n       cin >> k >> l; n       v.push_back(make_pair(k, l)); n     } n     int j = 0, ma = -1, b = 0; n     for (int i = 0; i < n; i++) { n       b = max(b, v[i].first); n       if ((v[i].first - v[i].second) > ma) { n         ma = max(v[i].first - v[i].second, ma); n         j = v[i].first; n       } else if ((v[i].first - v[i].second) == ma) { n         if (v[i].first > j) { n           j = v[i].first; n         } n       } n     } n     if (b >= x) { n       cout <<  1  << endl; n     } else if (ma > 0) { n       if ((x - b) % ma == 0) { n         cout << (x - b) / ma + 1 << endl; n       } else { n         cout << (x - b) / ma + 2 << endl; n       } n     } else { n       cout <<  -1  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int add[N << 6], num[N << 6], lazy[N << 6], ls[N << 6], rs[N << 6], total; n int init() { n   num[total] = add[total] = lazy[total] = 0; n   ls[total] = rs[total] = -1; n   return total++; n } n void down(int l, int r, int o) { n   if (ls[o] == -1) ls[o] = init(); n   if (rs[o] == -1) rs[o] = init(); n   int ll = ls[o], rr = rs[o]; n   add[ll] += add[o]; n   add[rr] += add[o] + num[o] * ((l + r) / 2 + 1 - l); n   num[ll] += num[o]; n   num[rr] += num[o]; n   lazy[ll] += lazy[o]; n   lazy[rr] += lazy[o]; n   num[o] = add[o] = lazy[o] = 0; n } n int L, R, V, op; n void update(int l, int r, int o) { n   if (L <= l && r <= R) { n     if (op == 0) { n       lazy[o] += V; n     } else { n       add[o] += l - L + 1; n       num[o]++; n     } n     return; n   } n   down(l, r, o); n   int ll = ls[o], rr = rs[o]; n   if (L <= (l + r) / 2) update(l, (l + r) / 2, ll); n   if (R > (l + r) / 2) update((l + r) / 2 + 1, r, rr); n } n int query(int l, int r, int o, int x) { n   if (l == r) { n     return add[o] + lazy[o]; n   } n   down(l, r, o); n   int ll = ls[o], rr = rs[o]; n   if (x <= (l + r) / 2) n     return query(l, (l + r) / 2, ll, x); n   else n     return query((l + r) / 2 + 1, r, rr, x); n } n struct Node { n   int l, t, fg; n   Node() {} n   Node(int l, int t, int fg) : l(l), t(t), fg(fg) {} n   bool operator<(const Node& th) const { n     if (l != th.l) return l < th.l; n     if (fg != th.fg) return fg > th.fg; n     return t < th.t; n   } n } node[N * 2]; n int ll[N * 4], rr[N * 4], tt[N * 4]; n multiset<int> st; n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) != EOF) { n     int tot = 0; n     for (int i = 0; i < m; i++) { n       int l, r, t; n       scanf( %d%d%d , &l, &r, &t); n       r--; n       node[tot++] = Node(l, t, 1); n       node[tot++] = Node(r, t, -1); n     } n     sort(node, node + tot); n     int cnt = 0, pre; n     st.clear(); n     for (int i = 0; i < tot; i++) { n       int l = node[i].l, t = node[i].t, fg = node[i].fg; n       if (st.size() == 0) { n         pre = l; n         st.insert(t); n         continue; n       } n       if (l > pre) { n         ll[cnt] = pre; n         rr[cnt] = l - 1; n         tt[cnt] = *st.begin(); n         cnt++; n         pre = l; n       } n       if (fg == -1) { n         if (pre == l) { n           ll[cnt] = pre; n           rr[cnt] = l; n           tt[cnt] = *st.begin(); n           cnt++; n           pre = l + 1; n         } n         st.erase(st.lower_bound(t)); n       } else { n         st.insert(t); n       } n     } n     int ed = 1e9; n     total = 0; n     init(); n     for (int i = 0; i < cnt; i++) { n       int l = ll[i], r = rr[i], t = tt[i]; n       op = 0; n       V = r - l + 1; n       L = max(0, t - l + 1), R = ed; n       if (L <= R) update(0, ed, 0); n       L = t - r, R = t - l; n       op = 1; n       if (R >= 0) update(0, ed, 0); n     } n     for (int i = 0; i < n; i++) { n       int a; n       scanf( %d , &a); n       printf( %d n , query(0, ed, 0, a)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 1000000 + 5; n const int maxn = 6400 + 5; n struct Edge { n   int to, next; n } edges[maxm]; n int indices[maxn]; n int deg[maxn]; n char ans[maxm]; n int an; n int over; n inline int Trans_index(char* s) { return (s[0] -  0 ) * 80 + (s[1] -  0 ); } n void DFS(int x) { n   while (indices[x] != -1) { n     int y = edges[indices[x]].to; n     indices[x] = edges[indices[x]].next; n     DFS(y); n   } n   ans[an++] = x / 80 +  0 ; n } n int main() { n   int n; n   scanf( %d n , &n); n   memset(indices, 0xff, sizeof(indices)); n   int start; n   for (int i = 0; i < n; i++) { n     char line[5]; n     gets(line); n     int x = start = Trans_index(line); n     edges[i].to = Trans_index(line + 1); n     ; n     edges[i].next = indices[x]; n     indices[x] = i; n     deg[edges[i].to]++; n     deg[x]--; n   } n   int cnt_err = 0; n   over = -1; n   for (int i = 0; i < maxn; i++) { n     if (deg[i] == 0) n       continue; n     else if (deg[i] < 0) n       start = i, cnt_err -= deg[i]; n     else n       over = i, cnt_err += deg[i]; n   } n   if (cnt_err != 0 && cnt_err != 2) { n     puts( NO ); n     return 0; n   } n   DFS(start); n   for (int i = 0; i < maxn; i++) { n     if (indices[i] != -1) { n       printf( NO ); n       return 0; n     } n   } n   puts( YES ); n   reverse(ans, ans + an); n   if (cnt_err == 0) over = start; n   ans[an++] =  0  + over % 80; n   ans[an++] =   0 ; n   puts(ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long dad = 1e6; n int n, m, l = 1, r; n long long k; n long long d[dad], a[dad], b[dad], res, ans[dad], dem[dad]; n struct yasu { n   int id, l, r; n } p[dad], c[dad]; n bool cmp(yasu u, yasu v) { n   return (u.l / 300 < v.l / 300 || (u.l / 300 == v.l / 300 && u.r < v.r)); n } n void update(int u, long long gt1, long long gt2, long long gt3) { n   res += dem[c[u].l] * gt2; n   res += dem[c[u].r] * gt3; n   dem[c[u].id] += gt1; n } n int getint() { n   int ww = 0; n   char cc = getchar(); n   while (cc >  9  || cc <  0 ) cc = getchar(); n   while (cc >=  0  && cc <=  9 ) { n     ww = ww * 10 + cc -  0 ; n     cc = getchar(); n   } n   return ww; n } n long long getll() { n   long long ww = 0; n   char cc = getchar(); n   while (cc >  9  || cc <  0 ) cc = getchar(); n   while (cc >=  0  && cc <=  9 ) { n     ww = ww * 10 + cc -  0 ; n     cc = getchar(); n   } n   return ww; n } n void input() { n   scanf( %d%lld , &n, &k); n   for (int i = 1; i <= n; ++i) { n     d[i] = getll(); n     d[i] = d[i] == 1 ? 1 : -1; n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i]); n     a[i] = a[i - 1] + a[i] * d[i]; n   } n   for (int i = 0; i <= n; ++i) { n     b[++*b] = a[i]; n     b[++*b] = a[i] + k; n     b[++*b] = a[i] - k; n   } n   sort(b + 1, b + *b + 1); n   *b = unique(b + 1, b + *b + 1) - b - 1; n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &p[i].l, &p[i].r); n     --p[i].l; n     p[i].id = i; n   } n   for (int i = 0; i <= n; ++i) { n     c[i].id = lower_bound(b + 1, b + *b + 1, a[i]) - b; n     c[i].l = lower_bound(b + 1, b + *b + 1, a[i] - k) - b; n     c[i].r = lower_bound(b + 1, b + *b + 1, a[i] + k) - b; n   } n   sort(p + 1, p + m + 1, cmp); n   for (int i = 1; i <= m; ++i) { n     int l1 = p[i].l; n     int r1 = p[i].r; n     while (l < l1) { n       --dem[c[l].id]; n       res -= dem[c[l].r]; n       ++l; n     } n     while (l > l1) { n       --l; n       res += dem[c[l].r]; n       ++dem[c[l].id]; n     } n     while (r < r1) { n       ++r; n       res += dem[c[r].l]; n       ++dem[c[r].id]; n     } n     while (r > r1) { n       --dem[c[r].id]; n       res -= dem[c[r].l]; n       --r; n     } n     ans[p[i].id] = res; n   } n   for (int i = 1; i <= m; ++i) printf( %lld n , ans[i]); n } n void output() {} n int main() { n   input(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = unsigned long long; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int k; n   cin >> k; n   int n = (1 << k); n   vector<vector<ll> > a(n, vector<ll>(n, 0)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   int t; n   cin >> t; n   vector<pair<int, int> > shape(t); n   for (int i = 0; i < t; i++) cin >> shape[i].first >> shape[i].second; n   for (int r = 0; r < k; r++) { n     vector<vector<ll> > b(n, vector<ll>(n, 0)); n     for (int c = 0; c < t; c++) { n       for (int i = 0; i < (1 << k); i++) { n         for (int j = 0; j < (1 << k); j++) { n           b[(i + shape[c].first) & ((1 << k) - 1)] n            [(j + shape[c].second) & ((1 << k) - 1)] ^= a[i][j]; n         } n       } n     } n     a = b; n     for (int c = 0; c < t; c++) { n       shape[c].first = (shape[c].first * 2) % n; n       shape[c].second = (shape[c].second * 2) % n; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (a[i][j]) ans++; n   cout << ans <<   n ; n } n