#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6 + 3; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m; n     cin >> n; n     long long int a[n + 1]; n     long long int i = 0; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     long long int flag = 0; n     for (i = 1; i < n; i++) { n       if (abs(a[i] - a[i - 1]) > 1) { n         flag = 1; n         break; n       } n     } n     if (flag == 1) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n int main() { n   int i, j, n, r = 0, c = 0, ans = 0; n   char str[107][107]; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) { n     scanf( %s , str[i]); n     r = 0; n     for (j = 0; j < n; ++j) { n       if (str[i][j] ==  C ) { n         ++r; n       } n     } n     ans += (r * (r - 1)) / 2; n   } n   for (j = 0; j < n; ++j) { n     c = 0; n     for (i = 0; i < n; ++i) { n       if (str[i][j] ==  C ) { n         ++c; n       } n     } n     ans += (c * (c - 1)) / 2; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int NAX = 2e5 + 5, MOD = 1000000007; n struct Node { n   set<pair<int, int>> vals; n   Node() {} n   Node lazylazyMerge(const Node &rhs) {} n   Node seglazyMerge(const Node &rhs, const int &l, const int &r) {} n   Node segSegMerge(const Node &rhs) {} n }; n template <typename segNode> n struct Segtree { n   vector<segNode> Seg, Lazy; n   vector<segNode> Base; n   vector<bool> isLazy; n   int n; n   Segtree(int _n = 2e5) { n     this->n = _n; n     Seg.resize(4 * _n + 10); n     Lazy.resize(4 * _n + 10); n     isLazy.resize(4 * _n + 10); n   } n   void merge(segNode &curr, segNode &l, segNode &r) { curr = l + r; } n   void propagate(int node, int L, int R) { n     if (isLazy[node]) { n       isLazy[node] = false; n       Seg[node] = Seg[node].seglazyMerge(Lazy[node], L, R); n       if (L != R) { n         Lazy[2 * node] = Lazy[2 * node].lazylazyMerge(Lazy[node]); n         Lazy[2 * node + 1] = Lazy[2 * node + 1].lazylazyMerge(Lazy[node]); n         isLazy[2 * node] = true; n         isLazy[2 * node + 1] = true; n       } n       Lazy[node] = segNode(); n     } n   } n   void build(int node, int start, int end) { n     if (start == end) { n       Seg[node] = Base[start]; n       return; n     } n     int mid = (start + end) / 2; n     build(2 * node, start, mid); n     build(2 * node + 1, mid + 1, end); n     Seg[node] = Seg[2 * node].segSegMerge(Seg[2 * node + 1]); n   } n   void build(vector<segNode> &Arr) { n     Base = Arr; n     n = Arr.size(); n     Seg.resize(4 * n + 10); n     Lazy.resize(4 * n + 10); n     isLazy.resize(4 * n + 10); n     build(1, 0, n - 1); n   } n   pair<int, int> Query(int node, int start, int end, int qstart, int qend) { n     propagate(node, start, end); n     if (qend < start || qstart > end || start > end) n       return pair<int, int>(MOD, MOD); n     if (qstart <= start && end <= qend) { n       if ((Seg[node].vals).empty()) return pair<int, int>(MOD, MOD); n       return *((Seg[node].vals).begin()); n     } n     int mid = (start + end) / 2; n     auto l = Query(2 * node, start, mid, qstart, qend); n     auto r = Query(2 * node + 1, mid + 1, end, qstart, qend); n     return min(l, r); n   } n   segNode qQuery(int node, int start, int end, int pos) { n     propagate(node, start, end); n     if (start == end) return Seg[node]; n     int mid = (start + end) / 2; n     if (pos <= mid) return qQuery(2 * node, start, mid, pos); n     return qQuery(2 * node + 1, mid + 1, end, pos); n   } n   void Update(int node, int start, int end, int qstart, int qend, segNode val) { n     propagate(node, start, end); n     if (qend < start || qstart > end || start > end) return; n     if (qstart <= start && end <= qend) { n       isLazy[node] = true; n       Lazy[node] = val; n       propagate(node, start, end); n       return; n     } n     int mid = (start + end) / 2; n     Update(2 * node, start, mid, qstart, qend, val); n     Update(2 * node + 1, mid + 1, end, qstart, qend, val); n     Seg[node] = Seg[2 * node].segSegMerge(Seg[2 * node + 1]); n   } n   void pUpdate(int node, int start, int end, int pos, pair<int, int> val) { n     Seg[node].vals.insert(val); n     if (start == end) { n       return; n     } n     int mid = (start + end) / 2; n     if (pos <= mid) n       pUpdate(2 * node, start, mid, pos, val); n     else n       pUpdate(2 * node + 1, mid + 1, end, pos, val); n   } n   void pdel(int node, int start, int end, int pos, pair<int, int> val) { n     Seg[node].vals.erase(val); n     if (start == end) { n       return; n     } n     int mid = (start + end) / 2; n     if (pos <= mid) n       pdel(2 * node, start, mid, pos, val); n     else n       pdel(2 * node + 1, mid + 1, end, pos, val); n   } n   segNode query(int pos) { return qQuery(1, 0, n - 1, pos); } n   pair<int, int> query(int left, int right) { n     return Query(1, 0, n - 1, left, right); n   } n   void update(int pos, pair<int, int> val) { pUpdate(1, 0, n - 1, pos, val); } n   void update(int start, int end, segNode val) { n     Update(1, 0, n - 1, start, end, val); n   } n }; n void solveCase() { n   int n; n   cin >> n; n   ; n   vector<pair<pair<int, int>, pair<int, pair<int, int>>>> a(n); n   vector<int> ranges; n   for (size_t i = 0; i < n; i++) { n     cin >> a[i].first.first >> a[i].first.second; n     cin >> a[i].second.first >> a[i].second.second.first; n     a[i].second.second.second = i; n     ranges.push_back(a[i].first.first); n     ranges.push_back(a[i].second.first); n   } n   sort(ranges.begin(), ranges.end()); n   ranges.erase(unique(ranges.begin(), ranges.end()), ranges.end()); n   for (size_t i = 0; i < n; i++) { n     a[i].first.first = n         lower_bound(ranges.begin(), ranges.end(), a[i].first.first) - n         ranges.begin(); n     a[i].second.first = n         lower_bound(ranges.begin(), ranges.end(), a[i].second.first) - n         ranges.begin(); n   } n   sort(a.begin(), a.end()); n   ; n   vector<int> prev(n, -1), dist(n, MOD); n   queue<int> Q; n   Segtree<Node> stree(n); n   for (size_t i = 0; i < n; i++) { n     if (a[i].first.first == 0 && a[i].first.second == 0) { n       Q.push(i); n       dist[i] = 0; n     } else n       stree.update(i, {a[i].first.second, i}); n   } n   while (Q.size()) { n     auto curr_idx = Q.front(); n     ; n     Q.pop(); n     int c = a[curr_idx].second.first; n     int d = a[curr_idx].second.second.first; n     int idx2 = lower_bound(a.begin(), a.end(), n                            make_pair(make_pair(c + 1, 0), n                                      make_pair(0, make_pair(0, 0)))) - n                a.begin(); n     --idx2; n     ; n     while (true) { n       auto q = stree.query(0, idx2); n       ; n       if (q.first > d) break; n       int idx = q.second; n       Q.push(idx); n       dist[idx] = dist[curr_idx] + 1; n       prev[idx] = curr_idx; n       stree.pdel(1, 0, n - 1, idx, q); n     } n   }; n   for (size_t i = 0; i < n; i++) { n     if (a[i].second.second.second == (n - 1)) { n       if (dist[i] != MOD) { n         cout << dist[i] + 1 <<   n ; n         vector<int> path; n         int curr = i; n         while (curr >= 0) { n           path.push_back(curr); n           curr = prev[curr]; n         } n         for (int i = path.size() - 1; i >= 0; i--) { n           cout << a[path[i]].second.second.second + 1 <<    ; n         } n         cout <<   n ; n         return; n       } n     } n   } n   cout << -1 <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   ; n   for (int i = 1; i <= t; ++i) solveCase(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mn[200010 * 4], lazy_value[4 * 200010]; n bool lazy[4 * 200010]; n void update_node(int idx, int st, int ed, int val) { n   lazy[idx] = 1; n   lazy_value[idx] += val; n   mn[idx] += val; n } n void update_lazy(int idx, int st, int ed) { n   if (st == ed) return; n   int mid = (st + ed) / 2; n   update_node(2 * idx, st, mid, lazy_value[idx]); n   update_node(2 * idx + 1, mid + 1, ed, lazy_value[idx]); n   lazy[idx] = lazy_value[idx] = 0; n } n void insert(int idx, int st, int ed, int s, int e, int val) { n   if (st == s && ed == e) { n     update_node(idx, st, ed, val); n     return; n   } n   if (lazy[idx]) update_lazy(idx, st, ed); n   int mid = (st + ed) / 2; n   if (e <= mid) n     insert(idx * 2, st, mid, s, e, val); n   else if (s > mid) n     insert(idx * 2 + 1, mid + 1, ed, s, e, val); n   else { n     insert(idx * 2, st, mid, s, mid, val); n     insert(idx * 2 + 1, mid + 1, ed, mid + 1, e, val); n   } n   mn[idx] = min(mn[idx * 2], mn[idx * 2 + 1]); n } n int query(int idx, int st, int ed, int s, int e) { n   if (st == s && ed == e) return mn[idx]; n   if (lazy[idx]) update_lazy(idx, st, ed); n   int mid = (st + ed) / 2; n   if (mid >= e) n     return query(2 * idx, st, mid, s, e); n   else if (s > mid) n     return query(2 * idx + 1, mid + 1, ed, s, e); n   else n     return min(query(idx * 2, st, mid, s, mid), n                query(idx * 2 + 1, mid + 1, ed, mid + 1, e)); n } n int main() { n   int n, val, i, q, cnt, st, ed; n   string s_line; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> val; n     insert(1, 0, n - 1, i, i, val); n   } n   cin >> q; n   getchar(); n   while (q--) { n     getline(cin, s_line); n     stringstream ss1; n     ss1 << s_line; n     ss1 >> st; n     ss1 >> ed; n     cnt = 0; n     while (ss1 >> val) cnt++; n     if (cnt) { n       if (st > ed) { n         insert(1, 0, n - 1, st, n - 1, val); n         insert(1, 0, n - 1, 0, ed, val); n       } else n         insert(1, 0, n - 1, st, ed, val); n     } else { n       if (st > ed) n         cout << min(query(1, 0, n - 1, st, n - 1), query(1, 0, n - 1, 0, ed)); n       else n         cout << query(1, 0, n - 1, st, ed); n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mp[1010000], ans; n int main() { n   int n, m, x; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     mp[x]++; n   } n   for (int i = 0; i <= 1e6 + 21; i++) { n     int num = i; n     int numcnt = mp[i]; n     if (numcnt % 2 == 1) ans++; n     mp[num + 1] += numcnt / 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n string s; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   long long left = 1; n   long long right = n; n   while (right - left > 1) { n     long long m1 = (right + left) / 2; n     long long m2 = m1 + 1; n     cout << 1 <<     << m1 <<     << m2 << endl; n     cout.flush(); n     cin >> s; n     if (s ==  TAK ) { n       right = m1; n     } else n       left = m2; n   } n   long long f; n   long long s1 = -1; n   long long s2 = -1; n   if (left == right) n     f = left; n   else { n     cout << 1 <<     << left <<     << right << endl; n     cout.flush(); n     cin >> s; n     if (s ==  TAK ) { n       f = left; n     } else n       f = right; n   } n   left = 1; n   right = f - 1; n   if (right >= left) { n     while (right - left > 1) { n       long long m1 = (right + left) / 2; n       long long m2 = m1 + 1; n       cout << 1 <<     << m1 <<     << m2 << endl; n       cout.flush(); n       cin >> s; n       if (s ==  TAK ) { n         right = m1; n       } else n         left = m2; n     } n     if (left == right) n       s1 = left; n     else { n       cout << 1 <<     << left <<     << right << endl; n       cout.flush(); n       cin >> s; n       if (s ==  TAK ) { n         s1 = left; n       } else n         s1 = right; n     } n   } n   left = f + 1; n   right = n; n   if (right >= left) { n     while (right - left > 1) { n       long long m1 = (right + left) / 2; n       long long m2 = m1 + 1; n       cout << 1 <<     << m1 <<     << m2 << endl; n       cout.flush(); n       cin >> s; n       if (s ==  TAK ) { n         right = m1; n       } else n         left = m2; n     } n     if (left == right) n       s2 = left; n     else { n       cout << 1 <<     << left <<     << right << endl; n       cout.flush(); n       cin >> s; n       if (s ==  TAK ) { n         s2 = left; n       } else n         s2 = right; n     } n   } n   if (s1 != -1) { n     cout <<  1   << s1 <<     << f << endl; n     cout.flush(); n     cin >> s; n     if (s ==  TAK ) { n       cout << 2 <<     << f <<     << s1 << endl; n       cout.flush(); n       return 0; n     } n   } n   cout << 2 <<     << f <<     << s2 << endl; n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) { n     return a; n   } n   return b; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) { n     return b; n   } n   return gcd(b % a, a); n } n long long int min(long long int a, long long int b) { n   if (a < b) { n     return a; n   } n   return b; n } n long long int power(long long int x, long long int n) { n   long long int res = 1; n   while (n > 0) { n     if (n % 2 == 1) { n       res = (res * x); n     } n     x = (x * x); n     n = n / 2; n   } n   return res; n } n long long int factorialNumInverse[1000001]; n long long int naturalNumInverse[1000001]; n long long int fact[1000001]; n void InverseofNumber(long long int n, long long int p) { n   naturalNumInverse[0] = naturalNumInverse[1] = 1; n   for (long long int i = 2; i <= n; i++) n     naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p; n } n void InverseofFactorial(long long int n, long long int p) { n   factorialNumInverse[0] = factorialNumInverse[1] = 1; n   for (long long int i = 2; i <= n; i++) n     factorialNumInverse[i] = n         (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; n } n void factorial(long long int n, long long int p) { n   fact[0] = 1; n   for (long long int i = 1; i <= n; i++) { n     fact[i] = (fact[i - 1] * i) % p; n   } n } n long long int nCrModp(long long int n, long long int r, long long int p) { n   long long int ans = n       ((fact[n] * factorialNumInverse[r]) % p * factorialNumInverse[n - r]) % p; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int tc; n   cin >> tc; n   factorial(200005, 1000000007); n   InverseofNumber(200005, 1000000007); n   InverseofFactorial(200005, 1000000007); n   while (tc--) { n     long long int n; n     cin >> n; n     string s; n     cin >> s; n     long long int ans = 0; n     long long int i = 1; n     long long int len = 1; n     while (i < n) { n       if (s[i] == s[0]) { n         len++; n       } n       i++; n     } n     if (len == n) { n       cout << ceil((long double)n / (long double)3) <<   n ; n       continue; n     } n     len = 0; n     i = 0; n     int j = s.length() - 1; n     while (j >= 0 && s[j] == s[0]) { n       len++; n       j--; n     } n     while (i <= j && s[i] == s[0]) { n       len++; n       i++; n     } n     ans += (len) / 3; n     len = 1; n     i++; n     while (i <= j) { n       if (s[i] == s[i - 1]) { n         len++; n       } else { n         ans += (len) / 3; n         len = 1; n       } n       i++; n     } n     ans += (len) / 3; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, si = 1, leaf; n long long ans[500010]; n struct Q { n   int s, e, ind; n }; n vector<Q> query[500010]; n vector<pair<int, int>> tree[500010]; n int rs[500010]; n struct seg { n   long long seg[2000010], lazy[2000010]; n   void prop(int x) { n     seg[x] += lazy[x]; n     if (x < si) { n       lazy[x * 2] += lazy[x]; n       lazy[x * 2 + 1] += lazy[x]; n     } n     lazy[x] = 0; n     return; n   } n   void add(int s, int e, long long v) { n     if (s > e) return; n     add(1, 1, si, s, e, v); n   } n   void add(int x, int l, int r, int s, int e, long long v) { n     prop(x); n     if (r < s || e < l) return; n     if (s <= l && r <= e) { n       lazy[x] += v; n       prop(x); n       return; n     } n     add(x * 2, l, (l + r) / 2, s, e, v); n     add(x * 2 + 1, (l + r) / 2 + 1, r, s, e, v); n     seg[x] = min(seg[x * 2], seg[x * 2 + 1]); n   } n   long long get(int s, int e) { return get(1, 1, si, s, e); } n   long long get(int x, int l, int r, int s, int e) { n     prop(x); n     if (r < s || e < l) return 1000000000000000LL; n     if (s <= l && r <= e) return seg[x]; n     return min(get(x * 2, l, (l + r) / 2, s, e), n                get(x * 2 + 1, (l + r) / 2 + 1, r, s, e)); n   } n } S; n void makeup(int x, long long dis) { n   rs[x] = x; n   if (tree[x].size() == 0) { n     S.add(x, x, dis); n     return; n   } else n     S.add(x, x, 1000000000000000LL); n   for (auto i : tree[x]) { n     makeup(i.first, dis + i.second); n     rs[x] = max(rs[x], rs[i.first]); n   } n } n void dfs(int x) { n   for (auto i : query[x]) ans[i.ind] = S.get(i.s, i.e); n   for (auto i : tree[x]) { n     S.add(i.first, rs[i.first], -i.second); n     S.add(1, i.first - 1, i.second); n     S.add(rs[i.first] + 1, n, i.second); n     dfs(i.first); n     S.add(i.first, rs[i.first], i.second); n     S.add(1, i.first - 1, -i.second); n     S.add(rs[i.first] + 1, n, -i.second); n   } n } n int main() { n   int i, a, b, c; n   scanf( %d %d , &n, &q); n   while (si < n) si <<= 1; n   for (i = 2; i <= n; i++) { n     scanf( %d %d , &a, &b); n     tree[a].push_back({i, b}); n   } n   for (i = 1; i <= q; i++) { n     scanf( %d %d %d , &a, &b, &c); n     query[a].push_back({b, c, i}); n   } n   makeup(1, 0); n   dfs(1); n   for (i = 1; i <= q; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef vector <int> vi; n typedef pair <int, int> pii; n  n const int MAXN = 2e5 + 5; n  n int N; n int perm[MAXN]; n bool bio[MAXN]; n deque <pii> ans; n  n void load() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     int coin; n     scanf( %d , &coin); n     perm[coin] = i; n   } n } n  n void add_front(int x, int y) { n   ans.push_front({x, y}); n } n  n void add_back(int x, int y) { n   ans.push_back({x, y}); n } n  n void one_cycle(const vi &cyc) { n   int len = cyc.size(); n   auto add_local = [&](int x, int y) { n     add_front(cyc[x % len], cyc[y % len]); n   }; n   add_local(1, 2); n   add_local(1, 3); n   for (int i = 3; i < len; i++) n     add_local(i, i + 1); n   add_local(1, 2); n   add_local(0, 1); n } n  n void two_cycles(vi cyc1, vi cyc2) { n   cyc2.insert(cyc2.begin(), cyc1.back()); n   cyc1.pop_back(); n   cyc1.insert(cyc1.begin(), cyc2.back()); n   cyc2.pop_back(); n   int len1 = cyc1.size(); n   for (int i = 0; i < len1 - 1; i++) n     add_front(cyc1[i], cyc1[i + 1]); n   int len2 = cyc2.size(); n   for (int i = 0; i < len2 - 1; i++) n     add_front(cyc2[i], cyc2[i + 1]); n   add_front(cyc1[0], cyc2[0]); n   add_front(cyc1.back(), cyc2.back()); n } n  n void fixed_and_transposition(int x, const vi &tau) { n   add_back(x, tau[0]); n   add_back(tau[0], tau[1]); n   add_back(x, tau[1]); n } n  n void solve() { n   int fixed = 0; n   vector <vi> other; n   for (int i = 1; i <= N; i++)  n     if (!bio[i]) { n       vi tmp; n       for (int x = i; !bio[x]; x = perm[x]) { n         tmp.push_back(x); n         bio[x] = true; n       } n       if (tmp.size() > 1) n         other.push_back(tmp); n       else n         fixed = tmp[0]; n     } n   if (!other.empty()) { n     int largest = max_element(other.begin(), other.end(), [&](const vi &u, const vi &v) { return u.size() < v.size(); }) - other.begin(); n     swap(other[0], other[largest]); n     int parity = other.size() % 2; n     for (int i = parity; i < other.size(); i += 2) n       two_cycles(other[i], other[i + 1]); n     if (parity) { n       if (other[0].size() >= 3)  n         one_cycle(other[0]); n       else  n         fixed_and_transposition(fixed ? fixed : other[1][0], other[0]); n     } n   } n   printf( %d n , ans.size()); n   for (auto it : ans) { n     printf( %d %d n , perm[it.first], perm[it.second]); n     swap(perm[it.first], perm[it.second]); n   } n } n  n int main() { n   load(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int u, v, w; n } arr[300010]; n bool cmp(node a, node b) { return (a.w < b.w); } n int n, m; n int f[300010], f2[300010]; n int main() { n   scanf( %d , &n); n   ; n   scanf( %d , &m); n   ; n   for (int i = 0; i < m; i++) { n     scanf( %d , &arr[i].u); n     ; n     scanf( %d , &arr[i].v); n     ; n     scanf( %d , &arr[i].w); n     ; n   } n   sort(arr, arr + m, cmp); n   int ans = 0; n   for (int i = 0, j = 0; i < m; i = j) { n     while (j < m && arr[i].w == arr[j].w) j++; n     for (int k = i; k < j; k++) f2[arr[k].v] = f[arr[k].v]; n     for (int k = i; k < j; k++) n       f2[arr[k].v] = max(f2[arr[k].v], 1 + f[arr[k].u]); n     for (int k = i; k < j; k++) f[arr[k].v] = f2[arr[k].v]; n     for (int k = i; k < j; k++) ans = max(ans, f[arr[k].v]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmin(T &x, T y) { n   x = x < y ? x : y; n } n template <typename T> n void chkmax(T &x, T y) { n   x = x > y ? x : y; n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   x *= f; n } n int a[100010], s[100010], f[100010][105], b[100010][105]; n void add(int &x, int y) { n   x += y; n   if (x >= 998244353) x -= 998244353; n } n int main() { n   int n, k, len; n   read(n), read(k), read(len); n   if (len == 1) return cout <<  0 n , 0; n   s[0] = 1; n   for (int i = 1; i <= n; i++) read(a[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= k; j++) n       if (a[i] == j || a[i] == -1) b[i][j] = 1; n   for (int j = 1; j <= k; j++) n     for (int i = 1; i <= n; i++) b[i][j] += b[i - 1][j]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= k; j++) { n       if (~a[i] && a[i] != j) continue; n       f[i][j] = s[i - 1]; n       if (i >= len && b[i][j] - b[i - len][j] == len) n         add(f[i][j], (f[i - len][j] - s[i - len] + 998244353) % 998244353); n       add(s[i], f[i][j]); n     } n   } n   cout << s[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double x, y; n   while (cin >> x >> y) { n     double l; n     l = sqrt(x * x + y * y); n     if (l == (int)l) { n       cout <<  black  << endl; n       continue; n     } n     int z = (int)l; n     if (z != l) z++; n     if (x * y >= 0) { n       if (z % 2) n         cout <<  black  << endl; n       else n         cout <<  white  << endl; n     } else { n       if (z % 2 == 0) n         cout <<  black  << endl; n       else n         cout <<  white  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) { n     return b; n   } n   if (b == 0) { n     return a; n   } n   return gcd(b % a, a); n } n int main() { n   long long ra, la, ta, rb, lb, tb; n   cin >> la >> ra >> ta; n   cin >> lb >> rb >> tb; n   long long g = gcd(ta, tb); n   if (ta > tb) { n     long long j = la, k = ra, l = ta; n     la = lb; n     ra = rb; n     ta = tb; n     lb = j; n     rb = k; n     tb = l; n   } n   lb -= la; n   rb -= la; n   long long buff = ((lb + tb) % tb) - lb; n   lb += buff; n   rb += buff; n   ra -= la; n   la -= la; n   long long bdif = 0; n   long long nf = (lb % g) - lb; n   lb += nf; n   rb += nf; n   bdif = max(bdif, 1 + min(rb, ra) - max(lb, la)); n   long long nl = ((lb + g - 1) % g) - g + 1; n   long long df = nl - lb; n   lb += df; n   rb += df; n   bdif = max(bdif, 1 + min(rb, ra) - max(lb, la)); n   cout << bdif << endl; n } n


