#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-loops ) n using namespace std; n namespace { n const int MOD107 = 1000000007; n const int MOD998 = 998244353; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <class T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <class T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n struct Modular { n  private: n   long long MODVALUE = 0; n   long long val = 0; n   Modular initModular(long long val) const { return Modular(val, MODVALUE); } n   inline void validate() const {} n   inline void validate(const Modular& other) const {} n  n  public: n   explicit operator long long() const { return val; } n   template <class T> n   Modular& operator=(const T& other) { n     val = (other % MODVALUE + MODVALUE) % MODVALUE; n     return *this; n   } n   long long getMod() { return MODVALUE; } n   template <class T> n   void setMod(const T& MOD) { n     MODVALUE = MOD; n   } n   bool operator==(const Modular& other) const { n     validate(other); n     return val == other.val; n   } n   bool operator!=(const Modular& other) const { return !(*this == other); } n   Modular& operator+=(const Modular& other) { n     validate(other); n     val += other.val; n     if (val >= MODVALUE) val -= MODVALUE; n     return *this; n   } n   Modular operator+(const Modular& other) const { n     Modular tmp = *this; n     return tmp += other; n   } n   template <class T> n   Modular& operator+=(const T& other) { n     return *this += initModular(other); n   } n   template <class T> n   Modular operator+(const T& other) const { n     return *this + initModular(other); n   } n   template <class T> n   friend Modular operator+(const T& other, const Modular& m) { n     return m.initModular(other) + m; n   } n   Modular& operator++() { return *this += 1; } n   Modular operator++(int) { n     Modular tmp = *this; n     ++*this; n     return tmp; n   } n   Modular operator-() const { return initModular(-val); } n   Modular& operator-=(const Modular& other) { n     validate(other); n     val -= other.val; n     if (val < 0) val += MODVALUE; n     return *this; n   } n   Modular operator-(const Modular& other) const { n     Modular tmp = *this; n     return tmp -= other; n   } n   template <class T> n   Modular& operator-=(const T& other) { n     return *this -= initModular(other); n   } n   template <class T> n   Modular operator-(const T& other) const { n     return *this - initModular(other); n   } n   template <class T> n   friend Modular operator-(const T& other, const Modular& m) { n     return m.initModular(other) - m; n   } n   Modular& operator--() { return *this -= 1; } n   Modular operator--(int) { n     Modular tmp = *this; n     --*this; n     return tmp; n   } n   Modular& operator*=(const Modular& other) { n     validate(other); n     val *= other.val; n     val %= MODVALUE; n     if (val < 0) val += MODVALUE; n     return *this; n   } n   Modular operator*(const Modular& other) const { n     Modular tmp = *this; n     return tmp *= other; n   } n   template <class T> n   Modular& operator*=(const T& other) { n     return *this *= initModular(other); n   } n   template <class T> n   Modular operator*(const T& other) const { n     return *this * initModular(other); n   } n   template <class T> n   friend Modular operator*(const T& other, const Modular& m) { n     return m.initModular(other) * m; n   } n   Modular pow(long long power) const { n     Modular m = *this, ans = initModular(1); n     while (power) { n       if (power & 1) ans *= m; n       m *= m; n       power >>= 1; n     } n     return ans; n   } n   Modular pow(const Modular& other) const { return pow(other.val); } n   Modular inv() const { return pow(MODVALUE - 2); } n   Modular& operator/=(const Modular& other) { n     val *= other.inv().val; n     val %= MODVALUE; n     if (val < 0) val += MODVALUE; n     return *this; n   } n   Modular operator/(const Modular& other) const { n     Modular tmp = *this; n     return tmp /= other; n   } n   template <class T> n   Modular& operator/=(const T& other) { n     return *this /= initModular(other); n   } n   template <class T> n   Modular operator/(const T& other) const { n     return *this / initModular(other); n   } n   template <class T> n   friend Modular operator/(const T& other, const Modular& m) { n     return m.initModular(other) / m; n   } n   friend istream& operator>>(istream& in, Modular& m) { n     in >> m.val; n     m.val %= m.MODVALUE; n     if (m.val < 0) m.val += m.MODVALUE; n     return in; n   } n   friend ostream& operator<<(ostream& out, const Modular& m) { n     return out << m.val; n   } n   Modular() { n     val = 0; n     MODVALUE = 0; n   } n   Modular(long long val, long long MOD) n       : MODVALUE(MOD), val((val % MOD + MOD) % MOD) {} n }; n using Mint = Modular; n inline ll binpow(ll b, ll p, ll mod) { n   b %= mod; n   ll ans = 1; n   for (; p > 0; p >>= 1) { n     if (p & 1) { n       ans *= b; n       ans %= mod; n     } n     b *= b; n     b %= mod; n   } n   return ans; n } n inline ll max(ll a, int b) { return (a > b ? a : b); } n inline ll max(int a, ll b) { return (a > b ? a : b); } n inline ll min(ll a, int b) { return (a < b ? a : b); } n inline ll min(int a, ll b) { return (a < b ? a : b); } n template <class T> n inline bool chkmin(T& x, const T& y) { n   return y < x ? x = y, 1 : 0; n } n template <class T> n inline bool chkmax(T& x, const T& y) { n   return x < y ? x = y, 1 : 0; n } n }  // namespace n namespace IO { n struct InputWrapper { n  private: n   istream& in; n  n  public: n   template <class T> n   inline void _R(T& x) { n     in >> x; n   } n   template <class T1, class T2> n   inline void _R(pair<T1, T2>& x) { n     _R(x.first); n     _R(x.second); n   } n   template <class T> n   inline void _R(vector<T>& x) { n     for (auto& i : x) _R(i); n   } n   template <class T> n   inline void _R(deque<T>& x) { n     for (auto& i : x) _R(i); n   } n   inline void R() {} n   template <class T1, class... T2> n   inline void R(T1& x, T2&... y) { n     _R(x); n     R(y...); n   } n   template <class... T> n   inline void operator()(T&... x) { n     R(x...); n   } n   InputWrapper(istream& in) : in(in) {} n } R(cin); n struct OutputWrapper { n  private: n   ostream& out; n  n  public: n   template <class Container> n   inline void printContainer(const Container& x) { n     bool isFirst = 1; n     for (auto i : x) { n       if (!isFirst) out <<    ; n       isFirst = 0; n       _W(i); n     } n   } n   template <class T> n   inline void _W(const T& x) { n     out << x; n   } n   template <class T1, class T2> n   inline void _W(const pair<T1, T2>& p) { n     _W(p.first); n     _W(   ); n     _W(p.second); n   } n   template <class... T> n   inline void _W(const vector<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const deque<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const forward_list<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const list<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const set<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const multiset<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const unordered_set<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const unordered_multiset<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const map<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const multimap<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const unordered_map<T...>& x) { n     printContainer(x); n   } n   template <class... T> n   inline void _W(const unordered_multimap<T...>& x) { n     printContainer(x); n   } n   inline void W() { out <<   n ; } n   template <class T> n   inline void W(const T& x) { n     _W(x); n     W(); n   } n   template <class T1, class... T2> n   inline void W(const T1& x, const T2&... y) { n     _W(x); n     _W(   ); n     W(y...); n   } n   template <class... T> n   inline void operator()(const T&... x) { n     W(x...); n   } n   OutputWrapper(ostream& out) : out(out) {} n } W(cout), D(cerr); n }  // namespace IO n using namespace IO; n namespace outputFormat { n inline string Case(int tc) { return  Case #  + to_string(tc) +  : ; } n inline string YN(bool b) { return b ?  Yes  :  No ; } n }  // namespace outputFormat n using namespace outputFormat; n namespace std { n template <class T1, class T2> n struct hash<pair<T1, T2>> { n   std::size_t operator()(pair<T1, T2> _) const { n     return hash<T1>{}(_.first) ^ hash<T2>{}(_.second); n   } n }; n }  // namespace std n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     R(n); n     string s, t; n     R(s, t); n     vector<vector<Mint>> cnt_table(n + 1, n                                    vector<Mint>(2 * n + 7, Mint(0, MOD107))), n         sum_table = cnt_table; n     vector<Mint*> cnt(n + 1), sum(n + 1); n     for (int i = 0; i <= n; i++) { n       cnt[i] = &cnt_table[i][n + 3]; n       sum[i] = &sum_table[i][n + 3]; n     } n     cnt[0][0] = 1; n     vector<vector<bool>> can(4, vector<bool>(n + 1, false)); n     for (int i = 1; i <= n; i++) { n       can[0][i] = s[i - 1] !=  1  && t[i - 1] !=  1 ; n       can[1][i] = s[i - 1] !=  1  && t[i - 1] !=  0 ; n       can[2][i] = s[i - 1] !=  0  && t[i - 1] !=  1 ; n       can[3][i] = s[i - 1] !=  0  && t[i - 1] !=  0 ; n       if (i % 2 == 1) swap(can[1][i], can[2][i]); n     } n     for (int i = 1; i <= n; i++) { n       for (int j = -i; j <= i; j++) { n         if (can[0][i]) { n           cnt[i][j] += cnt[i - 1][j]; n           sum[i][j] += sum[i - 1][j]; n         } n         if (can[1][i]) { n           cnt[i][j] += cnt[i - 1][j - 1]; n           sum[i][j] += sum[i - 1][j - 1] + n                        cnt[i - 1][j - 1] * i * (abs(j) < abs(j - 1) ? +1 : -1); n         } n         if (can[2][i]) { n           cnt[i][j] += cnt[i - 1][j + 1]; n           sum[i][j] += sum[i - 1][j + 1] + n                        cnt[i - 1][j + 1] * i * (abs(j) < abs(j + 1) ? +1 : -1); n         } n         if (can[3][i]) { n           cnt[i][j] += cnt[i - 1][j]; n           sum[i][j] += sum[i - 1][j]; n         } n       } n     } n     W(sum[n][0]); n   } n   return 0; n } n static auto __init__ = []() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(10); n   return 42; n }(); n


