#include <bits/stdc++.h>   using namespace std;   const int inf = (1 << 30) - 1;   const int maxn = 100010;   int IN() {     int c, f, x;     while (!isdigit(c = getchar()) && c !=  - )       ;     c ==  -  ? (f = 1, x = 0) : (f = 0, x = c -  0 );     while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c -  0 ;     return !f ? x : -x;   }   int n, T;   char s[maxn];   bool vis[maxn], c[maxn];   bool check() {     int ans = 0;     for (int i = 0; i < 26; i++)       if (vis[i]) ans++;     return ans == 1;   }   int main() {     scanf( %d , &n);     int ans = 0;     fill(vis, vis + maxn, 1);     fill(c, c + maxn, 1);     for (int i = 1; i <= n; i++) {       char op[10];       scanf( %s%s , op, s);       char tp = op[0];       int len = strlen(s);       if (tp ==  . ) {         for (int i = 0; i < len; i++) vis[s[i] -  a ] = 0;       } else if (tp ==  ! ) {         if (check()) ans++;         int t[27];         memset(t, 0, sizeof(t));         for (int i = 0; i < len; i++) t[s[i] -  a ]++;         for (int i = 0; i < 26; i++)           if (!t[i]) vis[i] = 0;       } else if (tp ==  ? ) {         if (i != n && check()) ans++;         vis[s[0] -  a ] = 0;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     char name[55];     int score;     int c[50];   } comp[55];   int cmp1(node x, node y) {     if (x.score > y.score)       return 1;     else if (x.score == y.score) {       for (int i = 0; i < 50; i++)         if (x.c[i] > y.c[i])           return 1;         else if (x.c[i] < y.c[i])           return 0;     } else       return 0;   }   int cmp2(node x, node y) {     if (x.c[0] > y.c[0])       return 1;     else if (x.c[0] == y.c[0]) {       if (x.score > y.score)         return 1;       else if (x.score == y.score) {         for (int i = 1; i < 50; i++)           if (x.c[i] > y.c[i])             return 1;           else if (x.c[i] < y.c[i])             return 0;       } else         return 0;     } else       return 0;   }   int s[10] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1};   int main() {     int t;     while (scanf( %d , &t) != EOF) {       memset(comp, 0, sizeof(comp));       int sum = 0;       while (t--) {         int n, i;         char na[55];         scanf( %d , &n);         for (i = 0; i < n; i++) {           scanf( %s , na);           int j;           for (j = 0; j < sum; j++) {             if (strcmp(comp[j].name, na) == 0) {               if (i < 10) comp[j].score += s[i];               comp[j].c[i]++;               break;             }           }           if (j == sum) {             strcpy(comp[sum].name, na);             if (i < 10) comp[sum].score += s[i];             comp[sum].c[i]++;             sum++;           }         }       }       sort(comp, comp + sum, cmp1);       printf( %s n , comp[0].name);       sort(comp, comp + sum, cmp2);       printf( %s n , comp[0].name);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long k;     cin >> k;     if (k <= 36) {       for (long long i = 1; i <= (k / 2); i++) {         cout << 8;       }       if (k % 2 == 1) {         cout << 4;       }     } else {       cout << -1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int getT(vector<vector<long long int>>&, int, int);   void _dp(vector<vector<long long int>>& dp, int n) {     for (int j = 0; j < n; j++) dp[0][j] = 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         for (int k = 0; k < i; k++)           dp[i][j] += dp[k][j - 1] * dp[(i - 1) - k][j - 1];   }   int main() {     int n, h;     scanf( %d %d , &n, &h);     vector<vector<long long int>> dp(n + 1, vector<long long int>(n + 1, 0));     _dp(dp, n);     cout << dp[n][n] - dp[n][h - 1] << endl;     return 0;   }


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


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int maxn = (int)1e4 + 10;   long long a[maxn], v[maxn], b[maxn];   long long gcd(long long A, long long B) {     A = abs(A);     B = abs(B);     while (B != 0) {       A %= B;       swap(A, B);     }     return A;   }   bool gcdEx(long long A, long long &X, long long B, long long &Y, long long C) {     long long lastX = 0;     long long lastY = 1;     X = 1;     Y = 0;     while (B != 0) {       long long sub = A / B;       X -= lastX * sub;       Y -= lastY * sub;       A -= B * sub;       swap(A, B);       swap(X, lastX);       swap(Y, lastY);     }     if (C % A != 0) return false;     X *= C / A;     Y *= C / A;     return true;   }   int main() {     long long n, m;     int k;     cin >> n >> m >> k;     long long lcm = 1;     for (int i = 0; i < k; i++) {       cin >> a[i];       b[i] = a[i];       long long g = gcd(lcm, a[i]);       if (a[i] / g > n / lcm) {         puts( NO );         return 0;       }       lcm = a[i] / g * lcm;     }     for (int i = 0; i < k; i++) {       v[i] = (-i % a[i] + a[i]) % a[i];     }     for (int i = 0; i < k - 1; i++) {       long long A = a[i];       long long B = -a[i + 1];       long long C = v[i + 1] - v[i];       long long G = gcd(A, B);       long long x, y;       if (!gcdEx(A, x, B, y, C)) {         puts( NO );         return 0;       }       assert(A * x + B * y == C);       long long X = (long long)1e18;       if (x < 0) {         long long add = (-x - B / G - 1) / (-B / G);         x += add * (-B / G);         y -= add * (-A / G);       }       long long sub = x / (-B / G);       x -= sub * (-B / G);       y += sub * (-A / G);       assert(x >= 0);       assert(B < 0 && -B == a[i + 1]);       assert(A * x + B * y == C);       assert(A * x >= 0);       assert(C < a[i + 1]);       if (y >= 0) {         X = min(X, a[i] * x + v[i]);       }       if (y < 0) {         long long add = (-y + A / G - 1) / (A / G);         y += add * (A / G);         x -= add * (B / G);       }       if (y >= 0) {         long long sub = y / (A / G);         y -= sub * (A / G);         x += sub * (B / G);       }       if (x >= 0) {         X = min(X, a[i + 1] * y + v[i + 1]);       }       if (X == (long long)1e18) {         puts( NO );         return 0;       }       if (X > m - k + 1) {         puts( NO );         return 0;       }       assert(X % a[i + 1] == v[i + 1]);       a[i + 1] = a[i] / gcd(a[i], a[i + 1]) * a[i + 1];       v[i + 1] = X;     }     long long X = v[k - 1];     if (X == 0) X = a[k - 1];     if (X > m - k + 1) {       puts( NO );       return 0;     }     for (int i = 0; i < k; i++) {       if (gcd(lcm, X + i) != b[i]) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   int Ans[MAXN * 2], L, R, n;   struct XY {     int sz = 0, totid = 0, Ret, ll, rr;     int las[MAXN * 10], top[MAXN], en[MAXN], pos[MAXN], siz[MAXN], dep[MAXN],         fa[MAXN], deleted[MAXN * 3];     struct Edge {       int to, pre;     } e[MAXN * 35];     void add(int a, int b) {       e[++sz].to = b;       e[sz].pre = las[a];       las[a] = sz;     }     void dfs1(int u, int father, int depth) {       dep[u] = depth;       fa[u] = father;       siz[u] = 1;       for (int i = las[u]; i; i = e[i].pre) {         dfs1(e[i].to, u, depth + 1);         siz[u] += siz[e[i].to];       }     }     void dfs2(int u, int tp) {       pos[u] = ++totid;       top[u] = tp;       int v = 0, maxisz = 0;       for (int i = las[u]; i; i = e[i].pre)         if (siz[e[i].to] > maxisz) v = e[i].to, maxisz = siz[e[i].to];       if (v) dfs2(v, tp);       for (int i = las[u]; i; i = e[i].pre)         if (e[i].to != v) dfs2(e[i].to, e[i].to);     }     void link(int rt, int l, int r) {       if (ll <= l && r <= rr) {         add(rt, Ret);         return;       }       int mid = (l + r) >> 1;       if (ll <= mid) link(rt << 1, l, mid);       if (rr > mid) link(rt << 1 | 1, mid + 1, r);     }     void insert(int x, int y, int id) {       while (top[x] != top[y]) {         if (dep[top[x]] < dep[top[y]]) swap(x, y);         ll = pos[top[x]];         rr = pos[x];         Ret = id;         link(1, 1, n);         x = fa[top[x]];       }       if (dep[x] < dep[y]) swap(x, y);       ll = pos[y] + 1;       rr = pos[x];       Ret = id;       if (ll <= rr) link(1, 1, n);     }     void query(int rt, int l, int r) {       for (int i = las[rt]; i; i = e[i].pre)         if (!deleted[e[i].to]) Ans[++R] = e[i].to, deleted[e[i].to] = 1;       las[rt] = 0;       int mid = (l + r) >> 1;       if (l == r) return;       if (ll <= mid)         query(rt << 1, l, mid);       else         query(rt << 1 | 1, mid + 1, r);     }     void doit(int t) {       ll = pos[t + 1];       query(1, 1, n);     }     void work(int l, int r) {       sort(Ans + l, Ans + r + 1);       for (int i = l; i <= r; ++i) printf( %d  , Ans[i]);       puts(  );       for (int i = l; i <= r; ++i) doit(Ans[i]);       L = r;     }     void read() {       for (int i = 2; i <= n; ++i) scanf( %d , &fa[i]), add(fa[i], i);       dfs1(1, -1, 1);       dfs2(1, 1);       for (int i = 0; i <= n + 9; ++i) las[i] = 0;       sz = 0;     }   } Tree[2];   int main() {     scanf( %d , &n);     Tree[0].read();     Tree[1].read();     for (int i = 2; i <= n; ++i) Tree[1].insert(Tree[0].fa[i], i, i - 1);     for (int i = 2; i <= n; ++i) Tree[0].insert(Tree[1].fa[i], i, i - 1);     scanf( %d , &Ans[++R]);     Tree[1].deleted[Ans[R]] = 1;     while (L < R) {       if (L < R) puts( Blue ), Tree[0].work(L + 1, R);       if (L < R) puts( Red ), Tree[1].work(L + 1, R);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[200007], q;   int l[200007], r[200007], prime[1000007], id[1000007];   long long bit[200007], ans[200007], inv[1000007], prd[200007], divide[200007],       cal[1000007];   vector<pair<int, int> > query[200007];   vector<int> dv[200007];   const long long mod = 1e9 + 7;   long long pw(long long a, long long b) {     if (b == 1) return a;     long long ret = pw(a, b / 2);     if (b % 2 == 0) return ret * ret % mod;     return ret * ret % mod * a % mod;   }   void upd(int pos, long long val) {     for (int i = pos; i <= n; i += i & -i) bit[i] = bit[i] * val % mod;   }   long long get(int pos) {     long long res = 1;     for (int i = pos; i >= 1; i -= i & -i) {       res = (res * bit[i]) % mod;     }     return res;   }   long long get(int l, int r) { return (get(r) * pw(get(l - 1), mod - 2)) % mod; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     for (int i = 2; i <= 1000000; i++) {       if (prime[i]) continue;       for (int j = i; j <= 1000000; j += i) {         if (!prime[j]) prime[j] = i;       }     }     inv[0] = 1;     for (int i = 1; i <= 1000000; i++) {       inv[i] = pw(i, mod - 2);       if (prime[i] == i) cal[i] = 1LL * (i - 1) * inv[i] % mod;     }     prd[0] = 1, divide[0] = 1;     for (int i = 1; i <= n; i++)       prd[i] = (prd[i - 1] * 1LL * a[i]) % mod,       divide[i] = (divide[i - 1] * inv[a[i]]) % mod;     for (int i = 1; i <= n; i++) {       bit[i] = 1;       int cur = a[i], prv = 1;       while (cur > 1) {         if (prime[cur] != prv) {           prv = prime[cur];           dv[i].push_back(prv);         }         cur /= prv;       }     }     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       scanf( %d %d , &l[i], &r[i]);       ans[i] = (prd[r[i]] * divide[l[i] - 1]) % mod;       query[r[i]].push_back(make_pair(l[i], i));     }     for (int i = 1; i <= n; i++) {       for (int j : dv[i]) {         int pos = id[j];         if (pos) upd(pos, 1LL * j * inv[j - 1] % mod);         upd(i, cal[j]);         id[j] = i;       }       for (int j = 0; j < (int)query[i].size(); j++) {         int L = query[i][j].first;         int idx = query[i][j].second;         ans[idx] = (ans[idx] * get(L, i)) % mod;       }     }     for (int i = 1; i <= q; i++) printf( %lld n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 5e5 + 3;   const long long MOD = 998244353;   int board[4][52];   int n;   vector<pair<int, pair<int, int> > > moves;   bool flag;   void make_move(int a, int b, int x, int y) {     if (board[a][b] == 0 && board[x][y] == 0) return;     moves.push_back(         pair<int, pair<int, int> >(board[a][b], pair<int, int>(x, y)));     board[x][y] = board[a][b];     board[a][b] = 0;     flag = true;   }   void enter() {     for (int i = 0; i < n; i++) {       if (board[0][i] == board[1][i]) {         make_move(1, i, 0, i);       }       if (board[3][i] == board[2][i]) {         make_move(2, i, 3, i);       }     }   }   const int dx[] = {0, 1, 0, -1};   const int dy[] = {-1, 0, 1, 0};   void print() {     for (int i = 0; i < 4; i++) {       for (int j = 0; j < n; j++) {         printf( %2d  , board[i][j]);       }       puts(  );     }     puts(  );   }   void move() {     int f = -1, c = -1;     for (int i = 0; i < n; i++) {       if (board[1][i] == 0) {         f = 1;         c = i;         break;       }       if (board[2][i] == 0) {         f = 2;         c = i;         break;       }     }     if (f == -1) return;     int d;     if (f == 1)       d = 0;     else       d = 2;     for (int t = 0; t < 2 * n - 1; t++) {       int nf = f + dx[d];       int nc = c + dy[d];       if (nf >= 1 && nf <= 2 && nc >= 0 && nc < n) {         make_move(nf, nc, f, c);         f = nf;         c = nc;       } else {         t--;         d = (d + 1) % 4;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int k;     cin >> n >> k;     for (int i = 0; i < 4; i++) {       for (int j = 0; j < n; j++) {         cin >> board[i][j];       }     }     flag = true;     while (flag) {       flag = false;       enter();       move();     }     if (moves.size()) {       cout << moves.size() <<   n ;       for (auto x : moves) {         cout << x.first <<     << x.second.first + 1 <<     << x.second.second + 1              <<   n ;       }     } else {       cout << -1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;      namespace NTT   {       const int P=998244353,g=3;       const int W=22,S=1<<W;       const int J=86583718;          inline int add(int a,int b) {int r=a+b; return r<P?r:r-P;}       inline int sub(int a,int b) {int r=a-b; return r<0?r+P:r;}       inline int mul(long long a,long long b) {return (a*b)%P;}       inline int inv(int a) {return a==1?a:mul(inv(P%a),P-P/a);}       inline int qpow(int a,long long k)       {           int r=1;           while (k)           {               if (k&1) r=mul(r,a);               k>>=1; a=mul(a,a);           }           return r;       }          int r[S],w[2][S];       void init(int lim)       {           int w0=qpow(g,(P-1)/lim);           w[0][0]=w[1][0]=1;           for (int i=1;i<lim;i++) w[0][i]=w[1][lim-i]=mul(w[0][i-1],w0);           for (int i=0;i<lim;i++) r[i]=(r[i>>1]>>1)|((i&1)*(lim>>1));       }          void ntt(int *a,int lim,int o)       {           for (int i=0;i<lim;i++) if (i<r[i]) swap(a[i],a[r[i]]);           for (int i=1;i<lim;i<<=1)           {               for (int j=0,t=lim/(i<<1);j<lim;j+=i<<1)               {                   for (int k=j,l=0;k<j+i;k++,l+=t)                   {                       int x=a[k],y=mul(w[o][l],a[k+i]);                       a[k]=add(x,y);                       a[k+i]=sub(x,y);                   }               }           }           if (o)           {               int tmp=NTT::inv(lim);               for (int i=0;i<lim;i++) a[i]=mul(a[i],tmp);           }       }          vector<int> poly_add(const vector<int> &a,const vector<int> &b)       {           int n=a.size(),m=b.size();           vector<int> c;           for (int i=0;i<max(n,m);i++) c.push_back(add((i<n?a[i]:0),(i<m?b[i]:0)));           return c;       }          vector<int> poly_sub(const vector<int> &a,const vector<int> &b)       {           int n=a.size(),m=b.size();           vector<int> c;           for (int i=0;i<max(n,m);i++) c.push_back(sub((i<n?a[i]:0),(i<m?b[i]:0)));           return c;       }          vector<int> poly_d(const vector<int> &a)       {           int n=a.size();           vector<int> b;           for (int i=1;i<n;i++) b.push_back(mul(a[i],i));           return b;       }          vector<int> poly_s(const vector<int> &a)       {           int n=a.size();           vector<int> b{0};           for (int i=0;i<n;i++) b.push_back(mul(a[i],inv(i+1)));           return b;       }          int p1[S],p2[S];       vector<int> poly_mul(const vector<int> &a,const vector<int> &b)       {           int n=a.size(),m=b.size();           int lim=1;           while (lim<(n<<1)) lim<<=1;           while (lim<(m<<1)) lim<<=1;           init(lim);           for (int i=0;i<lim;i++) r[i]=(i&1)*(lim>>1)+(r[i>>1]>>1);           copy_n(a.begin(),n,p1); fill(p1+n,p1+lim,0);           copy_n(b.begin(),m,p2); fill(p2+m,p2+lim,0);           ntt(p1,lim,0);           ntt(p2,lim,0);           for (int i=0;i<lim;i++) p1[i]=mul(p1[i],p2[i]);           ntt(p1,lim,1);           return vector<int>(p1,p1+n+m-1);       }          vector<int> poly_inv(const vector<int> &a)       {           int n=a.size();           if (n==1) return {inv(a[0])};           auto b=a; b.resize((n+1)>>1);           b=poly_inv(b);           int m=b.size();           int lim=1;           while (lim<(n<<1)) lim<<=1;           while (lim<(m<<1)) lim<<=1;           init(lim);           copy_n(a.begin(),n,p1); fill(p1+n,p1+lim,0);           copy_n(b.begin(),m,p2); fill(p2+m,p2+lim,0);           ntt(p1,lim,1);           ntt(p2,lim,1);           for (int i=0;i<lim;i++) p1[i]=mul(p2[i],sub(2,mul(p1[i],p2[i])));           ntt(p1,lim,-1);           return vector<int>(p1,p1+n);       }          vector<int> poly_div(const vector<int> &a,const vector<int> &b)       {           int n=a.size(),m=b.size();           if (m>n) return {0};           auto ar=a,br=b;           reverse(ar.begin(),ar.end()); reverse(br.begin(),br.end());           ar.resize(n-m+1); br.resize(n-m+1);           br=poly_inv(br);           auto q=poly_mul(ar,br); q.resize(n-m+1);           reverse(q.begin(),q.end());           return q;       }          vector<int> poly_mod(const vector<int> &a,const vector<int> &b)       {           int m=b.size();           auto c=poly_div(a,b);           c=poly_mul(b,c);           c=poly_sub(a,c); c.resize(m-1);           return c;       }          vector<int> poly_sqrt(const vector<int> &a)       {           int n=a.size();           if (n==1) return {1};           auto b=a; b.resize((n+1)>>1);           b=poly_sqrt(b); b.resize(n);           auto c=poly_add(b,poly_mul(poly_inv(b),a)); c.resize(n);           int i2=inv(2); for (int &i:c) i=mul(i,i2);           return c;       }          vector<int> poly_ln(const vector<int> &a)       {           int n=a.size();           auto b=poly_mul(poly_inv(a),poly_d(a));           b.resize(n-1); b=poly_s(b);           return b;       }          vector<int> poly_exp(const vector<int> &a)       {           int n=a.size();           if (n==1) return {1};           auto b=a; b.resize((n+1)>>1);           b=poly_exp(b); b.resize(n);           auto c=poly_sub(a,poly_ln(b)); c[0]=add(c[0],1);           c=poly_mul(b,c); c.resize(n);           return c;       }          vector<int> poly_sin(const vector<int> &a)       {           auto b=a;           for (int &i:b) i=mul(i,J);           auto c=poly_exp(b);           c=poly_sub(c,poly_inv(c));           int i2=inv(2),ij=inv(J);           for (int &i:c) i=mul(i,mul(i2,ij));           return c;       }          vector<int> poly_cos(const vector<int> &a)       {           auto b=a;           for (int &i:b) i=mul(i,J);           auto c=poly_exp(b);           c=poly_add(c,poly_inv(c));           int i2=inv(2);           for (int &i:c) i=mul(i,i2);           return c;       }          vector<int> poly_asin(const vector<int> &a)       {           int n=a.size();           auto b=poly_d(a),c=poly_mul(a,a); c.resize(n);           c=poly_inv(poly_sqrt(poly_sub({1},c)));           b=poly_mul(b,c); b.resize(n-1);           return poly_s(b);       }          vector<int> poly_atan(const vector<int> &a)       {           int n=a.size();           auto b=poly_d(a),c=poly_mul(a,a); c.resize(n);           c=poly_inv(poly_add({1},c));           b=poly_mul(b,c); b.resize(n-1);           return poly_s(b);       }   }      const int N=200010;      int n,a,b;   long long inv[N];   long long fac[N],invf[N];      void linear_inv(long long n,long long p)   {       inv[1]=1;       for (long long i=2;i<=n;i++) inv[i]=(p-p/i)*inv[p%i]%p;       fac[0]=1;       for (long long i=1;i<=n;i++) fac[i]=fac[i-1]*i%p;       invf[n]=NTT::inv(fac[n]);       for (long long i=n-1;i>=0;i--) invf[i]=invf[i+1]*(i+1)%p;   }      long long C(long long n,long long m)   {       if (m>n) return 0;       return NTT::mul(fac[n],NTT::mul(invf[m],invf[n-m]));   }      int main()   {       linear_inv(200000,NTT::P);       scanf( %d ,&n);       vector<int> ans={1};       int l=1;       for (int i=1;i<=n;i++)       {           scanf( %d%d ,&a,&b);           vector<int> f;           int sz=min(l+l+a-b-1,a+b+1);           for (int j=(a+b+1-sz)/2;j<=a+b-(a+b+1-sz)/2;j++)           {               f.push_back(C(a+b,j));           }           ans=NTT::poly_mul(ans,f);           l+=a-b;           int now=ans.size();           ans.erase(ans.begin(),ans.begin()+(now-l)/2);           ans.erase(ans.end()-(now-l)/2,ans.end());       }       int sum=0;       for (int i:ans)       {           sum=NTT::add(sum,i);       }       printf( %d ,sum);       getchar(); getchar();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   const int N = 100005;   LL a[N];   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       LL sum = 0;       LL x;       scanf( %d%lld , &n, &x);       LL q = n;       for (int i = 1; i <= n; ++i) {         scanf( %lld , &a[i]);         sum += a[i];       }       sort(a + 1, a + n + 1);       bool flag = true;       for (int i = 1; i <= n; ++i) {         if (sum * 1.0 / q * 1.0 >= x * 1.0) {           printf( %lld n , q);           flag = false;           break;         }         sum -= a[i], --q;       }       if (flag) printf( %lld n , q);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num_q;   int flag;   long long u, v, w;   map<long long, long long> hs;   int main() {     scanf( %d , &num_q);     while (num_q--) {       scanf( %d%lld%lld , &flag, &u, &v);       if (flag == 1) {         scanf( %lld , &w);         while (u != v) {           if (u > v) {             hs[u] += w;             u /= 2;           } else {             hs[v] += w;             v /= 2;           }         }       } else {         long long ans = 0;         while (u != v) {           if (u > v) {             ans += hs[u];             u /= 2;           } else {             ans += hs[v];             v /= 2;           }         }         printf( %lld n , ans);       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct EDGE {     int adj, next;   } edge[810000];   int n, m, q, top, gh[510000];   int size[510000], dfn[510000], low[510000], cnt, ind, stop, instack[510000],       stck[510000], belong[510000];   void addedge(int x, int y) {     edge[++top].adj = y;     edge[top].next = gh[x];     gh[x] = top;   }   void tarjan(int x, int pre) {     dfn[x] = low[x] = ++ind;     instack[x] = 1;     stck[++stop] = x;     for (int p = gh[x]; p; p = edge[p].next) {       if (edge[p].adj == pre) continue;       if (!dfn[edge[p].adj]) {         tarjan(edge[p].adj, x);         low[x] = min(low[x], low[edge[p].adj]);       } else if (instack[edge[p].adj]) {         low[x] = min(low[x], dfn[edge[p].adj]);       }     }     if (dfn[x] == low[x]) {       ++cnt;       int tmp = 0;       size[cnt] = 0;       while (tmp != x) {         size[cnt]++;         tmp = stck[stop--];         belong[tmp] = cnt;         instack[tmp] = 0;       }     }   }   int far[510000];   long long mul[510000];   void init() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) gh[i] = 0;     top = cnt = ind = stop = 0;     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       addedge(x, y);       addedge(y, x);     }     for (int i = 1; i <= n; i++) dfn[i] = 0;     for (int i = 1; i <= n; i++)       if (dfn[i] == 0) tarjan(i, 0);   }   int v[510000];   void prepare() {     for (int i = 1; i <= n; i++) v[i] = 0;     int tail = 1;     v[belong[1]]++;     for (int i = 1; i <= n; i++) {       while (tail < n && (v[belong[tail + 1]] + 1 < size[belong[tail + 1]] ||                           size[belong[tail + 1]] == 1)) {         v[belong[++tail]]++;       }       far[i] = tail;       mul[i] = tail - i + 1;       mul[i] += mul[i - 1];       v[belong[i]]--;     }   }   int main() {     init();     prepare();     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       int l, r;       scanf( %d%d , &l, &r);       int left = l;       int right = r;       if (far[l] > r)         left = left - 1;       else         while (left < right) {           int mid = (left + right) / 2 + 1;           if (far[mid] <= r)             left = mid;           else             right = mid - 1;         }       long long ans = mul[left] - mul[l - 1];       ans += (long long)(r - left + 1) * (r - left) / 2;       printf( %I64d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int squ(vector<long long> &v) {     for (int i = 0; i + 3 < v.size(); i++) {       if (v[i] == v[i + 1] && v[i] == v[i + 2] && v[i] == v[i + 3]) return v[i];     }     return -1;   }   vector<long long> pa(vector<long long> &v) {     vector<long long> q;     for (int i = 0; i + 1 < v.size(); i++) {       if (i == 0 || v[i - 1] != v[i]) {         if (v[i] == v[i + 1]) q.push_back(v[i]);       }     }     return q;   }   bool checkv(pair<long long, long long> a, pair<long long, long long> b) {     if ((a.first + a.second) * (a.first + a.second) * b.first * b.second >         (b.first + b.second) * (b.first + b.second) * a.first * a.second)       return true;     return false;   }   int main() {     ios_base::sync_with_stdio(0);     int T;     cin >> T;     while (T--) {       long long n;       cin >> n;       vector<long long> v;       for (int i = 0; i < n; i++) {         long long a;         cin >> a;         v.push_back(a);       }       sort(v.begin(), v.end());       long long res = squ(v);       if (res > 0) {         for (int i = 0; i < 4; i++) cout << res <<    ;         cout <<   n ;         continue;       }       vector<long long> u = pa(v);       pair<long long, long long> cur = {u[0], u[1]};       for (int i = 0; i + 1 < u.size(); i++) {         if (checkv(cur, {u[i], u[i + 1]})) {           cur = {u[i], u[i + 1]};         }       }       cout << cur.first <<     << cur.first <<     << cur.second <<                << cur.second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MN = 100011;   const int inf = 1e9 + 7;   int n, m;   char c[1000][1000];   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = (1), _b = (n); i <= _b; ++i)       for (int j = (1), _b = (m); j <= _b; ++j) cin >> c[i][j];     long long res = 0, ok = 0;     for (int i = (1), _b = (m); i <= _b; ++i) {       if (c[n][i] ==  B ) {         if (!ok) res++;         ok = 1;       } else         ok = 0;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int x[N], y[N];   vector<int> xx;   vector<int> yy;   int n;   bool sameline(int tar, int p1, int p2) {     return 1LL * (y[tar] - yy[p1]) * (x[tar] - xx[p2]) ==            1LL * (y[tar] - yy[p2]) * (x[tar] - xx[p1]);   }   bool check(int p1, int p2, int p3, int p4) {     for (int i = 1; i <= n; i++) {       if (!sameline(i, p1, p2) && !sameline(i, p3, p4)) return false;     }     return true;   }   int main() {     int flag;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]);     if (n <= 4) {       printf( YES n );       return 0;     }     xx.push_back(x[1]);     yy.push_back(y[1]);     xx.push_back(x[2]);     yy.push_back(y[2]);     for (int i = 3; i <= n; i++) {       flag = 1;       for (int j = 0; j < xx.size() - 1; j++) {         for (int k = j + 1; k < xx.size(); k++) {           if (1LL * (y[i] - yy[j]) * (x[i] - xx[k]) ==               1LL * (y[i] - yy[k]) * (x[i] - xx[j])) {             flag = 0;             break;           }         }       }       if (flag) {         xx.push_back(x[i]);         yy.push_back(y[i]);       }       if (xx.size() == 4) break;     }     if (xx.size() == 2)       printf( YES n );     else if (xx.size() == 3) {       if (check(0, 1, 1, 2) || check(0, 1, 0, 2) || check(0, 2, 1, 2))         printf( YES n );       else         printf( NO n );     } else {       if (check(0, 1, 2, 3) || check(0, 2, 1, 3) || check(0, 3, 1, 2))         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<int> a(100, 0);     int b;     for (int i = 0; i < n; i++) {       cin >> b;       b--;       a[b]++;     }     sort(a.begin(), a.end());     int s = 0;     s = a[99] / k;     if (a[99] % k > 0) {       s++;     }     int l = 0;     for (int i = 99; i >= 0; i--) {       if (a[i] > 0) {         l++;       }     }     cout << s * k * l - n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[65];   long long level(long long x) {     int sum = 0;     for (; x != 1; x /= 2) {       sum++;     }     return sum;   }   void write(long long a) {     if (a == 0)       putchar( 0 );     else {       static char c[20];       static int c0;       c0 = 0;       while (a) c[c0++] = a % 10 +  0 , a /= 10;       while (c0--) putchar(c[c0]);     }   }   void get_val(long long x, int l) {     write((x - a[l]) % (1ll << l) + (1ll << l));     putchar(   );     if (x == 1) return;     get_val(x / 2, l - 1);   }   int main() {     ios::sync_with_stdio(false);     int q;     cin >> q;     int i;     for (i = 0; i < q; i++) {       int t;       cin >> t;       if (t == 1) {         long long x, k;         cin >> x >> k;         x = level(x);         a[x] += k;         a[x] %= (1ll << x);         a[x] += (1ll << x);         a[x] %= (1ll << x);       } else if (t == 2) {         long long x, k;         cin >> x >> k;         x = level(x);         k %= (1ll << x);         for (; x <= 60; x++) {           a[x] += k;           a[x] %= (1ll << x);           a[x] += (1ll << x);           a[x] %= (1ll << x);           k <<= 1;         }       } else {         long long x;         cin >> x;         int k = level(x);         x += a[k];         x %= (1ll << k);         x += (1ll << k);         get_val(x, level(x));         putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T fact(T n) {     return (n == 1 || n == 0) ? 1 : n * fact(n - 1);   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   int subarraysum(int arr[], int n, int sum) {     int curr_sum = arr[0], start = 0, i;     for (i = 1; i <= n; i++) {       while (curr_sum > sum && start < i - 1) {         curr_sum = curr_sum - arr[start];         start++;       }       if (curr_sum == sum) {         if (start != i - 1) return 1;       }       if (i < n) curr_sum = curr_sum + arr[i];     }     return 0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       for (int i = 0; i < n; i++) cin >> a[i];       int c = 0;       for (int i = 0; i < n; i++) {         int x = a[i];         if (subarraysum(a, n, x)) c++;       }       cout << c << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 132674 + 10;   int n, len[2];   int R[2][2 * N];   vector<pair<int, int> > open[2 * N], clos[2 * N];   struct Cord {     int x1, asdioayseiowqe12, x2, y2;     Cord() {}     Cord(int x1, int asdioayseiowqe12, int x2, int y2)         : x1(x1), asdioayseiowqe12(asdioayseiowqe12), x2(x2), y2(y2) {}   } C[N];   struct SegmentTree {     int st[4 * 2 * N], lp[4 * 2 * N];     void Propagate(int k) {       if (lp[k] == 0) return;       int v = lp[k];       lp[k] = 0;       st[k << 1] += v;       st[k << 1 | 1] += v;       lp[k << 1] += v;       lp[k << 1 | 1] += v;     }     void Update(int k, int l, int r, int i, int j, int v) {       if (l > j || r < i) return;       if (i <= l && r <= j) {         st[k] += v;         lp[k] += v;         return;       }       Propagate(k);       Update(k << 1, l, ((l + r) >> 1), i, j, v);       Update(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j, v);       st[k] = max(st[k << 1], st[k << 1 | 1]);     }     int Find(int k, int l, int r) {       if (l == r) return l;       Propagate(k);       if (st[k << 1] >= n - 1)         return Find(k << 1, l, ((l + r) >> 1));       else         return Find(k << 1 | 1, ((l + r) >> 1) + 1, r);     }   } ST;   int Pos(int i, int v) {     return lower_bound(R[i] + 1, R[i] + len[i] + 1, v) - R[i];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int x1, asdioayseiowqe12, x2, y2;       scanf( %d%d%d%d , &x1, &asdioayseiowqe12, &x2, &y2);       C[i] = Cord(x1, asdioayseiowqe12, x2, y2);       R[0][++len[0]] = x1;       R[0][++len[0]] = x2;       R[1][++len[1]] = asdioayseiowqe12;       R[1][++len[1]] = y2;     }     for (int i = 0; i <= 1; ++i) {       sort(R[i] + 1, R[i] + len[i] + 1);       len[i] = unique(R[i] + 1, R[i] + len[i] + 1) - (R[i] + 1);     }     for (int i = 1; i <= n; ++i) {       int x1 = C[i].x1, asdioayseiowqe12 = C[i].asdioayseiowqe12, x2 = C[i].x2,           y2 = C[i].y2;       int x = Pos(1, asdioayseiowqe12), y = Pos(1, y2);       open[Pos(0, x1)].push_back(pair<int, int>(x, y));       clos[Pos(0, x2)].push_back(pair<int, int>(x, y));     }     for (int i = 1; i <= len[0]; ++i) {       for (int k = 0; k < open[i].size(); ++k) {         int x = open[i][k].first, y = open[i][k].second;         ST.Update(1, 1, len[1], x, y, +1);       }       if (ST.st[1] >= n - 1) {         int j = ST.Find(1, 1, len[1]);         printf( %d %d n , R[0][i], R[1][j]);         return 0;       }       for (int k = 0; k < clos[i].size(); ++k) {         int x = clos[i][k].first, y = clos[i][k].second;         ST.Update(1, 1, len[1], x, y, -1);       }     }     assert(false);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct hh {     int num;     int c;   };   bool cmp(const hh p, const hh q) { return p.c < q.c; }   int main() {     int n, k;     scanf( %d%d , &n, &k);     hh a[1010], b[1010];     int i;     int p = 0, q = 0;     vector<int> ss[1010];     for (i = 0; i < n; ++i) {       int x, y;       scanf( %d%d , &x, &y);       if (y == 1) {         a[p].num = i + 1;         a[p++].c = x;       } else {         b[q].num = i + 1;         b[q++].c = x;       }     }     sort(a, a + p, cmp);     sort(b, b + q, cmp);     double ans = 0;     if (p >= k) {       for (i = 1; i < k; ++i) {         ans += a[p - i].c * 1.0 / 2;         ss[i].push_back(a[p - i].num);       }       if (q > 0) {         if (a[0].c > b[0].c) {           ans += b[0].c * 1.0 / 2;           ans += a[0].c;         } else {           ans += a[0].c * 1.0 / 2;           ans += b[0].c;         }         ss[k].push_back(b[0].num);       } else         ans += a[0].c * 1.0 / 2;       ss[k].push_back(a[0].num);       for (i = 1; i < q; ++i) {         ss[k].push_back(b[i].num);         ans += b[i].c;       }       for (i = p - k; i > 0; --i) {         ss[k].push_back(a[i].num);         ans += a[i].c;       }     } else {       for (i = 1; i <= p; ++i) {         ss[i].push_back(a[i - 1].num);         ans += a[i - 1].c * 1.0 / 2;       }       int e = 0;       for (i = p + 1; i <= k; ++i) {         ss[i].push_back(b[e].num);         ans += b[e++].c;       }       for (i = e; i < q; ++i) {         ss[k].push_back(b[i].num);         ans += b[i].c;       }     }     printf( %.1f n , ans);     int j;     for (i = 1; i <= k; ++i) {       printf( %d , ss[i].size());       for (j = 0; j < ss[i].size(); ++j) printf(  %d , ss[i][j]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 2;   long long arr[N];   long long pos[N];   long long chance[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t, i, j;     long long n;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> arr[i];       pos[arr[i]] = i;     }     long long high = 0, low = 0;     long long ans = 0, ind = 0;     for (i = 1; i <= n; i++) {       if (arr[i] == i) {         high++;       } else if (arr[i] < i) {         high++;         ans += i - arr[i];         chance[arr[i] - i + n]++;       } else {         low++;         ans += arr[i] - i;         chance[arr[i] - i]++;       }     }     long long prev_ans = ans;     for (i = 1; i < n; i++) {       long long lst_elem = arr[n - (i - 1)];       long long cur_ans =           prev_ans + high - 1 - low - abs(n - lst_elem) + abs(1 - lst_elem);       if (cur_ans < ans) {         ans = cur_ans;         ind = i;       }       prev_ans = cur_ans;       high--;       high += chance[i];       low -= chance[i];       low++;     }     cout << ans <<     << ind;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string vowel =  AEIOUYaeiouy ;     string in;     char s;     bool x = false;     getline(cin, in);     int h = in.size();     for (int k = (h - 1); k >= 0; k--) {       if (isalpha(in[k])) {         s = in[k];         break;       }     }     for (int i = 0; i < vowel.size(); i++) {       if (s == vowel[i]) {         x = true;         break;       }     }     if (x == true)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const long double eps = 1e-7;   const long long inf = numeric_limits<long long>::max() / 2 - 1;   const long long mod = 1000000007;   const long long maxn = 800007;   struct plan {     long long l1, r1;     long long l2, r2;     long long w;   };   struct node {     set<long long> plans;     set<long long> plansin;   };   plan plans[maxn];   bool planused[maxn];   vector<pair<long long, long long>> mineq[maxn];   node tree[maxn];   long long n, q, s;   set<pair<long long, long long>> st;   void add(long long v, long long vl, long long vr, long long pv) {     if (vl >= plans[pv].r1 || vr <= plans[pv].l1) return;     tree[v].plans.insert(pv);     if (vl >= plans[pv].l1 && vr <= plans[pv].r1) {       tree[v].plansin.insert(pv);       return;     }     long long mid = (vl + vr) >> 1;     add(2 * v + 1, vl, mid, pv);     add(2 * v + 2, mid, vr, pv);   }   set<long long> mset;   void makeset(long long v, long long vl, long long vr, long long l,                long long r) {     if (vl >= r || vr <= l) return;     if (vl >= l && vr <= r) {       for (auto i : tree[v].plans) {         if (!planused[i]) {           mset.insert(i);           planused[i] = true;         }       }       tree[v].plans.clear();       return;     }     for (auto i : tree[v].plansin) {       if (!planused[i]) {         mset.insert(i);         planused[i] = true;       }     }     tree[v].plansin.clear();     long long mid = (vl + vr) >> 1;     makeset(2 * v + 1, vl, mid, l, r);     makeset(2 * v + 2, mid, vr, l, r);   }   inline int run() {     cin >> n >> q >> s;     s--;     mineq[s].push_back({1, 0});     mineq[s + 1].push_back({-1, 0});     for (int t = 0; t < q; ++t) {       long long type;       cin >> type;       if (type == 1) {         long long v, u, w;         cin >> v >> u >> w;         v--;         u--;         plans[t].l1 = v;         plans[t].r1 = v + 1;         plans[t].l2 = u;         plans[t].r2 = u + 1;         plans[t].w = w;       } else {         long long v, l, r, w;         cin >> v >> l >> r >> w;         l--;         v--;         if (type == 2) {           plans[t].l1 = v;           plans[t].r1 = v + 1;           plans[t].l2 = l;           plans[t].r2 = r;           plans[t].w = w;         } else {           plans[t].l1 = l;           plans[t].r1 = r;           plans[t].l2 = v;           plans[t].r2 = v + 1;           plans[t].w = w;         }       }       if (plans[t].l1 <= s && plans[t].r1 > s) {         planused[t] = true;         st.insert({plans[t].w, t});       } else {         add(0, 0, n, t);       }     }     while (!st.empty()) {       long long len = st.begin()->first;       long long v = st.begin()->second;       st.erase(st.begin());       mset.clear();       mineq[plans[v].l2].push_back({1, len});       mineq[plans[v].r2].push_back({-1, len});       makeset(0, 0, n, plans[v].l2, plans[v].r2);       for (auto j : mset) {         st.insert({len + plans[j].w, j});       }     }     multiset<long long> minset;     for (int i = 0; i < n; ++i) {       for (auto j : mineq[i]) {         if (j.first == 1) {           minset.insert(j.second);         } else {           minset.erase(minset.find(j.second));         }       }       long long minval = -1;       if (!minset.empty()) minval = *minset.begin();       cout << minval <<    ;     }     return 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     if (strlen(  ) > 0) {       freopen(                         .in ,            r , stdin);       freopen(                         .out ,            w , stdout);     }     run();     cerr << clock() * 1000 / CLOCKS_PER_SEC;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-5;   const int MOD = int(1e9) + 7, MX = int(5e4) + 1;   long long memo[100][3001][2];   int n, l;   pair<int, int> A[101];   long long dp(int i, int len, int f) {     if (len == l) return 1;     if (i == n || len > l) return 0;     long long &ret = memo[i][len][f];     if (ret + 1) return ret;     ret = 0;     int L = A[i].first, W = A[i].second;     if (f) swap(L, W);     for (int j = 0; j < n; j++) {       if (j != i) {         if (A[j].first == W)           ret += dp(j, len + A[j].first, 0);         else if (A[j].second == W)           ret += dp(j, len + A[j].second, 1);         if (ret >= MOD) ret -= MOD;       }     }     return ret;   }   long long sol() {     memset(memo, -1, sizeof memo);     long long res = 0;     for (int i = 0; i < n; i++) {       res += dp(i, A[i].first, 0) +              (A[i].first - A[i].second ? dp(i, A[i].second, 1) : 0);       if (res >= MOD) res -= MOD;     }     return res;   }   void solve() {     scanf( %d %d , &n, &l);     for (int i = 0; i < n; i++) {       scanf( %d %d , &A[i].first, &A[i].second);     }     printf( %lld n , sol() % MOD);   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   const int nmax = 110;   char s[nmax];   bool pal(int n) {     int i;     for (i = 0; i <= (n - 1) / 2; i++)       if (abs(s[i] - s[n - i - 1]) > 2 || abs(s[i] - s[n - i - 1]) == 1) return 0;     return 1;   }   int main() {     int t, n, i;     cin >> t;     for (i = 1; i <= t; i++) {       cin >> n >> s;       if (pal(n) == 1)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   int n;   vector<bool> b;   vector<int> v;   stack<int> st;   bool fi;   vector<bool> dins;   void dfs(int u) {     if (fi) return;     if (b[u]) {       if (dins[u]) {         fi = true;         v.push_back(u);         while (!st.empty() and st.top() != u) {           v.push_back(st.top());           st.pop();         }         v.push_back(u);       } else         return;     }     b[u] = true;     st.push(u);     dins[u] = true;     for (int i = 0; i < g[u].size(); ++i) {       dfs(g[u][i]);     }     if (fi) return;     st.pop();     dins[u] = false;   }   bool check(int x, int y, vector<int> d) {     --d[y];     queue<int> q;     for (int i = 0; i < n; ++i) {       if (d[i] == 0) q.push(i);     }     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = 0; i < g[u].size(); ++i) {         if (u != x or g[u][i] != y) {           --d[g[u][i]];           if (d[g[u][i]] == 0) q.push(g[u][i]);         }       }     }     for (int i = 0; i < n; ++i) {       if (d[i]) {         return false;       }     }     return true;   }   int main() {     int m;     cin >> n >> m;     g = vector<vector<int> >(n);     vector<int> d(n, 0);     for (int i = 0; i < m; ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       ++d[y];       g[x].push_back(y);     }     b = vector<bool>(n, false);     dins = vector<bool>(n, false);     fi = false;     for (int i = 0; i < n; ++i) {       if (!b[i] and !fi) dfs(i);     }     for (int i = 1; i < v.size(); ++i) {       if (check(v[i], v[i - 1], d)) {         cout <<  YES  << endl;         return 0;       }     }     if (v.empty())       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int* degree = new int[100005];     for (int i = 1; i <= n; i++) {       degree[i] = 0;     }     while (m--) {       int a, b;       cin >> a >> b;       degree[a]++;       degree[b]++;     }     int deg1 = 0, deg2 = 0, degG = 0;     for (int i = 1; i <= n; i++) {       if (degree[i] == 0) {         continue;       } else if (degree[i] == 1) {         deg1++;       } else if (degree[i] == 2) {         deg2++;       } else if (degree[i] == n - 1) {         degG++;       }     }     if (deg1 == n - 1 && deg2 == 0 && degG == 1) {       cout <<  star topology ;       return 0;     } else if (deg1 == 2 && deg2 == n - 2 && degG == 0) {       cout <<  bus topology ;       return 0;     } else if (deg1 == 0 && deg2 == n && degG == 0) {       cout <<  ring topology ;       return 0;     } else {       cout <<  unknown topology ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N, M, x, y, K, ml, T, ans;   long long f[1000000], s[1000000];   const long long md = 1e9 + 7;   const long long MAXN = 1e5 + 2;   string in;   int main() {     cin >> N >> K;     f[0] = 1;     for (int i = 1; i <= MAXN; i++) {       f[i] = f[i - 1];       if (i >= K) f[i] = (f[i] % md + f[i - K] % md) % md;     }     s[0] = 0;     for (int i = 1; i <= MAXN; i++) s[i] = (s[i - 1] % md + f[i] % md) % md;     for (int i = 0; i < N; i++) {       cin >> x >> y;       ans = (s[y] - s[x - 1] + md) % md;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec[201], v[201];   int afis[201], scor[201], n, test[201];   bool viz[201];   void elim(int val) {     for (int i = 0; i < v[val].size(); i++) scor[v[val][i]]--;   }   bool incearca(int start) {     int i, j;     for (i = 1; i <= n; i++) {       scor[i] = vec[i].size();       viz[i] = 0;     }     afis[1] = vec[start][0];     elim(vec[start][0]);     afis[2] = vec[start][1];     elim(vec[start][1]);     viz[vec[start][0]] = viz[vec[start][1]] = 1;     for (int i = 3; i <= n; i++) {       int cnt = 0;       for (j = 1; j < n; j++)         if (scor[j] == 1) cnt++;       if (cnt >= 2 || cnt == 0) return 0;       for (j = 1; j < n; j++)         if (scor[j] == 1) {           for (int k = 0; k < vec[j].size(); k++)             if (!viz[vec[j][k]]) {               viz[vec[j][k]] = 1;               elim(vec[j][k]);               afis[i] = vec[j][k];               break;             }           cnt = 0;           for (int k = i; k >= i - vec[j].size() + 1; k--) test[++cnt] = afis[k];           sort(test + 1, test + cnt + 1);           for (int a = 1, b = 0; b < vec[j].size(); a++, b++)             if (test[a] != vec[j][b]) return 0;           break;         }       if (j == n) return 0;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t, i, k, j, a;     cin >> t;     while (t--) {       cin >> n;       for (i = 1; i <= n; i++) {         vec[i].clear();         v[i].clear();       }       for (i = 1; i < n; i++) {         cin >> k;         for (j = 1; j <= k; j++) {           cin >> a;           v[a].push_back(i);           vec[i].push_back(a);         }       }       for (i = 1; i < n; i++)         if (vec[i].size() == 2) {           if (incearca(i)) break;           swap(vec[i][0], vec[i][1]);           if (incearca(i)) break;           swap(vec[i][0], vec[i][1]);         }       for (i = 1; i <= n; i++) cout << afis[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 1e6 + 5;   int n, x, y, ile[4 * maxN];   long long pref[4 * maxN], mini = 1e18;   bool czy = 0;   long long spr(long long x, long long y) {     long long t;     if (__builtin_smulll_overflow(x, y, &t)) czy = 1;     return x * y;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> x >> y;     for (int i = 0, a; i < n; i++) cin >> a, ile[a]++, pref[a] += a;     for (int i = 1; i < 4 * maxN; i++)       ile[i] += ile[i - 1], pref[i] += pref[i - 1];     for (int i = 2; i < maxN; i++) {       long long temp = 0;       int f = max<double>(0, ceil(i - double(x) / y) - 1);       czy = 0;       for (int j = 0; i * j < maxN && !czy; j++) {         temp += (long long)(ile[i * j + f] - ile[i * j]) * x;         if (temp < 0) czy = 1;         temp += spr(spr(i * j + i, ile[i * j + i] - ile[i * j + f]) -                         (pref[i * j + i] - pref[i * j + f]),                     y);         if (temp < 0) czy = 1;       }       if (!czy) {         mini = min(mini, temp);       }     }     cout << mini << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long x, long long y) {     long long res = 1;     while (y--) {       res *= x;     }     return res;   }   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   bool sortbysec(const pair<long long, long long>& a,                  const pair<long long, long long>& b) {     return (a.second < b.second);   }   void solve() {     long long n, a, b;     cin >> n >> a >> b;     vector<pair<long long, long long> > v;     for (long long i = 0; i < n; i++) {       long long x, y;       cin >> x >> y;       v.push_back({x, y});     }     long long g = 0;     for (long long i = 0; i < n - 1; i++) {       for (long long j = i + 1; j < n; j++) {         long long p = v[i].first, q = v[i].second, r = v[j].first,                   s = v[j].second;         if ((p + r <= a && max(q, s) <= b) || (q + s <= b && max(p, r) <= a) ||             (p + r <= b && max(q, s) <= a) || (q + s <= a && max(p, r) <= b)) {           g = max(g, ((p * q) + (r * s)));         } else {           long long t = p;           p = q;           q = t;           if ((p + r <= a && max(q, s) <= b) || (q + s <= b && max(p, r) <= a) ||               (p + r <= b && max(q, s) <= a) || (q + s <= a && max(p, r) <= b)) {             g = max(g, ((p * q) + (r * s)));           }         }       }     }     cout << g;   }   int main() {     fast();     long long t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int LIMIT = 20;   int n, dep[300001];   vector<int> adj[300001];   int dp[300001][LIMIT], dp2[300001][LIMIT];   void dfs(int x, int pre) {     vector<int> child;     for (auto y : adj[x]) {       if (y != pre) {         dfs(y, x);         child.push_back(y);         dep[x] = max(dep[x], dep[y] + 1);       }     }     dp[x][1] = dp2[x][1] = n;     for (int i = 2; i < LIMIT; ++i) {       int lo = 0, hi = n;       while (lo < hi) {         int md = lo + hi + 1 >> 1, tot = 0;         for (auto y : child) {           if (dp[y][i - 1] >= md) {             ++tot;           }         }         (tot >= md) ? lo = md : hi = md - 1;       }       dp[x][i] = dp2[x][i] = lo + hi >> 1;       for (auto y : child) {         dp2[x][i] = max(dp2[x][i], dp2[y][i]);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int x, y;       scanf( %d%d , &x, &y), --x, --y;       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(0, -1);     long long ans = 0LL;     for (int i = 0; i < n; ++i) {       ans += dep[i];     }     for (int u = 0; u < n; ++u) {       for (int i = 1; i < LIMIT; ++i) {         int lo = 2, hi = dp2[u][i];         if (i + 1 < LIMIT) {           lo = max(lo, dp2[u][i + 1] + 1);         }         if (lo <= hi) {           ans += (long long)(hi - lo + 1) * (i - 1);         }       }     }     ans += (long long)n * n;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a;     cin >> n;     long long A[n];     for (long long i = 0; i < n; i++) {       cin >> a;       A[i] = a;     }     long long sum = 0, odd = 0, small = 1000000009;     for (long long i = 0; i < n; i++) {       if (A[i] % 2 != 0) {         odd++;         if (A[i] < small) small = A[i];       }       sum = sum + A[i];     }     if (odd % 2 != 0) sum = sum - small;     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005500;   struct state {     int len, link;     map<char, int> next;   };   state st[N];   int sz, last;   void sa_init() {     last = st[0].len = 0;     sz = 1;     st[0].link = -1;   }   void sa_extend(char c) {     int k = sz++, p;     st[k].len = st[last].len + 1;     for (p = last; p != -1 && !st[p].next.count(c); p = st[p].link)       st[p].next[c] = k;     if (p == -1)       st[k].link = 0;     else {       int q = st[p].next[c];       if (st[p].len + 1 == st[q].len)         st[k].link = q;       else {         int w = sz++;         st[w].len = st[p].len + 1;         st[w].next = st[q].next;         st[w].link = st[q].link;         for (; p != -1 && st[p].next[c] == q; p = st[p].link) st[p].next[c] = w;         st[q].link = st[k].link = w;       }     }     last = k;   }   string s, tt;   long long dp[N];   bool t[N];   int vis[N], q;   long long f(int i) {     if (dp[i] != -1) return dp[i];     long long &r = dp[i] = t[i];     for (auto p : st[i].next) r += f(p.second);     return r;   }   long long go(int i, int p, int len) {     long long r = 0;     if (len == ((int)(tt).size()) / 2) {       if (vis[i] == q) return 0;       vis[i] = q;       r += dp[i];       if (st[st[i].link].len + 1 == len) {         r += go(st[i].link, p + 1, len - 1);       } else {         r += go(i, p + 1, len - 1);       }     } else {       if (p >= ((int)(tt).size()) / 2) return 0;       char c = tt[p + len];       if (st[i].next.count(c)) {         r += go(st[i].next[c], p, len + 1);       } else {         if (i && st[st[i].link].len + 1 == len) {           r += go(st[i].link, p + 1, len - 1);         } else {           r += go(i, p + 1, len - 1);         }       }     }     return r;   }   void solve() {     q++;     cin >> tt;     tt += tt;     cout << go(0, 0, 0) <<   n ;   }   int main() {     cin >> s;     sa_init();     memset(dp, -1, sizeof dp);     for (int i = 0, qwerty = ((int)(s).size()); i < qwerty; i++) sa_extend(s[i]);     int x = last;     while (x) {       t[x] = 1;       x = st[x].link;     }     f(0);     int tn;     cin >> tn;     for (int i = 0, qwerty = tn; i < qwerty; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   using vi = vector<ll>;   using ii = pair<ll, ll>;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     int mini = (int)1e9;     vi v;     for (int i = 0; i < n; i++) {       int aux;       cin >> aux;       v.push_back(aux);       if (aux < mini) mini = aux;     }     int idx = mini % n;     for (int i = 0; i < n; i++)       if (v[idx % n] - mini - i > 0)         idx++;       else         break;     cout << (idx % n) + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   const long long inf = 10000005;   int nsize, root, n, tot, nedge, hed[maxn], father[maxn], maxson[maxn], sz[maxn],       vis[maxn];   long long M, pw[maxn];   map<int, int> up[maxn], cst[maxn];   vector<int> dist[maxn], down[maxn];   struct Edge {     int todot, nex;     long long cost;   } edge[maxn << 1];   inline int read() {     int x = 0;     int w = 0;     char ch = 0;     while (ch <  0  || ch >  9 ) w |= ch ==  - , ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();     return w ? -x : x;   }   void addedge(int a, int b, long long c) {     edge[nedge].todot = b;     edge[nedge].nex = hed[a];     edge[nedge].cost = c;     hed[a] = nedge++;   }   void getsize(int k, int fa) {     ++nsize;     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if ((v == fa) || (vis[v])) continue;       getsize(v, k);     }   }   void getroot(int k, int fa) {     maxson[k] = 0;     sz[k] = 1;     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if ((v == fa) || (vis[v])) continue;       getroot(v, k);       sz[k] += sz[v];       maxson[k] = max(maxson[k], sz[v]);     }     maxson[k] = max(maxson[k], nsize - sz[k]);     if (maxson[k] < maxson[root]) root = k;   }   void ex_gcd(long long a, long long b, long long c, long long &x, long long &y) {     if (!b) {       x = c / a;       y = 0;       return;     }     if (!(a % b)) {       x = 0;       y = c / b;       return;     }     ex_gcd(b, a % b, c, y, x);     y -= (a / b) * x;   }   void Merge(int k, int fa, int gf, int f, int dis, long long nowdown) {     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if ((vis[v]) || (v == fa)) continue;       ++tot;       father[tot - 1] = f ? f : v;       dist[gf].push_back(dis + 1);       down[gf].push_back((nowdown * 10 + edge[i].cost) % M);       if (!f)         Merge(v, k, gf, v, dis + 1, (nowdown * 10 + edge[i].cost) % M);       else         Merge(v, k, gf, f, dis + 1, (nowdown * 10 + edge[i].cost) % M);     }   }   void getup(int k, int fa, int gf, long long nowup, int dis) {     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if ((vis[v]) || (v == fa)) continue;       up[gf][(pw[dis] * edge[i].cost + nowup) % M]++;       getup(v, k, gf, (pw[dis] * edge[i].cost + nowup) % M, dis + 1);     }   }   long long calc(int k) {     int t = dist[k].size();     long long ret = 0;     for (int i = 0; i <= t - 1; i++) {       long long x, y;       ex_gcd(M, pw[dist[k][i]], 1, x, y);       y = ((y % M) + M) % M;       y = (y * (-down[k][i]) % M + M) % M;       long long tx, ty;       ex_gcd(M, 10 % M, 1, tx, ty);       ty = ((ty % M) + M) % M;       ty = (((y - cst[k][father[i]]) * ty) % M + M) % M;       ret += up[k][y] - up[father[i]][ty] - (cst[k][father[i]] % M == y);       if (!down[k][i]) ret++;     }     ret += up[k][0];     up[k].clear();     dist[k].clear();     down[k].clear();     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if (vis[v]) continue;       dist[v].clear();       up[v].clear();       down[v].clear();     }     return ret;   }   long long getans(int k) {     vis[k] = 1;     tot = 0;     dist[k].clear();     down[k].clear();     Merge(k, k, k, 0, 0, 0);     up[k].clear();     getup(k, k, k, 0, 0);     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if (vis[v]) continue;       up[v].clear();       getup(v, k, v, 0, 0);     }     long long ret = calc(k);     for (int i = hed[k]; i != -1; i = edge[i].nex) {       int v = edge[i].todot;       if (vis[v]) continue;       nsize = 0;       getsize(v, k);       root = 0;       getroot(v, k);       ret += getans(root);     }     return ret;   }   int main() {     n = read();     M = read();     pw[0] = 1 % M;     for (int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * 10) % M;     nedge = 0;     memset(hed, -1, sizeof(hed));     for (int i = 1; i <= n - 1; i++) {       int a, b;       long long c;       a = read();       b = read();       c = read();       ++a;       ++b;       addedge(a, b, c);       addedge(b, a, c);       cst[a][b] = cst[b][a] = c;     }     maxson[0] = inf;     root = 0;     nsize = n;     getroot(1, 1);     long long ans = getans(root);     printf( %I64d n , ans);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T, class T1>   inline void gn(T &first, T1 &second) {     gn(first);     gn(second);   }   template <class T, class T1, class T2>   inline void gn(T &first, T1 &second, T2 &z) {     gn(first);     gn(second);     gn(z);   }   template <class T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   template <class T>   inline void printsp(T first) {     print(first);     putchar(   );   }   template <class T>   inline void println(T first) {     print(first);     putchar(  n );   }   template <class T, class U>   inline void print(T first, U second) {     printsp(first);     println(second);   }   template <class T, class U, class V>   inline void print(T first, U second, V z) {     printsp(first);     printsp(second);     println(z);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   int root[1111111], deg[1111111], edge[1111111];   int find_root(int u) {     if (u == root[u]) return u;     return root[u] = find_root(root[u]);   }   int main() {     int n, m, u, v;     int self = 0;     gn(n, m);     for (int i = 1; i <= n; i++) root[i] = i;     for (int i = 0; i < m; i++) {       gn(u, v);       if (u == v)         self++;       else         deg[u]++, deg[v]++;       u = find_root(u);       v = find_root(v);       if (u == v)         edge[u]++;       else {         root[v] = u;         edge[u] += (edge[v] + 1);       }     }     int yes = 0, cnt = 0;     for (int i = 1; i <= n; i++)       if (edge[i] == m) {         yes = 1;         break;       }     if (!yes) return puts( 0 );     long long ans = 0;     for (int i = 1; i <= n; i++) ans += (long long)deg[i] * (deg[i] - 1) / 2;     ans += (long long)self * (m - self) + (long long)self * (self - 1) / 2;     println(ans);   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   inline void chmin(A &a, B b) {     if (a > b) a = b;   }   template <typename A, typename B>   inline void chmax(A &a, B b) {     if (a < b) a = b;   }   struct Dinic {     struct edge {       long long to, cap, rev;       edge(long long to, long long cap, long long rev)           : to(to), cap(cap), rev(rev) {}     };     const long long INF = 1001001001;     vector<vector<edge>> G;     vector<long long> level, iter;     Dinic(long long v) : G(v), level(v), iter(v) {}     void addEdge(long long from, long long to, long long cap) {       G[from].push_back(edge(to, cap, G[to].size()));       G[to].push_back(edge(from, 0, G[from].size() - 1));     }     void bfs(long long s) {       fill(level.begin(), level.end(), -1);       queue<long long> que;       level[s] = 0;       que.push(s);       while (que.size()) {         long long v = que.front();         que.pop();         for (auto &e : G[v]) {           if (e.cap && level[e.to] == -1) {             level[e.to] = level[v] + 1;             que.push(e.to);           }         }       }     }     long long dfs(long long v, long long t, long long f) {       if (v == t) return f;       for (long long &i = iter[v]; i < G[v].size(); i++) {         edge &e = G[v][i];         if (e.cap > 0 && level[v] < level[e.to]) {           long long d = dfs(e.to, t, min(f, e.cap));           if (d > 0) {             e.cap -= d;             G[e.to][e.rev].cap += d;             return d;           }         }       }       return 0;     }     long long maxFlow(long long s, long long t) {       long long flow = 0;       while (true) {         bfs(s);         if (level[t] < 0) return flow;         fill(iter.begin(), iter.end(), 0);         long long f;         while ((f = dfs(s, t, INF)) > 0) flow += f;       }     }   };   long long N, M;   long long A[5555], B[5555];   bool check(long long x) {     Dinic d(N + 2);     long long src = N;     long long snk = N + 1;     vector<long long> cnt(N);     for (long long i = 0; i < (M); i++) {       cnt[A[i]]++;       d.addEdge(A[i], B[i], 1);     }     long long f = 0;     for (long long i = 0; i < (N); i++) {       if (cnt[i] > x) {         d.addEdge(src, i, cnt[i] - x);         f += cnt[i] - x;       } else if (cnt[i] < x) {         d.addEdge(i, snk, x - cnt[i]);       }     }     if (d.maxFlow(src, snk) != f) return false;     set<pair<long long, long long>> es;     for (long long i = 0; i < (N); i++)       for (auto &e : d.G[i])         if (e.cap) es.insert({i, e.to});     for (long long i = 0; i < (M); i++) {       if (es.find({A[i], B[i]}) == es.end()) swap(A[i], B[i]);     }     return true;   }   signed main() {     cin >> N >> M;     for (long long i = 0; i < (M); i++) cin >> A[i] >> B[i], A[i]--, B[i]--;     long long lb = 0, ub = M;     while (ub - lb > 1) {       long long mid = (ub + lb) / 2;       if (check(mid))         ub = mid;       else         lb = mid;     }     check(ub);     cout << ub << endl;     for (long long i = 0; i < (M); i++)       cout << A[i] + 1 <<     << B[i] + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 ,  unroll-loops )   #pragma GCC target( avx2 )   using namespace std;   const int maxn = 1e6 + 5;   int n;   bool used[maxn];   vector<int> v[maxn];   vector<int> g[maxn];   int ans[maxn];   int pr[maxn];   int t[4 * maxn];   void init() {     used[0] = true;     used[1] = true;     for (int i = 2; i < maxn; ++i) {       if (used[i]) continue;       v[i] = {i};       for (int j = 2 * i; j < maxn; j += i) {         used[j] = true;         v[j].push_back(i);       }     }   }   vector<int> slv(vector<pair<int, int> > z) {     vector<int> f;     for (auto h : z) f.push_back(h.second);     pr[0] = 0;     for (int i = 0; i < f.size(); ++i) pr[i + 1] = pr[i] + f[i];     stack<pair<int, int> > o;     for (int i = 0; i <= f.size(); ++i) ans[i] = 1e9;     for (int i = 0; i <= f.size(); ++i) {       while (true) {         if (o.empty()) {           o.push({pr[i], i});           break;         }         pair<int, int> z = o.top();         if (z.first <= pr[i]) {           o.push({pr[i], i});           break;         } else {           ans[z.second] = i;           o.pop();         }       }     }     vector<int> res;     for (int i = 0; i < f.size(); ++i) {       if (ans[i] == 1e9)         res.push_back(n);       else         res.push_back(z[ans[i] - 1].first);     }     return res;   }   void to(int node, int tl, int tr, int l, int r, int val) {     if (tl >= l && tr <= r) {       t[node] = min(t[node], val);       return;     }     if (tl >= r || tr <= l) {       return;     }     int tm = (tl + tr) / 2;     to(2 * node + 1, tl, tm, l, r, val);     to(2 * node + 2, tm, tr, l, r, val);   }   int get(int node, int tl, int tr, int pos) {     if (tl > pos || tr <= pos) return 1e9;     int ans = t[node];     if ((tr - tl) == 1) return ans;     int tm = (tl + tr) / 2;     ans = min(ans, get(2 * node + 1, tl, tm, pos));     ans = min(ans, get(2 * node + 2, tm, tr, pos));     return ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     for (int i = 0; i < 4 * maxn; ++i) t[i] = 1e9;     init();     cin >> n;     int a[n];     for (int i = 0; i < n; ++i) cin >> a[i];     string s;     cin >> s;     for (int i = 0; i < n; ++i)       for (auto h : v[a[i]]) g[h].push_back(i);     vector<pair<pair<int, int>, int> > o;     for (int i = 2; i < maxn; ++i) {       if (g[i].empty()) continue;       vector<pair<int, int> > z;       int o1 = 0;       for (auto j : g[i]) {         o1 = 0;         int x = a[j];         assert(x % i == 0);         while (x % i == 0) {           ++o1;           x /= i;         }         if (s[j] ==  * )           z.push_back({j, o1});         else           z.push_back({j, -o1});       }       vector<int> t = slv(z);       for (int i = 0; i < z.size(); ++i) {         if (i == 0)           o.push_back({{0, z[0].first + 1}, t[i]});         else           o.push_back({{z[i - 1].first + 1, z[i].first + 1}, t[i]});       }     }     to(0, 0, n, 0, n, n);     for (auto l : o) to(0, 0, n, l.first.first, l.first.second, l.second);     long long res = 0;     for (int i = 0; i < n; ++i) {       int val = get(0, 0, n, i);       res += max(0, val - i);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, i, j, k, x, y, w, m, Q, used[100500], start;   int a[100500], b[100500], c[100500];   vector<pair<int, int> > g[100500];   vector<int> f[1000500];   long long d[100500];   long long hea[200500];   int new_w[100500], new_d[100500];   int hea2[200500];   int st[100500], sz[100500], cur_pos[100500];   int G[100500];   int heapSize;   void add(pair<long long, int> value) {     hea[heapSize++] = value.first;     int i = heapSize - 1;     hea2[i] = value.second;     int parent = ((i - 1) >> 1);     while (i > 0 && hea[parent] < hea[i]) {       swap(hea[i], hea[parent]);       swap(hea2[i], hea2[parent]);       i = parent;       parent = ((i - 1) >> 1);     }   }   void heapify(int i) {     int largestChild;     for (;;) {       largestChild = i;       if (hea[2 * i + 1] > hea[largestChild]) {         largestChild = 2 * i + 1;       }       if (hea[2 * i + 2] > hea[largestChild]) {         largestChild = 2 * i + 2;       }       if (largestChild == i) {         break;       }       swap(hea[i], hea[largestChild]);       swap(hea2[i], hea2[largestChild]);       i = largestChild;     }   }   pair<long long, int> getMax() {     pair<int, int> result = make_pair(hea[0], hea2[0]);     heapSize--;     hea[0] = hea[heapSize];     hea2[0] = hea2[heapSize];     hea[heapSize] = -(long long)1e+18;     heapify(0);     return result;   }   inline unsigned int readUInt() {     int c = getc(stdin);     unsigned int x = 0;     while (c <= 32) c = getc(stdin);     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getc(stdin);     return x;   }   int main() {     cin >> n >> m >> Q;     for (i = 0; i <= 200000; i++) hea[i] = -(long long)1e+18;     for (i = 0; i < m; i++) {       int x, y, z;       a[i] = readUInt();       b[i] = readUInt();       c[i] = readUInt();     }     int s = 1;     for (i = 0; i < m; i++) {       g[a[i]].push_back(make_pair(b[i], c[i]));     }     st[1] = 0;     sz[1] = g[1].size();     for (i = 2; i <= n; i++) {       st[i] = cur_pos[i] = st[i - 1] + g[i - 1].size();       sz[i] = g[i].size();     }     for (i = 0; i < m; i++) {       G[cur_pos[a[i]]++] = i;     }     for (i = 1; i <= n; i++) d[i] = (long long)1e+18;     d[s] = 0;     for (i = 0; i < heapSize; i++) hea[i] = -(long long)1e+18;     heapSize = 0;     add(make_pair(0, s));     while (heapSize) {       pair<int, int> tmp = getMax();       int v = tmp.second;       long long cur_d = -tmp.first;       if (cur_d > d[v]) continue;       for (size_t j = 0; j < g[v].size(); ++j) {         int to = g[v][j].first;         long long len = g[v][j].second;         if (d[v] + len < d[to]) {           d[to] = d[v] + len;           add(make_pair(-d[to], to));         }       }     }     for (i = 0; i < m; i++) new_w[i] = min(100000000LL, c[i] + d[a[i]] - d[b[i]]);     int last_test = 1;     for (int ii = 0; ii < Q; ii++) {       int test;       cin >> test;       if (test == 1) {         if (last_test == 2) {           for (i = 0; i <= n; i++) {             new_d[i] = (int)1e+9;           }           for (i = 0; i <= x; i++) f[i].clear();           f[0].push_back(1);           new_d[1] = 0;           for (i = 0; i <= x; i++) {             for (j = 0; j < f[i].size(); j++) {               int v = f[i][j];               if (new_d[v] == i) {                 int tmp = st[v] + sz[v];                 for (k = st[v]; k < tmp; k++) {                   int to = b[G[k]];                   int val = new_w[G[k]];                   if (new_d[to] > i + val) {                     new_d[to] = min(100000000, i + val);                     if (new_d[to] <= x) f[new_d[to]].push_back(to);                   }                 }               }             }           }           for (i = 1; i <= n; i++) {             d[i] += new_d[i];             if (d[i] > (long long)1e+18) d[i] = (long long)1e+18;           }           for (i = 0; i < m; i++)             new_w[i] = min(100000000LL, c[i] + d[a[i]] - d[b[i]]);         }         int t;         cin >> t;         if (d[t] == (long long)1e+18)           cout << -1 << endl;         else           cout << d[t] << endl;       } else {         int tmp;         if (last_test == 1) x = 0;         cin >> tmp;         for (i = 0; i < tmp; i++) {           int y = readUInt();           new_w[y - 1]++;           c[y - 1]++;         }         x += tmp;       }       last_test = test;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class TH>   void _dbg(const char *sdbg, TH h) {     cerr << sdbg <<   =   << h << endl;   }   template <class TH, class... TA>   void _dbg(const char *sdbg, TH h, TA... a) {     while (*sdbg !=  , ) cerr << *sdbg++;     cerr <<   =   << h <<  ,  ;     _dbg(sdbg + 1, a...);   }   const int maxn = (1e6) + 7;   const int maxk = 20;   const int inf = (1e9) + 7;   const long long LLinf = (1e18) + 7;   const long double eps = 1e-9;   const long long mod = 1e9 + 7;   struct lit {     bool stan = 0;     int dlu = 1;     lit() {}     lit(int a, int b) {       stan = a;       dlu = b;     }   };   lit merge(lit a, lit b) {     assert(a.stan == b.stan);     return lit(a.stan, a.dlu + b.dlu);   }   vector<lit> a, b, A, B;   vector<pair<int, int> > odp;   void normuj(vector<lit> &a) {     vector<lit> b;     for (int i = 0; i < ((int)(a).size()); i++)       if (((int)(b).size()) > 0 && b.back().stan == a[i].stan)         b.back() = merge(b.back(), a[i]);       else         b.push_back(a[i]);     a = b;   }   vector<pair<int, int> > elo() {     odp.clear();     int ile = 0;     bool f = 0;     ile = ((int)(a).size()) + ((int)(b).size());     if ((((int)(a).size()) > 2 || ((int)(b).size()) > 2) &&         abs(((int)(a).size()) - ((int)(b).size())) > 2) {       if (((int)(a).size()) < ((int)(b).size())) f = 1, swap(a, b);       int le = ile / 2 - 1;       while (le + 1 < ((int)(a).size()) && a[le].stan == b.back().stan &&              a.back().stan != b.back().stan)         le++;       if (a.back().stan == b.back().stan) {         le = ile / 2 - 1;         while (le + 1 < ((int)(a).size()) && a[le].stan != b.back().stan) le++;       }       int cnt = 0;       vector<lit> xd, xd2;       for (int i = 0; i < le; i++) xd.push_back(a[i]);       for (int i = 0; i < ((int)(b).size()) - 1; i++) xd2.push_back(b[i]);       if (a.back().stan == b.back().stan) xd2.push_back(b.back());       int dlu = 0;       for (int i = le; i < ((int)(a).size()); i++)         xd2.push_back(a[i]), dlu += a[i].dlu;       if (a.back().stan != b.back().stan)         odp.push_back({dlu, b.back().dlu}), xd.push_back(b.back());       else         odp.push_back({dlu, 0});       b = xd2;       a = xd;       normuj(a);       normuj(b);     };     while (((int)(a).size()) > 1 && ((int)(b).size()) > 1) {       if (((int)(a).size()) == 2 && ((int)(b).size()) == 2) {         if (a.back().stan == b.back().stan) {           odp.push_back({a.back().dlu, 0});           odp.push_back({a[0].dlu, a.back().dlu + b.back().dlu});         } else           odp.push_back({a.back().dlu, b.back().dlu});         if (f)           for (auto &s : odp) swap(s.first, s.second);         return odp;       }       if (a.back().stan == b.back().stan) {         if (((int)(a).size()) >= ((int)(b).size())) {           pair<lit, lit> xd = {a[((int)(a).size()) - 2], a.back()};           odp.push_back({xd.first.dlu + xd.second.dlu, b.back().dlu});           a.pop_back();           a.pop_back();           lit xdxd = b.back();           b.pop_back();           a.back() = merge(a.back(), xdxd);           b.back() = merge(xd.first, b.back());           b.push_back(xd.second);         } else {           pair<lit, lit> xd = {b[((int)(b).size()) - 2], b.back()};           odp.push_back({a.back().dlu, xd.first.dlu + xd.second.dlu});           b.pop_back();           b.pop_back();           lit xdxd = a.back();           a.pop_back();           b.back() = merge(b.back(), xdxd);           a.back() = merge(xd.first, a.back());           a.push_back(xd.second);         }       } else {         odp.push_back({a.back().dlu, b.back().dlu});         lit xd = a.back();         lit xd2 = b.back();         a.pop_back();         b.pop_back();         b.back() = merge(b.back(), xd);         a.back() = merge(a.back(), xd2);       }     };     if (((int)(a).size()) == 3 && ((int)(b).size()) == 1) {       if (a.back().stan == b.back().stan) {         odp.push_back({a.back().dlu, 0});         odp.push_back(             {a[((int)(a).size()) - 2].dlu, a.back().dlu + b.back().dlu});       } else {         odp.push_back({a.back().dlu, b.back().dlu});         odp.push_back(             {a[((int)(a).size()) - 2].dlu + b.back().dlu, a.back().dlu});       }     }     if (((int)(a).size()) == 1 && ((int)(b).size()) == 3) {       if (b.back().stan == a.back().stan) {         odp.push_back({0, b.back().dlu});         odp.push_back(             {b.back().dlu + a.back().dlu, b[((int)(b).size()) - 2].dlu});       } else {         odp.push_back({a.back().dlu, b.back().dlu});         odp.push_back(             {b.back().dlu, b[((int)(b).size()) - 2].dlu + a.back().dlu});       }     }     if (((int)(a).size()) == 2 && ((int)(b).size()) == 1) {       if (a.back().stan == b.back().stan)         odp.push_back({a.back().dlu, 0});       else         odp.push_back({a.back().dlu, b.back().dlu});     }     if (((int)(b).size()) == 2 && ((int)(a).size()) == 1) {       if (a.back().stan == b.back().stan)         odp.push_back({0, b.back().dlu});       else         odp.push_back({a.back().dlu, b.back().dlu});     }     if (f)       for (auto &s : odp) swap(s.first, s.second);     return odp;   }   vector<pair<int, int> > eloelo() {     bool f = 0;     int ile = 0;     odp.clear();     ile = ((int)(a).size()) + ((int)(b).size());     if ((((int)(a).size()) > 2 || ((int)(b).size()) > 2) &&         abs(((int)(a).size()) - ((int)(b).size())) > 2) {       if (((int)(a).size()) < ((int)(b).size())) f = 1, swap(a, b);       int le = ile / 2 - 1;       while (le + 1 < ((int)(a).size()) && a[le].stan == b.back().stan) le++;       int cnt = 0;       vector<lit> xd, xd2;       for (int i = 0; i < le; i++) xd.push_back(a[i]);       for (int i = 0; i < ((int)(b).size()) - 1; i++) xd2.push_back(b[i]);       int dlu = 0;       for (int i = le; i < ((int)(a).size()); i++)         xd2.push_back(a[i]), dlu += a[i].dlu;       odp.push_back({dlu, b.back().dlu});       xd.push_back(b.back());       b = xd2;       a = xd;       normuj(a);       normuj(b);     }     while (((int)(a).size()) > 1 && ((int)(b).size()) > 1) {       if (((int)(a).size()) == 2 && ((int)(b).size()) == 2) {         if (a.back().stan == b.back().stan) {           odp.push_back({a.back().dlu, 0});           odp.push_back({a[0].dlu, a.back().dlu + b.back().dlu});         } else           odp.push_back({a.back().dlu, b.back().dlu});         if (f)           for (auto &s : odp) swap(s.first, s.second);         return odp;       }       if (a.back().stan == b.back().stan) {         if (((int)(a).size()) >= ((int)(b).size())) {           pair<lit, lit> xd = {a[((int)(a).size()) - 2], a.back()};           odp.push_back({xd.first.dlu + xd.second.dlu, b.back().dlu});           a.pop_back();           a.pop_back();           lit xdxd = b.back();           b.pop_back();           a.back() = merge(a.back(), xdxd);           b.back() = merge(xd.first, b.back());           b.push_back(xd.second);         } else {           pair<lit, lit> xd = {b[((int)(b).size()) - 2], b.back()};           odp.push_back({a.back().dlu, xd.first.dlu + xd.second.dlu});           b.pop_back();           b.pop_back();           lit xdxd = a.back();           a.pop_back();           b.back() = merge(b.back(), xdxd);           a.back() = merge(xd.first, a.back());           a.push_back(xd.second);         }       } else {         odp.push_back({a.back().dlu, b.back().dlu});         lit xd = a.back();         lit xd2 = b.back();         a.pop_back();         b.pop_back();         b.back() = merge(b.back(), xd);         a.back() = merge(a.back(), xd2);       }     };     if (((int)(a).size()) == 3 && ((int)(b).size()) == 1) {       if (a.back().stan == b.back().stan) {         odp.push_back({a.back().dlu, 0});         odp.push_back(             {a[((int)(a).size()) - 2].dlu, a.back().dlu + b.back().dlu});       } else {         odp.push_back({a.back().dlu, b.back().dlu});         odp.push_back(             {a[((int)(a).size()) - 2].dlu + b.back().dlu, a.back().dlu});       }     }     if (((int)(a).size()) == 1 && ((int)(b).size()) == 3) {       if (b.back().stan == a.back().stan) {         odp.push_back({0, b.back().dlu});         odp.push_back(             {b.back().dlu + a.back().dlu, b[((int)(b).size()) - 2].dlu});       } else {         odp.push_back({a.back().dlu, b.back().dlu});         odp.push_back(             {b.back().dlu, b[((int)(b).size()) - 2].dlu + a.back().dlu});       }     }     if (((int)(a).size()) == 2 && ((int)(b).size()) == 1) {       if (a.back().stan == b.back().stan)         odp.push_back({a.back().dlu, 0});       else         odp.push_back({a.back().dlu, b.back().dlu});     }     if (((int)(b).size()) == 2 && ((int)(a).size()) == 1) {       if (a.back().stan == b.back().stan)         odp.push_back({0, b.back().dlu});       else         odp.push_back({a.back().dlu, b.back().dlu});     }     if (f)       for (auto &s : odp) swap(s.first, s.second);     return odp;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     char poprz = 0;     int ile = 0;     for (auto v : s) {       if (v == poprz)         ile++;       else {         if (ile > 0) a.push_back(lit(poprz -  a , ile));         poprz = v;         ile = 1;       }     }     a.push_back(lit(poprz -  a , ile));     cin >> s;     ile = 0;     poprz =  a ;     for (auto v : s) {       if (v == poprz)         ile++;       else {         if (ile > 0) b.push_back(lit(poprz -  a , ile));         poprz = v;         ile = 1;       }     }     b.push_back(lit(poprz -  a , ile));     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     A = a;     B = b;     vector<pair<int, int> > x = elo();     a = A;     b = B;     vector<pair<int, int> > y = eloelo();     cout << min(((int)(x).size()), ((int)(y).size())) <<   n ;     if (((int)(x).size()) < ((int)(y).size()))       for (auto s : x) cout << s.first <<     << s.second <<   n ;     else       for (auto s : y) cout << s.first <<     << s.second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     for (int test = 0; test < q; test++) {       int a, b, c, d, k;       cin >> a >> b >> c >> d >> k;       int pen = (a - 1) / c + 1, pencil = (b - 1) / d + 1;       if (pen + pencil > k) {         cout << -1 <<   n ;       } else {         cout << pen <<     << pencil <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 2;   int t, n, m, U, V, sum, ans1, ans2;   bool kt[maxn], ch[maxn];   struct T {     int c, id;   };   vector<T> a[maxn];   vector<int> s[maxn];   void Enter() {     cin >> m >> n;     for (int i = 1; i <= m; i++) {       a[i].clear();       s[i].clear();       a[i].push_back({ a , 0});       s[i].push_back(0);     }     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) {         char u;         cin >> u;         s[i].push_back(u -  0 );       }     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) {         char c;         cin >> c;         a[i].push_back({c, -1});       }   }   void Findcir(int u, int v, int id) {     if (a[u][v].id != -1) {       U = u;       V = v;       sum = id - a[u][v].id;       return;     }     a[u][v].id = id;     char c = a[u][v].c;     if (c ==  U ) Findcir(u - 1, v, id + 1);     if (c ==  D ) Findcir(u + 1, v, id + 1);     if (c ==  L ) Findcir(u, v - 1, id + 1);     if (c ==  R ) Findcir(u, v + 1, id + 1);     a[u][v].id = -1;   }   void DFS(int u, int v, int id) {     a[u][v].id = id;     if (s[u][v] == 0) {       if (!kt[id % sum]) {         kt[id % sum] = true;         ans2++;         ans1 -= ch[id % sum];       }     } else {       if (!ch[id % sum] && !kt[id % sum]) {         ch[id % sum] = true;         ans1++;       }     }     if (u > 1 && a[u - 1][v].id == -1 && a[u - 1][v].c ==  D )       DFS(u - 1, v, id + 1);     if (u < m && a[u + 1][v].id == -1 && a[u + 1][v].c ==  U )       DFS(u + 1, v, id + 1);     if (v > 1 && a[u][v - 1].id == -1 && a[u][v - 1].c ==  R )       DFS(u, v - 1, id + 1);     if (v < n && a[u][v + 1].id == -1 && a[u][v + 1].c ==  L )       DFS(u, v + 1, id + 1);   }   void Solve() {     ans1 = ans2 = 0;     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++)         if (a[i][j].id == -1) {           Findcir(i, j, 0);           fill_n(kt, sum + 1, false);           fill_n(ch, sum + 1, false);           DFS(U, V, 0);         }     cout << ans1 + ans2 <<     << ans2 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> t;     while (t--) {       Enter();       Solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1000000000;   const long long INFLL = INF * INF;   long long pot2[100];   vector<long long> czek_all(long long l, long long r, long long k) {     vector<long long> wynik;     long long x = r - l + 1;     long long res = INFLL;     for (int i = (1); i <= (pot2[x] - 1); ++i) {       vector<long long> v;       long long a = i, ile = 0;       long long tmp = 0;       for (int j = (0); j <= ((x)-1); ++j) {         if (a % 2 == 1) {           v.push_back(l + j);           tmp = tmp ^ (l + j);           ile++;         }         a /= 2;       }       if (tmp < res && ile <= k) {         res = tmp;         wynik = v;       }     }     return wynik;   }   void jebaj() {     pot2[0] = 1;     for (int i = (0); i <= ((60) - 1); ++i) pot2[i + 1] = pot2[i] * 2;     long long l, r, k;     cin >> l >> r >> k;     if (k == 1) {       cout << l <<   n  << 1 <<   n  << l <<   n ;       return;     }     if (r - l < 8) {       vector<long long> v = czek_all(l, r, k);       long long kk = 0;       for (__typeof((v).begin()) it = ((v).begin()); it != (v).end(); ++it)         kk = kk ^ (*it);       cout << kk <<   n ;       cout << (int)((v).size()) <<   n ;       for (__typeof((v).begin()) it = ((v).begin()); it != (v).end(); ++it)         cout << *it <<    ;       cout <<   n ;       return;     }     long long a = l;     while (a % 4 > 0) {       a++;     }     if (k >= 4) {       cout << 0 <<   n  << 4 <<   n ;       for (int i = (0); i <= ((4) - 1); ++i) {         cout << a <<    ;         a++;       }       cout <<   n ;       return;     }     if (k == 2) {       cout << 1 <<   n  << 2 <<   n ;       cout << a <<     << a + 1 <<   n ;       return;     }     int h = 0;     while (pot2[h] <= l) h++;     long long pot = pot2[h];     if (pot >= r) {       cout << 1 <<   n  << 2 <<   n ;       cout << a <<     << a + 1 <<   n ;       return;     }     long long x = pot + pot2[h - 1];     if (x <= r) {       cout << 0 <<   n  << 3 <<   n ;       cout << pot - 1 <<     << x <<     << (x ^ (pot - 1)) <<   n ;       return;     }     cout << 1 <<   n  << 2 <<   n ;     cout << a <<     << a + 1 <<   n ;     return;     return;   }   int main() {     ios_base::sync_with_stdio(0);     int t;     t = 1;     for (int i = (0); i <= ((t)-1); ++i) {       jebaj();     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2,unroll-loops )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 10000000000000010LL;   const int MAXN = 100010, LOG = 20;   long long n, m, K, u, v, X, Y, t, a, b, ans, shit;   long long A[MAXN], f[MAXN], g[MAXN];   long long tav[MAXN];   int h[MAXN];   long long up[MAXN], dwn[MAXN];   int sz[MAXN];   bool dead[MAXN];   vector<int> G[MAXN];   vector<int> v1, v2;   int mp1[MAXN], mp2[MAXN];   int dfs1(int node, int par) {     sz[node] = 1;     for (int v : G[node])       if (!dead[v] && v != par) sz[node] += dfs1(v, node);     return sz[node];   }   int Centroid(int node, int par, int n) {     for (int v : G[node])       if (!dead[v] && v != par && 2 * sz[v] >= n) return Centroid(v, node, n);     return node;   }   void dfs2(int node, int par) {     v2.push_back(node);     h[node] = h[par] + 1;     dwn[node] = (dwn[par] + A[node] * tav[h[node]]) % Y;     up[node] = (K * up[par] + A[node]) % Y;     for (int v : G[node])       if (v != par && !dead[v]) dfs2(v, node);   }   long long powmod(long long a, long long b) {     b %= Y - 1;     if (b < 0) b += Y - 1;     long long res = 1;     for (; b; b >>= 1, a = a * a % Y)       if (b & 1) res = res * a % Y;     return res;   }   void Calc(vector<int> &vec, int w) {     int s1 = 0, s2 = 0;     for (int u : vec) mp1[s1++] = dwn[u];     sort(mp1, mp1 + s1);     for (int v : vec) {       long long tmp = X - up[v];       tmp = tmp * powmod(K, Y - 1 - h[v]) % Y;       if (tmp < 0) tmp += Y;       int gt = upper_bound(mp1, mp1 + s1, tmp) - lower_bound(mp1, mp1 + s1, tmp);       g[v] += gt * w;       mp2[s2++] = tmp;     }     sort(mp2, mp2 + s2);     for (int u : vec) {       int gt =           upper_bound(mp2, mp2 + s2, dwn[u]) - lower_bound(mp2, mp2 + s2, dwn[u]);       f[u] += gt * w;     }   }   void Decompose(int root) {     root = Centroid(root, root, dfs1(root, root));     dead[root] = 1;     up[root] = 0;     dwn[root] = A[root];     h[root] = 0;     v1 = {root};     for (int v : G[root])       if (!dead[v]) {         dfs2(v, root);         Calc(v2, -1);         for (int i : v2) v1.push_back(i);         v2.clear();       }     Calc(v1, +1);     for (int v : G[root])       if (!dead[v]) Decompose(v);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> Y >> K >> X;     tav[0] = 1;     for (int i = 1; i < MAXN; i++) tav[i] = tav[i - 1] * K % Y;     for (int i = 1; i <= n; i++) cin >> A[i];     for (int i = 1; i < n; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     Decompose(1);     for (int v = 1; v <= n; v++) {       ans += g[v] * (n - g[v]) * 2;       ans += f[v] * (n - f[v]) * 2;       ans += f[v] * (n - g[v]);       ans += g[v] * (n - f[v]);     }     ans /= 2;     ans = n * n * n - ans;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n, *p;     cin >> m >> n;     p = new int[m];     for (int i = 0; i < m; i++) cin >> p[i];     sort(p, p + m);     cout << p[m - n] << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long res = 1ll;     while (b > 0) {       if (b % 2 != 0) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b /= 2;     }     return res % 1000000007;   }   long long ncr(long long n, long long k) {     if (k > n - k) k = n - k;     long long pro = 1;     for (long long i = 0; i < k; i++) {       pro = (pro * (n - i)) % 1000000007;       pro /= (i + 1);     }     return (pro) % 1000000007;   }   void swap(long long *a, long long *b) {     long long temp = *a;     *a = *b;     *b = temp;   }   vector<long long> prime;   void seive() {     vector<bool> isprime(1000001, true);     for (int i = 2; i * i <= 1000000; i++) {       if (isprime[i]) {         for (int j = i * i; j <= 1000000; j += i) {           isprime[j] = false;         }       }     }     for (int i = 2; i <= 1000000; i++) {       if (isprime[i]) prime.push_back(i);     }   }   vector<long long> parent;   long long find(long long p) {     if (parent[p] == p) return parent[p];     return parent[p] = find(parent[p]);   }   void unun(long long a, long long b) {     long long r1 = find(a);     long long r2 = find(b);     if (r1 != r2) parent[r2] = r1;   }   void solve() {     long long n;     cin >> n;     set<long long> s;     s.insert(0);     s.insert(1);     s.insert(n);     for (int i = 1; i * i <= n; i++) {       long long x = (n / i);       long long y = (n / x);       s.insert((n / x));       s.insert((n / y));     }     cout << s.size() <<   n ;     for (auto itr : s) cout << itr <<    ;     cout <<   n ;   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long x;     cin >> x;     for (long long i = 1; i <= x; i++) {       for (long long j = i; j <= x; j++) {         if (j % i == 0 && i * j > x && j / i < x) {           cout << j <<     << i << endl;           return 0;         }       }     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110000;   long long init[N], tab[N * 2], ope[N][3], ch[N], h[N], num[N * 8], sum[N * 8],       ans[N * 8], nn, ss, aa;   bool ini[N * 2];   int sz = 0;   inline void Update(int x, int l, int r) {     if (l == r) return;     int lc = (x << 1), rc = lc + 1;     sum[x] = sum[lc] + sum[rc];     num[x] = num[lc] + num[rc];     ans[x] = ans[lc] + ans[rc] + num[lc] * sum[rc] - num[rc] * sum[lc];   }   inline void Build(int x, int l, int r) {     if (l == r) {       if (ini[l]) {         num[x] = 1;         sum[x] = tab[l];         ans[x] = 0;       }       return;     }     int mid = (l + r) >> 1, lc = (x << 1), rc = lc + 1;     Build(lc, l, mid);     Build(rc, mid + 1, r);     Update(x, l, r);   }   inline void Insert(int x, int l, int r, int pos, int tp) {     if (l == r) {       if (tp == 1) {         num[x] = 1;         sum[x] = tab[l];         ans[x] = 0;       } else         num[x] = sum[x] = ans[x] = 0;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid)       Insert(x << 1, l, mid, pos, tp);     else       Insert(x << 1 | 1, mid + 1, r, pos, tp);     Update(x, l, r);   }   inline void Query(int x, int l, int r, int a, int b) {     if (l > r) return;     if (l == a && r == b) {       aa = aa + ans[x] + nn * sum[x] - num[x] * ss;       nn += num[x];       ss += sum[x];       return;     }     if (l == r) return;     int mid = (l + r) >> 1;     if (b <= mid)       Query(x << 1, l, mid, a, b);     else if (a > mid)       Query(x << 1 | 1, mid + 1, r, a, b);     else       Query(x << 1, l, mid, a, mid), Query(x << 1 | 1, mid + 1, r, mid + 1, b);   }   int main() {     int n, q;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &init[i]);       tab[i] = ch[i] = h[i] = init[i];     }     sz = n;     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       scanf( %I64d%I64d%I64d , &ope[i][0], &ope[i][1], &ope[i][2]);       if (ope[i][0] == 1) {         ch[ope[i][1]] += ope[i][2];         tab[++sz] = ch[ope[i][1]];       }     }     sort(init + 1, init + n + 1);     sort(tab + 1, tab + sz + 1);     unique(tab + 1, tab + sz + 1);     for (int i = 1; i <= sz; i++)       if (tab[i] > tab[i + 1]) sz = i;     int p = 1;     for (int i = 1; i <= n; i++) {       while (tab[p] != init[i] && p <= sz) p++;       ini[p] = 1;     }     Build(1, 1, sz);     int l, r;     for (int i = 1; i <= q; i++) {       if (ope[i][0] == 2) {         l = std::lower_bound(tab + 1, tab + sz + 1, ope[i][1]) - tab;         while (tab[l] < ope[i][1] && l <= sz) l++;         if (l == 0) l = 1;         if (l > sz) {           printf( 0 n );           continue;         }         r = std::lower_bound(tab + 1, tab + sz + 1, ope[i][2]) - tab;         if (r > sz) r = sz;         while (tab[r] > ope[i][2] && r >= 1) r--;         if (r <= 0) {           printf( 0 n );           continue;         }         nn = ss = aa = 0;         Query(1, 1, sz, l, r);         printf( %I64d n , aa);       } else {         l = std::lower_bound(tab + 1, tab + sz + 1, h[ope[i][1]]) - tab;         h[ope[i][1]] += ope[i][2];         r = std::lower_bound(tab + 1, tab + sz + 1, h[ope[i][1]]) - tab;         Insert(1, 1, sz, l, -1);         Insert(1, 1, sz, r, 1);       }     }     return 0;   }