#include <bits/stdc++.h> n using namespace std; n auto start = std::chrono::high_resolution_clock::now(); n long double crrtime() { n   auto stop = std::chrono::high_resolution_clock::now(); n   auto duration = n       std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); n   return ((long double)duration.count()) / ((long double)1e9); n } n mt19937 rng(2); n int inline randn(int L, int R) { n   uniform_int_distribution<int> uid(L, R); n   return uid(rng); n } n int inline randn(int N) { n   uniform_int_distribution<int> uid(0, N - 1); n   return uid(rng); n } n const long long mod = 998244353; n long long n; n long long p[30]; n long long a[30][26]; n long long g[10000001]; n long long h[10000001]; n long long F[10000001]; n void solve() { n   cin >> n; n   p[0] = 1; n   for (long long i = 1; i < 30; i++) p[i] = 2 * p[i - 1]; n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (long long j = 0; j < 26; j++) a[i][j] = 0; n     for (char c : s) a[i][c -  a ]++; n   } n   g[0] = 0; n   for (long long x = 1; x < p[n]; x++) { n     vector<long long> tmp(26, 100000); n     long long k = 0; n     h[x] = 0; n     for (long long i = 0; i < n; i++) { n       if (p[i] & x) { n         for (long long j = 0; j < 26; j++) tmp[j] = min(tmp[j], a[i][j]); n         k++; n         h[x]++; n         h[x] += i; n       } n     } n     long long cnt = 1; n     for (long long i = 0; i < 26; i++) cnt = (cnt * (tmp[i] + 1)) % mod; n     h[x] *= k; n     g[x] = cnt; n     if ((k & 1) == 0) g[x] = -cnt; n   } n   for (long long i = 0; i < (1 << n); ++i) F[i] = g[i]; n   for (long long i = 0; i < n; ++i) n     for (long long mask = 0; mask < (1 << n); ++mask) { n       if (mask & (1 << i)) F[mask] += F[mask ^ (1 << i)]; n     } n   long long ans = 0; n   for (long long i = 0; i < p[n]; i++) { n     F[i] %= mod; n     if (F[i] < 0) F[i] += mod; n     ans ^= (F[i] * h[i]); n   } n   cout << (ans) <<   n ; n } n signed main() { n   start = std::chrono::high_resolution_clock::now(); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tt = 1; n   for (long long tc = 1; tc <= tt; ++tc) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int long long freq[100005], a[100005], final[100005]; n int long long pow1(int long long a, int long long b) { n   if (a == 0) return 0; n   int long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y); n       if (x > 1000000007) x %= 1000000007; n     } n     y = (y * y); n     if (y > 1000000007) y %= 1000000007; n     b /= 2; n   } n   return x % 1000000007; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int long long n; n   cin >> n; n   for (int long long i = 0; i < n; i++) { n     cin >> a[i]; n     freq[a[i]]++; n   } n   for (int long long i = 100000; i >= 1; i--) { n     int long long c1 = 0; n     for (int long long j = i; j <= 100000; j = j + i) { n       c1 = (c1 + freq[j]) % 1000000007; n     } n     final[i] = (final[i] + pow1(2, c1) - 1) % 1000000007; n     for (int long long j = 2 * i; j <= 100000; j = j + i) { n       final[i] = (final[i] + 1000000007 - final[j]) % 1000000007; n     } n   } n   cout << final[1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int xa, ya, xb, yb, n; n   cin >> xa >> ya >> xb >> yb >> n; n   vector<int> X(n), Y(n), R(n); n   for (int i = 0; i < n; ++i) cin >> X[i] >> Y[i] >> R[i]; n   if (xa > xb) swap(xa, xb); n   if (ya > yb) swap(ya, yb); n   int ans = 0; n   for (int i = xa; i <= xb; i++) { n     bool flag = false; n     for (int j = 0; j < n; ++j) { n       int d = (X[j] - i) * (X[j] - i) + (Y[j] - ya) * (Y[j] - ya); n       if (d <= R[j] * R[j]) flag = true; n     } n     ans += !flag; n     flag = false; n     for (int j = 0; j < n; ++j) { n       int d = (X[j] - i) * (X[j] - i) + (Y[j] - yb) * (Y[j] - yb); n       if (d <= R[j] * R[j]) flag = true; n     } n     ans += !flag; n   } n   for (int i = ya + 1; i < yb; ++i) { n     bool flag = false; n     for (int j = 0; j < n; ++j) { n       int d = (X[j] - xa) * (X[j] - xa) + (Y[j] - i) * (Y[j] - i); n       if (d <= R[j] * R[j]) flag = true; n     } n     ans += !flag; n     flag = false; n     for (int j = 0; j < n; ++j) { n       int d = (X[j] - xb) * (X[j] - xb) + (Y[j] - i) * (Y[j] - i); n       if (d <= R[j] * R[j]) flag = true; n     } n     ans += !flag; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n struct edge { n   int from; n   int to; n   int w; n }; n int comp(const edge& f, const edge& l) { n   if (f.from < l.from) n     return 1; n   else if (f.from > l.from) n     return 0; n   else { n     if (f.w > l.w) return 1; n   } n   return 0; n } n map<int, vector<edge> > d; n int check_cnt(int sum) { n   sort(d[sum].begin(), d[sum].end(), comp); n   set<pair<int, int> > wait; n   int cnt = 0; n   for (edge it : d[sum]) { n     if (it.w == 1) n       wait.insert({it.from, it.to}); n     else { n       if (wait.count({it.to, it.from})) { n         cnt++; n         wait.clear(); n       } n     } n   } n   return cnt; n } n void make(int sum) { n   sort(d[sum].begin(), d[sum].end(), comp); n   set<pair<int, int> > wait; n   for (edge& it : d[sum]) { n     if (it.w == 1) n       wait.insert({it.from, it.to}); n     else { n       if (wait.count({it.to, it.from})) { n         cout << it.to + 1 <<     << it.from + 1 <<   n ; n         wait.clear(); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> p(n + 1, 0); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     p[i + 1] = p[i] + a[i]; n   } n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       sum = p[j + 1] - p[i]; n       d[sum].push_back((edge){i, j, 1}); n       d[sum].push_back((edge){j, i, -1}); n     } n   } n   int max_val = 0, max_cnt = 0, cnt = 0; n   for (auto& it : d) { n     cnt = check_cnt(it.first); n     if (max_cnt < cnt) { n       max_cnt = cnt; n       max_val = it.first; n     } n   } n   cout << max_cnt <<   n ; n   make(max_val); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long fastexp(long long b, long long exp) { n   if (exp == 0) return 1; n   long long temp = fastexp(b, exp / 2); n   temp = (temp * temp) % 998244353; n   if (exp % 2 == 1) temp *= b; n   return temp % 998244353; n } n long long fact[100001], inv[100001]; n void precompute() { n   fact[0] = 1; n   inv[0] = 1; n   for (int i = 1; i <= 100000; i++) { n     fact[i] = (fact[i - 1] * i) % 998244353; n     inv[i] = fastexp(fact[i], 998244353 - 2); n   } n } n long long choose(int n, int k) { n   if (n < 0 || k < 0 || k > n) return 0; n   long long ans = (fact[n] * inv[k]) % 998244353; n   ans = (ans * inv[n - k]) % 998244353; n   return ans; n } n bool row[3605], col[3605]; n int H, W, N; n long long dp1[3605][3605], dp2[3605][3605]; n int emptyH, emptyW; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   precompute(); n   cin >> H >> W >> N; n   for (int i = 0; i < N; i++) { n     int r1, c1, r2, c2; n     cin >> r1 >> c1 >> r2 >> c2; n     row[r1] = true; n     row[r2] = true; n     col[c1] = true; n     col[c2] = true; n   } n   for (int i = 1; i <= H; i++) n     if (!row[i]) emptyH++; n   for (int i = 1; i <= W; i++) n     if (!col[i]) emptyW++; n   dp1[0][0] = true; n   for (int i = 1; i <= H; i++) { n     for (int j = 0; j <= i; j++) { n       if (row[i] || row[i - 1]) n         dp1[i][j] = dp1[i - 1][j]; n       else { n         dp1[i][j] = dp1[i - 1][j]; n         if (i - 2 >= 0 && j - 1 >= 0) dp1[i][j] += dp1[i - 2][j - 1]; n         dp1[i][j] %= 998244353; n       } n     } n   } n   dp2[0][0] = true; n   for (int i = 1; i <= W; i++) { n     for (int j = 0; j <= i; j++) { n       if (col[i] || col[i - 1]) n         dp2[i][j] = dp2[i - 1][j]; n       else { n         dp2[i][j] = dp2[i - 1][j]; n         if (i - 2 >= 0 && j - 1 >= 0) dp2[i][j] += dp2[i - 2][j - 1]; n         dp2[i][j] %= 998244353; n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i <= H; i++) { n     for (int j = 0; j <= W; j++) { n       long long R = dp1[H][j] * choose(emptyH - 2 * j, i); n       R %= 998244353; n       long long C = dp2[W][i] * choose(emptyW - 2 * i, j); n       C %= 998244353; n       long long F = (fact[i] * fact[j]) % 998244353; n       ans += ((R * C) % 998244353 * F) % 998244353; n       ans %= 998244353; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> /* n #include <ext/pb_ds/assoc_container.hpp> // Common file n #include <ext/pb_ds/tree_policy.hpp> // Including */ n using namespace std; n //using namespace __gnu_pbds; n  n //typedefs n  n typedef long long ll; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef pair<int,int> pii; n typedef pair<double, double> pdd; n typedef pair<ll, ll> pll; n typedef vector<pii> vii; n typedef vector<pll> vll; n typedef vector<int>::iterator vit; n typedef set<int>::iterator sit; n  n /* n template<typename T> n using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; n template<typename F, typename S> n using ordered_map = tree<F, S, less<F>, rb_tree_tag, tree_order_statistics_node_update>; n */ n  n //#Defines n #define rep(i,a,b) for(i=a;i<=b;i++) n #define repR(i,a,b) for(i=a;i>=b;i--) n //#define pb push_back n #define pb emplace_back n #define F first n #define S second n #define mp make_pair n #define all(c)  c.begin(),c.end() n #define endl   n  n #define pf printf n #define sf scanf n //#define left __left n //#define right __right n //#define tree __tree n #define MOD 1000000007 n //#define harmonic(n) 0.57721566490153286l+log(n) n  n #define RESET(a,b) memset(a,b,sizeof(a)) n #define gcd(a,b) __gcd(a,b) n #define lcm(a,b) (a*(b/gcd(a,b))) n #define sqr(a) ((a) * (a)) n  n #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); n  n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const ll infLL = 9000000000000000000; n  n  n //Bit Operations n inline bool checkBit(ll n, int i) { return n&(1LL<<i); } n inline ll setBit(ll n, int i) { return n|(1LL<<i);; } n inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); } n  n int fx[] = {0, 0, +1, -1}; n int fy[] = {+1, -1, 0, 0}; n //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; n //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1}; n  n  n //Inline functions n  n inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } n inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); } n inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } n inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } n inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } n inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } n inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } n inline ll modInverse(ll a) { return modPow(a, MOD-2); } n inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } n inline bool isInside(pii p,ll n,ll m) { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m); } n inline bool isInside(pii p,ll n)  { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n); } n inline bool isSquare(ll x)     { ll s = sqrt(x); return (s*s==x); } n inline bool isFib(ll x)     { return isSquare(5*x*x+4)|| isSquare(5*x*x-4); } n inline bool isPowerOfTwo(ll x)   { return ((1LL<<(ll)log2(x))==x); } n  n  n struct func n { n  //this is a sample overloading function for sorting stl n  bool operator()(pii const &a, pii const &b)  n  { n   if(a.F==b.F) n    return (a.S<b.S); n   return (a.F<b.F); n  } n }; n  n //Prime Number Generator n  n /* n #define M 100000000 n int marked[M/64 + 2]; n  n #define on(x) (marked[x/64] & (1<<((x%64)/2))) n #define mark(x)  marked[x/64] |= (1<<((x%64)/2)) n  n vl prime; n  n bool isPrime(int num) { n   return num > 1 && (num == 2 || ((num & 1) && !on(num)));  n } n  n void sieve(ll n) { n  for (ll i = 3; i * i < n; i += 2) { n   if (!on(i)) { n    for (ll j = i * i; j <= n; j += i + i) { n     mark(j); n    } n   } n  } n  prime.pb(2); n  for(ll i = 3; i <= n; i += 2) { n   if(!on(i)) prime.pb(i);  n  } n } n  n */ n  n // n //debug n #ifdef tajir n template < typename F, typename S > n ostream& operator << ( ostream& os, const pair< F, S > & p ) { n  return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const vector< T > &v ) { n  os <<  { ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  } ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const set< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  ] ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const multiset< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << *it; n  } n  return os <<  ] ; n } n  n template < typename F, typename S > n ostream &operator << ( ostream & os, const map< F, S > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << it -> first <<   =   << it -> second ; n   } n  return os <<  ] ; n } n  n #define dbg(args...) do {cerr << #args <<   :  ; faltu(args); } while(0) n  n clock_t tStart = clock(); n #define timeStamp dbg( Execution Time:  , (double)(clock() - tStart)/CLOCKS_PER_SEC) n  n void faltu () { n  cerr << endl; n } n  n template <typename T> n void faltu( T a[], int n ) { n  for(int i = 0; i < n; ++i) cerr << a[i] <<    ; n  cerr << endl; n } n  n template <typename T, typename ... hello> n void faltu( T arg, const hello &... rest) { n  cerr << arg <<    ; n  faltu(rest...); n } n #else n #define dbg(args...) n #endif // tajir n  n int main() n { n  #ifdef tajir n   freopen( input.txt ,  r , stdin); n  #else n   // online submission n  #endif n  optimize(); n   n  int n; n  int a[10001]; n  int x[10001]; n  n  cin >> n; n  n  a[0] = 0; n  n  for(int i = 1; i <= n; ++i) n   cin >> a[i]; n  n  for(int i = n; i > 0; --i) { n   x[i-1] = a[i] - a[i-1]; n  } n  dbg(x,n); n  n  vi res; n  n  for(int i = 1; i <= n; ++i) { n   vi v(i,-inf); n   dbg(i); n   bool flag = true; n   for(int j = 0; j < n; ++j) { n    if(v[j%i] == -inf) n     v[j%i] = x[j]; n    else { n     if(v[j%i] == x[j]) continue; n     else { n      dbg(j%i,v[j%i],x[j]); n      flag = false; n      break; n     } n    } n    dbg(v); n   } n   dbg(flag); n   if(flag) n    res.pb(i); n  } n  n  cout << res.size() << endl; n  n  for(auto x : res)  n   cout << x <<    ; n  cout << endl; n   n  return 0; n } n  n  n  n  n  n  n //? n


