#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n struct node { n   int a; n   int b; n   int sum; n }; n int n, q; n int nblock; n node nums[300315]; n int acum = 0; n int fib[300315]; n long long auxi; n void add(int &a, int b) { n   a += b; n   if (a > 1000000009) a -= 1000000009; n } n inline int calcf(int x) { n   if (x <= 1 || fib[x] != 0) return fib[x]; n   fib[x] = 0; n   add(fib[x], calcf(x - 2)); n   add(fib[x], calcf(x - 1)); n   return fib[x]; n } n inline int sumfibo(int a, int b, int posx, int posy) { n   int x = 0; n   auxi = (1LL * calcf(posx - 1) * a) % (1LL * 1000000009); n   add(x, int(auxi)); n   auxi = (1LL * calcf(posx) * b) % (1LL * 1000000009); n   add(x, int(auxi)); n   int y = 0; n   auxi = (1LL * calcf(posy) * a) % (1LL * 1000000009); n   add(y, int(auxi)); n   auxi = (1LL * calcf(posy + 1) * b) % (1LL * 1000000009); n   add(y, int(auxi)); n   add(y, 1000000009 - x); n   return y; n } n inline void update(int ini, int fin) { n   int inc = 1; n   int acumblock = 0; n   if ((ini - 1) / nblock == (fin - 1) / nblock) { n     for (int i = 0; i < nblock; i++) { n       int j = i + ini; n       if (j <= fin) { n         add(acumblock, calcf(inc)); n         inc++; n       } n       if (((j - 1) / nblock) == (fin - 1) / nblock) { n         add(nums[j].sum, acumblock); n       } n     } n   } else { n     int iniblock; n     iniblock = ini - ((ini - 1) % nblock); n     int l = iniblock; n     if (ini % nblock != 1) { n       for (int i = ini; i < iniblock + nblock; i++) { n         add(acumblock, calcf(inc)); n         inc++; n         add(nums[i].sum, acumblock); n       } n       l = iniblock + nblock; n     } n     while (l + nblock <= fin + 1) { n       add(nums[l].a, calcf(inc)); n       add(nums[l].b, calcf(inc + 1)); n       inc += nblock; n       l += nblock; n     } n     acumblock = 0; n     for (int i = 0; i < nblock; i++) { n       int j = i + l; n       if (j <= fin) { n         add(acumblock, calcf(inc)); n         inc++; n       } n       if (((j - 1) / nblock) == (fin - 1) / nblock) add(nums[j].sum, acumblock); n     } n   } n } n inline int get(int ini, int fin) { n   int iniblock; n   int ans = 0; n   if ((ini - 1) / nblock == (fin - 1) / nblock) { n     iniblock = ini - ((ini - 1) % nblock); n     if (ini % nblock) { n       if (fin % nblock) n         add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, n                          fin % nblock)); n       else n         add(ans, n             sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, nblock)); n     } else n       add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, nblock, nblock)); n     add(ans, nums[fin].sum); n     if (ini % nblock != 1) { n       add(ans, 1000000009 - nums[ini - 1].sum); n     } n     return ans; n   } else { n     iniblock = ini - ((ini - 1) % nblock); n     if (ini % nblock) n       add(ans, n           sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, nblock)); n     else n       add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, nblock, nblock)); n     add(ans, nums[iniblock + nblock - 1].sum); n     if (ini % nblock != 1) { n       add(ans, 1000000009 - nums[ini - 1].sum); n     } n     int l = iniblock + nblock; n     while (l + nblock <= fin + 1) { n       add(ans, sumfibo(nums[l].a, nums[l].b, 1, nblock)); n       add(ans, nums[l + nblock - 1].sum); n       l += nblock; n     } n     if (l <= fin) { n       add(ans, nums[fin].sum); n       add(ans, sumfibo(nums[l].a, nums[l].b, 1, fin % nblock)); n     } n     return ans; n   } n } n int main() { n   fib[0] = 0; n   fib[1] = 1; n   fib[2] = 1; n   scanf( %d %d , &n, &q); n   nblock = int(sqrt(n * 1.0)); n   if (nblock * nblock != n) nblock++; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     add(acum, x); n     nums[i].sum = acum; n     if (i % nblock == 0) acum = 0; n   } n   for (int i = 0; i < q; i++) { n     int x, y, z; n     scanf( %d %d %d , &x, &y, &z); n     if (x == 1) { n       update(y, z); n     } else if (x == 2) { n       printf( %d n , get(y, z)); 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 const double EPS = 1e-8; n const int MOD = 998244353; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n int mod = MOD; n struct ModInt { n   unsigned val; n   ModInt() : val(0) {} n   ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} n   ModInt pow(long long exponent) { n     ModInt tmp = *this, res = 1; n     while (exponent > 0) { n       if (exponent & 1) res *= tmp; n       tmp *= tmp; n       exponent >>= 1; n     } n     return res; n   } n   ModInt &operator+=(const ModInt &rhs) { n     if ((val += rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator-=(const ModInt &rhs) { n     if ((val += mod - rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator*=(const ModInt &rhs) { n     val = (unsigned long long)val * rhs.val % mod; n     return *this; n   } n   ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } n   bool operator==(const ModInt &rhs) const { return val == rhs.val; } n   bool operator!=(const ModInt &rhs) const { return val != rhs.val; } n   bool operator<(const ModInt &rhs) const { return val < rhs.val; } n   bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } n   bool operator>(const ModInt &rhs) const { return val > rhs.val; } n   bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } n   ModInt operator-() const { return ModInt(-val); } n   ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } n   ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } n   ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } n   ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } n   friend ostream &operator<<(ostream &os, const ModInt &rhs) { n     return os << rhs.val; n   } n   friend istream &operator>>(istream &is, ModInt &rhs) { n     long long x; n     is >> x; n     rhs = ModInt(x); n     return is; n   } n  n  private: n   ModInt inv() const { n     unsigned a = val, b = mod; n     int x = 1, y = 0; n     while (b) { n       unsigned tmp = a / b; n       a -= tmp * b; n       swap(a, b); n       x -= tmp * y; n       swap(x, y); n     } n     return ModInt(x); n   } n }; n ModInt abs(const ModInt &x) { return x.val; } n struct Combinatorics { n   Combinatorics(int MAX = 5000000) { n     MAX <<= 1; n     fact.resize(MAX + 1); n     fact_inv.resize(MAX + 1); n     fact[0] = 1; n     for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; n     fact_inv[MAX] = ModInt(1) / fact[MAX]; n     for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; n   } n   ModInt nCk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[k] * fact_inv[n - k]; n   } n   ModInt nPk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[n - k]; n   } n   ModInt nHk(int n, int k) { n     if (n < 0 || k < 0) return ModInt(0); n     return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); n   } n  n  private: n   vector<ModInt> fact, fact_inv; n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   vector<int> w(n); n   for (int i = (0); i < (n); ++i) cin >> w[i]; n   int dislike = 0, like = 0; n   for (int i = (0); i < (n); ++i) { n     if (a[i] == 0) n       dislike += w[i]; n     else n       like += w[i]; n   } n   for (int i = (0); i < (n); ++i) { n     vector<vector<vector<ModInt> > > dp( n         m + 1, vector<vector<ModInt> >(m + 1, vector<ModInt>(m + 1, 0))); n     dp[0][0][0] = 1; n     int dl = dislike, l = like; n     if (a[i] == 0) n       dl -= w[i]; n     else n       l -= w[i]; n     for (int x = (0); x < (m); ++x) { n       for (int y = (0); y < (m + 1); ++y) n         for (int z = (0); z < (m + 1); ++z) { n           int sum = like + dislike - z + (x - y - z) + (a[i] == 0 ? -y : y); n           if (a[i] == 0 && y > w[i]) continue; n           if (z > dl || y + z > x || sum <= 0) continue; n           dp[x + 1][y + 1][z] += n               dp[x][y][z] * (w[i] + (a[i] == 0 ? -y : y)) / sum; n           dp[x + 1][y][z + 1] += dp[x][y][z] * (dl - z) / sum; n           dp[x + 1][y][z] += dp[x][y][z] * (l + (x - y - z)) / sum; n         } n     } n     ModInt ans = 0; n     for (int y = (0); y < (m + 1); ++y) n       for (int z = (0); z < (m + 1); ++z) { n         if (a[i] == 0 && y > w[i]) continue; n         if (z > dl || y + z > m) continue; n         ans += dp[m][y][z] * (w[i] + (a[i] == 0 ? -y : y)); n       } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, v1, v2, t1, t2; n   cin >> s >> v1 >> v2 >> t1 >> t2; n   int res1 = (s * v1) + (t1 * 2); n   int res2 = (s * v2) + (t2 * 2); n   if (res1 < res2) { n     cout <<  First ; n   } n   if (res1 > res2) { n     cout <<  Second ; n   } n   if (res1 == res2) { n     cout <<  Friendship ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1200000; n const int inf = 0x3f3f3f3f; n int n, m, k; n vector<int> g[M]; n int ans; n int dfs(int u, int f) { n   vector<int> v = {0, 0}; n   for (int to : g[u]) { n     if (to == f) continue; n     v.push_back(dfs(to, u)); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   int sz = g[u].size(); n   ans = max(ans, v[0] + v[1] + sz + 1); n   return v[0] + sz - 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (int _ = (1); _ <= (t); ++_) { n     cin >> n; n     int u, v; n     for (int i = (1); i <= (n); ++i) g[i].clear(); n     for (int i = (2); i <= (n); ++i) { n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     ans = 0; n     dfs(1, 1); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class BIT { n  public: n   int size; n   vector<long long> arr; n   BIT(int n) { n     size = n + 5; n     arr.resize(size); n   } n   void add(int i, long long v) { n     for (int x = i + 1; x < size; x += (x & (-x))) arr[x] += v; n   } n   long long sum(int i) { n     i = min(i, size - 2); n     long long res = 0; n     for (int x = i + 1; x > 0; x -= (x & (-x))) res += arr[x]; n     return res; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, w; n   cin >> n >> w; n   vector<pair<long long, long long>> p(n); n   for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second; n   sort(p.begin(), p.end(), n        [&](pair<long long, long long> p0, pair<long long, long long> p1) { n          if (abs(p0.first) * abs(p1.second - w) == n              abs(p1.first) * abs(p0.second - w)) { n            return abs(p0.first) * abs(p1.second - w + 1) > n                   abs(p1.first) * abs(p0.second - w + 1); n          } else { n            return abs(p0.first) * abs(p1.second - w) < n                   abs(p1.first) * abs(p0.second - w); n          } n        }); n   vector<int> in(n); n   for (int i = 0; i < n; i++) in[i] = i; n   sort(in.begin(), in.end(), [&](int i0, int i1) { n     pair<long long, long long> p0 = p[i0], p1 = p[i1]; n     if (abs(p0.first) * abs(p1.second + w) == n         abs(p1.first) * abs(p0.second + w)) { n       return abs(p0.first) * abs(p1.second + w - 1) > n              abs(p1.first) * abs(p0.second + w - 1); n     } else { n       return abs(p0.first) * abs(p1.second + w) < n              abs(p1.first) * abs(p0.second + w); n     } n   }); n   BIT bit(n); n   long long res = 0; n   for (int i = n - 1; i >= 0; i--) { n     res += bit.sum(in[i]); n     bit.add(in[i], 1); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 50; n ; n const int M = 255; n const int mod = 998244353; n const int mo = 123; n const double pi = acos(-1.0); n int n, m; n int ans = 0; n int vis[N]; n vector<int> vec; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     int res = 0; n     for (int j = 0, x; j < m; j++) { n       scanf( %d , &x); n       if (x) res |= (1 << j); n     } n     if (!vis[res]) { n       vec.push_back(res); n       vis[res]++; n     } n   } n   bool ok = false; n   for (int i = 0; i < vec.size(); i++) { n     for (int j = 0; j < vec.size(); j++) { n       int x = vec[i] + vec[j]; n       int y = vec[i] ^ vec[j]; n       if (x == y) ok = true; n     } n   } n   if (ok) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int n, br; n int p[MAXN], niz[MAXN], num[MAXN], bio[MAXN]; n inline void ne() { n   printf( NO n ); n   exit(0); n } n void dfs1(int x) { n   if (bio[x]) return; n   bio[x] = br; n   num[br]++; n   dfs1(niz[x]); n } n void dfs2(int x) { n   if (bio[x]) return; n   bio[x] = 1; n   if (bio[niz[x]]) return; n   printf( %d %d n , x, niz[x]); n   dfs2(niz[x]); n } n void dfs3(int x, int y) { n   if (bio[x]) return; n   bio[x] = 1; n   printf( %d %d n , x, y); n   dfs3(niz[x], niz[y]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) niz[p[i]] = p[p[i]]; n   br = 1; n   for (int i = 1; i <= n; i++) n     if (!bio[i]) { n       dfs1(i); n       br++; n     } n   int poc = 0, val = MAXN, grupa = 0; n   for (int i = 1; i <= n; i++) n     if (num[bio[i]] < val) { n       val = num[bio[i]]; n       poc = i; n       grupa = bio[i]; n     } n   if (num[grupa] > 2) ne(); n   for (int i = 1; i < br; i++) n     if (num[i] % num[grupa] != 0) ne(); n   printf( YES n ); n   memset(bio, 0, sizeof(bio)); n   dfs2(poc); n   for (int i = 1; i <= n; i++) n     if (!bio[i]) dfs3(i, poc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> primes; n void sieve() { n   vector<bool> isComposite(100000000, false); n   for (int i = 2; i * i <= 100000000; i++) { n     if (isComposite[i] == false) n       for (int j = i * 2; j <= 100000000; j += i) isComposite[j] = true; n   } n   for (int i = 2; i <= 100000000; i++) n     if (isComposite[i] == false) primes.push_back(i); n   for (auto it : primes) cout << it <<    ; n } n long long LCM(int n) { n   long long lcm = 1; n   for (int i = 0; i < primes.size() && primes[i] <= n; i++) { n     int pp = primes[i]; n     while (pp * primes[i] <= n) pp = pp * primes[i]; n     lcm *= pp; n     lcm %= 1000000007; n   } n   return lcm; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     string keyboard; n     string s; n     cin >> keyboard; n     cin >> s; n     map<char, int> mp; n     for (int i = 0; i < keyboard.size(); i++) { n       mp[keyboard[i]] = i + 1; n     } n     long long int time = 0; n     for (int i = 1; i < s.length(); i++) { n       time += abs((mp[s[i]] - mp[s[i - 1]])); n     } n     cout << time <<   n ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n #define sfd(x) scanf( %d ,&x) n #define sfd2(x,y) scanf( %d%d ,&x,&y) n #define sfd3(x,y,z) scanf( %d%d%d ,&x,&y,&z) n #define sfs(x) scanf( %s ,x) n #define sfll(x) scanf( %lld ,&x) n #define sfll2(x,y) scanf( %lld%lld ,&x,&y) n #define pfd(x) printf( %d ,x) n #define pfs(x) printf( %s ,x) n #define pfll(x) printf( %lld ,x) n #define nl printf(  n ) n #define sp printf(   ) n #define pfyes printf( YES ) n #define pfno printf( NO ) n #define pf0 printf( 0 ) n #define FOR(i,a,b,in) for(int i=a;i<=b;i+=in) n #define RFOR(i,a,b,de) for(int i=a;i>=b;i-=de) n #define rep(i,n) for(int i=0;i<n;i++) n #define pb push_back n #define mkp make_pair n #define mod 998244353 n typedef long long int ll; n typedef vector<int> vi; n typedef pair<int,int> pii; n // ------------------------// n ll power(long long x, int y, int p) n { n     ll res = 1; n   x = x % p;  n    if (x == 0) return 0;  n   while (y > 0) n     { n         if (y & 1) n             res = (res*x) % p; n         y = y>>1;  n         x = (x*x) % p; n     } n     return res; n } n int main() n { n  int t; n  sfd(t); n  while(t--) n  { n   int n; n   sfd(n); n   char s[n+1]; n   sfs(s); n   int c0 = 0; n   int c1 = 0; n   int cx = 0; n   rep(i,n) n   { n    if(s[i]== 1 ) n    { n     cx+=1; n    } n    else n    { n     c1+=(cx/2); n     cx=0; n     c0+=1; n    } n   } n   c1+=(cx/2); n   ll v1=1,v2=1,v3=1; n   for(int i=1;i<=c0;i++) n   { n    v1=(v1*i)%mod; n   } n   for(int i=1;i<=c1;i++) n   { n    v2=(v2*i)%mod; n   } n   for(int i=1;i<=c0+c1;i++) n   { n    v3=(v3*i)%mod; n   } n   v1=(v1*v2)%mod; n   ll ans = (v3*power(v1,mod-2,mod))%mod; n   pfll(ans); n   nl; n    n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int fen1[200002]; n void upd1(int pos, int val) { n   for (; pos <= 200000; pos |= (pos + 1)) fen1[pos] += val; n } n int fnd1(int pos) { n   int ret = 0; n   for (; pos >= 0; pos = (pos & (pos + 1)) - 1) ret += fen1[pos]; n   return ret; n } n int fen2[200002]; n void upd2(int pos, int val) { n   for (; pos >= 0; pos = (pos & (pos + 1)) - 1) fen2[pos] += val; n } n int fnd2(int pos) { n   int ret = 0; n   for (; pos <= 200000; pos |= (pos + 1)) ret += fen2[pos]; n   return ret; n } n int now[200002]; n int main() { n   int n, k, a, b, q; n   scanf( %d%d%d%d%d , &n, &k, &a, &b, &q); n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 1) { n       int d, aa; n       scanf( %d%d , &d, &aa); n       --d; n       int add = max(0, min(aa, b - now[d])); n       upd1(d, add); n       add = max(0, min(aa, a - now[d])); n       upd2(d, add); n       now[d] += aa; n     } else { n       int p; n       scanf( %d , &p); n       --p; n       int ans = fnd1(p - 1) + fnd2(p + k); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     k = n * m / 2 - k; n     if (n % 2 == 1) { n       swap(n, m); n       k = n * m / 2 - k; n     } n     if (m % 2 == 1) { n       if ((k >= n / 2 && (k - n / 2) % (2) == 0)) { n         cout <<  YES  <<   n ; n         continue; n       } else { n         cout <<  NO  <<   n ; n         continue; n       } n     } else { n       if (k % 2 == 0) n         cout <<  YES  <<   n ; n       else n         cout <<  NO  <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n struct node { n   long long num; n   int p; n } nd[maxn]; n long long sum[maxn], f[maxn]; n bool cp1(node a, node b) { return a.num < b.num; } n int main() { n   int n, k; n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> nd[i].num; n     nd[i].p = i; n   } n   sort(nd + 1, nd + 1 + n, cp1); n   cin >> k; n   sum[0] = 0; n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nd[i].num; n   f[1] = 0; n   for (int i = 2; i <= k; i++) n     f[i] = nd[i].num * (i - 1) - sum[i - 1] + f[i - 1]; n   long long mm = f[k]; n   int p = 1; n   for (int i = k + 1; i <= n; i++) { n     f[i] = nd[i].num * (k - 1) + f[i - 1] + nd[i - k].num * (k - 1) - n            2 * (sum[i - 1] - sum[i - k]); n     if (f[i] < mm) { n       mm = f[i]; n       p = i - k + 1; n     } n   } n   for (int i = 0; i < k; i++) n     printf( %d%c , nd[i + p].p, i == k - 1 ?   n  :    ); n } n