#include <bits/stdc++.h> n using namespace std; n ofstream file( debug.txt ); n template <typename Int> n ostream &operator<<(ostream &s, const vector<Int> &v) { n   s <<  [ ; n   for (auto i = v.begin(); i != v.end(); i++) { n     s << *i; n     if (i != prev(v.end())) s <<  ,  ; n   } n   s <<  ] ; n   return s; n } n template <typename Int, typename INT> n ostream &operator<<(ostream &s, const map<Int, INT> &mp) { n   s <<  [ ; n   for (auto &i : mp) { n     s <<  [  << i.first <<  ,   << i.second <<  ] ; n     if (i != *mp.rbegin()) s <<  ,  ; n   } n   s <<  ] ; n   return s; n } n template <typename Int> n ostream &operator<<(ostream &s, const set<Int> &st) { n   s <<  [ ; n   for (auto &i : st) { n     s << i; n     if (i != *st.rbegin()) s <<  ,  ; n   } n   s <<  ] ; n   s <<   n ; n   return s; n } n template <typename Int, typename INt> n ostream &operator<<(ostream &s, const pair<Int, INt> &x) { n   s <<  [  << x.first <<  ,   << x.second <<  ] ; n   return s; n } n template <typename T> n void print(T *x, int n) { n   for (int i = 0; i < n; i++) { n     file << x[i] <<    n [i == n - 1]; n   } n } n template <typename T> n ostream &operator<<(ostream &s, const complex<T> &a) { n   s <<  (  << a.real() <<  ,   << a.imag() <<  ) ; n   return s; n } n template <typename T> n T debug(const T &x, const string &s =   ) { n   file << s <<   =   << x <<   n  << flush; n   return x; n } n const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1}; n const int dc[]{0, 1, 1, 1, 0, -1, -1, -1}; n const int N = 2e5 + 10; n int n, m, q, ans; n bool dead[N]; n set<int> edg[N]; n void add(int u, int v) { n   if (!dead[u]) { n     dead[u] = 1; n     ans--; n   } n   edg[u].insert(v); n } n void rem(int u, int v) { n   if (dead[u] && edg[u].size() == 1) { n     dead[u] = 0; n     ans++; n   } n   edg[u].erase(v); n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   ans = n; n   for (int i = 0, u, v; i < m; i++) { n     cin >> u >> v; n     if (u > v) swap(u, v); n     add(u, v); n   } n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t < 3) { n       int u, v; n       cin >> u >> v; n       if (u > v) swap(u, v); n       if (t == 1) n         add(u, v); n       else n         rem(u, v); n     } else n       cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s, ans, x, y, a; n   bool flag = false; n   cin >> n >> s; n   s = s * 100; n   ans = -1; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     a = x * 100 + y; n     if (a <= s) { n       ans = max(ans, (100 - a % 100) % 100); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n bool work() { n   int a = read(), b = read(); n   if (a > b) swap(a, b); n   if (a == 0) return (b == 0); n   if (a * 2 < b) return 0; n   if (a * 2 == b) return 1; n   int c = (abs(b - a - a)); n   return (a + b) % 3 == 0; n } n signed main() { n   int n = read(); n   while (n--) n     if (work()) n       puts( YES ); n     else n       puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100010; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long n, m, p, a[N], b[N], q[N], yue[N], miu[N], f[N], tot; n long long ans; n long long power(long long a, long long b, long long m) { n   long long re = 1; n   while (b) { n     if (b & 1) re = (long long)re * a % m; n     a = (long long)a * a % m; n     b >>= 1; n   } n   return re; n } n long long calc(long long x) { n   long long i, j, k, l; n   for (i = 1; i <= tot; i++) n     if (power(x, yue[i], p) == 1) return yue[i]; n } n int main() { n   long long i, j, k; n   scanf( %I64d%I64d%I64d , &n, &m, &p); n   for (i = 1; i * i <= p - 1; i++) { n     if ((p - 1) % i == 0) { n       yue[++tot] = i; n       if (i * i < p - 1) yue[++tot] = (p - 1) / i; n     } n   } n   sort(yue + 1, yue + tot + 1); n   for (i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (i = 1, k = 0; i <= m; i++) { n     scanf( %I64d , &j); n     k = gcd(k, j); n   } n   k = gcd(k, p - 1); n   for (i = 1; i <= n; i++) { n     q[i] = calc(a[i]); n     a[i] = (p - 1) * gcd(q[i], k) / q[i]; n   } n   for (i = 1; i <= tot; i++) n     for (j = 1; j <= n; j++) n       if (yue[i] % a[j] == 0) { n         f[i] = 1; n         break; n       } n   for (i = 1; i <= tot; i++) { n     ans += (f[i] - miu[i]) * ((p - 1) / yue[i]); n     for (j = i + 1; j <= tot; j++) n       if (yue[j] % yue[i] == 0) miu[j] += (f[i] - miu[i]); n     miu[i] += (f[i] - miu[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1.0); n int n, m; n bool check(int x) { n   int a2 = x / 2, a3 = x / 3, a6 = x / 6; n   a2 -= a6; n   a3 -= a6; n   if (max(n - a2, 0) + max(m - a3, 0) <= a6) n     return true; n   else n     return false; n } n int main() { n   cin >> n >> m; n   int l = 1, r = 1e7; n   int ans = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n const int K = 23; n const long long INF = 1e18; n int id[N], a[N], b[N], w[N]; n int n, m; n vector<int> pos[N]; n long long dp[N]; n int ls[N * K], rs[N * K], rootcnt = 0; n long long vs[N * K]; n void copy(int root, int newroot) { n   ls[newroot] = ls[root]; n   rs[newroot] = rs[root]; n   vs[newroot] = vs[root]; n } n void updates(int root, int& newroot, int l, int r, int pos, int v) { n   newroot = ++rootcnt; n   copy(root, newroot); n   vs[newroot] += v; n   if (l == r || v == 0) return; n   int m = (l + r) >> 1; n   if (pos <= m) n     updates(ls[root], ls[newroot], l, m, pos, v); n   else n     updates(rs[root], rs[newroot], m + 1, r, pos, v); n } n long long querys(int lt, int rt, int l, int r, int a, int b) { n   if (l == a && r == b) { n     return vs[rt] - vs[lt]; n   } n   int m = (l + r) >> 1; n   if (b <= m) n     return querys(ls[lt], ls[rt], l, m, a, b); n   else if (a > m) n     return querys(rs[lt], rs[rt], m + 1, r, a, b); n   else { n     return querys(ls[lt], ls[rt], l, m, a, m) + n            querys(rs[lt], rs[rt], m + 1, r, m + 1, b); n   } n } n long long val[N << 2]; n long long tag[N << 2]; n void build(int root, int l, int r) { n   val[root] = -INF; n   tag[root] = 0; n   if (l == r) return; n   int m = (l + r) >> 1; n   build((root << 1), l, m); n   build((root << 1 | 1), m + 1, r); n } n void pushdown(int root) { n   long long c = tag[root]; n   tag[root] = 0; n   val[(root << 1)] += c; n   tag[(root << 1)] += c; n   val[(root << 1 | 1)] += c; n   tag[(root << 1 | 1)] += c; n } n void setv(int root, int l, int r, int pos, long long v) { n   if (l == r) { n     val[root] = v; n     return; n   } n   pushdown(root); n   int m = (l + r) >> 1; n   if (pos <= m) n     setv((root << 1), l, m, pos, v); n   else n     setv((root << 1 | 1), m + 1, r, pos, v); n   val[root] = max(val[(root << 1)], val[(root << 1 | 1)]); n } n void update(int root, int l, int r, int a, int b, long long v) { n   if (v == 0) return; n   if (a > b) return; n   if (l == a && r == b) { n     val[root] += v; n     tag[root] += v; n     return; n   } n   pushdown(root); n   int m = (l + r) >> 1; n   if (b <= m) n     update((root << 1), l, m, a, b, v); n   else if (a > m) n     update((root << 1 | 1), m + 1, r, a, b, v); n   else { n     update((root << 1), l, m, a, m, v); n     update((root << 1 | 1), m + 1, r, m + 1, b, v); n   } n   val[root] = max(val[(root << 1)], val[(root << 1 | 1)]); n } n long long query(int root, int l, int r, int a, int b) { n   if (a > b) return -INF; n   if (l == a && r == b) return val[root]; n   pushdown(root); n   int m = (l + r) >> 1; n   if (b <= m) n     return query((root << 1), l, m, a, b); n   else if (a > m) n     return query((root << 1 | 1), m + 1, r, a, b); n   else n     return max(query((root << 1), l, m, a, m), n                query((root << 1 | 1), m + 1, r, m + 1, b)); n } n bool hasAns() { n   int len = 0; n   for (int i = 1; i <= n && len < m; i++) { n     if (a[i] == b[len + 1]) len++; n   } n   return len == m; n } n int root[N]; n long long lessSum(int l, int r, int v) { n   if (l > r || v <= 0) return 0; n   return querys(root[l - 1], root[r], 1, n, 1, v); n } n void solve() { n   scanf( %d , &n); n   root[0] = 0; n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     scanf( %d , w + i); n     updates(root[i - 1], root[i], 1, n, a[i], max(0, w[i])); n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d , b + i), id[b[i]] = i; n   for (int i = 1; i <= n; i++) { n     if (id[a[i]]) pos[id[a[i]]].push_back(i); n   } n   if (!hasAns()) { n     printf( NO n ); n     return; n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) dp[i] = -INF; n   build(1, 1, n); n   for (auto p : pos[m]) { n     dp[p] = lessSum(p + 1, n, b[m]) + w[p]; n     setv(1, 1, n, p, dp[p]); n   } n   for (int i = m - 1; i >= 1; i--) { n     vector<int> vec; n     for (auto j : pos[i + 1]) vec.push_back(j); n     for (auto j : pos[i]) vec.push_back(j); n     sort(vec.begin(), vec.end()); n     for (int j = (int)vec.size() - 2; j >= 0; j--) { n       update(1, 1, n, vec[j + 1], n, lessSum(vec[j] + 1, vec[j + 1] - 1, b[i])); n       if (a[vec[j]] == b[i]) { n         dp[vec[j]] = query(1, 1, n, vec[j + 1], n) + w[vec[j]]; n         if (w[vec[j]] > 0) { n           update(1, 1, n, vec[j + 1], n, w[vec[j]]); n         } n       } n     } n     for (int j = (int)vec.size() - 2; j >= 0; j--) { n       update(1, 1, n, vec[j + 1], n, n              -lessSum(vec[j] + 1, vec[j + 1] - 1, b[i])); n       if (a[vec[j]] == b[i]) { n         if (w[vec[j]] > 0) { n           update(1, 1, n, vec[j + 1], n, -w[vec[j]]); n         } n       } n     } n     for (auto j : pos[i + 1]) { n       setv(1, 1, n, j, -INF); n     } n     assert(val[1] == -INF); n     for (auto j : pos[i]) { n       setv(1, 1, n, j, dp[j]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans += w[i]; n   ans -= val[1]; n   printf( %lld n , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Pow_Mod(long long a, long long b) { n   if (b == 0) return 1; n   long long tmp = Pow_Mod(a, b / 2); n   tmp = (tmp * tmp) % 1000003; n   if (b % 2) return (tmp * a) % 1000003; n   return tmp; n } n long long C(long long a, long long b) { n   if (b == 0 || b == a) return 1; n   long long ans = 1; n   for (int i = 1; i <= b; i++) { n     ans = (ans * (a - i + 1)) % 1000003; n     ans = (ans * Pow_Mod(i, 1000003 - 2)) % 1000003; n   } n   return ans; n } n int main() { n   long long a, b; n   cin >> a >> b; n   a += b; n   cout << (C(a, b) - 1 + 1000003) % 1000003 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     int n, k, x, rem; n     cin >> n >> k; n     x = k / (n - 1); n     rem = k - x * (n - 1); n     if (!rem) n       cout << x * n - 1 <<   n ; n     else n       cout << x * n + rem <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n using pii = pair<int, int>; n const int MOD = 1e9 + 7, N = 2e3 + 10; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<pair<long long, long long>> v(n); n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     long long cm = -1; n     long long salary; n     for (long long i = 0; i < x; i++) { n       cin >> salary; n       if (salary > cm) cm = salary; n     } n     v[i].first = cm; n     v[i].second = x; n   } n   sort(v.begin(), v.end()); n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     sum += (v[n - 1].first - v[i].first) * v[i].second; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 123; n const int mod = 1e9 + 9; n int n; n long long k, d; n int a[maxn]; n int m; n int L[maxn]; n long long t[maxn * 4]; n long long ad[maxn * 4]; n void build(int v, int l, int r) { n   ad[v] = 0; n   t[v] = 0; n   if (l == r) { n   } else { n     int mid = (l + r) / 2; n     build(v * 2, l, mid); n     build(v * 2 + 1, mid + 1, r); n   } n } n void push(int v, int tl, int tr) { n   if (tl == tr) return; n   t[v * 2] += ad[v]; n   t[v * 2 + 1] += ad[v]; n   ad[v * 2] += ad[v]; n   ad[v * 2 + 1] += ad[v]; n   ad[v] = 0; n } n int get(int v, int tl, int tr, int l, int r, long long k) { n   push(v, tl, tr); n   if (tl > r || l > tr) { n     return -1; n   } n   if (t[v] > k) { n     return -1; n   } n   if (tl == tr) { n     return tl; n   } n   int mid = (tl + tr) / 2; n   int res = get(v * 2, tl, mid, l, r, k); n   if (res != -1) return res; n   return get(v * 2 + 1, mid + 1, tr, l, r, k); n } n void add(int v, int tl, int tr, int l, int r, long long d) { n   push(v, tl, tr); n   if (tl > r || l > tr) { n     return; n   } n   if (l <= tl && tr <= r) { n     t[v] += d; n     ad[v] += d; n     return; n   } n   int mid = (tl + tr) / 2; n   add(v * 2, tl, mid, l, r, d); n   add(v * 2 + 1, mid + 1, tr, l, r, d); n   t[v] = min(t[v * 2], t[v * 2 + 1]); n } n pair<int, int> calc(vector<int> b) { n   m = b.size(); n   map<int, int> c; n   for (int i = 0; i < m; i++) { n     if (c.count(b[i]) == 0) { n       L[i] = 0; n     } else { n       L[i] = c[b[i]] + 1; n     } n     c[b[i]] = i; n   } n   for (int i = 1; i < m; i++) { n     L[i] = max(L[i], L[i - 1]); n   } n   build(1, 0, m - 1); n   vector<int> mx; n   vector<int> mi; n   pair<int, int> ans = make_pair(0, 0); n   for (int i = 0; i < m; i++) { n     while (mx.size() > 0 && b[mx.back()] < b[i]) { n       int cur = mx.back(); n       mx.pop_back(); n       if (mx.size() > 0) { n         add(1, 0, m - 1, mx.back() + 1, cur, -b[cur]); n       } else { n         add(1, 0, m - 1, 0, cur, -b[cur]); n       } n     } n     if (mx.size() > 0) { n       add(1, 0, m - 1, mx.back() + 1, i, b[i]); n     } else { n       add(1, 0, m - 1, 0, i, b[i]); n     } n     mx.push_back(i); n     while (mi.size() > 0 && b[mi.back()] > b[i]) { n       int cur = mi.back(); n       mi.pop_back(); n       if (mi.size() > 0) { n         add(1, 0, m - 1, mi.back() + 1, cur, b[cur]); n       } else { n         add(1, 0, m - 1, 0, cur, b[cur]); n       } n     } n     if (mi.size() > 0) { n       add(1, 0, m - 1, mi.back() + 1, i, -b[i]); n     } else { n       add(1, 0, m - 1, 0, i, -b[i]); n     } n     mi.push_back(i); n     int cnt = i - get(1, 0, m - 1, L[i], i, k) + 1; n     if (ans.first < cnt) { n       ans.first = cnt; n       ans.second = i; n     } n     add(1, 0, m - 1, 0, i, -1); n   } n   return ans; n } n void solve() { n   cin >> n >> k >> d; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int mi = *min_element(a, a + n); n   for (int i = 0; i < n; i++) { n     a[i] -= mi; n   } n   pair<int, int> ans = make_pair(0, 0); n   if (d == 0) { n     int cnt = 1; n     for (int i = 1; i < n; i++) { n       if (a[i] != a[i - 1]) { n         if (ans.first < cnt) { n           ans.first = cnt; n           ans.second = i - 1; n         } n         cnt = 1; n       } else { n         cnt++; n       } n     } n     if (ans.first < cnt) { n       ans.first = cnt; n       ans.second = n - 1; n     } n     cout << ans.second - ans.first + 2 <<     << ans.second + 1 <<   n ; n     return; n   } n   for (int i = 0; i < n;) { n     int j = i; n     vector<int> cur; n     while (i < n && a[j] % d == a[i] % d) { n       cur.push_back(a[i] / d); n       i++; n     } n     pair<int, int> res = calc(cur); n     res.second += j; n     if (res.first > ans.first) { n       ans = res; n     } n   } n   cout << ans.second - ans.first + 2 <<     << ans.second + 1 <<   n ; n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123, BITS = 11, MAX = 7; n int c[N], num[N]; n int a[MAX][MAX]; n char s[N]; n int bit(int x) { n   int res = 0; n   while (x != 0) { n     res += x % 2; n     x /= 2; n   } n   return res; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s); n     if (s[0] ==  R ) c[i] = 0; n     if (s[0] ==  G ) c[i] = 1; n     if (s[0] ==  B ) c[i] = 2; n     if (s[0] ==  Y ) c[i] = 3; n     if (s[0] ==  W ) c[i] = 4; n     num[i] = s[1] -  1 ; n   } n   int m = 0; n   for (int i = 0; i < n; ++i) { n     if (a[c[i]][num[i]] == 0) { n       ++m; n       a[c[i]][num[i]] = 1; n     } n   } n   int ans = 10; n   for (int cnt = 0; cnt < (1 << BITS); ++cnt) { n     int count = 0; n     for (int i = 0; i < 5; ++i) { n       for (int j = 0; j < 5; ++j) { n         for (int k = 0; k < 5; ++k) { n           for (int l = 0; l < 5; ++l) { n             if (a[i][j] == 1 && a[k][l] == 1) { n               int tmp = 0; n               if (i != k) n                 tmp = ((1 << (i + 5)) & cnt) || ((1 << (k + 5)) & cnt); n               int tmp2 = 0; n               if (j != l) tmp2 = ((1 << j) & cnt) || ((1 << l) & cnt); n               if (tmp || tmp2) ++count; n             } n           } n         } n       } n     } n     if (count == m * (m - 1)) { n       int temp = bit(cnt); n       if (ans > temp) ans = temp; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool graf[100][100]; n int n, m; n bool visited[100]; n bool spoken[100]; n int stupid; n int tab[100]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (x == 0) stupid++; n     for (int j = 0; j < x; j++) { n       cin >> tab[j]; n       tab[j]--; n       spoken[tab[j]] = true; n     } n     for (int j = 0; j < x; j++) { n       for (int q = j + 1; q < x; q++) { n         graf[tab[j]][tab[q]] = true; n         graf[tab[q]][tab[j]] = true; n       } n     } n   } n   queue<int> q; n   int add = 0; n   for (int i = 0; i < m; i++) { n     if (!spoken[i] || visited[i]) continue; n     q.push(i); n     visited[i] = true; n     add++; n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (int j = 0; j < m; j++) { n         if (graf[v][j] && !visited[j]) { n           visited[j] = true; n           q.push(j); n         } n       } n     } n   } n   if (add > 0) add--; n   stupid += add; n   cout << stupid << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, n, m, l, t; n bool check(int r) { n   if (a + 1ll * b * (r - 1) <= t && n       (1ll * a * (r - l + 1) + n        1ll * b * (1ll * r * (r - 1) / 2 - 1ll * (l - 1) * (l - 2) / 2)) <= n           1ll * m * t) n     return 1; n   else n     return 0; n } n int main() { n   scanf( %d %d %d , &a, &b, &n); n   int res; n   while (n--) { n     scanf( %d %d %d , &l, &t, &m); n     res = l; n     if (check(res) == 0) n       cout <<  -1  << endl; n     else { n       int low = l; n       int mid; n       int up = 1000100; n       while (low <= up) { n         mid = (low + up) / 2; n         if (check(mid) == 1) { n           res = mid; n           low = mid + 1; n         } else { n           up = mid - 1; n         } n       } n       printf( %d n , res); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[100005]; n set<long long> present; n int ans; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   for (int i = n - 1; i >= 0; i--) { n     if (present.find((long long)a[i] * k) == present.end()) { n       ans++; n       present.insert(a[i]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pp(T v) { n   for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) n     cout << *it <<    ; n   cout << endl; n } n template <class T> n void pp(T v, int n) { n   for (int i = 0; i < (int)n; i++) cout << v[i] <<    ; n   cout << endl; n } n const int INF = 1 << 28; n const double EPS = 1.0e-9; n bool solve(const vector<pair<int, string> > &data) { n   const int n = data.size(); n   for (int i = 0; i < (int)n; i++) { n     if (data[i].first > i) return false; n   } n   vector<int> height(n); n   vector<int> ans; n   for (int i = 0, h = n; i < n; i++, h--) { n     ans.insert(ans.begin() + data[i].first, i); n     height[i] = h; n   } n   for (int i = 0; i < (int)n; i++) { n     cout << data[ans[i]].second <<     << height[ans[i]] << endl; n   } n   return true; n } n int main(void) { n   int n; n   cin >> n; n   vector<pair<int, string> > data(n); n   for (int i = 0; i < (int)n; i++) { n     string name; n     int a; n     cin >> name >> a; n     data[i] = make_pair(a, name); n   } n   sort((data).begin(), (data).end()); n   bool ans = solve(data); n   if (ans == false) cout << -1 << endl; n   return 0; n } n