#include <bits/stdc++.h> n using namespace std; n int a[100000], b[100000], c[100000][2]; n int n, m; n long long k, sum; n int main() { n   cin >> n >> k; n   sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n     long long aa = a[i]; n     sum += aa; n   } n   if (sum <= k) { n     if (sum < k) { n       printf( -1 n ); n     } n     return 0; n   } n   sort(b, b + n); n   m = 0; n   c[0][0] = b[0]; n   c[0][1] = 0; n   for (int i = 1; i < n; i++) n     if (b[i] != b[i - 1]) { n       m++; n       c[m][0] = b[i]; n       c[m][1] = i; n     } n   int pre = 0; n   for (int i = 0; i <= m; i++) { n     long long u, v, duc; n     v = c[i][0] - pre; n     u = n - c[i][1]; n     duc = u * v; n     if (duc <= k) { n       k -= duc; n     } else { n       int h = -1; n       for (int j = 0; j < n; j++) n         if (a[j] >= c[i][0]) { n           h++; n           a[h] = a[j]; n           b[h] = j; n         } n       long long s, t; n       s = k / u; n       t = k % u; n       int tt, ss; n       tt = t; n       ss = s; n       for (int j = tt; j <= h; j++) { n         if (j != tt) { n           printf(   ); n         } n         printf( %d , b[j] + 1); n       } n       for (int j = 0; j < tt; j++) n         if ((ss + 1 != v) || (a[j] != c[i][0])) { n           printf(  %d , b[j] + 1); n         } n       printf(  n ); n       break; n     } n     pre = c[i][0]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, turn; n   cin >> n >> k; n   turn = n / k; n   if (turn % 2) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   int n; n   cin >> n >> a >> b; n   int ans = 0; n   for (int i = 0; i < n - 1; i++) { n     if (a[i] != b[i]) { n       if (a[i] ==  1 ) { n         if (a[i + 1] ==  0  && a[i + 1] != b[i + 1]) { n           a[i] =  0 ; n           a[i + 1] =  1 ; n           ans++; n         } else { n           a[i] =  0 ; n           ans++; n         } n       } else { n         if (a[i + 1] ==  1  && a[i + 1] != b[i + 1]) { n           a[i] =  1 ; n           a[i + 1] =  0 ; n           ans++; n         } else { n           a[i] =  1 ; n           ans++; n         } n       } n     } n   } n   if (a[n - 1] != b[n - 1]) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> t, a, b; n vector<pair<int, int>> p; n void push(int v) { n   if (t[v] != -1) { n     t[v * 2] = t[v * 2 + 1] = t[v]; n     t[v] = -1; n   } n } n void update(int v, int l, int r, int vl, int vr, int color) { n   if (vl > vr) return; n   if (l == vl && r == vr) n     t[v] = color; n   else { n     push(v); n     int m = (l + r) / 2; n     update(v * 2, l, m, vl, min(vr, m), color); n     update(v * 2 + 1, m + 1, r, max(vl, m + 1), vr, color); n   } n } n int get(int v, int l, int r, int pos) { n   if (l == r) return t[v]; n   push(v); n   int m = (l + r) / 2; n   if (m >= pos) n     return get(v * 2, l, m, pos); n   else n     return get(v * 2 + 1, m + 1, r, pos); n } n int main() { n   cin >> n >> m; n   a.resize(n); n   b.resize(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   int bin = 1; n   while (bin < n) bin *= 2; n   n = bin; n   t.assign(n * 2, -1); n   for (int i = 0; i < m; ++i) { n     int type; n     cin >> type; n     if (type == 1) { n       int l, q, k; n       cin >> l >> q >> k; n       update(1, 0, n - 1, q - 1, q + k - 2, p.size()); n       p.push_back({l, q}); n     } else { n       int x, y = -1; n       cin >> x; n       y = get(1, 0, n - 1, x - 1); n       if (y == -1) n         cout << b[x - 1] <<   n ; n       else n         cout << a[p[y].first + x - p[y].second - 1] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[11][11][4][100010]; n int n; n char s[100010]; n char c[100010]; n int ch(char s) { n   if (s ==  A ) n     return 0; n   else if (s ==  C ) n     return 1; n   else if (s ==  T ) n     return 2; n   else if (s ==  G ) n     return 3; n   return -1; n } n void Add(int *tr, int x, int d) { n   for (; x <= n; x += x & (-x)) { n     tr[x] += d; n   } n } n int Sum(int *tr, int x) { n   if (x == 0) return 0; n   int s = 0; n   while (x) { n     s += tr[x]; n     x -= x & (-x); n   } n   return s; n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (int i = n - 1; i >= 0; i--) s[i + 1] = s[i]; n   s[0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= 10; j++) Add(f[j][i % j][ch(s[i])], i, 1); n   int q, x, d, l, r; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &x); n     if (x == 1) { n       scanf( %d%s , &d, c); n       for (int i = 1; i <= 10; i++) Add(f[i][d % i][ch(s[d])], d, -1); n       s[d] = c[0]; n       for (int i = 1; i <= 10; i++) Add(f[i][d % i][ch(s[d])], d, 1); n     } else { n       scanf( %d%d%s , &l, &r, c); n       int len = strlen(c); n       int ans = 0; n       for (int i = 0; i < len; i++) n         ans += Sum(f[len][(l + i) % len][ch(c[i])], r) - n                Sum(f[len][(l + i) % len][ch(c[i])], l - 1); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void EnableFileIO(const string& fileName, bool local = 0) { n   if (fileName.empty()) return; n   if (local) { n     freopen((fileName +  .in ).c_str(),  r , stdin); n     freopen((fileName +  .out ).c_str(),  w , stdout); n     return; n   } n   freopen((fileName +  .in ).c_str(),  r , stdin); n   freopen((fileName +  .out ).c_str(),  w , stdout); n } n const int INF = (1 << 30) - 1; n const long long LINF = (1LL << 61) - 1; n const double EPS = 1e-10, PI = acos(-1); n const int N = 5e4 + 100, L = 31, P = 1e9 + 7; n int tot = 1; n int trans[N * L][2]; n short cnt[N * 30]; n short sum[N * 30][L][2]; n __attribute__((__optimize__( -O3 ))) void ins(int k) { n   int u = 1; n   for (int i = L - 1; i >= 0; i--) { n     int c = k >> i & 1; n     if (!trans[u][c]) trans[u][c] = ++tot; n     ++sum[u][i][c]; n     u = trans[u][c]; n   } n   ++cnt[u]; n } n __attribute__((__optimize__( -O3 ))) void dfs(int u, int d) { n   if (!u) return; n   dfs(trans[u][0], d - 1); n   dfs(trans[u][1], d - 1); n   cnt[u] = cnt[u] + cnt[trans[u][0]] + cnt[trans[u][1]]; n   for (int i = 0; i <= d; i++) { n     sum[u][i][0] += sum[trans[u][0]][i][0] + sum[trans[u][1]][i][0]; n     sum[u][i][1] += sum[trans[u][0]][i][1] + sum[trans[u][1]][i][1]; n   } n } n int n, m, a[N]; n long long mid, num; n __attribute__((__optimize__( -O3 ))) int dfs(int u, int d, int eq) { n   if (!u) return 0; n   if (d < 0 || !eq) return cnt[u]; n   int cb = mid >> d & 1, cn = num >> d & 1, res = 0; n   if (cb) { n     if (cn) { n       res += dfs(trans[u][0], d - 1, 1); n     } else { n       res += dfs(trans[u][1], d - 1, 1); n     } n   } else { n     if (cn) { n       res += dfs(trans[u][1], d - 1, 1); n       res += dfs(trans[u][0], d - 1, 0); n     } else { n       res += dfs(trans[u][0], d - 1, 1); n       res += dfs(trans[u][1], d - 1, 0); n     } n   } n   return res; n } n __attribute__((__optimize__( -O3 ))) inline int check() { n   long long res = 0; n   for (int i = 0; i < n; i++) { n     num = a[i]; n     res += dfs(1, L - 1, 1); n   } n   return int(res / 2); n } n __attribute__((__optimize__( -O3 ))) pair<long long, long long> dfsans(int u, n                                                                        int d, n                                                                        int eq) { n   if (!u) return make_pair(0, 0); n   long long res = 0, nodes = 0; n   if (d < 0 || !eq) { n     for (int i = 0; i <= d; ++i) { n       int c = num >> i & 1; n       if (c) { n         res += sum[u][i][0] * (1LL << i); n       } else { n         res += sum[u][i][1] * (1LL << i); n       } n     } n     return make_pair(cnt[u], res); n   } n   int cb = mid >> d & 1, cn = num >> d & 1; n   pair<long long, long long> p; n   if (cb) { n     if (cn) { n       p = dfsans(trans[u][0], d - 1, 1); n       res += p.first * (1LL << d) + p.second; n       nodes += p.first; n     } else { n       p = dfsans(trans[u][1], d - 1, 1); n       res += p.first * (1LL << d) + p.second; n       nodes += p.first; n     } n   } else { n     if (cn) { n       p = dfsans(trans[u][1], d - 1, 1); n       res += p.second; n       nodes += p.first; n       p = dfsans(trans[u][0], d - 1, 0); n       res += p.first * (1LL << d) + p.second; n       nodes += p.first; n     } else { n       p = dfsans(trans[u][0], d - 1, 1); n       res += p.second; n       nodes += p.first; n       p = dfsans(trans[u][1], d - 1, 0); n       res += p.first * (1LL << d) + p.second; n       nodes += p.first; n     } n   } n   return make_pair(nodes, res); n } n __attribute__((__optimize__( -O3 ))) long long calcans() { n   long long res = 0; n   for (int i = 0; i < n; i++) { n     num = a[i]; n     res += dfsans(1, L - 1, 1).second; n   } n   return res / 2; n } n __attribute__((__optimize__( -O3 ))) int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n   } n   for (int i = 0; i < n; i++) { n     ins(a[i]); n   } n   dfs(1, L - 1); n   long long lb = 0, rb = 2147483647LL; n   while (rb - lb > 1) { n     mid = (lb + rb) >> 1; n     if (check() >= m) { n       lb = mid; n     } else { n       rb = mid; n     } n   } n   mid = lb + 1; n   long long ans = (m - check()) * 1LL * lb; n   mid = lb + 1; n   ans += calcans(); n   printf( %lld n , ans % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ds[3001], sz[3001], ot[3001], a, b, c, no, ans, sum; n vector<pair<int, pair<int, int> > > v; n int get(int x) { n   if (ds[x] == x) n     return x; n   else n     return ds[x] = get(ds[x]); n } n void merge(int x, int y) { n   sz[x] += sz[y]; n   ds[y] = x; n   ot[x] += ot[y]; n   if (sz[x] > sum - ot[x]) no = 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     cin >> a >> b >> c; n     v.push_back(make_pair(c, make_pair(a, b))); n   } n   sort(v.begin(), v.end()); n   for (int i = 1; i <= n; ++i) { n     cin >> ot[i]; n     sz[i] = 1; n     ds[i] = i; n     sum += ot[i]; n   } n   for (int i = 1; i <= n; ++i) n     if (sz[i] > sum - ot[i]) no = 1; n   if (!no && n > 1) ans = v[0].first; n   for (int i = 0; i < v.size();) { n     int j = i; n     while (j < v.size() && v[i].first == v[j].first) { n       merge(get(v[j].second.first), get(v[j].second.second)); n       ++j; n     } n     i = j; n     if (!no && i < v.size()) ans = v[i].first; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}, n, m; n const int N = 1505; n char c[N][N]; n pair<int, int> seen[N][N]; n int mod(int a, int m) { n   while (a < 0) a += m; n   return a % m; n } n bool dfs2(int x, int y) { n   if (c[mod(x, n)][mod(y, m)] ==  # ) return false; n   if (seen[mod(x, n)][mod(y, m)] != make_pair(1 << 27, 1 << 27)) { n     if (seen[mod(x, n)][mod(y, m)] != make_pair(x, y)) n       return true; n     else n       return false; n   } n   seen[mod(x, n)][mod(y, m)] = make_pair(x, y); n   for (int k = 0; k < 4; k++) n     if (dfs2(x + dx[k], y + dy[k])) return true; n   return false; n } n int k, comp[N][N], start_comp, start_x, start_y; n bool seen_edge; n vector<pair<int, int> > edges; n vector<int> edge_style; n void dfs(int x, int y) { n   if (x < 0 || x >= n || y < 0 || y >= m) { n     seen_edge = true; n     return; n   } n   if (comp[x][y] || c[x][y] ==  # ) return; n   if (c[x][y] ==  S ) { n     start_x = x; n     start_y = y; n     start_comp = k; n   } n   comp[x][y] = k; n   for (int k = 0; k < 4; k++) dfs(x + dx[k], y + dy[k]); n } n int dist[10000]; n const int inf = 1 << 27; n bool relax(int a, int b, int w) { n   if (dist[b] < inf && dist[a] > dist[b] + w) { n     dist[a] = dist[b] + w; n     return true; n   } n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> c[i]; n   k = 1; n   start_comp = -1; n   memset(comp, 0, sizeof(comp)); n   start_x = -1; n   start_y = -1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (!comp[i][j] && c[i][j] !=  # ) { n         seen_edge = false; n         dfs(i, j); n         if (seen_edge) n           k++; n         else if (start_x >= 0 && start_y >= 0 && comp[start_x][start_y] == k) { n           cout <<  No  << endl; n           return 0; n         } n       } n   edges.clear(); n   edge_style.clear(); n   for (int i = 0; i < n; i++) { n     if (c[i][0] !=  #  && c[i][m - 1] !=  # ) { n       edge_style.push_back(1); n       edges.push_back(make_pair(comp[i][m - 1], comp[i][0])); n     } n   } n   for (int j = 0; j < m; j++) { n     if (c[0][j] !=  #  && c[n - 1][j] !=  # ) { n       edge_style.push_back(2); n       edges.push_back(make_pair(comp[n - 1][j], comp[0][j])); n     } n   } n   bool flag = false; n   for (int z = 0; z < 4; z++) { n     for (int i = 1; i < k; i++) dist[i] = inf; n     dist[start_comp] = 0; n     for (int i = 0; i < k; i++) n       for (int j = 0; j < edges.size(); j++) { n         int a = edges[j].first, b = edges[j].second; n         int dw = (edge_style[j] == 1) ? (z % 2 ? 1 : -1) : (z / 2 ? 1 : -1); n         bool u = relax(a, b, dw), v = relax(b, a, -dw); n         if (i == k - 1 && (u || v)) flag = true; n       } n   } n   cout << (flag ?  Yes  :  No ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[200010]; n int addnum, vel[400010], head[400010], ne[400010]; n int fath[200010][21]; n int fa[200010], dep[200010]; n pair<int, int> s[200010]; n int tot, n, m; n vector<int> o, o1, o2; n int id[11]; n int d[200010], f[200010][2]; n int num; n int p[200010]; 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 -  0 , ch = getchar(); n   return ans; n } n inline int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n } n void dfs(int u, int fa) { n   vis[u] = 1; n   for (int e = head[u]; e; e = ne[e]) { n     int v = vel[e]; n     if (v == fa) continue; n     fath[v][0] = u; n     dep[v] = dep[u] + 1; n     dfs(v, u); n   } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int dt = dep[x] - dep[y]; n   for (int i = 19; i >= 0; i--) n     if (dt >> i & 1) x = fath[x][i]; n   for (int i = 19; i >= 0; i--) n     if (fath[x][i] != fath[y][i]) x = fath[x][i], y = fath[y][i]; n   if (x == y) return x; n   return fath[x][0]; n } n void calc(int num) { n   int x = s[num].first, y = s[num].second; n   int dt = lca(x, y); n   o1.clear(), o2.clear(); n   while (x != dt) o1.push_back(x), x = fath[x][0]; n   o1.push_back(dt); n   while (y != dt) o2.push_back(y), y = fath[y][0]; n   while (o1.size()) o2.push_back(o1.back()), o1.pop_back(); n   o.clear(); n   int pos = 0, cnt = o2.size(); n   for (int i = 0; i < cnt; i++) n     if (p[o2[i]] == 1 && !p[o2[(i + 1) % cnt]]) { n       pos = i; n       break; n     } n   o.push_back(id[0] == o2[pos] ? id[0] : id[1]); n   for (int i = pos + 1; i < cnt + pos; i++) n     if (p[o2[i % cnt]]) n       break; n     else n       o.push_back(o2[i % cnt]); n   o.push_back(id[0] == o2[pos] ? id[1] : id[0]); n   printf( %d  , (int)o.size()); n   if (o.back() != id[0]) reverse(o.begin(), o.end()); n   while (o.size()) { n     int u = o.back(); n     o.pop_back(); n     printf( %d%c , u, !o.size() ?   n  :    ); n   } n } n void add(int u, int v) { n   addnum++, vel[addnum] = v, ne[addnum] = head[u], head[u] = addnum; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     if (find(x) != find(y)) n       fa[find(x)] = find(y), add(x, y), add(y, x); n     else n       s[++tot] = make_pair(x, y); n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i, -1); n   for (int i = 1; i < 20; i++) n     for (int j = 1; j <= n; j++) fath[j][i] = fath[fath[j][i - 1]][i - 1]; n   for (int i = 1; i <= tot; i++) { n     int x = s[i].first, y = s[i].second; n     while (x != y) { n       if (dep[x] < dep[y]) swap(x, y); n       if (!d[x]) n         d[x] = i; n       else { n         num = d[x]; n         break; n       } n       x = fath[x][0]; n     } n     if (num) { n       printf( YES n ); n       x = s[i].first, y = s[i].second; n       while (x != y) { n         if (dep[x] < dep[y]) swap(x, y); n         f[x][0] = 1; n         x = fath[x][0]; n       } n       x = s[num].first, y = s[num].second; n       while (x != y) { n         if (dep[x] < dep[y]) swap(x, y); n         f[x][1] = 1; n         x = fath[x][0]; n       } n       for (int i = 1; i <= n; i++) n         if (f[i][0] && f[i][1]) p[fath[i][0]]++, p[i]++; n       int cnt = 0; n       for (int i = 1; i <= n; i++) n         if (p[i] == 1) id[cnt++] = i; n       x = id[0]; n       y = id[1]; n       while (p[x]) o1.push_back(x), x = fath[x][0]; n       while (p[y]) o2.push_back(y), y = fath[y][0]; n       printf( %d  , (int)(o1.size() + o2.size() - 1)); n       for (int i = 0; i < (int)o1.size() - 1; i++) printf( %d  , o1[i]); n       while (o2.size()) { n         int u = o2.back(); n         o2.pop_back(); n         printf( %d%c , u, !o2.size() ?   n  :    ); n       } n       calc(i), calc(num); n       return 0; n     } n   } n   printf( NO n ); n } n


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


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> graph[100010]; n int deg[100010]; n int sum[100010]; n long long dpa[100010], dpb[100010]; n long long power(long long x, long long n) { n   if (n == 0) return 1; n   long long y = power(x, n / 2); n   y = y * y % 1000000007ll; n   if (n % 2 == 1) y = y * x % 1000000007ll; n   return y; n } n void dfs(int p, int x) { n   int i; n   if (deg[x] == 1) { n     dpa[x] = dpb[x] = 0; n     return; n   } n   long long A = 0, B = 0; n   int K = 0; n   for ((i) = 0; (i) < (int)(graph[x].size()); (i)++) { n     int y = graph[x][i]; n     if (y == p) continue; n     K++; n     dfs(x, y); n     A = (A + dpa[y]) % 1000000007ll; n     B = (B + dpb[y]) % 1000000007ll; n   } n   int K2 = K + 1; n   if (x == 0) K2--; n   dpa[x] = power((K2 - A + 1000000007ll) % 1000000007ll, 1000000007ll - 2); n   dpb[x] = (B + sum[x]) * dpa[x] % 1000000007ll; n } n int main(void) { n   int i; n   cin >> N; n   for ((i) = 0; (i) < (int)(N - 1); (i)++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     graph[a].push_back(b); n     graph[b].push_back(a); n     deg[a]++; n     deg[b]++; n     sum[a] += c; n     sum[b] += c; n   } n   dfs(-1, 0); n   cout << dpb[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(T x) { n   return x * x; n } n const int maxn = 1000; n int n, m, val; n char s[maxn][maxn]; n int f[maxn][maxn]; n int sum[maxn][maxn]; n long long res; n int main() { n   for (int i = 0; i < int(maxn); i++) n     for (int j = 0; j < int(maxn); j++) { n       s[i][j] = 0; n       f[i][j] = 0; n       sum[i][j] = 0; n     } n   cin >> n >> m >> val; n   for (int i = int(1); i < int(n + 1); i++) n     for (int j = int(1); j < int(m + 1); j++) cin >> s[i][j]; n   for (int i = int(1); i < int(n + 1); i++) n     for (int j = int(1); j < int(m + 1); j++) { n       if (s[i][j] ==  1  && s[i - 1][j] ==  1  && s[i + 1][j] ==  1  && n           s[i][j - 1] ==  1  && s[i][j + 1] ==  1 ) n         f[i + 1][j + 1] = 1; n     } n   for (int i = int(1); i < int(n + 1); i++) n     for (int j = int(1); j < int(m + 1); j++) { n       sum[i][j] = (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]) + f[i][j]; n     } n   for (int i = int(1); i < int(n); i++) n     for (int j = int(1); j < int(m); j++) { n       for (int k = int(1); k < int(m - j + 2); k++) { n         int l = i + 1, r = n + 1, cur; n         while (l < r - 1) { n           cur = (l + r) >> 1; n           if (sum[cur][j + k - 1] - sum[cur][j + 1] - sum[i + 1][j + k - 1] + n                   sum[i + 1][j + 1] < n               val) n             l = cur; n           else n             r = cur; n         } n         res += n - l; n       } n     } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long expo(long long n) { n   if (n == 0) return 1; n   if (n % 2) { n     long long x = expo(n / 2); n     return x * x * 2; n   } else { n     long long x = expo(n / 2); n     return x * x; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   long long x, y, z, i, j, k, f = 0, n, m; n   cin >> n >> m; n   x = m; n   while (x > 0) { n     f++; n     x /= 2; n   } n   if (f < n) { n     cout << m; n   } else { n     cout << m % expo(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9; n const long long INF = 8e18; n const int maxn = 1e5 + 5; n int n; n int cnt[maxn], val[maxn]; n long long S, SUM; n vector<pair<int, int> > e[maxn]; n inline void dfs(int u, int pre) { n   if (u != 1 && e[u].size() == 1) { n     cnt[u] = 1; n   } n   for (auto &x : e[u]) { n     int v = x.first, w = x.second; n     if (v == pre) { n       continue; n     } n     val[v] = w; n     dfs(v, u); n     cnt[u] += cnt[v]; n   } n } n inline void init() { n   for (int i = 0; i < n + 5; ++i) { n     e[i].clear(); n     cnt[i] = 0; n   } n   SUM = 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> S; n     for (int i = 1; i <= n - 1; ++i) { n       int u, v, w; n       cin >> u >> v >> w; n       e[u].push_back({v, w}); n       e[v].push_back({u, w}); n     } n     dfs(1, 1); n     priority_queue<pair<long long, int> > pq; n     for (int i = 2; i <= n; ++i) { n       SUM += 1ll * val[i] * cnt[i]; n       pq.push({1ll * (val[i] - (val[i] >> 1)) * cnt[i], i}); n       val[i] >>= 1; n     } n     int ans = 0; n     while (SUM > S) { n       ++ans; n       auto now = pq.top(); n       pq.pop(); n       long long w = now.first, id = now.second; n       SUM -= w; n       pq.push({1ll * (val[id] - (val[id] >> 1)) * cnt[id], id}); n       val[id] >>= 1; n     } n     cout << ans <<   n ; n     init(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(long long a) { n   write(a); n   puts(  ); n } n inline void wri(long long a) { n   write(a); n   putchar(   ); n } n inline unsigned long long rnd() { n   return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; n } n const int N = 100005, ZT = 500, K = 5, mod = 1e9 + 7; n struct data { n   int a[K]; n   bool ins(int x) { n     for (int i = (int)(K - 1); i >= (int)(0); i--) n       if (x >> i & 1) { n         if (a[i]) { n           x ^= a[i]; n         } else { n           for (int j = (int)(0); j <= (int)(i - 1); j++) n             if (x >> j & 1) x ^= a[j]; n           a[i] = x; n           for (int j = (int)(i + 1); j <= (int)(K - 1); j++) n             if (a[j] >> i & 1) a[j] ^= x; n           return 1; n         } n       } n     return 0; n   } n   int& operator[](int x) { return a[x]; } n } A[ZT], ffff, qj; n bool operator<(data a, data b) { n   for (int i = (int)(0); i <= (int)(K - 1); i++) n     if (a[i] != b[i]) return a[i] < b[i]; n   return 0; n } n map<data, int> M; n vector<pair<int, int> > v[N]; n bool vis[N], bj[N]; n int dis[N], alb[N], fa[N], Q, QJ, QQ; n void dfs(int p) { n   vis[p] = 1; n   for (auto i : v[p]) n     if (i.first != 1) { n       if (!vis[i.first]) { n         dis[i.first] = dis[p] ^ i.second; n         fa[i.first] = p; n         dfs(i.first); n       } else { n         if (bj[p] && bj[i.first]) { n           Q = 1; n           QQ = alb[p] ^ alb[i.first] ^ i.second; n         } else if (i.first != fa[p] && i.first < p) { n           QJ *= qj.ins(i.second ^ dis[p] ^ dis[i.first]); n         } n       } n     } n } n int dq, gg[ZT][ZT], zy[ZT][ZT]; n long long ans, f[ZT], g[ZT]; n inline void add(long long& a, long long b) { a = (a + b) % mod; } n void dfs(data a) { n   if (!M.count(a)) { n     M[a] = ++dq; n     A[dq] = a; n   } else n     return; n   for (int j = (int)(0); j <= (int)((1 << K) - 1); j++) { n     data b = a; n     b.ins(j); n     dfs(b); n   } n } n int main() { n   dfs(ffff); n   for (int i = (int)(1); i <= (int)(dq); i++) n     for (int j = (int)(1); j <= (int)(dq); j++) { n       data a = A[i]; n       gg[i][j] = 1; n       for (int o = (int)(0); o <= (int)(K - 1); o++) n         if (A[j][o]) gg[i][j] &= a.ins(A[j][o]); n       zy[i][j] = M[a]; n     } n   int n = read(), m = read(); n   for (int i = (int)(1); i <= (int)(m); i++) { n     int a = read(), b = read(), w = read(); n     v[a].push_back(make_pair(b, w)); n     v[b].push_back(make_pair(a, w)); n   } n   for (auto i : v[1]) { n     bj[i.first] = 1; n     alb[i.first] = i.second; n   } n   vis[1] = 1; n   f[1] = 1; n   for (auto i : v[1]) n     if (!vis[i.first]) { n       Q = 0; n       QJ = 1; n       memset((qj.a), 0, sizeof(qj.a)); n       dfs(i.first); n       if (!QJ) continue; n       memcpy(g, f, sizeof(f)); n       if (Q) { n         int x = M[qj]; n         QJ *= qj.ins(QQ); n         int y = M[qj]; n         for (int j = (int)(1); j <= (int)(dq); j++) { n           if (gg[j][x]) add(g[zy[j][x]], f[j] * 2); n           if (gg[j][y]) add(g[zy[j][y]], f[j] * QJ); n         } n       } else { n         int x = M[qj]; n         for (int j = (int)(1); j <= (int)(dq); j++) n           if (gg[j][x]) add(g[zy[j][x]], f[j]); n       } n       swap(f, g); n     } n   for (int i = (int)(1); i <= (int)(dq); i++) add(ans, f[i]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n template <typename T1, typename T2> n inline void chkmin(T1 &x, const T2 &y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1 &x, const T2 &y) { n   if (x < y) x = y; n } n int n, m, k; n vector<pair<int, int>> a[4]; n set<pair<int, int>> L, R; n int sumSet; n void read() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     int t, f1, f2; n     cin >> t >> f1 >> f2; n     a[f1 * 2 + f2].push_back({t, i}); n   } n   for (int i = 0; i <= 3; i++) sort(a[i].begin(), a[i].end()); n } n void relax(int sz) { n   sz = max(sz, 0); n   while (L.size() > sz) { n     auto x = *(--L.end()); n     L.erase(--L.end()); n     sumSet -= x.first; n     R.insert(x); n   } n   while (L.size() < sz) { n     if (R.empty()) break; n     auto x = *(R.begin()); n     R.erase(R.begin()); n     L.insert(x); n     sumSet += x.first; n   } n } n void add(pair<int, int> a) { n   if (R.empty()) { n     L.insert(a); n     sumSet += a.first; n   } else if (L.empty()) { n     R.insert(a); n   } else if (*(R.begin()) < a) { n     R.insert(a); n   } else { n     L.insert(a); n     sumSet += a.first; n   } n } n int ans; n vector<int> fans; n void run() { n   ans = 2e9 + 228 + 1337; n   for (int it = 0; it < 2; it++) { n     L.clear(); n     R.clear(); n     sumSet = 0; n     int sum = 0; n     int top1 = (int)a[1].size() - 1; n     int top2 = (int)a[2].size() - 1; n     for (auto i : a[0]) add(i); n     for (auto i : a[1]) sum += i.first; n     for (auto i : a[2]) sum += i.first; n     for (int i = 0; i <= min(m, (int)a[3].size()); i++) { n       if (i > 0) sum += a[3][i - 1].first; n       while (top1 >= 0 && top1 + 1 + i > k) { n         sum -= a[1][top1].first; n         add(a[1][top1--]); n       } n       while (top2 >= 0 && top2 + 1 + i > k) { n         sum -= a[2][top2].first; n         add(a[2][top2--]); n       } n       if (min(top1, top2) + 1 + i < k) continue; n       int sz = m - i - (top1 + 1) - (top2 + 1); n       if (sz < 0) continue; n       if (L.size() + R.size() < sz) continue; n       relax(sz); n       chkmin(ans, sum + sumSet); n       if (!it || sum + sumSet != ans) continue; n       for (auto j : L) { n         fans.push_back(j.second); n       } n       for (int j = 0; j <= top1; j++) { n         fans.push_back(a[1][j].second); n       } n       for (int j = 0; j <= top2; j++) { n         fans.push_back(a[2][j].second); n       } n       for (int j = 0; j < i; j++) { n         fans.push_back(a[3][j].second); n       } n       return; n     } n   } n   cout << -1 << endl; n   exit(0); n } n void write() { n   cout << ans << endl; n   sort(fans.begin(), fans.end()); n   for (auto i : fans) { n     cout << i + 1 <<    ; n   } n   cout << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   read(); n   run(); n   write(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   scanf( %d , &n); n   int array[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &array[i]); n   } n   int min = 9999999, max = 0, count; n   for (i = 0; i < n; i++) { n     if (array[i] < min) { n       min = array[i]; n     } n     if (array[i] > max) { n       max = array[i]; n     } n   } n   int maxIndex, minIndex; n   for (i = 0; i < n; i++) { n     if (array[i] == max) { n       maxIndex = i; n       break; n     } n   } n   for (i = n - 1; i >= 0; i--) { n     if (array[i] == min) { n       minIndex = i; n       break; n     } n   } n   int maxDistance, minDistance; n   int swapCount = 0; n   for (i = maxIndex; i > 0; i--) { n     swapCount++; n     int temp = array[i]; n     array[i] = array[i - 1]; n     array[i - 1] = temp; n   } n   for (i = 0; i < n; i++) { n     if (array[i] == max) { n       maxIndex = i; n       break; n     } n   } n   for (i = n - 1; i >= 0; i--) { n     if (array[i] == min) { n       minIndex = i; n       break; n     } n   } n   for (i = minIndex; i < n - 1; i++) { n     swapCount++; n     int temp = array[i]; n     array[i] = array[i + 1]; n     array[i + 1] = temp; n   } n   printf( %d , swapCount); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <class T> n void chmin(T &t, T f) { n   if (t > f) t = f; n } n template <class T> n void chmax(T &t, T f) { n   if (t < f) t = f; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n int uf[110]; n int root(int x) { return (uf[x] < 0) ? x : (uf[x] = root(uf[x])); } n bool conn(int x, int y) { n   x = root(x); n   y = root(y); n   if (x == y) return 0; n   if (uf[x] > uf[y]) swap(x, y); n   uf[x] += uf[y]; n   uf[y] = x; n   return 1; n } n int N, M; n int A[10010], B[10010]; n int len; n int usLens[110]; n int us[110][110]; n int main() { n   int i; n   int u, v; n   int j, k; n   for (; ~scanf( %d%d , &N, &M);) { n     for (i = 0; i < M; ++i) { n       A[i] = in() - 1; n       B[i] = in() - 1; n     } n     memset(uf, ~0, N << 2); n     for (i = 0; i < M; ++i) { n       conn(A[i], B[i]); n     } n     len = 0; n     memset(usLens, 0, sizeof(usLens)); n     for (u = 0; u < N; ++u) n       if (uf[u] < 0) { n         if (-uf[u] > 3) { n           goto failed; n         } n         if (-uf[u] < 2) { n           continue; n         } n         for (v = 0; v < N; ++v) n           if (root(v) == u) { n             us[len][usLens[len]++] = v; n           } n         ++len; n       } n     if (len > N / 3) { n       goto failed; n     } n     for (u = 0; u < N; ++u) n       if (uf[u] < 0) { n         if (-uf[u] < 2) { n           for (j = 0; j < N / 3; ++j) { n             if (usLens[j] < 3) { n               us[j][usLens[j]++] = u; n               break; n             } n           } n         } n       } n     for (j = 0; j < N / 3; ++j) { n       for (k = 0; k < 3; ++k) { n         if (k) printf(   ); n         printf( %d , us[j][k] + 1); n       } n       puts(  ); n     } n     continue; n   failed:; n     puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int dir8[8][2] = {{-1, 0}, {1, 0},  {0, 1}, {0, -1}, n                   {-1, 1}, {1, -1}, {1, 1}, {-1, -1}}; n int dir8h[8][2] = {{2, 1}, {-2, -1}, {-1, 2}, {1, -2}, n                    {1, 2}, {-2, 1},  {2, -1}, {-1, -2}}; n long double eps = 1e-7; n const int N = 52; n long long dp[N][N]; n vector<string> v[N]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   string s; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> s; n     v[i].push_back(s); n     for (long long j = 1; j <= s.size() - 1; j++) { n       string s1; n       for (long long k = 1; k <= s.size() - 1; k++) s1 += s[k]; n       s1 += s[0]; n       v[i].push_back(s1); n       s = s1; n     } n   } n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < v[0].size(); j++) dp[i][j] = 1e9; n   for (long long i = 0; i < v[0].size(); i++) dp[0][i] = i; n   for (long long i = 1; i <= n - 1; i++) { n     for (long long j = 0; j < v[i].size(); j++) { n       for (long long k = 0; k < v[i - 1].size(); k++) n         if (v[i][j] == v[i - 1][k]) dp[i][j] = min(dp[i][j], j + dp[i - 1][k]); n     } n   } n   long long mn = 1e9; n   for (long long i = 0; i < v[n - 1].size(); i++) mn = min(mn, dp[n - 1][i]); n   if (mn == 1e9) n     cout << -1; n   else n     cout << mn; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i = 1, j = 1, c1, c2; n   cin >> n; n   if (n % 2) n     cout << 9 <<     << (n - 9) << endl; n   else n     cout << 8 <<     << (n - 8) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t > 0) { n     long long int n, s; n     cin >> n >> s; n     long long int a[n]; n     long long int sum = 0; n     for (long long int i = (0); i < (n); i++) { n       cin >> a[i]; n       sum = sum + a[i]; n     } n     if (s >= sum) { n       cout << 0 << endl; n       t--; n       continue; n     } n     long long int flag = 0; n     long long int index; n     long long int count = 0; n     for (long long int i = (0); i < (n); i++) { n       count = count + a[i]; n       if (count > s) { n         index = i; n         flag = 1; n         break; n       } n     } n     long long int m = INT_MIN; n     long long int ans = 0; n     for (long long int i = (0); i < (index + 1); i++) { n       if (a[i] > m) { n         m = a[i]; n         ans = i; n       } n     } n     if (ans == index) { n       if (count - a[index] + a[index + 1] <= s) { n         cout << index + 1 << endl; n       } else { n         cout << 0 << endl; n       } n       t--; n       continue; n     } else { n       cout << ans + 1 << endl; n       t--; n       continue; n     } n   } 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 = 1; n   while (T--) { n     int n, u; n     cin >> n >> u; n     vector<int> v; n     for (int i = 0; i < n; i++) { n       int t; n       cin >> t; n       v.push_back(t); n     } n     int I, K, J; n     double ans = 0.0; n     for (int i = 0; i < n - 2; i++) { n       I = v[i]; n       J = v[i + 1]; n       K = lower_bound((v).begin(), (v).end(), u + I) - v.begin(); n       if (K == n) { n         K = n - 1; n       } else if (v[K] != u + I) { n         K--; n       } n       K = v[K]; n       double a = double(K - J) / (K - I); n       ans = max(a, ans); n     } n     if (ans == 0) { n       cout << -1; n       exit(0); n     } n     printf( %.20f , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b, c; n   cin >> a >> b; n   sort(a.begin(), a.end()); n   if (a.length() < b.length()) { n     reverse(a.begin(), a.end()); n     cout << a << endl; n   } else { n     for (int i = 0; i < a.length(); i++) { n       for (int j = a.length() - 1; j > i; j--) { n         c = a; n         swap(a[i], a[j]); n         sort(a.begin() + i + 1, a.end()); n         if (a > b) { n           a = c; n         } else n           break; n       } n     } n     cout << a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, M; n   cin >> N >> M; n   long long ans = 0; n   int step = 1; n   for (int i = 1; i <= M; ++i) { n     int T; n     cin >> T; n     if (T >= step) { n       ans += T - step; n     } else { n       ans += N - step + T; n     } n     step = T; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = long long; n using f64 = double; n using f80 = long double; n using pii = pair<int, int>; n using pll = pair<i64, i64>; n using ptx = pair<f64, f64>; n const int N = 3e5 + 5, LG = 20; n int f[N], conv[N], tmp[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   for (int t, i = 1; i <= n; ++i) { n     cin >> t; n     f[t] = conv[t] = 1; n     if (t == 1) { n       cout <<  1 n ; n       return 0; n     } n   } n   for (int tid = 2; tid <= 7; ++tid) { n     i64 drop, st, dr; n     for (int i = N - 1; i >= 1; --i) { n       drop = st = dr = 0; n       for (int j = i; j < N; j += i) { n         drop += tmp[j]; n         st += conv[j]; n         dr += f[j]; n       } n       tmp[i] = st * dr - drop; n     } n     if (tmp[1]) { n       cout << tid << endl; n       return 0; n     } n     for (int i = 0; i < N; ++i) { n       conv[i] = int(tmp[i] > 0); n       tmp[i] = 0; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long suf(long long a) { return (a * (a + 1)) / 2; } n const long long OO = 1e6 + 10, MOD = 1e9 + 7; n vector<vector<int> > adj, can; n int dx[]{1, -1, 0, 0, 1, -1, 1, -1}; n int dy[]{0, 0, 1, -1, 1, -1, -1, 1}; n int main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, k, a, b, c, d; n   string s, s1; n   char ch; n   while (cin >> n) { n     vector<vector<int> > v(1e5 + 5); n     vector<pair<int, int> > ans; n     for (int i = 0; i < n; i++) { n       cin >> a; n       v[a].push_back(i + 1); n     } n     for (int i = 0; i < v.size(); i++) { n       if (v[i].size() == 1) n         ans.push_back({i, 0}); n       else if (v[i].size() > 1) { n         int now = v[i][1] - v[i][0]; n         bool ok = true; n         for (int j = 2; j < v[i].size(); j++) { n           if (v[i][j] - v[i][j - 1] != now) { n             ok = false; n             break; n           } n         } n         if (ok) ans.push_back({i, now}); n       } n     } n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); i++) n       cout << ans[i].first <<     << ans[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1e5; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int a[N]; n int n; n int dfs(int h, int w, int s) { n   if (s > n) return h; n   int sum = 0; n   for (int i = s; i <= n; i++) { n     if (a[i] > a[i + 1]) sum++; n     if (sum == w) return dfs(h + 1, i - s + 1, i + 1); n   } n   return h + 1; n } n int main() { n   int T; n   T = read(); n   while (T--) { n     n = read(); n     for (int i = 1; i <= n; i++) a[i] = read(); n     printf( %d n , dfs(1, 1, 2) - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 200000; n int n; n int a[MAXN]; n void run() { n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   int ndiff = unique(a, a + n) - a; n   int ret = n + 1 - ndiff; n   printf( %d n , ret); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n } e[200010]; n bool operator<(node t1, node t2) { return t1.r < t2.r; } n int n, ans; n int main() { n   scanf( %d , &n); n   int w, x; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &w); n     e[i].l = x - w; n     e[i].r = x + w; n   } n   sort(e + 1, e + n + 1); n   int L = -0x7fffffff; n   for (int i = 1; i <= n; i++) { n     if (e[i].l >= L) { n       ans++; n       L = e[i].r; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using cd = complex<double>; n const double pi = acos(-1); n const double EPS = 1e-5; n void iter(vector<cd> &a, bool invert) { n   int n = a.size(); n   for (int i = 1, j = 0; i < n; i++) { n     int bit = n >> 1; n     for (; j & bit; bit >>= 1) j ^= bit; n     j ^= bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     double ang = 2 * pi / len * (invert ? -1 : 1); n     cd wlen(cos(ang), sin(ang)); n     for (int i = 0; i < n; i += len) { n       cd w(1); n       for (int j = 0; j < len / 2; j++) { n         cd u = a[i + j], v = a[i + j + len / 2] * w; n         a[i + j] = u + v; n         a[i + j + len / 2] = u - v; n         w *= wlen; n       } n     } n   } n   if (invert) n     for (cd &x : a) x /= n; n } n void iter2d(vector<vector<cd>> &a, bool invert) { n   for (auto &it : a) iter(it, invert); n   for (int j = 0; j < a[0].size(); j++) { n     vector<cd> col(a.size()); n     for (int i = 0; i < a.size(); i++) col[i] = a[i][j]; n     iter(col, invert); n     for (int i = 0; i < a.size(); i++) a[i][j] = col[i]; n   } n } n vector<vector<int>> mult(vector<vector<cd>> &a, vector<vector<cd>> &b) { n   int nr = 1; n   while (nr < a.size() + b.size()) nr <<= 1; n   a.resize(nr); n   b.resize(nr); n   int nc = 1; n   while (nc < a[0].size() + b[0].size()) nc <<= 1; n   for (auto &it : a) it.resize(nc, 0); n   for (auto &it : b) it.resize(nc, 0); n   iter2d(a, 0); n   iter2d(b, 0); n   for (int i = 0; i < nr; i++) n     for (int j = 0; j < nc; j++) a[i][j] *= b[i][j]; n   iter2d(a, 1); n   vector<vector<int>> ret(nr, vector<int>(nc)); n   for (int i = 0; i < nr; i++) n     for (int j = 0; j < nc; j++) ret[i][j] = (int)(a[i][j].real() + EPS); n   return ret; n } n const int N = 401; n int n, m, r, c; n string str[N]; n string pat[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> str[i]; n   cin >> r >> c; n   for (int i = 0; i < r; i++) cin >> pat[i]; n   vector<vector<cd>> a(n + r, vector<cd>(m + c)), b(r, vector<cd>(c, 0)); n   for (int i = 0; i < n + r; i++) n     for (int j = 0; j < m + c; j++) { n       int cur = str[i % n][j % m] -  a ; n       double ang = 2 * pi * cur / 26; n       a[i][j] = cd(cos(ang), sin(ang)); n     } n   int cnt = 0; n   for (int i = 0; i < r; i++) n     for (int j = 0; j < c; j++) { n       if (pat[i][j] ==  ? ) continue; n       cnt++; n       int cur = pat[i][j] -  a ; n       double ang = 2 * pi * cur / 26; n       b[r - 1 - i][c - 1 - j] = cd(cos(ang), -sin(ang)); n     } n   vector<vector<int>> ret; n   ret = mult(a, b); n   for (int i = r - 1; i < n + r - 1; i++) { n     for (int j = c - 1; j < m + c - 1; j++) cout << (ret[i][j] == cnt); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 5; n int boy[maxn], girl[maxn]; n int n, m; n int main() { n   int b, g; n   while (scanf( %d%d , &n, &m) != EOF) { n     memset(boy, 0, sizeof(boy)); n     memset(girl, 0, sizeof(girl)); n     int in; n     scanf( %d , &b); n     for (int i = 0; i < b; i++) { n       scanf( %d , &in); n       boy[in] = 1; n     } n     scanf( %d , &g); n     for (int i = 0; i < g; i++) { n       scanf( %d , &in); n       girl[in] = 1; n     } n     bool flag; n     int stb = 0, stg = 0; n     int cnt = maxn * maxn; n     while (cnt) { n       flag = true; n       stb %= n; n       stg %= m; n       if (boy[stb] || girl[stg]) { n         boy[stb] = 1; n         girl[stg] = 1; n       } n       for (int i = 0; i < n; i++) { n         if (!boy[i]) { n           flag = false; n           break; n         } n       } n       for (int i = 0; i < m; i++) { n         if (!girl[i]) { n           flag = false; n           break; n         } n       } n       if (flag) break; n       stb++; n       stg++; n       cnt--; n     } n     printf( %s n , flag ?  Yes  :  No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9; n long double sqr(long double a) { return a * a; } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   long long k; n   cin >> k; n   map<long long, pair<long long, long long> > sm; n   for (auto i = 0; i < k; ++i) { n     long long n, sum = 0; n     cin >> n; n     vector<long long> a(n); n     for (auto &it : a) { n       cin >> it; n       sum += it; n     } n     for (auto j = 0; j < n; ++j) { n       if (sm.find(sum - a[j]) != sm.end()) { n         cout <<  YES n  n              << sm[sum - a[j]].first <<     << sm[sum - a[j]].second <<   n  n              << i + 1 <<     << j + 1; n         return 0; n       } n     } n     for (auto j = 0; j < n; ++j) { n       sm[sum - a[j]] = {i + 1, j + 1}; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n int res[1100002]; n int mark[1100002]; n void set(int i, int L, int R, int l, int r, int x) { n   int LR, RL; n   if (L <= R && l <= r && L <= l && r <= R && res[i] <= 0 && mark[i] == 0) { n     if (L == l && R == r) mark[i] = 1; n     if (res[i] == 0 && L == l && R == r) n       res[i] = x; n     else { n       if (res[i] >= 0) { n         res[(i << 1) + 1] = res[i]; n         res[(i << 1) + 2] = res[i]; n         res[i] = -1; n         mark[(i << 1) + 1] = 0; n         mark[(i << 1) + 2] = 0; n       } n       LR = L + ((R - L) >> 1); n       RL = LR + 1; n       if (l <= LR) { n         if (r < LR) n           set((i << 1) + 1, L, LR, l, r, x); n         else n           set((i << 1) + 1, L, LR, l, LR, x); n       } n       if (r >= RL) { n         if (l > RL) n           set((i << 1) + 2, RL, R, l, r, x); n         else n           set((i << 1) + 2, RL, R, RL, r, x); n       } n     } n   } n } n void out(int i, int L, int R) { n   int j; n   if (res[i] >= 0) n     for (j = L; j <= R; j++) printf( %d  , res[i]); n   else { n     out((i << 1) + 1, L, L + ((R - L) >> 1)); n     out((i << 1) + 2, L + ((R - L) >> 1) + 1, R); n   } n } n int main() { n   int i; n   int n; n   int m; n   int l, r, x; n   scanf( %d%d , &n, &m); n   res[0] = 0; n   mark[0] = 0; n   for (i = 0; i < m; i++) { n     scanf( %d%d%d , &l, &r, &x); n     if (x - 1 >= l) set(0, 1, n, l, x - 1, x); n     if (x + 1 <= r) set(0, 1, n, x + 1, r, x); n   } n   out(0, 1, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5; n char s[maxn]; n int a[maxn]; n int n, l, r, i, x; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (i = 1; i <= n; i++) a[i] = s[i] -  0 ; n   l = 1; n   r = n; n   if (a[l] != a[r]) { n     a[l]--; n     a[l + 1] += 10; n     if (!a[l]) n       l++; n     else if (a[l] != a[r]) { n       printf( 0 ); n       return 0; n     } n   } n   while (l <= r) { n     if (a[l] >= a[r] + 10 && a[r] < 10) a[r - 1]--, a[r] += 10; n     if (a[l] == a[r] + 1) a[l + 1] += 10, a[l]--; n     if (a[l] != a[r]) break; n     if (l != r) { n       a[l] = a[l] - a[l] / 2; n       a[r] -= a[l]; n     } else if ((a[l] & 1) == 0) n       a[l] >>= 1; n     else n       break; n     if (a[l] < 0 || a[l] > 9 || a[r] < 0 || a[r] > 9) break; n     l++; n     r--; n   } n   if (l <= r) { n     printf( 0 ); n   } else { n     l = 1; n     if (!a[l]) l++; n     while (l <= n) { n       printf( %d , a[l]); n       l++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Imx = 2147483647; n const long long Lbig = 2e18; n const int mod = 1e9 + 7; n struct fastio { n   char s[100000]; n   int it, len; n   fastio() { it = len = 0; } n   inline char get() { n     if (it < len) return s[it++]; n     it = 0; n     len = fread(s, 1, 100000, stdin); n     if (len == 0) n       return EOF; n     else n       return s[it++]; n   } n   bool notend() { n     char c = get(); n     while (c ==     || c ==   n ) c = get(); n     if (it > 0) it--; n     return c != EOF; n   } n } _buff; n inline long long getnum() { n   long long r = 0; n   bool ng = 0; n   char c; n   c = _buff.get(); n   while (c !=  -  && (c <  0  || c >  9 )) c = _buff.get(); n   if (c ==  - ) ng = 1, c = _buff.get(); n   while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = _buff.get(); n   return ng ? -r : r; n } n template <class T> n inline void putnum(T x) { n   if (x < 0) putchar( - ), x = -x; n   register short a[20] = {}, sz = 0; n   while (x) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n inline char getreal() { n   char c = _buff.get(); n   while (c ==     || c ==   n ) c = _buff.get(); n   return c; n } n int n; n int head[500111], nxt[1000111], to[1000111], tot = 1; n int head2[500111], nxt2[1000111], to2[1000111], tot2 = 1; n int f[500111]; n bool oc[500111], oc2[500111]; n void adde(int x, int y) { n   nxt[++tot] = head[x]; n   head[x] = tot; n   to[tot] = y; n } n void adde2(int x, int y) { n   nxt2[++tot2] = head2[x]; n   head2[x] = tot2; n   to2[tot2] = y; n } n int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); } n map<pair<int, int>, int> mpid; n int nid[500111]; n void dfs(int x, int p = -1) { n   for (int i = head[x]; i; i = nxt[i]) { n     int u = to[i]; n     if (u == p) continue; n     dfs(u, x); n     if (oc[i >> 1]) continue; n     int tmp = nid[gf(u)]; n     if (!tmp) continue; n     putnum(u), putchar(   ), putnum(x), putchar(   ); n     putnum(to2[tmp]), putchar(   ), putnum(to2[tmp ^ 1]), putchar(  n ); n   } n } n void dfs2(int x, int p = -1) { n   for (int i = head2[x]; i; i = nxt2[i]) { n     int u = to2[i]; n     if (u == p) continue; n     if (!oc2[i >> 1]) nid[gf(u)] = i; n     dfs2(u, x); n   } n } n int main() { n   n = getnum(); n   for (int i = 1; i <= n; i++) f[i] = i; n   for (int i = 1; i < n; i++) { n     int x, y; n     x = getnum(), y = getnum(); n     if (x > y) swap(x, y); n     mpid[make_pair(x, y)] = i; n     adde(x, y); n     adde(y, x); n   } n   int cnt = n - 1; n   for (int i = 1; i < n; i++) { n     int x, y; n     x = getnum(), y = getnum(); n     if (x > y) swap(x, y); n     if (mpid.find(make_pair(x, y)) != mpid.end()) { n       oc[mpid[make_pair(x, y)]] = 1; n       oc2[i] = 1; n       cnt--; n       f[gf(x)] = gf(y); n     } n     adde2(x, y); n     adde2(y, x); n   } n   dfs2(1); n   putnum(cnt), putchar(  n ); n   dfs(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, c1 = 0, c2 = 0, i1 = 0, i2 = 0, t, cou = 0; n   cin >> x >> y; n   if (y < x) { n     t = x; n     x = y; n     y = t; n   } n   while (x != y) { n     if (c1 <= c2) { n       if (c1 == 0) c1 = 1; n       x++; n       i1++; n       c1 += i1; n       cou += i1; n     } else { n       if (c2 == 0) c2 = 1; n       y--; n       i2++; n       c2 += i2; n       cou += i2; n     } n   } n   cout << cou; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q, w, e, r; n   cin >> q >> w >> e >> r; n   string s; n   cin >> s; n   unsigned long long int c = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) { n       c += q; n     } n     if (s[i] ==  2 ) { n       c += w; n     } n     if (s[i] ==  3 ) { n       c += e; n     } n     if (s[i] ==  4 ) { n       c += r; n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   char arr[26]; n   map<int, int> mmap; n   for (int i = 0; i < 26; i++) { n     mmap[i] = i; n     arr[i] = i; n   } n   for (int i = 0; i < m; i++) { n     char pr, nx; n     cin >> pr >> nx; n     arr[mmap[pr -  a ]] = nx -  a ; n     arr[mmap[nx -  a ]] = pr -  a ; n     int temp = mmap[nx -  a ]; n     mmap[nx -  a ] = mmap[pr -  a ]; n     mmap[pr -  a ] = temp; n     for (int i = 0; i < 26; i++) { n       if (false) n         cout << (char)( a  + i) <<   ->   << (char)(arr[i] +  a ) << endl; n     } n   } n   string ans =   ; n   for (int i = 0; i < s.length(); i++) { n     ans += (char)(arr[s[i] -  a ] +  a ); n   } n   cout << ans << endl; n } n int main() { solve(); } n


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


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n char str[2001000], g[2001000]; n int a[2001000]; n int main() { n   scanf( %s , str); n   int m; n   scanf( %d , &m); n   memset((a), (0), sizeof(a)); n   for (int(i) = (0); (i) < (m); ++(i)) { n     int x; n     scanf( %d , &x); n     x--; n     a[x] ^= 1; n   } n   int n = strlen(str); n   int k = 0; n   for (int(i) = (0); (i) < (n / 2); ++(i)) { n     k ^= a[i]; n     if (k) { n       g[i] = str[n - 1 - i]; n       g[n - 1 - i] = str[i]; n     } else { n       g[i] = str[i]; n       g[n - 1 - i] = str[n - 1 - i]; n     } n   } n   if (n & 1) g[n >> 1] = str[n >> 1]; n   g[n] = 0; n   printf( %s n , g); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 10; n char s[M]; n map<char, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < n; i++) { n     mp[s[i]]++; n   } n   int minn1 = M; n   if (mp[ o ] && mp[ n ] && mp[ e ]) { n     minn1 = min(minn1, mp[ o ]); n     minn1 = min(minn1, mp[ n ]); n     minn1 = min(minn1, mp[ e ]); n     mp[ o ] -= minn1; n     mp[ e ] -= minn1; n   } n   int minn2 = M; n   if (mp[ z ] && mp[ e ] && mp[ r ] && mp[ o ]) { n     minn2 = min(minn2, mp[ z ]); n     minn2 = min(minn2, mp[ e ]); n     minn2 = min(minn2, mp[ r ]); n     minn2 = min(minn2, mp[ o ]); n   } n   if (minn1 == M) minn1 = 0; n   if (minn2 == M) minn2 = 0; n   for (int i = 1; i <= minn1; i++) printf( 1  ); n   for (int i = 1; i <= minn2; i++) printf( 0  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n   return 0; n } n long long num(long long x, long long n, long long m) { n   long long res = 0; n   --x; n   for (long long i = 1; i <= n; i++) res += min(x / i, m); n   return res; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   long long k; n   cin >> k; n   long long l = 1; n   long long r = n * m + 1; n   while (l < r) { n     long long x = (l + r) >> 1; n     if (num(x, n, m) < k) n       l = x + 1; n     else n       r = x; n   } n   cout << l - 1; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[210000], pos[210000], Log2[210000]; n int f[210000][21]; n void init() { n   for (int i = 1; i <= n; i++) f[i][0] = i; n   for (int j = 1; (1 << j) <= n; j++) n     for (int i = 1; i + (1 << j) - 1 <= n; i++) n       if (a[f[i][j - 1]] > a[f[i + (1 << (j - 1))][j - 1]]) n         f[i][j] = f[i][j - 1]; n       else n         f[i][j] = f[i + (1 << (j - 1))][j - 1]; n } n int query(int l, int r) { n   int k = Log2[r - l + 1]; n   if (a[f[l][k]] > a[f[r - (1 << k) + 1][k]]) n     return f[l][k]; n   else n     return f[r - (1 << k) + 1][k]; n } n int dfs(int l, int r) { n   if (l >= r) return 0; n   int k = query(l, r), ans = 0, v; n   if (k - l <= r - k) n     for (int i = l; i <= k; i++) { n       v = a[k] - a[i]; n       if (pos[v] > k && pos[v] <= r) ans++; n     } n   else n     for (int i = k + 1; i <= r; i++) { n       v = a[k] - a[i]; n       if (pos[v] >= l && pos[v] <= k) ans++; n     } n   return ans + dfs(l, k - 1) + dfs(k + 1, r); n } n int main() { n   scanf( %d , &n); n   Log2[0] = -1; n   for (int i = 1; i <= n; i++) Log2[i] = Log2[i >> 1] + 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     pos[a[i]] = i; n   } n   init(); n   printf( %d n , dfs(1, n)); n   return 0; n } n


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