#include<bits/stdc++.h> n #define ll int n #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); n using namespace std; n ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum; n string s1, s2, s; n int main() n { n     FAST_IO; n     cin >> n >> m >> k; n     ll h[n + 1][m], l[n][m + 1]; n     vector<vector<ll> >re ( n + 1, vector<ll> ( m + 1, 0 ) ); n     vector<vector<ll> >temp ( n + 1, vector<ll> ( m + 1, 0 ) ); n  n     for ( i = 1; i <= n; i++ ) n         for ( j = 1; j < m; j++ ) n             cin >> h[i][j]; n  n     for ( i = 1; i < n; i++ ) n         for ( j = 1; j <= m; j ++ ) n             cin >> l[i][j]; n  n     for ( t = 1; t <= k / 2; t++ ) n     { n         for ( i = 1; i <= n; i++ ) n         { n             for ( j = 1; j <= m; j++ ) n             { n                 temp[i][j] = ( int ) 1e9;; n                 if ( i != n ) n                     temp[i][j] = min ( temp[i][j], re[i + 1][j] + l[i][j] * 2 ); n                 if ( i != 1 ) n                     temp[i][j] = min ( temp[i][j], re[i - 1][j] + l[i - 1][j] * 2 ); n                 if ( j != m ) n                     temp[i][j] = min ( temp[i][j], re[i][j + 1] + h[i][j] * 2 ); n                 if ( j != 1 ) n                     temp[i][j] = min ( temp[i][j], re[i][j - 1] + h[i][j - 1] * 2 ); n             } n         } n         swap ( re, temp ); n     } n     for ( i = 1; i <= n; cout << endl, i++ ) n         for ( j = 1; j <= m; j++ ) n         { n             if ( j > 1 ) cout <<    ; n             cout << ( k % 2 ? -1 : re[i][j] ); n         } n  n }