n // Created by aadi n  n //  Never give up , Miracles never happen in real life. n  n #include<bits/stdc++.h> n using namespace std ; n #define FAST  ios_base::sync_with_stdio(false);cin.tie(NULL); n #define int long long int n #define MIN LLONG_MIN n #define MAX LLONG_MAX n #define set1D(a,val,n) { for(int i=0;i<n;i++) a[i]=val; } n #define set2D(a,val,n,m) { for(int i=0;i<n;i++) for(int j=0;j<m;j++)a[i][j]=val; } n const double pi = 3.14159265358979323846; n #define endl   n  n #define db1(x) cout<<#x<< = <<x<<  n  n #define db2(x,y) cout<<#x<< = <<x<< , <<#y<< = <<y<<  n  n #define db3(x,y,z) cout<<#x<< = <<x<< , <<#y<< = <<y<< , <<#z<< = <<z<<  n  n #define all(x) x.begin(),x.end() n  n  n int32_t main() { n  //code; n  FAST n  int tt; n  cin >> tt; n  while (tt--) { n   //write code n   int n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   int red = 0; n   int blue = 0; n   for (int i = 0; i < n; i++) { n    if (a[i] -  0  > b[i] -  0 ) { n     red++; n    } n    else if (a[i] -  0  < b[i] -  0 ) { n     blue++; n    } n   } n   if (red > blue) { n    cout <<  RED ; n   } n   else if (blue > red) { n    cout <<  BLUE ; n   } n   else { n    cout <<  EQUAL ; n   } n   cout << endl; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &t) { n   t = 0; n   char ch = getchar(); n   int f = 1; n   while ( 0  > ch || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   do { n     (t *= 10) += ch -  0 ; n     ch = getchar(); n   } while ( 0  <= ch && ch <=  9 ); n   t *= f; n } n int n, pos, ans[2010], vis[2010]; n struct node { n   long long x, y; n   bool operator<(node t) { n     if (x != t.x) return y < t.y; n     return x < t.x; n   } n   node operator-(node t) { return (node){x - t.x, y - t.y}; } n   long long operator*(node t) { return x * t.y - y * t.x; } n } d[2010]; n long long t; n char s[2010]; n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(d[i].x), read(d[i].y); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) n     if (!pos || d[i] < d[pos]) pos = i; n   ans[1] = pos; n   vis[pos] = 1; n   int tmp; n   for (int i = 2; i < n; i++) { n     tmp = 0; n     for (int j = 1; j <= n; j++) n       if (!vis[j]) { n         t = (d[j] - d[pos]) * (d[tmp] - d[pos]); n         if (!tmp || (s[i - 1] ==  L  && t > 0) || (s[i - 1] ==  R  && t < 0)) n           tmp = j; n       } n     ans[i] = pos = tmp; n     vis[pos] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       ans[n] = i; n       break; n     } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T0, typename T1> n std::ostream& operator<<(std::ostream& os, const map<T0, T1>& v) { n   for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) { n     os << p->first <<  :   << p->second <<    ; n   } n   return os; n } n template <typename T0, typename T1> n std::ostream& operator<<(std::ostream& os, const pair<T0, T1>& v) { n   os << v.first <<  :   << v.second <<    ; n   return os; n } n template <typename T> n std::ostream& operator<<(std::ostream& os, const vector<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] <<    ; n   } n   return os; n } n template <typename T> n std::ostream& operator<<(std::ostream& os, const vector<vector<T> >& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] << endl; n   } n   return os; n } n template <typename T> n std::ostream& operator<<(std::ostream& os, const set<T>& v) { n   vector<T> tmp(v.begin(), v.end()); n   os << tmp; n   return os; n } n template <typename T> n std::ostream& operator<<(std::ostream& os, const deque<T>& v) { n   vector<T> tmp(v.begin(), v.end()); n   os << tmp; n   return os; n } n int main() { n   long long N, W, H; n   while (cin >> H >> W) { n     vector<string> w(H); n     for (int y = 0, _n = (H); (y) < (int)_n; ++y) cin >> w[y]; n     vector<vector<long long> > tb(H, vector<long long>(W)); n     vector<vector<long long> > tbX(H, vector<long long>(W)); n     vector<vector<long long> > tbY(H, vector<long long>(W)); n     for (int y = 0, _n = (H); (y) < (int)_n; ++y) n       for (int x = 0, _n = (W); (x) < (int)_n; ++x) { n         { n           long long v = 0; n           if (0 <= y - 1) v += tb[y - 1][x]; n           if (0 <= y - 1 && w[y - 1][x] ==  .  && w[y][x] ==  . ) v++; n           if (0 <= x - 1) v += tb[y][x - 1]; n           if (0 <= x - 1 && w[y][x - 1] ==  .  && w[y][x] ==  . ) v++; n           if (0 <= x - 1 && 0 <= y - 1) v -= tb[y - 1][x - 1]; n           tb[y][x] = v; n         } n         { n           long long v = 0; n           if (0 <= x - 1) v += tbY[y][x - 1]; n           if (0 <= y - 1 && w[y - 1][x] ==  .  && w[y][x] ==  . ) v++; n           tbY[y][x] = v; n         } n         { n           long long v = 0; n           if (0 <= y - 1) v += tbX[y - 1][x]; n           if (0 <= x - 1 && w[y][x - 1] ==  .  && w[y][x] ==  . ) v++; n           tbX[y][x] = v; n         } n       } n     cin >> N; n     for (int i = 0, _n = (N); (i) < (int)_n; ++i) { n       long long y1, x1, y2, x2; n       cin >> y1 >> x1 >> y2 >> x2; n       y1--; n       x1--; n       y2--; n       x2--; n       long long v = tb[y2][x2]; n       if (0 <= y1 - 1) v -= tb[y1 - 1][x2]; n       { n         long long vv = tbY[y1][x2]; n         if (0 <= x1 - 1) vv -= tbY[y1][x1 - 1]; n         v -= vv; n       } n       if (0 <= x1 - 1) v -= tb[y2][x1 - 1]; n       { n         long long vv = tbX[y2][x1]; n         if (0 <= y1 - 1) vv -= tbX[y1 - 1][x1]; n         v -= vv; n       } n       if (0 <= x1 - 1 && 0 <= y1 - 1) v += tb[y1 - 1][x1 - 1]; n       cout << v << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n int n, g, p, d, a, b, act, nxt; n long long res; n int Next[(1000000 + 6)]; n pair<int, int> Stacje[(1000000 + 6)]; n stack<int> S; n int main() { n   scanf( %d %d %d , &d, &g, &n); n   p = g; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &a, &b); n     Stacje[i] = make_pair(a, b); n   } n   Stacje[0] = make_pair(0, INF); n   Stacje[n + 1] = make_pair(d, -INF); n   sort(Stacje + 1, Stacje + n + 1); n   for (int i = n + 1; i >= 0; i--) { n     while (!S.empty() && Stacje[S.top()].second > Stacje[i].second) { n       S.pop(); n     } n     Next[i] = S.empty() ? -1 : S.top(); n     S.push(i); n   } n   while (act != n + 1) { n     if (Stacje[Next[act]].first - Stacje[act].first <= g) { n       int dis = Stacje[Next[act]].first - Stacje[act].first; n       p -= dis; n       if (p < 0) { n         res += (-p) * (long long)Stacje[act].second; n         p = 0; n       } n       act = Next[act]; n     } else { n       int dis = Stacje[act + 1].first - Stacje[act].first; n       if (dis > g) { n         break; n       } n       res += (g - p) * (long long)Stacje[act].second; n       p = g - dis; n       act++; n     } n   } n   printf( %lld n , act != n + 1 ? -1 : res); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   T sig = 1; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) sig = -1; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= sig; n } n class Solution { n  public: n   void solve() { n     int n; n     read(n); n     string s; n     cin >> s; n     string t(s); n     for (int i = 0; i < n; ++i) n       if (s[i] ==  ? ) s[i] =  0 , t[i] =  1 ; n     vector<int> p(n + 2), q(n + 2); n     for (int i = n; i >= 1; --i) n       p[i] = s[i - 1] ==  0  ? p[i + 1] + 1 : 0, n       q[i] = t[i - 1] ==  1  ? q[i + 1] + 1 : 0; n     vector<int> np(n + 1), nq(n + 1); n     for (int i = n, pp = 0x3f3f3f3f, qq = 0x3f3f3f3f, npp = 0x3f3f3f3f, n              nqq = 0x3f3f3f3f; n          i >= 1; --i) { n       if (s[i - 1] ==  0 ) pp = i; n       if (s[i - 1] ==  1 ) npp = pp; n       np[i] = npp; n       if (t[i - 1] ==  0 ) nqq = qq; n       if (t[i - 1] ==  1 ) qq = i; n       nq[i] = nqq; n     } n     printf( %d  , n); n     for (int i = 2; i <= n; ++i) { n       int cnt = 0, idx = 1, last = 0; n       while (idx + i - 1 <= n) { n         int delta = max(p[idx], q[idx]); n         if (p[idx] <= i && np[last] == idx) np[last] = np[idx]; n         if (q[idx] <= i && nq[last] == idx) nq[last] = nq[idx]; n         last = idx; n         if (delta >= i) n           cnt += delta / i, idx += delta / i * i; n         else n           idx = min(np[idx], nq[idx]); n       } n       printf( %d  , cnt); n     } n   } n }; n int main() { n   Solution solution = Solution(); n   solution.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n template <typename T> n static T aabs(T x) { n   if (x < 0) return -x; n   return x; n } n template <typename T> n static void read(T &S) { n   char K[1000009]; n   scanf( %s , &K); n   S = K; n } n template <typename T, typename U> n static void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n static void amin(T &x, U y) { n   if (x > y) x = y; n } n template <typename T, typename U> n static void aswap(T &x, U &y) { n   T temp; n   temp = x; n   x = y; n   y = temp; n } n template <typename T, typename U> n static long long gcd(T a, U b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <typename T, typename U> n static long long lcm(T a, U b) { n   return a / gcd(a, b) * b; n } n template <typename T, typename U> n static long long fpow(T n, U k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n const int N5 = 100009; n const int N6 = 1000009; n int st[2 * N5 * 4]; n string s; n set<int> mst[200]; n void build(int id, int l, int r) { n   if (l == r) { n     st[id] = 1; n     return; n   } n   int mid = (l + r) / 2; n   build(id * 2, l, mid); n   build(id * 2 + 1, mid + 1, r); n   st[id] = st[id * 2] + st[id * 2 + 1]; n } n void update(int id, int l, int r, int pos, int value) { n   if (r < pos || l > pos) return; n   if (l == r) { n     st[id] = value; n     return; n   } n   int mid = (l + r) / 2; n   update(id * 2, l, mid, pos, value); n   update(id * 2 + 1, mid + 1, r, pos, value); n   st[id] = st[id * 2] + st[id * 2 + 1]; n } n int query(int id, int l, int r, int pos) { n   if (l == r) return l; n   int mid; n   mid = (l + r) / 2; n   if (pos > st[id * 2]) n     query(id * 2 + 1, mid + 1, r, pos - st[id * 2]); n   else n     query(id * 2, l, mid, pos); n } n void finish(int id, int l, int r) { n   if (l == r) { n     if (st[id]) printf( %c , s[l]); n     return; n   } n   int mid; n   mid = (l + r) / 2; n   finish(id * 2, l, mid); n   finish(id * 2 + 1, mid + 1, r); n } n int conv(char c) { n   if (c >=  a  && c <=  z ) return c -  a ; n   if (c >=  A  && c <=  z ) return c -  A  + 27; n   return c -  0  + 55; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   string ss; n   read(ss); n   s =     + ss; n   for (int i = 1; i <= n; i++) mst[conv(s[i])].insert(i); n   for (int i = 0; i <= 100; i++) mst[i].insert(n + 1); n   build(1, 1, n); n   while (q--) { n     int l, r; n     string re; n     char ch; n     scanf( %d%d , &l, &r); n     read(re); n     ch = re[0]; n     l = query(1, 1, n, l); n     r = query(1, 1, n, r); n     set<int>::iterator it, ait; n     it = mst[conv(ch)].lower_bound(l); n     while (*it <= r) { n       update(1, 1, n, *it, 0); n       ait = it; n       ++it; n       mst[conv(ch)].erase(ait); n     } n   } n   finish(1, 1, n); n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int S = 0; n   for (int i = 1; S < n; ++i) S += i; n   puts(S == n ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000000000; n struct pkt { n   int k, r; n }; n struct prze { n   int k, r1, r2; n }; n bool porownanie(pkt a, pkt b) { n   if (a.k < b.k) return true; n   if (a.k > b.k) return false; n   if (a.r < b.r) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<pkt> pkty(m); n   for (int i = 0; i < m; ++i) { n     cin >> pkty[i].r >> pkty[i].k; n     --pkty[i].r; n     --pkty[i].k; n   } n   sort(pkty.begin(), pkty.end(), porownanie); n   vector<vector<prze> > zab; n   for (int L = 0; L < m;) { n     int P = L; n     while (P < m && pkty[L].k == pkty[P].k) ++P; n     --P; n     vector<prze> t; n     for (int l = L; l <= P;) { n       int p = l; n       while (p <= P && pkty[p].r - pkty[l].r == p - l) ++p; n       --p; n       t.push_back({pkty[l].k, pkty[l].r, pkty[p].r}); n       l = p + 1; n     } n     zab.push_back(t); n     L = P + 1; n   } n   for (int i = 0; i < zab.size(); ++i) { n     if (zab[i].size() == 1 && zab[i][0].r1 == 0 && zab[i][0].r2 == n - 1) { n       cout <<  -1 ; n       return 0; n     } n   } n   vector<vector<prze> > doz; n   if (zab[0][0].k != 0) { n     vector<prze> t; n     t.push_back({0, 0, n - 1}); n     doz.push_back(t); n   } n   for (int i = 0; i < zab.size(); ++i) { n     if (i != 0) { n       if (zab[i - 1][0].k != zab[i][0].k - 1) { n         vector<prze> t; n         t.push_back({zab[i][0].k - 1, 0, n - 1}); n         doz.push_back(t); n       } n     } n     int ile_prze = zab[i].size(); n     vector<prze> t; n     if (zab[i][0].r1 - 1 >= 0) t.push_back({zab[i][0].k, 0, zab[i][0].r1 - 1}); n     for (int j = 0; j < ile_prze - 1; ++j) n       t.push_back({zab[i][0].k, zab[i][j].r2 + 1, zab[i][j + 1].r1 - 1}); n     if (zab[i][ile_prze - 1].r2 < n - 1) n       t.push_back({zab[i][0].k, zab[i][ile_prze - 1].r2 + 1, n - 1}); n     doz.push_back(t); n   } n   if (zab.back()[0].k != n - 1) { n     vector<prze> t; n     t.push_back({n - 1, 0, n - 1}); n     doz.push_back(t); n   } n   zab.clear(); n   vector<vector<int> > lo(doz.size()); n   for (int i = 0; i < doz.size(); ++i) n     for (int j = 0; j < doz[i].size(); ++j) lo[i].push_back(INF); n   for (int i = 0; i < lo[0].size(); ++i) { n     if (i == 0) n       lo[0][i] = 0; n     else n       lo[0][i] = INF; n   } n   for (int i = 1; i < doz.size(); ++i) { n     vector<int> g(doz[i].size(), INF); n     int k = 0; n     for (int j = 0; j < doz[i].size(); ++j) { n       while (k < doz[i - 1].size() && doz[i - 1][k].r2 < doz[i][j].r1) ++k; n       if (k < doz[i - 1].size()) g[j] = k; n     } n     vector<int> d(doz[i].size(), INF); n     k = doz[i - 1].size() - 1; n     for (int j = doz[i].size() - 1; j >= 0; --j) { n       while (k >= 0 && doz[i - 1][k].r1 > doz[i][j].r2) --k; n       if (k >= 0) d[j] = k; n     } n     for (int j = 0; j < doz[i].size(); ++j) { n       if (d[j] != INF && g[j] != INF && g[j] <= d[j]) { n         for (int k = g[j]; k <= d[j]; ++k) { n           if (lo[i - 1][k] != INF) { n             if (lo[i - 1][k] < doz[i][j].r1) n               lo[i][j] = min(lo[i][j], doz[i][j].r1); n             else { n               if (lo[i - 1][k] <= doz[i][j].r2) n                 lo[i][j] = min(lo[i][j], lo[i - 1][k]); n             } n           } n         } n       } n     } n   } n   int x = doz.size(); n   int y = doz[x - 1].size(); n   if (doz[x - 1][y - 1].r2 == n - 1 && lo[x - 1][y - 1] != INF) n     cout << 2 * (n - 1); n   else n     cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxi = 1e6 + 10; n const long long mo = 1e9 + 7; n long long a[maxi]; n long long m, l, r, k; n string s; n vector<int> v[maxi]; n long long mat[53][3][3]; n void mul_mat(int idx1, int idx2, int idx) { n   for (int i = 1; i <= 2; i++) n     for (int j = 1; j <= 2; j++) n       for (int k = 1; k <= 2; k++) n         mat[idx][i][j] += mat[idx1][i][k] * mat[idx2][k][j]; n   for (int i = 1; i <= 2; i++) n     for (int j = 1; j <= 2; j++) mat[idx][i][j] %= m; n } n void fib(long long x) { n   long long fi = 1; n   long long fj = 1; n   mat[51][1][1] = 1; n   mat[51][2][2] = 1; n   for (int i = 0; i <= 50; i++) n     if (x & (1ll << i)) { n       mul_mat(i, 51, 52); n       for (int j = 1; j <= 2; j++) n         for (int o = 1; o <= 2; o++) { n           mat[51][j][o] = mat[52][j][o]; n           mat[52][j][o] = 0; n         } n     } n   cout << mat[51][2][1] << endl; n   return; n } n int main() { n   cin >> m >> l >> r >> k; n   long long x = 1; n   mat[0][1][1] = 1; n   mat[0][1][2] = 1; n   mat[0][2][1] = 1; n   mat[0][2][2] = 0; n   for (long long i = 1; i * i <= r; i++) { n     long long last = r / i; n     long long first = (r / (i + 1)) + 1; n     if (i - k >= 0) { n       long long val = i - k; n       long long li = last; n       long long ri = first + 1; n       while (li < ri - 1) { n         long long mid = (li + ri) / 2; n         if ((l - 1) / mid <= val) n           li = mid; n         else n           ri = mid; n       } n       if ((l - 1) / li <= val) { n         x = max(x, li); n       } n     } n   } n   long long sqr = floor(sqrt(r)); n   for (long long num = sqr; num > 0; num--) { n     long long i = r / num; n     long long last = r / i; n     long long first = r / (i + 1) + 1; n     if (i - k >= 0) { n       long long val = i - k; n       long long li = last; n       long long ri = first + 1; n       while (li < ri - 1) { n         long long mid = (li + ri) / 2; n         if ((l - 1) / mid <= val) n           li = mid; n         else n           ri = mid; n       } n       if ((l - 1) / li <= val) { n         x = max(x, li); n       } n     } n   } n   for (int i = 1; i <= 50; i++) mul_mat(i - 1, i - 1, i); n   fib(x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   char c = getchar(); n   long long x = 1; n   long long s = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) x = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   return s * x; n } n const long long N = 4e5 + 5; n vector<long long> v[N]; n long long n, x, y, siz[N], tong; n void dfs(long long u, long long fa) { n   long long cnt = 1, tmp = 0; n   for (unsigned long long i = 0; i < v[u].size(); i++) { n     if (v[u][i] == fa) continue; n     dfs(v[u][i], u); n     cnt++; n     if (siz[v[u][i]] == 1) tmp++; n   } n   siz[u] = cnt; n   if (tmp) { n     tong = tong + tmp - 1; n   } else { n     siz[u] = 1; n   } n } n void solve() { n   n = read(); n   for (long long i = 1; i < n; i++) { n     x = read(); n     y = read(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   if (n == 2) { n     cout << 1 <<   n ; n   } else { n     dfs(1, 0); n     cout << tong + 1 <<   n ; n   } n   for (long long i = 1; i <= n; i++) v[i].clear(), siz[i] = 0; n   tong = 0; n } n signed main() { n   long long T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1100; n const long long MOD = 998244353; n int N, K; n int arr[MAXN]; n int dp[MAXN][MAXN]; n int ps[MAXN][MAXN]; n long long figure(int k) { n   if (k * (K - 1) > arr[N - 1]) return 0; n   for (int i = 0; i <= N; i++) n     for (int j = 0; j <= K; j++) dp[i][j] = ps[i][j] = 0; n   for (int i = 1; i <= N; i++) dp[i][1] = 1; n   int cstop = 0; n   for (int i = 0; i < N; i++) { n     while (cstop < i && arr[i] - arr[cstop] >= k) cstop++; n     for (int j = 1; j < K; j++) { n       dp[i + 1][j + 1] = ps[cstop][j]; n     } n     for (int j = 0; j <= K; j++) { n       ps[i + 1][j] = (ps[i][j] + dp[i + 1][j]) % MOD; n     } n   } n   return ps[N][K]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N >> K; n   for (int i = 0; i < N; i++) cin >> arr[i]; n   sort(arr, arr + N); n   long long ans = 0; n   for (int i = 1; i <= 100000; i++) ans = (ans + figure(i)) % MOD; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n inline int gi() { n   register int x = 0; n   register char c = getchar(); n   bool f = 0; n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) c = getchar(), f = 1; n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f ? -x : x; n } n struct node { n   int to, next; n } g[N << 1]; n int last[N], gl; n inline void add(int x, int y) { n   g[++gl] = (node){y, last[x]}; n   last[x] = gl; n   return; n } n int sum, f[N], siz[N], rt; n bool vis[N]; n void getroot(int u, int fa) { n   siz[u] = 1; n   f[u] = 0; n   for (int i = last[u]; i; i = g[i].next) { n     int v = g[i].to; n     if (v == fa || vis[v]) continue; n     getroot(v, u); n     f[u] = max(f[u], siz[v]); n     siz[u] += siz[v]; n   } n   f[u] = max(f[u], sum - siz[u]); n   if (f[rt] > f[u]) rt = u; n   return; n } n char a[N]; n int s[N], t[10050000]; n void dfs(int x, int fa, int p, int S) { n   t[S ^= (1 << s[x])] += p; n   for (int i = last[x]; i; i = g[i].next) { n     int v = g[i].to; n     if (v == fa || vis[v]) continue; n     dfs(v, x, p, S); n   } n } n long long ans[N]; n long long calc(int x, int fa, int S) { n   S ^= (1 << s[x]); n   long long cnt = t[S]; n   for (int i = 0; i < 20; i++) cnt += t[S ^ (1 << i)]; n   for (int i = last[x]; i; i = g[i].next) { n     int v = g[i].to; n     if (v == fa || vis[v]) continue; n     cnt += calc(v, x, S); n   } n   ans[x] += cnt; n   return cnt; n } n void solve(int x) { n   vis[x] = 1; n   dfs(x, 0, 1, 0); n   long long cnt = t[0]; n   for (int i = 0; i < 20; i++) cnt += t[1 << i]; n   for (int i = last[x]; i; i = g[i].next) { n     int v = g[i].to; n     if (vis[v]) continue; n     dfs(v, x, -1, 1 << s[x]); n     cnt += calc(v, x, 0); n     dfs(v, x, 1, 1 << s[x]); n   } n   dfs(x, 0, -1, 0); n   ans[x] += cnt / 2; n   for (int i = last[x]; i; i = g[i].next) { n     int v = g[i].to; n     if (vis[v]) continue; n     sum = siz[v]; n     rt = 0; n     getroot(v, 0); n     solve(rt); n   } n   return; n } n int main() { n   int n = gi(); n   for (int i = 1; i < n; i++) { n     int u = gi(), v = gi(); n     add(u, v); n     add(v, u); n   } n   scanf( %s , a); n   for (int i = 0; i < n; i++) s[i + 1] = a[i] -  a ; n   f[0] = sum = n; n   rt = 0; n   getroot(1, 0); n   solve(rt); n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int r[200], l[200]; n int main() { n   memset(l, 10, sizeof(l)); n   memset(r, -1, sizeof(r)); n   cin.sync_with_stdio(false); n   cin >> n >> m; n   string s; n   int w = 0; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     for (int j = 0; j < m; ++j) n       if (s[j] ==  W ) { n         if (l[i] > m) l[i] = j; n         r[i] = j; n         w++; n       } n   } n   if (!w) { n     cout << 0; n     return 0; n   } n   int e = 0; n   for (int i = n - 1; i >= 0; --i) n     if (l[i] < m) { n       e = i + 1; n       break; n     } n   int j = 0; n   int res = 0; n   for (int i = 0; i < n; ++i) { n     if (i == e) { n       --res; n       break; n     } n     if (i & 1) { n       if (i == n - 1) n         res += j - l[i]; n       else { n         int k = min(l[i], l[i + 1]); n         if (k >= m) k = j; n         res += abs(j - k) + 1; n         j = k; n       } n     } else { n       if (i == n - 1) n         res += r[i] - j; n       else { n         int k = max(r[i], r[i + 1]); n         if (k == -1) k = j; n         res += abs(k - j) + 1; n         j = k; n       } n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int sum, n, i, t, p, sum1; n   char z[260]; n   scanf( %d , &n); n   getchar(); n   for (i = 0; i < n; i++) { n     scanf( %c , &z[i]); n   } n   getchar(); n   sum = 0; n   sum1 = 0; n   p = 0; n   t = 0; n   for (i = 0; i < n; i++) { n     if (z[i] >=  a  && z[i] <=  z ) { n       sum++; n       if (sum >= t) t = sum; n     } n     if (z[i] >=  A  && z[i] <=  Z ) { n       sum++; n       if (sum >= t) t = sum; n     } n     if (z[i] ==  _ ) { n       sum = 0; n     } n     if (z[i] ==  ( ) { n       sum = 0; n       i++; n       for (;; i++) { n         if (z[i] >=  a  && z[i] <=  z ) { n           sum++; n           if (sum > p) p = sum; n         } n         if (z[i] >=  A  && z[i] <=  Z ) { n           sum++; n           if (sum > p) p = sum; n         } n         if (z[i] ==  _ ) { n           p = sum; n           if (sum != 0) sum1++; n           sum = 0; n         } n         if (z[i] ==  ) ) { n           if (sum != 0) sum1++; n           sum = 0; n           break; n         } n       } n     } n   } n   printf( %d %d , t, sum1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n istream &operator>>(istream &o, pair<A, B> &p) { n   o >> p.first >> p.second; n   return o; n } n template <class type> n istream &operator>>(istream &o, vector<type> &v) { n   for (int i = 0; i < (int)v.size(); i++) o >> v[i]; n   return o; n } n template <class A, class B> n ostream &operator<<(ostream &o, pair<A, B> p) { n   o <<  (  << p.first <<  ,  << p.second <<  ) ; n   return o; n } n template <class type> n ostream &operator<<(ostream &o, vector<type> v) { n   o <<  { ; n   for (int i = 0; i < (int)v.size(); i++) { n     if (i > 0) o <<  , ; n     o << v[i]; n   } n   o <<  } ; n   return o; n } n long long toNum(string a) { n   if (a.size() == 0u) return 0; n   long long ret = 0; n   for (int i = (a[0] ==  - ); i < int(a.size()); i++) n     ret = ret * 10 + a[i] -  0 ; n   return ret * (a[0] ==  -  ? -1 : 1); n } n string toStr(long long a) { n   string ret =   ; n   bool sgn = a < 0; n   a = a < 0 ? -a : a; n   do { n     ret.push_back(a % 10 +  0 ); n     a /= 10; n   } while (a > 0); n   if (sgn) ret.push_back( - ); n   reverse(ret.begin(), ret.end()); n   return ret; n } n int dp[1005][2][2]; n vector<int> cost, a, b; n int countCost(int a, int t) { n   switch (t) { n     case 0: n       return a * cost[t]; n     default: n       return cost[t]; n   } n } n int solve(int n, int bs, int tr) { n   if (n < 0) return 0; n   if (bs == 1 && tr == 1) return 0; n   int &ret = dp[n][bs][tr]; n   if (ret != -1) return ret; n   ret = (1 << 30); n   for (int i = 0; i < int(3); i++) n     for (int j = 0; j < int(3); j++) { n       int t = countCost(a[n] * (!bs), i) + countCost(b[n] * (!tr), j); n       ret = min(ret, t + solve(n - 1, (i == 2) || bs, (j == 2) || tr)); n     } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cost = vector<int>(4); n   cin >> cost; n   int m, n; n   cin >> m >> n; n   a = b = vector<int>(max(m, n)); n   for (int i = 0; i < int(m); i++) cin >> a[i]; n   for (int i = 0; i < int(n); i++) cin >> b[i]; n   n = max(m, n); n   memset(dp, -1, sizeof(dp)); n   cout << min(cost[3], solve(n - 1, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010], b[100010], x[100010], y[100010]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m, ans = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   a[0] = 0; n   a[n + 1] = m; n   for (int i = 1; i <= n + 1; i++) { n     b[i] = a[i] - a[i - 1]; n     if (i & 1) ans += b[i]; n     x[i] = x[i - 1]; n     y[i] = y[i - 1]; n     if (i & 1) n       x[i] += b[i]; n     else n       y[i] += b[i]; n   } n   for (int i = 1; i <= n + 1; i++) { n     if (b[i] > 1) { n       ans = max(ans, x[i - 1] + y[n + 1] - y[i] + b[i] - 1); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int S = 1003; n bool a[S]; n bool b[S]; n string s; n int k; n int i, j; n int main() { n   cin >> s; n   j = s.size() - 1; n   while (i < j) { n     if (s[i] ==  (  && s[j] ==  ) ) { n       b[i] = 1; n       b[j] = 1; n       k++; n       i++; n       j--; n     } else { n       if (s[i] ==  ) ) i++; n       if (s[j] ==  ( ) j--; n     } n   } n   if (k == 0) { n     cout <<  0 ; n     return 0; n   }; n   cout << 1 << endl; n   cout << 2 * k << endl; n   for (int i = 0; i < s.size(); i++) n     if (b[i]) cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > b, c; n map<vector<pair<int, int> >, int> d; n ; n int n, m, ask, a[100086]; n long long ans; n int main() { n   scanf( %d %d , &n, &m); n   ans = 0ll; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b.clear(); n     c.clear(); n     int qs = a[i]; n     for (int j = 2; j * j <= qs; j++) { n       if (a[i] % j == 0) { n         int sum = 0; n         while (a[i] % j == 0) { n           a[i] /= j; n           sum++; n         } n         sum %= m; n         if (sum) { n           b.push_back(make_pair(j, sum)); n           c.push_back(make_pair(j, m - sum)); n         } n       } n     } n     if (a[i] > 1) { n       b.push_back(make_pair(a[i], 1)); n       c.push_back(make_pair(a[i], m - 1)); n     } n     ans = ans + 1ll * d[c]; n     d[b]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long _pow(long long base, long long exp) { n   return exp == 0 ? 1 : base * _pow(base, exp - 1); n } n string int_to_str(int x) { n   string xs; n   stringstream ss; n   ss << x; n   xs = ss.str(); n   return xs; n } n int str_vec_find(string x, vector<string> y) { n   vector<string>::iterator it; n   it = find(y.begin(), y.end(), x); n   int pos = distance(y.begin(), it); n   return pos; n } n int main() { n   long long temp, tempa, tempb, maxa, maxb; n   vector<long long> a, b; n   int n; n   cin >> n; n   maxa = maxb = -1; n   for (int i = 0; i < n; i++) { n     cin >> tempa >> tempb; n     if (tempa >= maxa) maxa = tempa; n     if (tempb >= maxb) maxb = tempb; n     a.push_back(tempa); n     b.push_back(tempb); n   } n   for (int w = 0; w < b.size(); w++) { n     for (int r = 0; r < b.size() - 1; r++) { n       if (a[r] > a[r + 1]) { n         temp = a[r]; n         a[r] = a[r + 1]; n         a[r + 1] = temp; n         temp = b[r]; n         b[r] = b[r + 1]; n         b[r + 1] = temp; n       } n     } n   } n   if (n == 1) n     cout << maxb << endl; n   else { n     long long stamp = b[0]; n     for (int i = 1; i < n; i++) { n       if (b[i] >= stamp) n         stamp = b[i]; n       else { n         if (a[i] != a[i - 1]) stamp = a[i]; n       } n     } n     cout << stamp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nc2[(int)1e5]; n void pre() { n   for (long long i = 1; i < 1e5; ++i) { n     nc2[i] = i * (i - 1) / 2; n   } n } n int binSearch(int l, int r, int val) { n   while (l <= r) { n     int mid = (l + r) / 2; n     if (nc2[mid] == val) n       return mid; n     else if (nc2[mid] < val) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return -1; n } n bool isPossible(int a00, int a01, int a10, int a11, int n, int m) { n   if (a10 + a01 != n * m or abs(a10 - a01) % 2 != n * m % 2) { n     return false; n   } n   if (!n) { n     for (int i = 0; i < m; ++i) cout << 0; n     return true; n   } n   if (!m) { n     for (int i = 0; i < n; ++i) cout << 1; n     return true; n   } n   for (int i = 0; i < a01 / n; ++i) cout << 0; n   for (int i = 0; i < n - (a01 % n); ++i) cout << 1; n   if (a01 / n < m) cout << 0; n   for (int i = 0; i < a01 % n; ++i) cout << 1; n   for (int i = 0; i < (m - a01 / n - 1); ++i) cout << 0; n   return true; n } n int main() { n   pre(); n   int a00, a01, a10, a11; n   cin >> a00 >> a01 >> a10 >> a11; n   int n = binSearch(1, 1e5 - 1, a11), m = binSearch(1, 1e5 - 1, a00); n   if (n == -1 or m == -1 or (n == 0 and m == 0)) { n     cout <<  Impossible ; n     return 0; n   } n   if (!n) { n     for (int i = 0; i < m; ++i) cout << 0; n     return 0; n   } n   if (!m) { n     for (int i = 0; i < n; ++i) cout << 1; n     return 0; n   } n   bool ans = false; n   if (n == 1 and m == 1) { n     ans = ans or isPossible(a00, a01, a10, a11, n, m) or n           isPossible(a00, a01, a10, a11, n, 0) or n           isPossible(a00, a01, a10, a11, 0, m); n     if (!ans) cout <<  Impossible ; n   } else if (n == 1) { n     ans = ans or isPossible(a00, a01, a10, a11, 0, m) or n           isPossible(a00, a01, a10, a11, n, m); n     if (!ans) cout <<  Impossible ; n   } else if (m == 1) { n     ans = ans or isPossible(a00, a01, a10, a11, n, 0) or n           isPossible(a00, a01, a10, a11, n, m); n     if (!ans) cout <<  Impossible ; n   } else { n     ans = isPossible(a00, a01, a10, a11, n, m); n     if (!ans) cout <<  Impossible ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> matches; n void computeLPSArray(vector<pair<char, long long> >& pat, long long M, n                      long long* lps); n void KMPSearch(vector<pair<char, long long> >& pat, n                vector<pair<char, long long> >& txt) { n   long long M = pat.size(); n   long long N = txt.size(); n   long long* lps = (long long*)malloc(sizeof(long long) * M); n   long long j = 0; n   computeLPSArray(pat, M, lps); n   long long i = 0; n   while (i < N) { n     if (pat[j] == txt[i]) { n       j++; n       i++; n     } n     if (j == M) { n       matches.push_back(i - j); n       j = lps[j - 1]; n     } else if (i < N && pat[j] != txt[i]) { n       if (j != 0) n         j = lps[j - 1]; n       else n         i = i + 1; n     } n   } n   free(lps); n } n void computeLPSArray(vector<pair<char, long long> >& pat, long long M, n                      long long* lps) { n   long long len = 0; n   long long i; n   lps[0] = 0; n   i = 1; n   while (i < M) { n     if (pat[i] == pat[len]) { n       len++; n       lps[i] = len; n       i++; n     } else { n       if (len != 0) { n         len = lps[len - 1]; n       } else { n         lps[i] = 0; n         i++; n       } n     } n   } n } n int main() { n   long long N, M; n   cin >> N >> M; n   bool s = false; n   if (N == 199999) s = true; n   vector<pair<char, long long> > txt(N), pat(M); n   for (long long i = 0; i < N; i++) n     scanf( %I64d-%c , &txt[i].second, &txt[i].first); n   for (long long i = 0; i < M; i++) n     scanf( %I64d-%c , &pat[i].second, &pat[i].first); n   vector<pair<char, long long> > txtM; n   char currc = txt[0].first; n   long long currn = txt[0].second; n   for (long long i = 1; i < txt.size(); i++) { n     if (txt[i].first == currc) n       currn += txt[i].second; n     else { n       txtM.push_back(make_pair(currc, currn)); n       currc = txt[i].first; n       currn = txt[i].second; n     } n   } n   txtM.push_back(make_pair(currc, currn)); n   vector<pair<char, long long> > patM; n   currc = pat[0].first; n   currn = pat[0].second; n   for (long long i = 1; i < pat.size(); i++) { n     if (pat[i].first == currc) n       currn += pat[i].second; n     else { n       patM.push_back(make_pair(currc, currn)); n       currc = pat[i].first; n       currn = pat[i].second; n     } n   } n   patM.push_back(make_pair(currc, currn)); n   long long cnt = 0; n   if (patM.size() == 1) { n     for (long long i = 0; i < txtM.size(); i++) { n       if (txtM[i].first == patM[0].first) n         cnt += max(0LL, txtM[i].second - patM[0].second + 1); n     } n     cout << cnt << endl; n     return 0; n   } else if (patM.size() == 2) { n     for (long long i = 0; i < txtM.size() - 1; i++) { n       if (txtM[i].first == patM[0].first && n           txtM[i + 1].first == patM[1].first) { n         if (txtM[i].second >= patM[0].second && n             txtM[i + 1].second >= patM[1].second) n           cnt += 1; n       } n     } n     cout << cnt << endl; n     return 0; n   } n   vector<pair<char, long long> > mod(patM.begin() + 1, n                                      patM.begin() + patM.size() - 1); n   KMPSearch(mod, txtM); n   for (long long i = 0; i < matches.size(); i++) { n     if (matches[i] - 1 >= 0 && txtM[matches[i] - 1].first == patM[0].first && n         txtM[matches[i] - 1].second >= patM[0].second) { n       long long fin = matches[i] + patM.size() - 2; n       long long le = patM.size() - 1; n       if (fin < txtM.size() && txtM[fin].first == patM[le].first && n           txtM[fin].second >= patM[le].second) { n         cnt += 1; n       } n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& os, vector<T> v) { n   for (T x : v) os << x <<    ; n   return os; n } n template <class T> n istream& operator>>(istream& is, vector<T> v) { n   for (T& x : v) is >> x; n   return is; n } n const long long INF = 1LL << 60; n bool startsWith(string s, string t) { n   return s.size() >= t.size() && s.substr(0, t.size()) == t; n } n string chomp(string s) { n   if (s.empty()) return   ; n   long long start = -1, end = -1; n   for (long long i = 0; i < (long long)(s.size()); i++) { n     if (s[i] ==    ) continue; n     start = i; n     break; n   } n   for (long long i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==    ) continue; n     end = i; n     break; n   } n   string ans =   ; n   for (long long i = start; i <= end; i++) ans.push_back(s[i]); n   return ans; n } n string parseThrow(string s) { n   long long a, b; n   for (long long i = 0; i < (long long)(s.size()); i++) { n     char c = s[i]; n     if (c ==  ( ) n       a = i; n     else if (c ==  ) ) n       b = i; n   } n   return chomp(s.substr(a + 1, b - a - 1)); n } n pair<string, string> parseCatch(string s) { n   string a, b; n   long long x, y = -1, z, w; n   for (long long i = 0; i < (long long)(s.size()); i++) { n     char c = s[i]; n     if (c ==  ( ) { n       x = i; n     } else if (c ==    ) { n       if (y == -1) n         y = i; n       else n         z = i; n     } else if (c ==  , ) n       w = i; n   } n   a = chomp(s.substr(x + 1, w - x - 1)); n   b = chomp(s.substr(y + 1, z - y - 1)); n   return make_pair(a, b); n } n void run() { n   long long n; n   cin >> n; n   cin.ignore(); n   vector<string> v; n   string buf; n   for (long long i = 0; i < (long long)(n); i++) { n     getline(cin, buf); n     v.push_back(chomp(buf)); n   } n   string exn; n   bool flg = false; n   long long depth = 0; n   for (long long i = 0; i < (long long)(n); i++) { n     string s = v[i]; n     if (s.empty()) continue; n     if (startsWith(s,  throw )) { n       flg = true; n       exn = parseThrow(s); n     } n     if (!flg) continue; n     if (startsWith(s,  try )) { n       depth++; n     } else if (startsWith(s,  catch )) { n       if (depth > 0) { n         depth--; n         continue; n       } else { n         pair<string, string> e = parseCatch(s); n         if (e.first == exn) { n           cout << e.second << endl; n           return; n         } n       } n     } n   } n   cout <<  Unhandled Exception  << endl; n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 5; n const int mod = 1000 * 1000 * 1000 + 7; n vector<int> p[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     p[i].resize(k); n     for (int j = 0; j < k; j++) cin >> p[i][j]; n     sort(p[i].begin(), p[i].end()); n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int pre = i ? i - 1 : n - 1; n     int nxt = (i + 1) % n; n     int p1 = 0, p2 = 0; n     for (int j = 0; j < (int)p[i].size(); j++) { n       int cnt = 0; n       while (p1 < (int)p[pre].size() && p[pre][p1] < p[i][j]) cnt++, p1++; n       while (p2 < (int)p[nxt].size() && p[nxt][p2] < p[i][j]) cnt--, p2++; n       if (cnt != 0 && j != 0) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int t; n long long n; n vector<int> v; n long long cnt[30]; n long long pos[30]; n long long fact[21]; n void init() { n   for (int i = 0; i < int(30); i++) cnt[i] = pos[i] = 0; n   v.clear(); n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   fact[0] = 1; n   for (int i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i; n   cin >> t; n   while (t--) { n     init(); n     long long n; n     cin >> n; n     int cur = 2; n     while (n) { n       v.push_back(n % cur); n       n /= cur; n       cur++; n     } n     sort(v.begin(), v.end()); n     for (int i = 0; i < int(v.size()); i++) cnt[v[i]]++; n     pos[1] = cnt[0]; n     for (int i = 2; i <= v.size() + 1; i++) pos[i] = pos[i - 1] + cnt[i - 1]; n     long long A = 1; n     for (int i = 2; i <= v.size() + 1; i++) A *= (pos[i] - (i - 2)); n     long long B = 1; n     if (cnt[0] == 0) n       B = 0; n     else { n       B = cnt[0]; n       for (int i = 2; i <= v.size(); i++) B *= (pos[i] - (i - 1)); n     } n     long long ans = A - B; n     for (int i = 0; i < int(21); i++) ans /= fact[cnt[i]]; n     cout << ans - 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, K, w[N]; n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   if (p1 == p2) { n     p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); n     if (p1 == p2) return EOF; n   } n   return *p1++; n } n inline void read(int& x) { n   char tr = nc(), b = 1; n   for (; tr <  0  || tr >  9 ; tr = nc()) n     if (tr ==  - ) b = -1; n   for (x = 0; tr >=  0  && tr <=  9 ; n        x = (x << 3) + (x << 1) + tr - 48, tr = nc()) n     ; n   x *= b; n } n inline int count() { n   int ret = 0, x; n   for (int i = 0; i < n - 1; i++) n     for (int j = 0; j < m - 1; j++) n       x = i * m + j, ret += (w[x] & w[x + m] & w[x + 1]) + n                             (w[x + m + 1] & w[x + m] & w[x + 1]) + n                             (w[x] & w[x + m] & w[x + m + 1]) + n                             (w[x] & w[x + 1] & w[x + m + 1]); n   return ret; n } n int Q[N], l, r, ins[N]; n const int dx[6] = {0, 0, 1, -1}; n const int dy[6] = {1, -1, 0, 0}; n inline int pd() { n   l = r = -1; n   for (int i = 0; i < n * m; i++) ins[i] = 0; n   for (int i = 0; i < n * m; i++) n     if (w[i]) { n       Q[++r] = i; n       break; n     } n   while (l < r) { n     ++l; n     int x = Q[l] / m, y = Q[l] % m; n     for (int k = 0; k < 4; k++) { n       int sx = x + dx[k], sy = y + dy[k]; n       if (sx < 0 || sy < 0 || sx >= n || sy >= m || ins[sx * m + sy] || n           !w[sx * m + sy]) n         continue; n       Q[++r] = sx * m + sy; n       ins[sx * m + sy] = 1; n     } n   } n   for (int i = 0; i < n * m; i++) n     if (w[i] && !ins[i]) return 0; n   return 1; n } n inline int check(int mid) { n   for (int i = 0; i < n * m; i++) w[i] = (i <= mid); n   return count(); n } n inline int Solve() { n   int C = 4 * (n - 1) * (m - 1), L = -1, R = n * m, MID; n   if (K > C) return 0; n   if (n == 3 || m == 3) { n     if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5) return 0; n   } else { n     if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5 || K == C - 8) n       return 0; n   } n   while (L + 1 < R) n     if (check(MID = (L + R) >> 1) >= K) n       R = MID; n     else n       L = MID; n   int cur = check(R); n   if (cur == K) return 1; n   vector<int> bl, em; n   for (int i = 0; i < n * m; i++) n     if (min(i / m, abs(i / m - R / m)) <= 1) n       w[i] ? bl.push_back(i) : em.push_back(i); n   int x, p; n   while (cur != K) { n     if (cur < K) { n       x = em[p = rand() % ((int)em.size())]; n       w[x] = 1; n       if (!pd()) { n         w[x] = 0; n         continue; n       } n       em.erase(em.begin() + p); n       bl.push_back(x); n       cur = count(); n     } else { n       x = bl[p = rand() % ((int)bl.size())]; n       w[x] = 0; n       if (!pd()) { n         w[x] = 1; n         continue; n       } n       bl.erase(bl.begin() + p); n       em.push_back(x); n       cur = count(); n     } n   } n   return 1; n } n int main() { n   int T; n   read(T); n   while (T--) { n     read(n); n     read(m); n     read(K); n     int flag = 0; n     if (n < m) swap(n, m), flag = 1; n     if (Solve()) { n       if (!flag) { n         for (int i = 0; i < n; i++, putchar(  n )) n           for (int j = 0; j < m; j++) putchar(w[i * m + j] ?  *  :  . ); n       } else { n         for (int j = 0; j < m; j++, putchar(  n )) n           for (int i = 0; i < n; i++) putchar(w[i * m + j] ?  *  :  . ); n       } n       putchar(  n ); n     } else n       printf( -1 n n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[12345]; n int calc(int x, int y) { n   int ret = 0; n   vector<int> v; n   for (int i = 0; i < 2 * n; ++i) { n     if (i != x && i != y) v.push_back(a[i]); n   } n   for (int i = 0; i < v.size(); i += 2) ret += abs(v[i] - v[i + 1]); n   return ret; n } n int main() { n   cin >> n; n   int mn = 1e9, i; n   for (i = 0; i < 2 * n; i++) cin >> a[i]; n   sort(a, a + 2 * n); n   for (int i = 0; i < n * 2; i++) { n     for (int j = i + 1; j < n * 2; j++) { n       mn = min(mn, calc(i, j)); n     } n   } n   cout << mn; n } n