#include <bits/stdc++.h> n using namespace std; n // #include <atcoder/all> n // using namespace atcoder; n  n #define rep(i,n) for (int i = 0; i < (n); ++i) n #define STRING(str) #str n #define ll long long n #define ld long double n  n template <typename T> n void print(T x, string name) { n  cout << name+ :   << x << endl; n } n  n template <typename T > n void print_vec_1d(vector<T> x, string name) { n  cout << name << endl; n  cout <<  [  ; n  for (int i=0;i<x.size();i++) { n   cout << x[i]; n   if (i != x.size()-1) cout <<  ,  ; n   else cout <<   ]  << endl; n  } n } n  n template <typename T > n void print_vec_2d(vector<vector<T>> x, string name) { n  cout << name << endl; n  cout <<  [  ; n  for (int i=0;i<x.size();i++) { n   cout <<  [  ; n   for (int j=0;j<x[i].size();j++) { n    cout << x[i][j]; n    if (j != x[i].size()-1) cout <<  ,  ; n    else { n     cout <<   ] ; n     if (!((i == x.size()-1) && (j == x[i].size()-1))) { n      cout <<  ,  << endl; n     } n    } n   } n  } n  cout <<   ]  << endl; n } n  n template <typename T > n void print_vec_3d(vector<vector<vector<T>>> x, string name) { n  cout << name << endl; n  cout <<  [  ; n  for (int i=0;i<x.size();i++) { n   cout <<  [  ; n   for (int j=0;j<x[i].size();j++) { n    cout <<  [  ; n    for (int k=0;k<x[i][j].size();k++) { n     cout << x[i][j][k]; n     if (k != x[i][j].size()-1) cout <<  ,  ; n     else { n      cout <<   ] ; n      if (!((i == x.size()-1) && (j == x[i].size()-1) && (k == x[i][j].size()-1))) { n       cout <<  ,  << endl; n      } n     } n    } n    if (j != x[i].size()-1) cout <<  ,  ; n    else { n     cout <<   ] ; n     if (!((i == x.size()-1) && (j == x[i].size()-1))) { n      cout <<  ,  << endl; n     } n    } n   } n  } n  cout <<   ]  << endl; n } n  n int main() { n  // ios::sync_with_stdio(0); n  // cin.tie(0); n  n  int n; n  cin >> n; n  int ind_2; n  cout <<  ? 1   << n <<   n ; n  cin >> ind_2; n  ind_2--; n  n  int a1, a2; n  if (ind_2 != 0 && ind_2 != n-1) { n   cout <<  ? 1   << ind_2+1 <<   n ; n   cin >> a1; n   a1--; n  n   int a2; n   cout <<  ?   << ind_2+1 <<     << n <<   n ; n   cin >> a2; n   a2--; n  } n  n  bool left_flag; n  if (ind_2 == n-1) { n   left_flag = true; n  } else if (ind_2 == 0) { n   left_flag = false; n  } else if (ind_2 == a1) { n   left_flag = true; n  } else { // ind_2 == a2 n   left_flag = false; n  } n  if (left_flag) { n   // ? ? ? 1 ? ? ? 2 ? ? ? ? n   int ok = 0; n   int ng = ind_2; n   while (abs(ok-ng) > 1) { n    int mid = (ok+ng)/2; n    int b; n    cout <<  ?   << mid+1 <<     << ind_2+1 <<   n ; n    cin >> b; n    b--; n    if (b == ind_2) { n     ok = mid; n    } else { n     ng = mid; n    } n   } n   cout <<  !   << ok+1 <<   n ; n  } else { n   // ? ? ? ? ? ? ? 2 ? 1 ? ? n   int ok = n-1; n   int ng = ind_2; n   while (abs(ok-ng) > 1) { n    int mid = (ok+ng)/2; n    int b; n    cout <<  ?   << ind_2+1 <<     << mid+1 <<   n ; n    cin >> b; n    b--; n    if (b == ind_2) { n     ok = mid; n    } else { n     ng = mid; n    } n   } n   cout <<  !   << ok+1 <<   n ; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int INFint = 2147483647; n const long long INF = 9223372036854775807ll; n const long long MOD = 1000000007ll; n long long cnt[100000]; n int main() { n   ios_base::sync_with_stdio(0); n   long long a; n   cin >> a; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     long long sum = 0; n     for (int j = i; j < s.size(); j++) { n       sum += s[j] -  0 ; n       cnt[sum]++; n     } n   } n   long long ans = 0; n   if (a == 0) { n     ans = cnt[0] * cnt[0]; n     for (int i = 1; i < 100000; i++) { n       ans += cnt[0] * cnt[i] * 2; n     } n   } else { n     for (int i = 1; i < 100000; i++) { n       if (i <= a) { n         if (a % i == 0 && a / i < 100000) ans += cnt[i] * cnt[a / i]; n       } else n         break; n     } n   } n   cout << ans << endl; n   fprintf(stderr,   nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void debug(T a, T b) { n   ; n } n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n namespace std { n template <class S, class T> n ostream& operator<<(ostream& out, const pair<S, T>& a) { n   out <<  (  << a.first <<  ,  << a.second <<  ) ; n   return out; n } n }  // namespace std n long long int readL() { n   long long int res; n   scanf( %I64d , &res); n   return res; n } n void printL(long long int res) { printf( %I64d , res); } n const int INF = 5e8; n int n, m; n vector<int> g[100005]; n pair<int, int> mst2[100005]; n pair<int, int> es[100005]; n int idx[100005], rev[100005]; n vector<int> g2[100005]; n int res[100005]; n bool cut[100005]; n int size[100005], whole; n int gsize(int v, int p) { n   size[v] = 1; n   for (auto to : g2[v]) { n     if (cut[to] || to == p) continue; n     size[v] += gsize(to, v); n   } n   return size[v]; n } n int root, cost; n void groot(int v, int p) { n   int C = whole - size[v]; n   for (auto to : g2[v]) { n     if (cut[to] || to == p) continue; n     groot(to, v); n     chmax(C, size[to]); n   } n   if (cost > C) { n     cost = C; n     root = v; n   } n } n void dfs(int v, int rank) { n   whole = gsize(v, -1); n   cost = INF; n   groot(v, -1); n   v = root; n   res[v] = rank; n   if (whole == 1) return; n   cut[v] = 1; n   for (auto to : g2[v]) { n     if (cut[to]) continue; n     dfs(to, rank + 1); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < (m); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a; n     --b; n     if (a > b) swap(a, b); n     es[i] = {a, b}; n   } n   es[m++] = {0, n - 1}; n   sort(es, es + m, [&](pair<int, int> a, pair<int, int> b) { n     return a.second - a.first < b.second - b.first; n   }); n   set<pair<pair<int, int>, int> > range; n   int cnt = 0; n   for (int i = 0; i < (m); ++i) { n     int a = es[i].first, b = es[i].second; n     auto it = range.lower_bound({{a, -1}, -1}); n     pair<int, int> last = {-1, -1}; n     while (it != range.end() && it->first.second <= b) { n       last = it->first; n       g[cnt].push_back(it->second); n       g[it->second].push_back(cnt); n       range.erase(it++); n     } n     if (last.second == b) n       mst2[cnt] = {last.first, b}; n     else n       mst2[cnt] = {b - 1, b}; n     range.insert({{a, b}, cnt}); n     ++cnt; n   } n   for (int i = 0; i < (cnt); ++i) n     if (mst2[i].second > mst2[i].first) swap(mst2[i].first, mst2[i].second); n   for (int i = 0; i < (cnt); ++i) idx[i] = i; n   sort(idx, idx + cnt, [&](int a, int b) { return mst2[a] < mst2[b]; }); n   for (int i = 0; i < (cnt); ++i) rev[idx[i]] = i; n   for (int i = 0; i < (cnt); ++i) n     for (auto to : g[i]) g2[rev[i]].push_back(rev[to]); n   dfs(0, 0); n   for (int i = 0; i < (cnt); ++i) printf( %d n , res[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p[110], S = 0, P = 1, ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lf , &p[i]); n   sort(p + 1, p + n + 1); n   for (int i = n; i >= 1; i--) n     if (1.0 - S >= 1e-12) { n       ans += P * p[i] * (1 - S); n       S += p[i] / (1.0 - p[i]); n       P *= (1.0 - p[i]); n     } n   printf( %.12lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int x[N], p[N], n, d, ind[N]; n long long ans = 0; n vector<pair<int, int> > tank; n int head, cur; n bool cmp(int a, int b) { return x[a] < x[b]; } n bool use(int dis) { n   while (dis) { n     if (head == (int)tank.size()) return 1; n     int tmp = min(dis, tank[head].first); n     dis -= tmp; n     tank[head].first -= tmp; n     cur -= tmp; n     if (tank[head].first == 0) head++; n   } n   return 0; n } n void sale(int p) { n   while ((int)tank.size() > head && tank.back().second > p) { n     ans -= 1LL * tank.back().second * tank.back().first; n     cur -= tank.back().first; n     tank.pop_back(); n   } n } n void buy(int p) { n   ans += 1LL * p * (n - cur); n   tank.push_back(make_pair(n - cur, p)); n   cur = n; n } n int main() { n   int m; n   scanf( %d%d%d , &d, &n, &m); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &x[i], &p[i]), ind[i] = i; n   x[m + 1] = d; n   p[m + 1] = 0; n   ind[m + 1] = m + 1; n   sort(ind + 1, ind + 1 + m, cmp); n   tank.push_back(make_pair(n, 0)); n   cur = n; n   for (int i = 1; i <= m + 1; ++i) { n     int dis = x[ind[i]] - x[ind[i - 1]]; n     if (use(dis)) return 0 * puts( -1 ); n     sale(p[ind[i]]); n     buy(p[ind[i]]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 1, INF = 1e17, MOD = 1e9 + 7; n long long n; n string s, t =  UDRL ; n map<char, long long> m; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> s; n   for (long long i = 0; i < n; ++i) { n     m[s[i]]++; n   } n   cout << 2 * (min(m[t[0]], m[t[1]]) + min(m[t[2]], m[t[3]])) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buffer[100000], *p1, *p2; n template <class T> n void read(T& x) { n   char ch = ((p1 == p2 && n               (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) n                  ? EOF n                  : *p1++); n   x = 0; n   bool f = 1; n   while (!( 0  <= ch && ch <=  9 ) && ch !=  - ) n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   if (ch ==  - ) n     f = 0, n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   while ( 0  <= ch && ch <=  9 ) n     x = (x << 1) + (x << 3) + ch -  0 , n     ch = ((p1 == p2 && n            (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) n               ? EOF n               : *p1++); n   x = (f) ? x : -x; n } n int n, m; n int a[605]; n bool vis[605][605]; n int dp[605][605]; n int DP(int l, int r) { n   if (l >= r) return 1; n   if (vis[l][r]) return dp[l][r]; n   vis[l][r] = 1; n   int& ans = dp[l][r]; n   int mi = n + 1, mip = -1; n   for (register int i = l; i <= r; i++) n     if (mi > a[i]) mi = a[i], mip = i; n   int le = 0, ri = 0; n   for (register int i = l - 1; i < mip; i++) n     le += (long long int)DP(l, i) * DP(i + 1, mip - 1) % 998244353, n         le %= 998244353; n   for (register int i = r + 1; i > mip; i--) n     ri += (long long int)DP(i, r) * DP(mip + 1, i - 1) % 998244353, n         ri %= 998244353; n   return ans = (long long int)le * ri % 998244353; n } n int main() { n   read(n), read(m); n   for (register int i = 1; i <= n; i++) read(a[i]); n   printf( %d , DP(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const int N = 1000000 + 10; n const int M = 1000; n const string st2 =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ; n const string s1 =  abcdefghijklmnopqrstuvwxyz ; n pair<int, int> a[N]; n int b[N]; n string ans[N], da[N]; n int main() { n   cin >> b[1] >> b[2] >> b[3] >> b[4] >> b[5] >> b[6]; n   int n; n   cin >> n; n   da[1] =  S ; n   da[2] =  M ; n   da[3] =  L ; n   da[4] =  XL ; n   da[5] =  XXL ; n   da[6] =  XXXL ; n   for (int i = 1; i <= n; i++) { n     string second; n     cin >> second; n     if (second ==  S ) n       a[i] = make_pair(1, 0); n     else if (second ==  M ) n       a[i] = make_pair(2, 0); n     else if (second ==  L ) n       a[i] = make_pair(3, 0); n     else if (second ==  XL ) n       a[i] = make_pair(4, 0); n     else if (second ==  XXL ) n       a[i] = make_pair(5, 0); n     else if (second ==  XXXL ) n       a[i] = make_pair(6, 0); n     else if (second[0] ==  S ) n       a[i] = make_pair(1, 1); n     else if (second[0] ==  M ) n       a[i] = make_pair(2, 1); n     else if (second[0] ==  L ) n       a[i] = make_pair(3, 1); n     else if (second[1] ==  L ) n       a[i] = make_pair(4, 1); n     else if (second[2] ==  L ) n       a[i] = make_pair(5, 1); n     else n       a[i] = make_pair(6, 1); n   } n   for (int i = 1; i <= n; i++) n     if (a[i].second == 0) { n       if (b[a[i].first] != 0) { n         ans[i] = da[a[i].first]; n         b[a[i].first]--; n       } n     } n   for (int i = 1; i <= 6; i++) { n     for (int j = 1; j <= n; j++) n       if (ans[j] ==    && a[j].second == 1) { n         if (a[j].first + 1 == i && b[i] > 0) { n           b[i]--; n           ans[j] = da[i]; n         } n       } n     for (int j = 1; j <= n; j++) n       if (ans[j] ==    && a[j].second == 1) { n         if (a[j].first == i && b[i] > 0) { n           b[i]--; n           ans[j] = da[i]; n         } n       } n   } n   for (int i = 1; i <= n; i++) n     if (ans[i] ==   ) { n       cout <<  NO  << endl; n       return 0; n     } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) cout << ans[i] << endl; n } n


#include<bits/stdc++.h> n using namespace std; n typedef unsigned long long int ull; n typedef long long int ll; n typedef long double ld; n #define fr(i,a,b) for(ll i=a; i<b; i++) n #define rf(i,a,b) for(ll i=a; i>=b; i--) n #define pb push_back n #define mp make_pair n #define show(a) for(auto el:a)cout<<el<<    n #define ff first n #define ss second n #define pairv vector<pair<ll,ll>> n #define vec vector <ll>  n #define all(a) a.begin(),a.end() n #define fam(v,i) for(auto i=v.begin();i!=v.end();i++) n #define fai(v,i) for(auto i : v) n #define mo 1000000007 n #define nn cout<<  n ; n #define Y cout<< YES n ; n #define N cout<< NO n ; n #define cout_dec cout << fixed << setprecision(9)  n #define casepr(ti)  cout<< case # <<ti+1<< :   n #define fill(a,b) memset(a, b, sizeof(a)) n ll pow1(ll n,ll p) n { n if(p==0) return 1;  n ll x=pow1(n, p/2); n x=(x*x)%mo; n if(p%2==0) n return x; n else n return (x*n)%mo; n } n bool sortbysec(const pair<ll,ll> &a,  n const pair<ll,ll> &b)  n {  n return (a.second < b.second);  n }  n bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2) n { n if(p1.ff < p2.ff) return true; n if(p1.ff == p2.ff) return p1.ff < p2.ss; n return false; n } n ll const maxn=200007; n ll nxt[maxn]; n void seive() n { n       for (ll i = 2; i < maxn; ++i) { n         if (nxt[i] == 0) { n             nxt[i] = i; n             for (ll j = i * i; j < maxn; j += i) { n                 if(j>=maxn) n                 { n                     break; n                 } n                 if (nxt[j] == 0) nxt[j] = i; n             } n         } n     } n } n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n  n     int test=1; n     cin>>test; n     for(int ti=0;ti<test;ti++) n     { n         ll i,j,k,e,f,n,c=0,x=0; n         cin>>n; n         ll m=n+2; n         ll b[n+2]; n         fr(i,0,n+2) n         cin>>b[i]; n         sort(b,b+m); n         fr(i,0,n) n         { n             c=c+b[i]; n         } n         if(c== b[n] || c==b[n+1]) n         { n             fr(i,0,n) n             { n                 cout<<b[i]<<   ; n  n             } n                             nn n                 continue; n         } n         x=-1; n         fr(i,0,n) n         { n             if(c-b[i]+b[n]==b[n+1]) n             { n                 x=i; n                 break; n             } n         } n         if(x==-1) n         { n             cout<<-1; n              n         } n         else n         { n             fr(i,0,n+1) n             { n                 if(i==x) n                 { n                     continue; n                 } n                 cout<<b[i]<<   ; n             } n         } n          n      n  n          n      nn n          n     } n      n      return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300000 + 10; n int n, a[MAXN], p[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i], p[a[i]] = i; n   for (int i = 0; i < n; i++) { n     for (int j = min(i + 100, n - 1); j > i; j--) { n       int nx = 2 * a[j] - a[i], nxt = 2 * a[i] - a[j]; n       if (nx > 0 && nx <= n && p[nx] > j) return cout <<  YES  << endl, 0; n       if (nxt > 0 && nxt <= n && p[nxt] < i) return cout <<  YES  << endl, 0; n     } n   } n   cout <<  NO  << 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   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> S) { n   os <<  {  ; n   for (auto s : S) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class L, class R> n ostream& operator<<(ostream& os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.F <<  :  << m.S <<  ) ; n   return os <<  } ; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << 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   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int R = 65; n char G[R][R]; n int solve() { n   int n, m; n   cin >> n >> m; n   bool all = true, none = true; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> G[i][j]; n       if (G[i][j] ==  A ) n         none = false; n       else n         all = false; n     } n   } n   if (all) return 0; n   if (none) return -1; n   bool row = true; n   for (int i = 1; i <= m; ++i) n     if (G[1][i] ==  P ) row = false; n   if (row) return 1; n   row = true; n   for (int i = 1; i <= m; ++i) n     if (G[n][i] ==  P ) row = false; n   if (row) return 1; n   row = true; n   for (int i = 1; i <= n; ++i) n     if (G[i][1] ==  P ) row = false; n   if (row) return 1; n   row = true; n   for (int i = 1; i <= n; ++i) n     if (G[i][m] ==  P ) row = false; n   if (row) return 1; n   if (G[1][1] ==  A ) return 2; n   if (G[1][m] ==  A ) return 2; n   if (G[n][1] ==  A ) return 2; n   if (G[n][m] ==  A ) return 2; n   for (int i = 2; i <= n - 1; ++i) { n     bool eq = true; n     for (int j = 1; j <= m; ++j) { n       if (G[i][j] ==  P ) eq = false; n     } n     if (eq) return 2; n   } n   for (int i = 2; i <= m - 1; ++i) { n     bool eq = true; n     for (int j = 1; j <= n; ++j) { n       if (G[j][i] ==  P ) eq = false; n     } n     if (eq) return 2; n   } n   bool any = false; n   for (int i = 2; i <= n - 1; ++i) { n     if (G[i][1] ==  A  || G[i][m] ==  A ) any = true; n   } n   for (int i = 2; i <= m - 1; ++i) { n     if (G[1][i] ==  A  || G[n][i] ==  A ) any = true; n   } n   if (any) return 3; n   return 4; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed << setprecision(25); n   int t; n   cin >> t; n   while (t--) { n     int ans = solve(); n     if (ans != -1) n       cout << ans <<   n ; n     else n       cout <<  MORTAL  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define     mod     1000000007 n #define     int      long long  n #define     pb      push_back n #define     endl      n  n #define     ff      first n #define     ss      second n #define     FIO     ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); n #define     all(x)  (x).begin(),(x).end() n int32_t main() n { n       FIO n       int t; cin >> t; n       while(t--) n       { n             int n, x, t; cin >> n >> x >> t; n             if(x > t) cout << 0 << endl; n             else  n             { n                   int a = t/x; n                   int ans=0; n                   if(a>n) n                         ans+= (n*(n-1))/2; n                   else n                   { n                         ans+= (n-a) * a; n                         ans+= (a*(a-1))/2; n                   } n                   cout << ans << endl; n             } n       } n }


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e5 + 7; n long long parent[MAXN], rank1[MAXN]; n vector<pair<long long, pair<long long, long long>>> edges; n void makest(long long v) { n   parent[v] = v; n   rank1[v] = 0; n } n long long find(long long v) { n   if (v == parent[v]) return v; n   return parent[v] = find(parent[v]); n } n void merge(long long a, long long b) { n   a = find(a); n   b = find(b); n   if (a != b) { n     if (rank1[a] < rank1[b]) swap(a, b); n     parent[b] = a; n     if (rank1[a] == rank1[b]) rank1[a]++; n   } n } n signed main() { n   long long n, m, mini = 1e14, node, ans = 0; n   cin >> n >> m; n   long long a[n + 1]; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (mini > a[i]) { n       mini = a[i]; n       node = i; n     } n   } n   for (long long i = 1; i <= n; ++i) { n     makest(i); n   } n   long long x, y, z; n   for (long long i = 1; i <= m; ++i) { n     cin >> x >> y >> z; n     edges.push_back({z, {x, y}}); n   } n   for (long long i = 1; i <= n; ++i) { n     if (i != node) { n       edges.push_back({mini + a[i], {i, node}}); n     } n   } n   sort(edges.begin(), edges.end()); n   for (auto t : edges) { n     x = t.second.first; n     y = t.second.second; n     z = t.first; n     if (find(x) != find(y)) { n       ans += z; n       merge(x, y); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, n, k, h; n int main() { n   int arr[3]; n   cin >> arr[0] >> arr[1] >> arr[2]; n   int y = 1, b = 2, r = 3; n   while (y < arr[0] && b < arr[1] && r < arr[2]) { n     y++; n     r++; n     b++; n   } n   cout << y + b + r; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, SQ = 780; n const long long inf = 1e18; n long long dp[2][SQ]; n vector<int> v[maxn]; n int n, m; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     if ((a = n + 1 - a) >= (SQ - 1)) n       ans += 3; n     else n       v[b].push_back(a); n   } n   for (int i = 1; i < SQ; i++) dp[0][i] = inf; n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     dp[i & 1][SQ - 1] = inf; n     sort(v[i].begin(), v[i].end()); n     int num = 0; n     for (int j = SQ - 2; j >= 0; j--) { n       dp[i & 1][j] = min(dp[i & 1][j + 1], n                          num + min(dp[(i + 1) & 1][0] + (j == 0 ? 0 : 2) + n                                        1ll * ((j * (j + 1)) / 2), n                                    dp[(i + 1) & 1][j + 1])); n       if (!v[i].empty() && v[i].back() == j) num += 3, v[i].pop_back(); n     } n   } n   ans += dp[n & 1][0]; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long modex(long long a, long long b) { n   long long ans = 1LL; n   while (b) { n     if (b % 2) { n       ans *= a; n       ans %= 1000000009; n     } n     a *= a; n     a %= 1000000009; n     b /= 2LL; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, x, y; n   cin >> n >> m; n   if (m <= 20) { n     x = 1LL << m; n     if (x <= n) { n       cout << 0 <<   n ; n       return 0; n     } n   } n   x = modex(2LL, m); n   y = 1LL; n   for (int i = 1; i <= n; i++) { n     if (i >= x) n       y *= (x - i + 1000000009); n     else n       y *= (x - i); n     y %= 1000000009; n   } n   cout << y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if ((abs(a - b)) % 2) { n       cout << -1 << endl; n       continue; n     } n     if (a == b && b == 0) { n       cout << 0 << endl; n       continue; n     } n     if (a == b) n       cout << 1 << endl; n     else n       cout << 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int ckmax(int &a, int b) { return a < b ? a = b, 1 : 0; } n inline int ckmin(int &a, int b) { return a > b ? a = b, 1 : 0; } n int n, r, x[1010]; n double y[1010]; n int main() { n   scanf( %d%d , &n, &r); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , x + i); n     y[i] = r; n     for (int j = 1; j < i; ++j) { n       if (abs(x[i] - x[j]) <= 2 * r) { n         int d = abs(x[i] - x[j]); n         double l = sqrt(4 * r * r - d * d); n         y[i] = max(y[i], y[j] + l); n       } n     } n     printf( %.10f  , y[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream& operator<<(ostream& o, pair<A, B> a) { n   return o << a.first <<     << a.second; n } n template <typename A, typename B> n istream& operator>>(istream& o, pair<A, B>& a) { n   return o >> a.first >> a.second; n } n const int mod = 1e9 + 7, abc = 864197532, N = 300087, K = 111; n long long int modpow(long long int a, int b) { n   long long int ans = 1; n   for (; b; b >>= 1, a = a * a % mod) { n     if (b & 1) ans = ans * a % mod; n   } n   return ans; n } n int inter(vector<int> l, vector<int> r) { n   int ll = *max_element(l.begin(), l.end()), n       rr = *min_element(r.begin(), r.end()); n   if (ll <= rr) return rr - ll + 1; n   return 0; n } n void add(long long int& a, long long int b) { n   a += b; n   if (a >= mod) a -= mod; n } n void del(long long int& a, long long int b) { n   a -= b; n   if (a < 0) a += mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> l(n), r(n), sz(n), inv(n); n   for (int i = 0; i < n; ++i) cin >> l[i]; n   long long int al = 1; n   for (int i = 0; i < n; ++i) { n     cin >> r[i]; n     sz[i] = r[i] - l[i] + 1; n     al = al * sz[i] % mod; n     inv[i] = modpow(sz[i], mod - 2); n   } n   long long int square = 0, sum = 0; n   for (int i = 0; i + 1 < n; ++i) { n     add(sum, inter({l[i], l[i + 1]}, {r[i], r[i + 1]}) * al % mod * inv[i] % n                  mod * inv[i + 1] % mod); n   } n   square = sum; n   for (int i = 0; i + 2 < n; ++i) { n     add(square, inter({l[i], l[i + 1], l[i + 2]}, {r[i], r[i + 1], r[i + 2]}) * n                     al % mod * inv[i] % mod * inv[i + 1] % mod * inv[i + 2] % n                     mod * 2 % mod); n   } n   long long int now = 0; n   for (int i = 0; i + 1 < n; ++i) n     add(now, 1ll * inter({l[i], l[i + 1]}, {r[i], r[i + 1]}) * inv[i] % mod * n                  inv[i + 1] % mod); n   for (int i = 0; i + 1 < n; ++i) { n     if (i - 1 >= 0) n       del(now, 1ll * inter({l[i - 1], l[i]}, {r[i - 1], r[i]}) * inv[i - 1] % n                    mod * inv[i] % mod); n     del(now, 1ll * inter({l[i], l[i + 1]}, {r[i], r[i + 1]}) * inv[i] % mod * n                  inv[i + 1] % mod); n     if (i + 2 < n) n       del(now, 1ll * inter({l[i + 1], l[i + 2]}, {r[i + 1], r[i + 2]}) * n                    inv[i + 1] % mod * inv[i + 2] % mod); n     add(square, now * inter({l[i], l[i + 1]}, {r[i], r[i + 1]}) % mod * inv[i] % n                     mod * inv[i + 1] % mod * al % mod); n     if (i - 1 >= 0) n       add(now, 1ll * inter({l[i - 1], l[i]}, {r[i - 1], r[i]}) * inv[i - 1] % n                    mod * inv[i] % mod); n     add(now, 1ll * inter({l[i], l[i + 1]}, {r[i], r[i + 1]}) * inv[i] % mod * n                  inv[i + 1] % mod); n     if (i + 2 < n) n       add(now, 1ll * inter({l[i + 1], l[i + 2]}, {r[i + 1], r[i + 2]}) * n                    inv[i + 1] % mod * inv[i + 2] % mod); n   } n   cout << ((square + mod - sum * n * 2 % mod) * modpow(al, mod - 2) + n            1ll * n * n) % n               mod; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005]; n long long dp[1005]; n long long dpsum[1005]; n bool b[1005]; n long long ch[1005][1005]; n long long gc(int c, int x) { n   if (x == 0 || c == x) return 1; n   if (!ch[c][x]) { n     ch[c][x] = (gc(c - 1, x - 1) + gc(c - 1, x)) % 998244353; n   } n   return ch[c][x]; n } n int getdp(int x) { n   if (!b[x]) { n     b[x] = 1; n     if (a[x] > 0) { n       for (int i = x + a[x] + 1; i <= n + 1; ++i) { n         dp[x] = n             (dp[x] + gc(i - x - 2, a[x] - 1) * (1LL + dpsum[i])) % 998244353; n       } n     } n   } n   return dp[x]; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   long long ans = 0; n   for (int i = n; i >= 1; --i) { n     dpsum[i] = dpsum[i + 1] + getdp(i); n     dpsum[i] %= 998244353; n   } n   for (int i = 1; i <= n; ++i) { n     ans = ans + getdp(i); n     ans %= 998244353; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   short n, pos, l, r; n   cin >> n >> pos >> l >> r; n   if (1 == l && n == r) { n     cout <<  0 ; n     return 0; n   } n   if (1 == l) { n     cout << abs(r - pos) + 1; n     return 0; n   } n   if (n == r) { n     cout << abs(pos - l) + 1; n     return 0; n   } n   short time = 0; n   if (abs(r - pos) <= abs(pos - l)) { n     time += abs(r - pos) + 1; n     pos = r; n     time += abs(pos - l) + 1; n   } else { n     time += abs(pos - l) + 1; n     pos = l; n     time += abs(r - pos) + 1; n   } n   cout << time; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n      n     int t,n; n     cin>>t; n     while(t--) n     { n         cin>>n; n         string str; n         cin>>str; n          n         int cnt_t=0,cnt_m=0; n          n         for(int i=0;i<n;i++) n         { n             if(str[i]== T ) n             { n                 cnt_t++; n             } n             else n             { n                 cnt_m++; n             } n         } n          n         if(cnt_t!=2*cnt_m) n         { n             cout<< NO <<endl; n         } n          n         else n         { n             int x=0,y=0; n              n             int flag=0; n              n             // forward check n              n             for(int i=0;i<n;i++) n             { n                 if(str[i]== T ) n                 { n                     x++; n                 } n                 if(str[i]== M ) n                 { n                     y++; n                 } n                 if(y>x) n                 { n                     cout<< NO <<endl; n                     flag=1; n                     break; n                 } n             } n              n             if(flag==0) n             { n                 x=0; n                 y=0; n                  n                  n                 // backward check n                 for(int i=n-1;i>=0;i--) n                 { n                     if(str[i]== T ) n                     { n                         x++; n                     } n                     if(str[i]== M ) n                     { n                         y++; n                     } n                     if(y>x) n                     { n                         cout<< NO <<endl; n                         flag=1; n                         break; n                     } n                 } n             } n              n             if(flag==0) n             { n                 cout<< YES <<endl; n             } n         } n          n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n char getc() { n   char c = getchar(); n   while ((c <  A  || c >  Z ) && (c <  a  || c >  z ) && (c <  0  || c >  9 )) n     c = getchar(); n   return c; n } n int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return x * f; n } n int n, a[200010], pre[200010][32], suf[200010][32], PRE[200010], SUF[200010]; n long long ans; n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 32; j++) pre[i][j] = pre[i - 1][j]; n     for (int j = 0; j < 32; j++) n       if (a[i - 1] & (1 << j)) pre[i][j] = i - 1; n   } n   for (int j = 0; j < 32; j++) suf[n + 1][j] = n + 1; n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j < 32; j++) suf[i][j] = suf[i + 1][j]; n     for (int j = 0; j < 32; j++) n       if (a[i + 1] & (1 << j)) suf[i][j] = i + 1; n   } n   a[0] = a[n + 1] = 1000000010; n   for (int i = 1; i <= n; i++) { n     int j = i - 1; n     while (a[j] < a[i]) j = PRE[j]; n     PRE[i] = j; n   } n   SUF[n + 1] = n + 1; n   for (int i = n; i >= 1; i--) { n     int j = i + 1; n     while (a[j] <= a[i]) j = SUF[j]; n     SUF[i] = j; n   } n   for (int i = 1; i <= n; i++) { n     int x = PRE[i], y = SUF[i]; n     int u = PRE[i], v = SUF[i]; n     for (int j = 0; j < 32; j++) n       if (!(a[i] & (1 << j))) u = max(u, pre[i][j]), v = min(v, suf[i][j]); n     ans += 1ll * (SUF[i] - i) * (u - PRE[i]) + 1ll * (i - u) * (SUF[i] - v); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool pal(string s) { n   long long n = s.size(); n   for (long long i = 0; i < n / 2; i++) n     if (s[i] != s[n - i - 1]) return false; n   return true; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test = 1; n   while (test--) { n     long long n, k, x, ab = 1e10, p, c = 0, f; n     cin >> n >> k >> x; n     vector<long long> v(n); n     for (long long i = 0; i < n; i++) { n       cin >> v[i]; n       if (abs(v[i]) < abs(ab)) { n         ab = abs(v[i]); n         p = i; n       } n       if (v[i] < 0) c++; n     } n     if (c % 2 == 0) { n       f = abs(ab) / x + 1; n       f = min(f, k); n       k -= f; n       if (v[p] < 0) n         v[p] += f * x; n       else n         v[p] -= f * x; n     } n     set<pair<long long, long long> > s; n     for (long long i = 0; i < n; i++) s.insert({abs(v[i]), i}); n     while (k > 0) { n       k--; n       p = s.begin()->second; n       if (v[p] < 0) { n         v[p] -= x; n         s.erase(s.begin()); n         s.insert({abs(v[p]), p}); n       } else { n         v[p] += x; n         s.erase(s.begin()); n         s.insert({v[p], p}); n       } n     } n     for (long long i = 0; i < n; i++) cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 550; n char grid[maxn][maxn]; n int rdp[maxn][maxn], cdp[maxn][maxn], dp[maxn][maxn]; n int val[maxn][maxn]; n int main() { n   int n, m, q, c1, c2, r1, r2; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , grid[i]); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (!i) n         rdp[i][j] = 0; n       else { n         rdp[i][j] = rdp[i - 1][j]; n         if (grid[i][j] ==  .  && grid[i - 1][j] ==  . ) rdp[i][j]++; n       } n       if (!j) n         cdp[i][j] = 0; n       else { n         cdp[i][j] = cdp[i][j - 1]; n         if (grid[i][j] ==  .  && grid[i][j - 1] ==  . ) cdp[i][j]++; n       } n     } n   } n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d%d%d , &r1, &c1, &r2, &c2); n     r1--, c1--, r2--, c2--; n     int ans = 0; n     for (int i = r1; i <= r2; i++) ans += cdp[i][c2] - cdp[i][c1]; n     for (int j = c1; j <= c2; j++) ans += rdp[r2][j] - rdp[r1][j]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float H, L; n   cin >> H >> L; n   float x; n   x = ((L * L) - (H * H)) / (2 * H); n   printf( %0.6f , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100, maxp = 20; n int n, a[maxp][maxp], ma[(1 << maxp)], cur[1 << maxp], dp[1 << maxp], cnt[maxn], n     f[maxp][maxn]; n int ans, mp, p; n char s[maxn]; n vector<int> pos[20]; n void solve() { n   dp[mp] = 1; n   for (int i = mp; i >= 0; i--) { n     if (!dp[i]) continue; n     for (int j = 0; j < p; j++) n       if ((i >> j) & 1) dp[(i ^ (1 << j))] = ma[(i ^ (1 << j))]; n   } n   return; n } n void init() { n   for (int i = 1; i <= n; i++) { n     cnt[s[i] -  a ]++; n     pos[s[i] -  a ].push_back(i); n     for (int c = 0; c < p; c++) f[c][i + 1] = f[c][i] + (s[i] == ( a  + c)); n   } n   for (int i = 0; i <= mp; i++) ma[i] = 1; n   for (int i = 0; i < p; i++) { n     for (int j = 0; j <= i; j++) { n       memset(cur, 0, sizeof(cur)); n       if (a[i][j]) continue; n       int pos1 = 0, pos2 = 0; n       while (pos1 < pos[i].size() && pos2 < pos[j].size()) { n         if (i == j && pos[i][pos1] == pos[j][pos2]) { n           pos2++; n           continue; n         } n         int m = 0; n         if (pos[i][pos1] < pos[j][pos2]) { n           for (int c = 0; c < p; c++) { n             if (f[c][pos[j][pos2]] - f[c][pos[i][pos1] + 1] != 0) { n               m |= (1 << c); n             } n           } n           pos1++; n         } else { n           for (int c = 0; c < p; c++) { n             int pp1 = f[c][pos[j][pos2] + 1]; n             int pp2 = f[c][pos[i][pos1]]; n             if (f[c][pos[i][pos1]] - f[c][pos[j][pos2] + 1] != 0) { n               m = m ^ (1 << c); n             } n           } n           pos2++; n         } n         if ((m >> i) & 1) continue; n         if ((m >> j) & 1) continue; n         m = m ^ (mp); n         cur[m] = 1; n       } n       for (int i = 0; i <= mp; i++) cur[i] = !cur[i]; n       for (int ii = mp; ii > 0; ii--) { n         if (cur[ii]) continue; n         for (int jj = 0; jj < p; jj++) { n           if (jj != i && jj != j && (ii >> jj) & 1) { n             cur[(ii ^ (1 << jj))] = 0; n           } n         } n         ma[ii] &= cur[ii]; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> p; n   ans = n; n   mp = (1 << p) - 1; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   for (int i = 0; i < p; i++) n     for (int j = 0; j < p; j++) cin >> a[i][j]; n   init(); n   solve(); n   for (int i = 0; i <= mp; i++) { n     int res = 0; n     if (!dp[i]) continue; n     for (int j = 0; j < p; j++) n       if ((i >> j) & 1) { n         res += cnt[j]; n       } n     ans = min(ans, res); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r, ans = 0; n bool kk(long long x, long long i) { n   if (x == 1) { n     if (i == 1) n       return 1; n     else n       return 0; n   } else { n     long long t = 1, g; n     while (t <= x) t *= 2; n     t--; n     g = t / 2; n     if (g + 1 == i) { n       if (x % 2) n         return 1; n       else n         return 0; n     } else { n       if (i <= g) n         return kk(x / 2, i); n       else { n         return kk(x / 2, i - g - 1); n       } n     } n   } n } n int main() { n   long long i, j; n   cin >> n >> l >> r; n   if (n == 0) { n     cout << 0; n     return 0; n   } n   for (i = l; i <= r; i++) { n     if (kk(n, i) == 1) ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1061109567; n const long long LINF = 4557430888798830399ll; n void TestCase() { n   long long d; n   scanf( %lld , &d); n   d %= 360; n   if (d < 0) d += 360; n   int ans = INF, cnt = 0; n   for (int i = 0; i < 4; ++i) { n     int nd = abs(d - 90 * i); n     if (min(nd, 360 - nd) < ans) { n       ans = min(nd, 360 - nd); n       cnt = i; n     } n   } n   printf( %d n , cnt); n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; ++i) { n     TestCase(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[3010][3010], t[3010][3010], S[3010], T[3010]; n int nxt[3010], len, pos, tl, tr, p[3010], pre[3010][3010], n, len_s[3010]; n void change(int id) { n   int n = strlen(s[id] + 1), j = 0; n   for (int i = 1; i <= n; i++) { n     while (j && S[j + 1] != s[id][i]) j = nxt[j]; n     if (S[j + 1] == s[id][i]) j++; n     if (j == len) { n       for (int k = 1; k <= len; k++) s[id][i - len + k] = T[k]; n       break; n     } n   } n } n void check(int id) { n   int j = 0, diff = 0; n   for (int i = 1; i <= len_s[id]; i++) { n     while (j && S[j + 1] != s[id][i]) j = nxt[j]; n     if (S[j + 1] == s[id][i]) j++; n     if (j == len && pre[id][i] - pre[id][i - len]) { n       p[id] = i - len + 1; n       break; n     } else if (j == len) n       j = nxt[j]; n   } n } n void get_S() { n   int mx = 0; n   for (int i = 1; i <= tl - 1; i++) { n     int flag = 1; n     for (int j = 1; j <= n; j++) n       if (p[j] && (p[j] - i <= 0 || s[j][p[j] - i] != s[pos][tl - i])) { n         flag = 0; n         break; n       } n     if (flag) n       mx++; n     else n       break; n   } n   tl -= mx; n   mx = 0; n   for (int i = 1; i <= n; i++) n     if (p[i]) p[i] = p[i] + len - 1; n   for (int i = 1; i <= len_s[pos] - tr; i++) { n     int flag = 1; n     for (int j = 1; j <= n; j++) n       if (p[j] && (p[j] + i > len_s[j] || s[j][p[j] + i] != s[pos][tr + i])) { n         flag = 0; n         break; n       } n     if (flag) n       mx++; n     else n       break; n   } n   tr += mx; n   len = tr - tl + 1; n   for (int i = 1; i <= len; i++) { n     S[i] = s[pos][i + tl - 1]; n     T[i] = t[pos][i + tl - 1]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n     len_s[i] = strlen(s[i] + 1); n   } n   for (int i = 1; i <= n; i++) scanf( %s , t[i] + 1); n   for (int i = 1; i <= n; i++) { n     int lef = len_s[i] + 1, righ = 0; n     for (int j = 1; j <= len_s[i]; j++) n       if (s[i][j] != t[i][j]) { n         lef = min(lef, j); n         righ = max(righ, j); n         pre[i][j] = pre[i][j - 1] + 1; n       } else n         pre[i][j] = pre[i][j - 1]; n     if (righ - lef + 1 > len) { n       len = righ - lef + 1; n       for (int j = lef; j <= righ; j++) { n         S[j - lef + 1] = s[i][j]; n         T[j - lef + 1] = t[i][j]; n       } n       pos = i; n       tl = lef; n       tr = righ; n     } n   } n   int j = 0; n   for (int i = 2; i <= len; i++) { n     while (j && S[j + 1] != S[i]) j = nxt[j]; n     if (S[j + 1] == S[i]) j++; n     nxt[i] = j; n   } n   for (int i = 1; i <= n; i++) check(i); n   get_S(); n   j = 0; n   for (int i = 2; i <= len; i++) { n     while (j && S[j + 1] != S[i]) j = nxt[j]; n     if (S[j + 1] == S[i]) j++; n     nxt[i] = j; n   } n   for (int i = 1; i <= n; i++) { n     change(i); n     int len = strlen(s[i] + 1); n     for (int j = 1; j <= len; j++) n       if (s[i][j] != t[i][j]) { n         puts( NO ); n         return 0; n       } n   } n   puts( YES ); n   puts(S + 1); n   puts(T + 1); n   return 0; n } n


#include<bits/stdc++.h> n #define int long long n #define reg register n using namespace std; n const int maxn=5e5+5; n const int mod=1e9+7; n const int INF=2e9; n inline int read() n { n     int x=0,f=1; n     char ch=getchar(); n     while(ch< 0  || ch> 9 ) n     { n         if(ch== - ) n         f=-1; n         ch=getchar(); n     } n     while(ch>= 0  && ch<= 9 ) n     { n         x=x*10+ch- 0 ; n         ch=getchar(); n     } n     return x*f; n } n int T; n int d; n int prime[maxn]; n int tot=0; n int check[maxn]; n void Pre(int maxx) n { n  check[1]=1; n  for(int i=2;i<=maxx;i++) n  { n   if(check[i]==0) n   prime[++tot]=i; n   for(int j=1;j<=tot && prime[j]*i<=maxx;j++) n   { n    check[i*prime[j]]=1; n    if(i%prime[j]==0) n    break; n   } n  } n } n signed main() n {  n  Pre(500000); n  //for(int i=1;i<=20;i++) n  //cout<<prime[i]<<   ; n  T=read(); n  //cout<<tot<<endl; n  while(T--) n  { n   d=read(); n  /* if(d==2) n   { n    printf( 15 n ); n    continue; n   } n   else n   if(d==1) n   { n    printf( 6 n ); n    continue; n   }*/ n   int l=1,r=tot; n   while(l<r) n   { n    int mid=(l+r)>>1; n    if(prime[mid]-1>=d) n    r=mid; n    else n    l=mid+1; n   } n   int ans1=l; n   //cout<<ans1<<endl; n   l=ans1+1,r=tot; n   while(l<r) n   { n    int mid=(l+r)>>1; n    if(prime[mid]-prime[ans1]>=d) n    r=mid; n    else n    l=mid+1; n   } n   int ans2=l; n   printf( %lld n ,prime[ans1]*prime[ans2]); n  } n  return 0; n } n /* n 4 n 3 3 2 1 n 6 n 3 4 5 1 6 2 n  n 0 n */


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int idx[2 * n + 1], chk = 2 * n; n     vector<long long int> coin; n     coin.push_back(0LL); n     for (long long int i = 1; i <= 2 * n; i++) { n       long long int x; n       cin >> x; n       idx[x] = i; n     } n     for (long long int i = 2 * n; i >= 1; i--) { n       if (idx[i] <= chk) { n         coin.push_back(chk - idx[i] + 1); n         chk = idx[i] - 1; n       } n     } n     long long int cost = n, coin_no = coin.size() - 1; n     long long int dp[coin_no + 1][cost + 1]; n     memset(dp, 0, sizeof(dp)); n     for (long long int i = 0; i <= coin_no; i++) dp[i][0] = 1; n     for (long long int i = 1; i <= coin_no; i++) { n       for (long long int j = 1; j <= cost; j++) { n         dp[i][j] = dp[i - 1][j]; n         if (coin[i] <= j) dp[i][j] |= dp[i - 1][j - coin[i]]; n       } n     } n     if (dp[coin_no][cost]) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, p[10]; n map<long long, string> m; n set<long long> s[10]; n template <class T> n string toString(T x) { n   ostringstream os; n   os << x; n   return os.str(); n } n int getlen(long long x) { n   int ans = 0; n   while (x) { n     ans++; n     x /= 10; n   } n   return max(ans, 1); n } n string get(long long x) { n   if (m.count(x)) return m[x]; n   return toString(x); n } n void relaxPower(long long x, string str) { n   if (x > n || str.length() >= getlen(x)) return; n   if (m.count(x) && m[x].length() <= str.length()) return; n   s[m[x].length()].erase(x); n   m[x] = str; n   s[str.length()].insert(x); n } n void getPowers() { n   for (long long x = 2; x * x <= n; x++) { n     long long rs = x * x; n     int p = 2; n     while (rs <= n) { n       string tmp = toString(x) +  ^  + toString(p); n       relaxPower(rs, tmp); n       rs *= x; n       p++; n     } n   } n } n void getPowerAndPower(int len) { n   for (int i = 1; i <= len; i++) n     for (int j = i; i + j + 1 <= len; j++) n       for (auto x : s[i]) n         for (auto y : s[j]) relaxPower(x * y, get(x) +  *  + get(y)); n } n void getSimpleAndPower(int len) { n   for (int i = 1; i + 2 <= len; i++) { n     for (long long x = 1; x < p[len - i - 1]; x++) n       for (long long y : s[i]) relaxPower(x * y, toString(x) +  *  + get(y)); n   } n } n void precalc() { n   getPowers(); n   getPowerAndPower(7); n   getSimpleAndPower(7); n } n string ans; n void relaxAnswer(string str) { n   if (str.length() < ans.length()) ans = str; n } n void checkAnswer() { n   for (int i = 1; i * 2 + 1 < ans.length(); i++) { n     for (long long x = 1; x <= p[i]; x++) { n       relaxAnswer(get(n - x) +  +  + get(x)); n       if (n % x == 0) relaxAnswer(get(n / x) +  *  + get(x)); n     } n     for (auto x : s[i]) { n       relaxAnswer(get(n - x) +  +  + get(x)); n       if (n % x == 0) relaxAnswer(get(n / x) +  *  + get(x)); n     } n   } n } n int main() { n   p[0] = 1; n   for (int i = 1; i < 10; i++) p[i] = p[i - 1] * 10; n   cin >> n; n   precalc(); n   ans = get(n); n   checkAnswer(); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, v; n   cin >> n >> v; n   map<int, int> s; n   for (int i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     for (int j = 0; j < a; j++) { n       long long b; n       cin >> b; n       if (v > b) { n         s[i] = 1; n       } n     } n   } n   cout << s.size() << endl; n   for (map<int, int>::iterator it = s.begin(); it != s.end(); it++) n     cout << it->first <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n enum { AABB, ABAB, ABBA, AAAA }; n int n, k; n string s1, s2, s3, s4; n bool isvow(char c) { n   return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ); n } n string suf(string s) { n   static int sp = 0; n   auto i = s.end(), beg = s.begin(); n   int tk = 0; n   while (tk < k && i > beg) { n     --i; n     tk += isvow(*i); n   } n   string res(i, s.end()); n   if (tk < k) { n     res = string(sp++,    ); n   } n   return res; n } n void foo() { n   cout <<  NO n ; n   exit(0); n } n int main(void) { n   int i; n   cin >> n >> k; n   int ans = AAAA; n   for (i = 0; i < n; ++i) { n     cin >> s1 >> s2 >> s3 >> s4; n     auto suf1 = suf(s1), suf2 = suf(s2), suf3 = suf(s3), suf4 = suf(s4); n     if (suf1 == suf2 && suf2 == suf3 && suf3 == suf4) { n     } else if (suf1 == suf2 && suf3 == suf4) { n       if (ans == AAAA || ans == AABB) { n         ans = AABB; n       } else { n         foo(); n       } n     } else if (suf1 == suf3 && suf2 == suf4) { n       if (ans == AAAA || ans == ABAB) { n         ans = ABAB; n       } else { n         foo(); n       } n     } else if (suf1 == suf4 && suf2 == suf3) { n       if (ans == AAAA || ans == ABBA) { n         ans = ABBA; n       } else { n         foo(); n       } n     } else { n       foo(); n     } n   } n   switch (ans) { n     case AABB: n       cout <<  aabb n ; n       break; n     case ABAB: n       cout <<  abab n ; n       break; n     case ABBA: n       cout <<  abba n ; n       break; n     case AAAA: n       cout <<  aaaa n ; n       break; n     default: n       foo(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct outputer; n struct outputable {}; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n const int N = 200000; n struct Input { n   int n; n   int a[N]; n   vector<int> e[N]; n   bool read() { n     if (!(cin >> n)) { n       return 0; n     } n     for (int i = int(0); i < int(n); ++i) { n       scanf( %d , &a[i]); n       --a[i]; n     } n     for (int i = int(0); i < int(n - 1); ++i) { n       int x, y; n       scanf( %d%d , &x, &y); n       --x; n       --y; n       e[x].emplace_back(y); n       e[y].emplace_back(x); n     } n     return 1; n   } n   void init(const Input& input) { *this = input; } n }; n struct Data : Input { n   int ans; n   void write() { cout << ans << endl; } n }; n namespace Main { n struct Solution : Data { n   int mx; n   array<int, 2> calc(int v, int pr) { n     array<int, 2> res{0, 0}; n     if (a[v] != -1) { n       res[a[v]] = 1; n     } n     for (int to : e[v]) { n       if (to == pr) { n         continue; n       } n       array<int, 2> cur = calc(to, v); n       for (int t = int(0); t < int(2); ++t) { n         umx(mx, res[t] + cur[!t]); n       } n       for (int t = int(0); t < int(2); ++t) { n         umx(res[t], cur[t]); n         if (a[v] == t) { n           umx(res[t], cur[!t] + 1); n         } n       } n     } n     for (int t = int(0); t < int(2); ++t) { n       umx(mx, res[t]); n     } n     return res; n   } n   void solve() { n     mx = 0; n     calc(0, -1); n     ans = mx / 2 + 1; n   } n   void clear() { n     for (int i = int(0); i < int(n); ++i) { n       e[i] = vector<int>(); n     } n   } n }; n }  // namespace Main n Main::Solution sol; n int main() { n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   int t; n   cin >> t; n   for (int i = int(0); i < int(t); ++i) { n     sol.read(); n     sol.solve(); n     sol.write(); n     sol.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n vector<pair<int, long long> > forc[N]; n vector<pair<int, pair<int, long long> > > other[N]; n vector<pair<int, pair<int, long long> > > centroid[N]; n long long dist[N]; n int sub[N]; n bool alive[N]; n int belongs[N]; n vector<pair<int, int> > g[N]; n int perm[N], pos[N]; n int v_goes_to[N]; n vector<int> all; n void dfs(int v, int pr) { n   all.push_back(v); n   sub[v] = 1; n   int sz = g[v].size(); n   for (int j = 0; j < sz; j++) { n     int u = g[v][j].first; n     if (!alive[u] || u == pr) { n       continue; n     } n     int len = g[v][j].second; n     dist[u] = dist[v] + len; n     belongs[u] = (belongs[v] == -1 ? u : belongs[v]); n     dfs(u, v); n     sub[v] += sub[u]; n   } n } n void build(int v) { n   all.clear(); n   dfs(v, -1); n   { n     int old_v = v; n     int total = sub[v]; n     int pr = -1; n     while (true) { n       bool found = false; n       int sz = g[v].size(); n       for (int j = 0; j < sz; j++) { n         int u = g[v][j].first; n         if (!alive[u] || u == pr) { n           continue; n         } n         if (2 * sub[u] >= total) { n           pr = v; n           v = u; n           found = true; n           break; n         } n       } n       if (!found) { n         break; n       } n     } n     v_goes_to[old_v] = v; n   } n   all.clear(); n   dist[v] = 0; n   belongs[v] = -1; n   dfs(v, -1); n   int cnt = all.size(); n   for (int i = 0; i < cnt; i++) { n     int u = all[i]; n     centroid[u].push_back(make_pair(v, make_pair(belongs[u], dist[u]))); n   } n   for (int i = 0; i < cnt; i++) { n     int u = all[i]; n     forc[v].push_back(make_pair(pos[u], dist[u])); n   } n   sort(forc[v].begin(), forc[v].end()); n   for (int j = 1; j < cnt; j++) { n     forc[v][j].second += forc[v][j - 1].second; n   } n   for (int i = 1; i < cnt; i++) { n     int u = all[i]; n     other[v].push_back(make_pair(belongs[u], make_pair(pos[u], dist[u]))); n   } n   sort(other[v].begin(), other[v].end()); n   for (int j = 1; j < cnt - 1; j++) { n     other[v][j].second.second += other[v][j - 1].second.second; n   } n   vector<int> children; n   for (int i = 0; i < cnt; i++) { n     int u = all[i]; n     if (belongs[u] == u) { n       children.push_back(u); n     } n   } n   alive[v] = false; n   for (int i = 0; i < (int)children.size(); i++) { n     build(children[i]); n   } n } n int main() { n   int n, tt; n   scanf( %d %d , &n, &tt); n   for (int i = 0; i < n; i++) { n     scanf( %d , perm + i); n     perm[i]--; n     pos[perm[i]] = i; n   } n   for (int i = 0; i < n - 1; i++) { n     int x, y, z; n     scanf( %d %d %d , &x, &y, &z); n     x--; n     y--; n     g[x].push_back(make_pair(y, z)); n     g[y].push_back(make_pair(x, z)); n   } n   for (int i = 0; i < n; i++) { n     alive[i] = true; n   } n   build(0); n   int last = 0; n   while (tt--) { n     int com; n     scanf( %d , &com); n     if (com == 1) { n       int from, to, ver; n       scanf( %d %d %d , &from, &to, &ver); n       from ^= last; n       to ^= last; n       ver ^= last; n       from--; n       to--; n       ver--; n       long long ans = 0; n       for (pair<int, pair<int, long long> > p : centroid[ver]) { n         int v = p.first; n         int bel = p.second.first; n         long long d = p.second.second; n         long long sumd = 0; n         int cnt = 0; n         { n           int pf = lower_bound(forc[v].begin(), forc[v].end(), n                                make_pair(from, -1LL)) - n                    forc[v].begin(); n           int pt = lower_bound(forc[v].begin(), forc[v].end(), n                                make_pair(to + 1, -1LL)) - n                    forc[v].begin(); n           if (pf < pt) { n             sumd += forc[v][pt - 1].second; n             if (pf > 0) { n               sumd -= forc[v][pf - 1].second; n             } n             cnt += pt - pf; n           } n         } n         { n           int pf = lower_bound(other[v].begin(), other[v].end(), n                                make_pair(bel, make_pair(from, -1LL))) - n                    other[v].begin(); n           int pt = lower_bound(other[v].begin(), other[v].end(), n                                make_pair(bel, make_pair(to + 1, -1LL))) - n                    other[v].begin(); n           if (pf < pt) { n             sumd -= other[v][pt - 1].second.second; n             if (pf > 0) { n               sumd += other[v][pf - 1].second.second; n             } n             cnt -= pt - pf; n           } n         } n         ans += sumd + cnt * d; n       } n       printf( %lld n , ans); n       last = ans & ((1 << 30) - 1); n     } else { n       int x; n       scanf( %d , &x); n       x ^= last; n       x--; n       int v1 = perm[x]; n       int v2 = perm[x + 1]; n       int c1 = centroid[v1].size(); n       int c2 = centroid[v2].size(); n       int i1 = 0; n       int i2 = 0; n       while (i1 < c1 && i2 < c2 && n              centroid[v1][i1].first == centroid[v2][i2].first) { n         pair<int, pair<int, long long> > p = centroid[v1][i1]; n         int v = p.first; n         int bel1 = p.second.first; n         int bel2 = centroid[v2][i2].second.first; n         long long d2 = centroid[v2][i2].second.second; n         { n           int at = lower_bound(forc[v].begin(), forc[v].end(), n                                make_pair(pos[v1], -1LL)) - n                    forc[v].begin(); n           forc[v][at].second = (at == 0 ? 0LL : forc[v][at - 1].second) + d2; n         } n         { n           if (bel1 == bel2) { n             int at = lower_bound(other[v].begin(), other[v].end(), n                                  make_pair(bel1, make_pair(pos[v1], -1LL))) - n                      other[v].begin(); n             other[v][at].second.second = n                 (at == 0 ? 0LL : other[v][at - 1].second.second) + d2; n           } else { n             if (v != v1) { n               int at = lower_bound(other[v].begin(), other[v].end(), n                                    make_pair(bel1, make_pair(pos[v1], -1LL))) - n                        other[v].begin(); n               other[v][at].second.first = pos[v2]; n             } n             if (v != v2) { n               int at = lower_bound(other[v].begin(), other[v].end(), n                                    make_pair(bel2, make_pair(pos[v2], -1LL))) - n                        other[v].begin(); n               other[v][at].second.first = pos[v1]; n             } n           } n         } n         i1++; n         i2++; n       } n       for (int rot = 0; rot < 2; rot++) { n         while (i1 < c1) { n           pair<int, pair<int, long long> > p = centroid[v1][i1]; n           int v = p.first; n           int bel = p.second.first; n           { n             int at = lower_bound(forc[v].begin(), forc[v].end(), n                                  make_pair(pos[v1], -1LL)) - n                      forc[v].begin(); n             forc[v][at].first = pos[v2]; n           } n           if (v != v1) { n             int at = lower_bound(other[v].begin(), other[v].end(), n                                  make_pair(bel, make_pair(pos[v1], -1LL))) - n                      other[v].begin(); n             other[v][at].second.first = pos[v2]; n           } n           i1++; n         } n         swap(v1, v2); n         swap(c1, c2); n         swap(i1, i2); n       } n       swap(perm[x], perm[x + 1]); n       pos[perm[x]] = x; n       pos[perm[x + 1]] = x + 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, A, B, inf = 1e9, f[110][110], x, y, K, g[110][110], Cnt[110], n                 d[110][110]; n bool vis[110][110], V[110][110], Must[110][110]; n vector<int> E1[110], E2[110], E[110][110]; n queue<pair<int, int> > h; n void read(int &x) { n   char ch = getchar(); n   int mark = 1; n   for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar()) n     ; n   if (ch ==  - ) mark = -1, ch = getchar(); n   for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48; n   x *= mark; n } n int main() { n   read(n); n   read(m); n   read(A); n   read(B); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = inf; n   for (int i = 1; i <= m; i++) { n     read(x); n     read(y); n     f[x][y] = 1; n     E1[x].push_back(y); n     E2[y].push_back(x); n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]); n   read(K); n   for (int i = 1; i <= K; i++) { n     read(x); n     read(y); n     int dis = f[x][y]; n     if (dis == inf) continue; n     memset(Cnt, 0, sizeof(Cnt)); n     for (int j = 1; j <= n; j++) n       if (f[x][j] + f[j][y] == dis) { n         V[i][j] = 1; n         Cnt[f[x][j]]++; n         for (int k = 0; k < E1[j].size(); k++) { n           int v = E1[j][k]; n           if (f[x][j] + 1 + f[v][y] == dis) E[i][v].push_back(j), d[i][j]++; n         } n       } n     for (int j = 1; j <= n; j++) n       if (V[i][j] && Cnt[f[x][j]] == 1) Must[i][j] = 1; n   } n   for (int i = 1; i <= K; i++) n     if (V[i][B]) { n       vis[B][i] = 1, g[B][i] = 1; n       h.push(make_pair(B, i)); n     } n   for (int _ = 1; _ <= K; _++) { n     while (h.size()) { n       pair<int, int> now = h.front(); n       h.pop(); n       int u = now.first, x = now.second; n       for (int i = 0; i < E[x][u].size(); i++) { n         int v = E[x][u][i]; n         d[x][v]--; n         if (d[x][v] == 0 && !vis[v][x]) { n           vis[v][x] = 1; n           g[v][x] = g[u][x]; n           h.push(make_pair(v, x)); n         } n       } n     } n     for (int i = 1; i <= K; i++) n       if (vis[A][i] && Must[i][A]) { n         printf( %d n , _); n         return 0; n       } n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= K; j++) n         if (vis[i][j] && Must[j][i] && g[i][j] == _) { n           for (int k = 1; k <= K; k++) n             if (!vis[i][k] && V[k][i]) { n               g[i][k] = g[i][j] + 1; n               vis[i][k] = 1; n               h.push(make_pair(i, k)); n             } n         } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S; n signed long long mo = 1000000007; n int N; n string qs[100005]; n signed long long memo[20][100005]; n signed long long dig[20][100005]; n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> qs[0]; n   cin >> N; n   N++; n   qs[0] =  0->  + qs[0]; n   for (i = 1; i < N; i++) cin >> qs[i]; n   for (i = 0; i < 10; i++) memo[i][N] = i, dig[i][N] = 10; n   for (i = N - 1; i >= 0; i--) { n     for (j = 0; j < 10; j++) n       memo[j][i] = memo[j][i + 1], dig[j][i] = dig[j][i + 1]; n     int from = qs[i][0] -  0 ; n     memo[from][i] = 0; n     dig[from][i] = 1; n     for (j = qs[i].size() - 1; j >= 3; j--) { n       int nn = qs[i][j] -  0 ; n       memo[from][i] = (memo[from][i] + memo[nn][i + 1] * dig[from][i]) % mo; n       dig[from][i] = (dig[from][i] * dig[nn][i + 1]) % mo; n     } n   } n   cout << memo[0][0] << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false); n   for (i = 0; i < argc - 1; i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); 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); n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int N = 2e5 + 5; n int n, m, idx; n char s[N], ans[N]; n bool st[10]; n set<int> all; n bool check(int x) { n   if (x == 2) return 0; n   for (int i = 2; i <= x / i; ++i) n     if (x % i == 0) return 1; n   return 0; n } n void dfs(int i, int sum) { n   if (i > n) { n     all.insert(sum); n     return; n   } n   ans[++idx] = s[i]; n   dfs(i + 1, sum * 10 + s[i] -  0 ); n   --idx; n   dfs(i + 1, sum); n } n int main() { n   st[2] = st[3] = st[5] = st[7] = 1; n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> s + 1; n     int cnt[10] = {0}; n     idx = 0; n     for (int i = 1; i <= n; ++i) { n       if (!st[s[i] -  0 ]) { n         ans[++idx] = s[i]; n         goto out; n       } n     } n     for (int i = 1; i <= n; ++i) { n       cnt[s[i] -  0 ]++; n       if (cnt[s[i] -  0 ] > 1) { n         ans[++idx] = s[i]; n         ans[++idx] = s[i]; n         goto out; n       } n     } n     all.clear(); n     dfs(1, 0); n     for (int i : all) n       if (check(i)) { n         int k = i; n         while (k) { n           ans[++idx] = k % 10 +  0 ; n           k /= 10; n         } n         reverse(ans + 1, ans + 1 + idx); n         break; n       } n   out: n     printf( %d n , idx); n     for (int i = 1; i <= idx; ++i) printf( %c , ans[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[100001]; n long long start = 0; n long long h; n long long res; n long long c[100001]; n long long n; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> h; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a + 1, a + n + 1); n   res = a[n].first + a[n - 1].first - a[1].first - a[2].first; n   for (int i = n; i >= 2; --i) { n     long long curmax, curmin; n     if (i == n) n       curmax = n           max(a[n].first + a[n - 1].first + h, a[n - 1].first + a[n - 2].first); n     else n       curmax = n           max(a[n].first + a[n - 1].first, a[n].first + a[i - 1].first + h); n     if (i != 2) n       curmin = min(a[i].first + a[1].first + h, a[1].first + a[2].first); n     else n       curmin = min(a[2].first + a[1].first + h, a[2].first + a[3].first); n     long long curdif = curmax - curmin; n     if (curdif < res) { n       res = curdif; n       start = i - 1; n     } n   } n   for (int i = 1; i <= start; ++i) { n     c[a[i].second] = 1; n   } n   cout << res << endl; n   for (int i = 1; i <= n; ++i) { n     if (c[i]) n       cout << c[i] <<    ; n     else n       cout << 2 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n clock_t timeStart, timeFinish; n void timeBegin() { timeStart = clock(); } n void timeEnd() { timeFinish = clock(); } n void timeDuration() { n   double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); n   cout <<  Time taken by program is :   << fixed << time_taken n        << setprecision(5); n   cout <<   sec   << endl; n } n int blo[1000005]; n bool fun(const pair<pair<int, int>, int> &x, n          const pair<pair<int, int>, int> &y) { n   if (blo[x.first.first] != blo[y.first.first]) { n     return blo[x.first.first] < blo[y.first.first]; n   } n   if (blo[x.first.first] % 2) { n     return x.first.second > y.first.second; n   } else { n     return x.first.second < y.first.second; n   } n } n void solve() { n   int n; n   cin >> n; n   int x[n + 3], y[n + 3]; n   vector<pair<pair<int, int>, int>> v; n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n     v.push_back({{x[i], y[i]}, i}); n   } n   for (int i = 0; i <= 1000000; i++) { n     blo[i] = i / 1000; n   } n   sort(v.begin(), v.end(), fun); n   for (int i = 0; i < n; i++) cout << v[i].second <<    ; n   cout <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Dinic { n   static const int MXN = (int)2e6; n   struct Edge { n     int v, f, re; n   }; n   int n, s, t, level[MXN]; n   vector<Edge> E[MXN]; n   void init(int _n, int _s, int _t) { n     n = _n; n     s = _s; n     t = _t; n   } n   void add_edge(int u, int v, int f) { n     E[u].push_back({v, f, (int)E[v].size()}); n     E[v].push_back({u, 0, (int)E[u].size() - 1}); n   } n   bool BFS() { n     for (int i = 0; i < n; i++) level[i] = -1; n     queue<int> que; n     que.push(s); n     level[s] = 0; n     while (!que.empty()) { n       int u = que.front(); n       que.pop(); n       for (auto &it : E[u]) { n         if (it.f > 0 && level[it.v] == -1) { n           level[it.v] = level[u] + 1; n           que.push(it.v); n         } n       } n     } n     return level[t] != -1; n   } n   int DFS(int u, int nf) { n     if (u == t) return nf; n     int res = 0; n     for (auto &it : E[u]) { n       if (it.f > 0 && level[it.v] == level[u] + 1) { n         int tf = DFS(it.v, min(nf, it.f)); n         res += tf; n         nf -= tf; n         it.f -= tf; n         E[it.v][it.re].f += tf; n         if (nf == 0) return res; n       } n     } n     if (!res) level[u] = -1; n     return res; n   } n   int flow(int res = 0) { n     while (BFS()) res += DFS(s, 2147483647); n     return res; n   } n } flow; n struct Node { n   int l, r, y, v; n   bool operator<(const Node &rhs) const { return y < rhs.y; } n }; n int n, q, s, t, tot; n vector<Node> seg; n namespace segtree { n static const int N = (int)1e4 + 5; n static const int inf = (int)1e9; n int id[N * 4], id2[N * 4], tag[N * 4]; n void pull(int o, int lson, int rson) { n   id[o] = tot++; n   if (!tag[lson]) flow.add_edge(id[o], id[lson], inf); n   if (!tag[rson]) flow.add_edge(id[o], id[rson], inf); n } n void build1(int l, int r, int o = 0) { n   if (l + 1 == r) return void(id[o] = n + l); n   int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2; n   build1(l, mid, lson); n   build1(mid, r, rson); n   pull(o, lson, rson); n } n void build2(int l, int r, int o = 0) { n   if (l + 1 == r) return void(id2[o] = l); n   int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2; n   id2[o] = tot++; n   build2(l, mid, lson); n   build2(mid, r, rson); n   flow.add_edge(id2[lson], id2[o], inf); n   flow.add_edge(id2[rson], id2[o], inf); n } n void build(int l, int r) { n   build1(l, r); n   build2(l, r); n } n void modify1(int l, int r, int ql, int qr, int v, int o = 0) { n   if (ql <= l && r <= qr) return void(tag[o] += v); n   int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2; n   if (ql < mid) modify1(l, mid, ql, qr, v, lson); n   if (mid < qr) modify1(mid, r, ql, qr, v, rson); n   pull(o, lson, rson); n } n void modify2(int l, int r, int ql, int qr, int o = 0) { n   if (ql <= l && r <= qr) return void(flow.add_edge(id2[o], id[0], inf)); n   int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2; n   if (ql < mid) modify2(l, mid, ql, qr, lson); n   if (mid < qr) modify2(mid, r, ql, qr, rson); n } n void modify(int l, int r, int ql1, int qr1, int v, int ql2, int qr2) { n   if (!tag[0] && ql2 < qr2) modify2(l, r, ql2, qr2); n   modify1(l, r, ql1, qr1, v); n } n }  // namespace segtree n void init() { n   cin >> n >> q; n   for (int i = 0; i < q; ++i) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     seg.push_back({x1, x2 + 1, y1, 1}); n     seg.push_back({x1, x2 + 1, y2 + 1, -1}); n   } n   sort(seg.begin(), seg.end()); n } n void solve() { n   tot = 2 * n + 2, s = 0, t = 2 * n + 1; n   for (int i = 1; i <= n; ++i) { n     flow.add_edge(s, i, 1); n     flow.add_edge(i + n, t, 1); n   } n   segtree::build(1, n + 1); n   int preY = 1; n   for (auto [l, r, y, v] : seg) { n     segtree::modify(1, n + 1, l, r, v, preY, y); n     preY = y; n   } n   if (preY < n + 1) segtree::modify2(1, n + 1, preY, n + 1); n   flow.init(tot, s, t); n   cout << flow.flow() <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int n) { n   if (x == 0) return 0; n   if (n == 0) return 1; n   if (n == 1) return x; n   if (n % 2 == 0) { n     long long int temp = power(x, n / 2); n     return (temp % 1000000007 * temp % 1000000007) % 1000000007; n   } else { n     long long int temp = power(x, n / 2); n     return (x % 1000000007 * n             ((temp % 1000000007 * temp % 1000000007) % 1000000007) % n             1000000007) % n            1000000007; n   } n } n long long int findroot(long long int i, long long int Arr[]) { n   while (Arr[i] != i) { n     Arr[i] = Arr[Arr[i]]; n     i = Arr[i]; n   } n   return i; n } n void weightedunion(long long int i, long long int j, long long int arr[], n                    long long int size[]) { n   long long int x = findroot(i, arr); n   long long int y = findroot(j, arr); n   if (size[x] > size[y]) { n     size[x] += size[y]; n     arr[y] = arr[x]; n   } else { n     size[y] += size[x]; n     arr[x] = arr[y]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, k, i, j, temp, ans = 0; n   cin >> n >> k; n   long long int arr[n]; n   set<long long int> s; n   map<long long int, long long int> mp; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     mp[arr[i]]++; n   } n   if (k == 1) n     cout << mp.size() <<   n ; n   else { n     sort(arr, arr + n); n     for (i = 0; i < n; i++) { n       if (s.find(arr[i]) != s.end()) n         continue; n       else { n         s.insert(arr[i]); n         vector<long long int> inc; n         vector<long long int> ninc; n         inc.push_back(mp[arr[i]]); n         ninc.push_back(0); n         for (temp = k * arr[i]; temp <= (long long int)1e9; temp = temp * k) { n           s.insert(temp); n           if (mp.find(temp) == mp.end()) { n             inc.push_back(max(inc.back(), ninc.back())); n             ninc.push_back(inc.back()); n           } else { n             long long int x = inc.back(), y = ninc.back(); n             inc.push_back(mp[arr[i]] + ninc.back()); n             ninc.push_back(max(x, y)); n           } n         } n         ans += max(inc.back(), ninc.back()); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ara[1000005], n, m, dp[1005][1005]; n long long int func(long long int i, long long int j) { n   if ((j + ara[i]) % m == 0) { n     return 1; n   } n   if (i >= n - 1) { n     if ((j + ara[i]) % m == 0) { n       return 1; n     } else n       return 0; n   } n   if (dp[i][j] != -1) { n     return dp[i][j]; n   } n   long long int p1 = 0, p2 = 0; n   p1 = func(i + 1, (j + ara[i]) % m); n   p2 = func(i + 1, j); n   dp[i][j] = p1 | p2; n   return dp[i][j]; n } n int main() { n   long long int i, k, sum = 0; n   scanf( %I64d %I64d , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %I64d , &ara[i]); n   } n   if (n > m) { n     printf( YES n ); n     return 0; n   } n   memset(dp, -1, sizeof dp); n   k = func(0, 0); n   if (k == 1) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   std::cout.tie(0); n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   vector<long long> cnt(n + 1); n   for (long long(i) = 0; (i) < (n); (i)++) { n     cin >> a[i]; n     cnt[a[i]]++; n   } n   vector<long long> ans(n + 1); n   vector<long long> add; n   long long i = 0; n   for (; i < n + 1; i++) { n     ans[i] = cnt[i]; n     if (cnt[i] == 0) break; n     for (long long j = 0; j < cnt[i] - 1; j++) add.push_back(i); n   } n   i++; n   for (; i < n + 1; i++) { n     if (ans[i - 1] == -1) { n       ans[i] = -1; n       continue; n     } n     ans[i] = ans[i - 1] + cnt[i] - cnt[i - 1]; n     if (cnt[i - 1] == 0) { n       if (add.empty()) { n         ans[i] = -1; n         continue; n       } n       ans[i] += (i - add.back() - 1); n       add.pop_back(); n     } n     for (long long j = 0; j < cnt[i] - 1; j++) add.push_back(i); n   } n   for (auto x : ans) cout << x <<    ; n   cout <<   n ; n } n signed main() { n   fast(); n   long long t = 1; n   cin >> t; n   for (long long(i) = 0; (i) < (t); (i)++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1100; n int a[MAXN]; n int main() { n   int n, k; n   while (scanf( %d%d , &n, &k) != EOF) { n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     int res = 0; n     int sum = (1 << 29); n     for (int i = 0; i < n; i++) { n       int tmp = 0, flag = true; n       for (int j = 0; j < n; j++) n         if (a[j] != a[i] + (j - i) * k) { n           tmp++; n           if (a[i] + (j - i) * k <= 0) flag = false; n         } n       if (flag && tmp < sum) sum = tmp, res = i; n     } n     printf( %d n , sum); n     if (sum) { n       for (int i = 0; i < n; i++) { n         int tmp = a[res] + (i - res) * k; n         if (a[i] < tmp) n           printf( + %d %d n , i + 1, tmp - a[i]); n         else if (a[i] > tmp) n           printf( - %d %d n , i + 1, a[i] - tmp); n       } n     } n   } n   return 0; n } n