#include <iostream> n #include <cstdio> n #include <cmath> n #include <cstring> n #include <vector> n #include <map> n  n #include <set> n #include <cmath> n #include <queue> n #include <algorithm> n #include <numeric> n using namespace std; n typedef long long ll; n const int N=1e6+5; n const int M=1e3+5; n const int mod=1e9+7; n int n,m; n int T,a[N]; n std::vector<int> ans; n int rns; n bool cal(vector<int> v,std::vector<int> v1){ n     for(int i=0;i<n*2;i++){ n         if(v[i]!=v1[i])return 0; n     } n     return 1; n } n map<std::vector<int>,int>mp; n void bfs(){ n     queue<vector<int> >q; n     std::vector<int>G; n     for(int i=1;i<=n*2;i++)G.push_back(i); n         rns=-1; n     q.push(G); n     mp[G]=0; n     while(!q.empty()){ n         std::vector<int> now=q.front(); n         q.pop(); n         if(cal(now,ans)==1){ n             rns=mp[now]; n             return ; n         } n         std::vector<int> v1=now; n         for(int i=0;i+1<n*2;i+=2){ n             swap(v1[i],v1[i+1]); n         } n         std::vector<int> v2=now; n         for(int i=0;i+n<n*2;i++){ n             swap(v2[i],v2[i+n]); n         } n         if(!mp.count(v1)){ n             q.push(v1); n             mp[v1]=mp[now]+1; n         } n         if(!mp.count(v2)){ n             q.push(v2); n             mp[v2]=mp[now]+1; n         } n     } n  n  n  n  n } n int main() { n     scanf( %d ,&n); n     for(int i=1;i<=n*2;i++)scanf( %d ,&a[i]),ans.push_back(a[i]); n     bfs(); n cout<<rns<<endl; n  n  n      n      n      n      n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, c, i, a, m, l, r; n   cin >> t >> i >> m >> a >> l; n   r = i; n   c = 1; n   t = t - r; n   while (t > 0) { n     c++; n     r = r + a; n     if (r > m) r = m; n     t = t + l - r; n     if (t < 0) t = 0; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q, n, m, u1, d1, l1, r1, u2, d2, l2, r2, u3, d3, l3, r3; n long long count(int a, int b) { return (1ll * a * b + 1) / 2; } n long long count(int u, int d, int l, int r) { n   return count(d, r) + count(u - 1, l - 1) - count(d, l - 1) - count(u - 1, r); n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d %d , &m, &n); n     scanf( %d %d %d %d , &u1, &l1, &d1, &r1); n     scanf( %d %d %d %d , &u2, &l2, &d2, &r2); n     long long ans = count(n, m); n     u3 = max(u1, u2), l3 = max(l1, l2); n     d3 = min(d1, d2), r3 = min(r1, r2); n     ans -= count(u2, d2, l2, r2); n     ans += 1ll * (d1 - u1 + 1) * (r1 - l1 + 1) - count(u1, d1, l1, r1); n     if (d3 >= u3 && r3 >= l3) n       ans -= 1ll * (d3 - u3 + 1) * (r3 - l3 + 1) - count(u3, d3, l3, r3); n     printf( %I64d %I64d n , ans, 1ll * n * m - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string in[1003]; n int a[1003][1003]; n int X[] = {-1, 0, 0, 1}; n int Y[] = {0, -1, 1, 0}; n bool d[1003][1003]; n int getSm(int i, int j, int u, int v) { n   return a[u][v] - a[i - 1][v] - a[u][j - 1] + a[i - 1][j - 1]; n } n bool check() { n   for (int i = 1; i <= n; i++) { n     bool ok = 0; n     int cnt = 0; n     for (int j = 1; j <= m; j++) { n       if (in[i][j] ==  # ) { n         if (!ok) { n           cnt++; n           ok = 1; n         } n       } else { n         ok = 0; n       } n     } n     if (cnt > 1) { n       return 0; n     } n     if (cnt == 0) { n       ok = 0; n       for (int j = 1; j <= m; j++) { n         if (getSm(1, j, n, j) == 0) { n           ok = 1; n           continue; n         } n       } n       if (!ok) { n         return 0; n       } n     } n   } n   for (int j = 1; j <= m; j++) { n     bool ok = 0; n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (in[i][j] ==  # ) { n         if (!ok) { n           cnt++; n           ok = 1; n         } n       } else { n         ok = 0; n       } n     } n     if (cnt > 1) { n       return 0; n     } n     if (cnt == 0) { n       ok = 0; n       for (int i = 1; i <= n; i++) { n         if (getSm(i, 1, i, m) == 0) { n           ok = 1; n           continue; n         } n       } n       if (!ok) { n         return 0; n       } n     } n   } n   return 1; n } n bool canC(int u, int v) { n   return (1 <= u) && (u <= n) && (1 <= v) && (v <= m) && (in[u][v] ==  # ) && n          (d[u][v] == 0); n } n void DFS(int u, int v) { n   d[u][v] = 1; n   for (int k = 0; k < 4; k++) { n     int nu = u + X[k]; n     int nv = v + Y[k]; n     if (canC(nu, nv)) { n       DFS(nu, nv); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> in[i]; n     in[i].insert(0,    ); n     for (int j = 1; j <= m; j++) n       a[i][j] = (in[i][j] ==  # ) + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; n   } n   if (a[n][m] == 0) { n     cout << 0; n     return 0; n   } n   if (n == 1) { n     int cnt = 0; n     for (int j = 1; j <= m; j++) { n       cnt += (in[1][j] ==  # ); n     } n     if ((0 < cnt) && (cnt < m)) { n       cout << -1; n       return 0; n     } n   } n   if (m == 1) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       cnt += (in[i][1] ==  # ); n     } n     if ((0 < cnt) && (cnt < n)) { n       cout << -1; n       return 0; n     } n   } n   if (!check()) { n     cout << -1; n     return 0; n   } n   memset(d, 0, sizeof d); n   int res = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if ((in[i][j] ==  # ) && (d[i][j] == 0)) { n         res++; n         DFS(i, j); n       } n     } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e3 + 5; n const int mod = 998244353; n char s[maxn], t[maxn]; n int m, n, dp[maxn][maxn]; n int main() { n   scanf( %s%s , s + 1, t + 1); n   n = strlen(s + 1); n   m = strlen(t + 1); n   for (int i = 1; i <= n; i++) { n     if (i > m || t[i] == s[1]) dp[i][i] = 2; n   } n   for (int len = 2; len <= n; len++) { n     char ch = s[len]; n     for (int l = 1; l + len - 1 <= n; l++) { n       int r = l + len - 1; n       if (l > m || ch == t[l]) dp[l][r] = (dp[l][r] + dp[l + 1][r]) % mod; n       if (r > m || ch == t[r]) dp[l][r] = (dp[l][r] + dp[l][r - 1]) % mod; n     } n   } n   int ans = 0; n   for (int i = m; i <= n; i++) ans = (ans + dp[1][i]) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int N = 1e5 + 5; n vector<pair<int, int> > aa, bb; n bool valid(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first == p2.first || p1.second == p2.second || p1.first == 0 || n       p1.second == 0 || p2.first == 0 || p2.second == 0) n     return 0; n   if (p1.first * p2.first + p1.second * p2.second != 0) return 0; n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int a, b; n   cin >> a >> b; n   for (int i = 1; i <= a; i++) { n     for (int j = 1; j <= a; j++) { n       if (i * i + j * j == a * a) aa.push_back({i, j}); n     } n   } n   for (int i = 1; i <= b; i++) { n     for (int j = 1; j <= b; j++) { n       if (i * i + j * j == b * b) bb.push_back({-i, j}); n     } n   } n   for (auto p1 : aa) { n     for (auto p2 : bb) { n       if (valid(p1, p2)) { n         cout <<  YES n ; n         cout <<  0 0 n ; n         cout << p1.first <<     << p1.second <<   n ; n         cout << p2.first <<     << p2.second <<   n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k = 0, t = 0, l = 0, q = 0, z = 0; n   string s, s1, s2; n   cin >> s; n   if (s[0] ==  - ) { n     q = -1; n     s.erase(0, 1); n   } n   k = s.size(); n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  . ) { n       t = i; n       break; n     } n   if (t == 0) s +=  .00 , t = k; n   if (s.size() - t == 2) s +=  0 ; n   l = t; n   if (q == -1) cout <<  ( ; n   cout <<  $ ; n   if (l % 3 != 0) n     z = l % 3; n   else n     z = 3; n   for (int i = 0; i < l + 3; i++) { n     if (i == z && z < l) { n       cout <<  , ; n       z += 3; n     } n     if (s[i] !=  - ) cout << s[i]; n   } n   if (q == -1) cout <<  ) ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n long long a3, b3; n bool check1(long long a1, long long b1, long long a2, long long b2) { n   if ((a2 * a3 + b2 * b3 + a1 * b3 - a3 * b1) % (a3 * a3 + b3 * b3)) n     return false; n   if ((a2 * b3 - b2 * a3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3)) n     return false; n   return true; n } n bool check2(long long a1, long long b1, long long a2, long long b2) { n   if ((a2 * a3 + b2 * b3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3)) n     return false; n   if ((a1 * b3 - b1 * a3 - a2 * b3 + b2 * a3) % (a3 * a3 + b3 * b3)) n     return false; n   return true; n } n bool check(long long a1, long long b1, long long a2, long long b2) { n   if (a1 == a2 && b1 == b2) return true; n   if (b1 == a2 && -a1 == b2) return true; n   if (-a1 == a2 && -b1 == b2) return true; n   if (-b1 == a2 && a1 == b2) return true; n   return false; n } n int main() { n   long long a1, a2, b1, b2; n   while (~scanf( %lld%lld%lld%lld%lld%lld , &a1, &b1, &a2, &b2, &a3, &b3)) { n     if (a3 == b3 && a3 == 0) { n       puts(check(a1, b1, a2, b2) ?  YES  :  NO ); n     } else { n       int ok = 0; n       ok += check(a1, b1, a2, b2); n       ok += check1(a1, b1, a2, b2); n       ok += check1(a1, b1, b2, -a2); n       ok += check1(a1, b1, -a2, -b2); n       ok += check1(a1, b1, -b2, a2); n       ok += check2(a1, b1, a2, b2); n       ok += check2(a1, b1, b2, -a2); n       ok += check2(a1, b1, -a2, -b2); n       ok += check2(a1, b1, -b2, a2); n       puts(ok ?  YES  :  NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n template <bool directed> n struct Euler { n   int N; n   vector<vector<pair<int, int>>> adj; n   vector<vector<pair<int, int>>::iterator> its; n   vector<bool> used; n   Euler(int N_) : N(N_), adj(N), its(N) {} n   void ae(int a, int b) { n     int M = used.size(); n     used.push_back(0); n     adj[a].emplace_back(b, M); n     if (!directed) adj[b].emplace_back(a, M); n   } n   vector<pair<int, int>> solve(int src = 0) { n     while (!adj[src].size()) src++; n     for (int i = 0; i < N; i++) its[i] = begin(adj[i]); n     vector<pair<pair<int, int>, int>> ret, second = {{{src, -1}, -1}}; n     while (second.size()) { n       int x = second.back().first.first; n       auto &it = its[x], en = end(adj[x]); n       while (it != en && used[it->second]) it++; n       if (it == en) { n         if (ret.size() && ret.back().first.second != x) return {}; n         ret.push_back(second.back()), second.pop_back(); n       } else { n         second.push_back({{it->first, x}, it->second}); n         used[it->second] = 1; n       } n     } n     if (ret.size() != used.size() + 1) return {}; n     vector<pair<int, int>> ans; n     for (auto& t : ret) ans.push_back({t.first.first, t.second}); n     reverse(ans.begin(), ans.end()); n     return ans; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int N; n   cin >> N; n   vector<pair<int, int>> v(N); n   for (auto& i : v) cin >> i.first >> i.second; n   for (int i = 20; i >= 0; i--) { n     Euler<false> E(1 << i); n     for (auto [l, r] : v) E.ae(l & ((1 << i) - 1), r & ((1 << i) - 1)); n     auto ans = E.solve(); n     if (!ans.size()) continue; n     if (ans[0].first != ans.back().first) continue; n     ans.erase(ans.begin()); n     cout << i <<   n ; n     for (auto [j, id] : ans) { n       int a = 2 * id + 1, b = 2 * id + 2; n       if ((v[id].first & ((1 << i) - 1)) == j) swap(a, b); n       cout << a <<     << b <<    ; n     } n     cout <<   n ; n     break; n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e3 + 1; n int n, d[mxN], k; n vector<int> adj[mxN]; n vector<string> a[mxN]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   for (int i = 0, c; i < n; i++) { n     cin >> c; n     string s1; n     for (int j = 0; j < k; j++) { n       cin >> s1; n       a[c].push_back(s1); n     } n   } n   vector<string> s; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < k; j++) s.push_back(a[i][j]); n   n *= k; n   vector<bool> ok(26); n   int al = 0; n   for (string x : s) n     for (char c : x) { n       if (!ok[c -  a ]) ++al, ok[c -  a ] = 1; n     } n   for (int i = 1; i < n; i++) { n     int n1 = s[i - 1].size(), n2 = s[i].size(), i1 = 0; n     for (; i1 < n1 || i1 < n2; i1++) { n       if (i1 >= n2 && i1 < n1) { n         cout <<  IMPOSSIBLE ; n         return 0; n       } else if (i1 >= n1 || i1 >= n2) n         break; n       if (s[i - 1][i1] == s[i][i1]) continue; n       adj[s[i - 1][i1] -  a ].push_back(s[i][i1] -  a ); n       d[s[i][i1] -  a ]++; n       break; n     } n   } n   queue<int> qu; n   string ans; n   for (int i = 0; i < 26; i++) n     if (!d[i] && ok[i]) qu.push(i); n   while (qu.size()) { n     int u = qu.front(); n     qu.pop(); n     for (int x : adj[u]) { n       --d[x]; n       if (!d[x]) qu.push(x); n     } n     ans += (u +  a ); n   } n   cout << (ans.size() == al ? ans :  IMPOSSIBLE ); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n long long bit[400001]; n void add(int x, int v) { n   for (int i = x; i <= 400000; i += i & -i) bit[i] += v; n } n long long query(int x) { n   long long ret = 0; n   for (int i = x; i > 0; i -= i & -i) ret += bit[i]; n   return ret; n } n long long ar[100001][4]; n map<long long, long long> mx, my; n vector<pair<long long, long long> > horizontal[400001], vertical[400001]; n vector<int> in[400001], out[400001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (4); ++j) cin >> ar[i][j]; n     mx[ar[i][0]] = mx[ar[i][2]] = my[ar[i][1]] = my[ar[i][3]] = 1; n     if (ar[i][0] > ar[i][2]) swap(ar[i][0], ar[i][2]); n     if (ar[i][1] > ar[i][3]) swap(ar[i][1], ar[i][3]); n   } n   int c1 = 0, c2 = 0; n   for (auto &v : mx) { n     v.second = ++c1; n   } n   for (auto &v : my) { n     v.second = ++c2; n   } n   for (int i = (0); i < (n); ++i) { n     if (ar[i][0] == ar[i][2]) n       horizontal[mx[ar[i][0]]].push_back(make_pair(ar[i][1], ar[i][3])); n     else n       vertical[my[ar[i][1]]].push_back(make_pair(ar[i][0], ar[i][2])); n   } n   long long res = 0; n   for (int i = 1; i <= c1; i++) { n     sort(horizontal[i].begin(), horizontal[i].end()); n     if (horizontal[i].size() == 0) continue; n     int last = 0; n     for (int j = 1; j < horizontal[i].size(); j++) { n       if (horizontal[i][j].first <= horizontal[i][last].second) { n         horizontal[i][last].second = n             max(horizontal[i][last].second, horizontal[i][j].second); n       } else { n         res += horizontal[i][last].second - horizontal[i][last].first + 1; n         in[my[horizontal[i][last].first]].push_back(i); n         out[my[horizontal[i][last].second]].push_back(i); n         last = j; n       } n     } n     res += horizontal[i][last].second - horizontal[i][last].first + 1; n     in[my[horizontal[i][last].first]].push_back(i); n     out[my[horizontal[i][last].second]].push_back(i); n   } n   for (int i = 1; i <= c2; i++) { n     sort(vertical[i].begin(), vertical[i].end()); n     for (int j = 0; j < in[i].size(); j++) { n       add(in[i][j], 1); n     } n     if (vertical[i].size() == 0) { n       for (int j = 0; j < out[i].size(); j++) { n         add(out[i][j], -1); n       } n       continue; n     } n     int last = 0; n     for (int j = 1; j < vertical[i].size(); j++) { n       if (vertical[i][j].first <= vertical[i][last].second) { n         vertical[i][last].second = n             max(vertical[i][last].second, vertical[i][j].second); n       } else { n         res += vertical[i][last].second - vertical[i][last].first + 1; n         res -= query(mx[vertical[i][last].second]) - n                query(mx[vertical[i][last].first] - 1); n         last = j; n       } n     } n     res += vertical[i][last].second - vertical[i][last].first + 1; n     res -= query(mx[vertical[i][last].second]) - n            query(mx[vertical[i][last].first] - 1); n     for (int j = 0; j < out[i].size(); j++) { n       add(out[i][j], -1); n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n clock_t clk = clock(); n long long int i, j, k; n void solve(void); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t = 1; n   while (t--) solve(); n   return 0; n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   bool rev = 0; n   for (i = 1; i <= (n + 1) / 2; ++i) { n     for (j = 1; j <= m; ++j) n       if (!rev) { n         if (n + 1 != 2 * i) { n           cout << i <<     << j <<   n ; n           cout << n + 1 - i <<     << m + 1 - j <<   n ; n         } else if (j & 1) n           cout << i <<     << (j - 1) / 2 + 1 <<   n ; n         else n           cout << i <<     << m - j / 2 + 1 <<   n ; n       } else { n         if (n + 1 != 2 * i) { n           cout << i <<     << m + 1 - j <<   n ; n           cout << n + 1 - i <<     << j <<   n ; n         } else if (j & 1) n           cout << i <<     << m - (j - 1) / 2 <<   n ; n         else n           cout << i <<     << j / 2 <<   n ; n       } n     rev ^= 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<set<string> > N, F; n   vector<pair<string, string> > ans; n   set<string> p; n   for (int i = 0; i < 26; ++i) { n     N.push_back(p); n     F.push_back(p); n   } n   int n, k, c; n   cin >> n; n   c = (int) A ; n   string s; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     k = (int)s[0]; n     N[k - c].insert(s); n   } n   for (int i = 0; i < n; ++i) { n     cin >> s; n     k = (int)s[0]; n     F[k - c].insert(s); n   } n   queue<string> qN, qF; n   set<string>::iterator el, el1; n   for (int i = 0; i < 26; ++i) { n     if (N[i].size() <= F[i].size()) { n       while (N[i].size() < F[i].size() && !qN.empty()) { n         el = F[i].begin(); n         ans.push_back(make_pair(qN.front(), *el)); n         F[i].erase(el); n         qN.pop(); n       } n       while (!N[i].empty()) { n         el = F[i].begin(); n         el1 = N[i].begin(); n         ans.push_back(make_pair(*el1, *el)); n         F[i].erase(el); n         N[i].erase(el1); n       } n       while (!F[i].empty()) { n         el = F[i].begin(); n         qF.push(*el); n         F[i].erase(el); n       } n     } else { n       while (F[i].size() < N[i].size() && !qF.empty()) { n         el = N[i].begin(); n         ans.push_back(make_pair(*el, qF.front())); n         N[i].erase(el); n         qF.pop(); n       } n       while (!F[i].empty()) { n         el = F[i].begin(); n         el1 = N[i].begin(); n         ans.push_back(make_pair(*el1, *el)); n         F[i].erase(el); n         N[i].erase(el1); n       } n       while (!N[i].empty()) { n         el = N[i].begin(); n         qN.push(*el); n         N[i].erase(el); n       } n     } n   } n   sort(ans.begin(), ans.end()); n   int i; n   for (i = 0; i < ans.size() - 1; ++i) n     cout << ans[i].first <<     << ans[i].second <<  ,  ; n   cout << ans[i].first <<     << ans[i].second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long double PI = 4 * atan((long double)1); n const long long INF = 1e18; n const long long NINF = -1e18; n long long get_hash(string s) { n   long long N = 1000001; n   long long base[N], A = 11, MD = 1110111110111; n   base[0] = 1; n   for (long long i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD; n   long long hs = 0; n   for (long long i = (0); i < (s.size()); ++i) { n     hs += (s[i] * base[i]); n     hs %= MD; n   } n   return hs; n } n long long power(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) res *= a; n     a *= a; n     n /= 2; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long k = 1, m = 2; n   while (k != n + 1) { n     long long p = (k + 1) * k; n     long long u = k + 1; n     long long v = (k + 1) * (k + 1) * k; n     cout << v - (m) / k <<   n ; n     m = p; n     k++; n   } n   return 0; n } n