#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   inline int add(int a, int b) {     if ((a += b) >= mod) a -= mod;     return a;   }   inline int mult(int a, int b) {     long long t = 1ll * a * b;     if (t >= mod) t %= mod;     return t;   }   inline int dec(int a, int b) {     if ((a -= b) < 0) a += mod;     return a;   }   int n, q, a[200010], w[200010];   inline int lowbit(int x) { return x & (-x); }   class tree1 {    public:     long long dat[200010];     void modify(int p, int v) {       for (; p <= n; p += lowbit(p)) dat[p] += v;     }     long long query(int p) {       long long ret = 0;       for (; p; p -= lowbit(p)) ret += dat[p];       return ret;     }     long long query(int l, int r) { return query(r) - query(l - 1); }   } T1;   class tree2 {    public:     int dat[200010];     void modify(int p, int v) {       for (; p <= n; p += lowbit(p)) dat[p] = add(dat[p], v);     }     int query(int p) {       int ret = 0;       for (; p; p -= lowbit(p)) ret = add(ret, dat[p]);       return ret;     }     int query(int l, int r) { return dec(query(r), query(l - 1)); }   } T2;   int x, y;   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &w[i]);       T1.modify(i, w[i]);       T2.modify(i, mult(w[i], a[i] - i));     }     while (q--) {       scanf( %d%d , &x, &y);       if (x < 0) {         x = -x;         T1.modify(x, y - w[x]);         T2.modify(x, mult(dec(y, w[x]), a[x] - x));         w[x] = y;       } else {         if (x == y) {           puts( 0 );           continue;         }         int L = x, R = y, pos = L;         while (L <= R) {           int mid = (L + R) >> 1;           if (T1.query(x, mid) >= T1.query(mid + 1, y))             pos = mid, R = mid - 1;           else             L = mid + 1;         }         printf( %d n , add(dec(mult(a[pos] - pos, T1.query(x, pos) % mod),                                T2.query(x, pos)),                            dec(T2.query(pos, y),                                mult(a[pos] - pos, T1.query(pos, y) % mod))));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     bool b = false;     for (int i = 2; i < n; i++) {       if (a[i - 2] + a[i - 1] > a[i] && a[i] + a[i - 1] > a[i - 2] &&           a[i - 2] + a[i] > a[i - 1]) {         b = true;         break;       }     }     cout << (b ?  YES  :  NO ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int nCase;     scanf( %d , &nCase);     while (nCase--) {       int a, b;       scanf( %d%d , &a, &b);       if (a == 0) {         if (b == 0)           printf( 1 n );         else           printf( 0.5 n );         continue;       }       if (b == 0) {         printf( 1 n );         continue;       }       b *= 4;       if (a >= b)         printf( %.6f n , (0.5 * (a - b + a) * b + 1.0 * a * b) / (2.0 * b * a));       else         printf( %.6f n , (1.0 * a * b + 0.5 * a * a) / (2.0 * b * a));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void faster() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const int MAXN = 2e5 + 1;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   const long double PI = acos(-1);   long long n, a[MAXN], dp[MAXN], d[MAXN], cnt[MAXN], ans;   vector<pair<int, bool>> g[MAXN];   bool is_lucky(int x) {     bool res = 1;     while (x) {       res &= (((x % 10) == 7) | ((x % 10) == 4));       x /= 10;     }     return res;   }   void dfs(int v, int pr = 0) {     cnt[v] = 1;     for (pair<int, int> x : g[v]) {       int to = x.first, cost = x.second;       if (to == pr) continue;       dfs(to, v);       cnt[v] += cnt[to];       if (cost)         d[v] += cnt[to];       else         d[v] += d[to];     }   }   void dfs_a(int v, int pr, long long res) {     ans += (res * (res - 1));     for (pair<int, int> x : g[v]) {       int to = x.first, cost = x.second;       if (pr == to) continue;       if (cost)         dfs_a(to, v, d[to] + (cnt[1] - cnt[to]));       else         dfs_a(to, v, res);     }   }   int main() {     faster();     cin >> n;     for (int i = 1, v, u, w; i < n; ++i) {       cin >> u >> v >> w;       w = is_lucky(w);       g[u].push_back({v, w});       g[v].push_back({u, w});     }     dfs(1);     dfs_a(1, 0, d[1]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int powmod(int x, int y, int p) {     int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y % 2 == 1) res = (res * x) % p;       y /= 2;       x = (x * x) % p;     }     return res;   }   string dectobin(int x) {     string s =   ;     while (x > 0) {       int t = x % 2;       s.push_back(t +  0 );       x /= 2;     }     reverse(s.begin(), s.end());     if (s.compare(  ) == 0)       return  0 ;     else       return s;   }   int long long bintodec(string s) {     int long long ans = 0;     int long long n = s.size();     for (int long long i = n - 1; i >= 0; i--) {       if (s[i] ==  1 ) ans += pow(2, n - i - 1);     }     return ans;   }   int find(int k, int n) { return ((n & (1 << (k - 1))) >> (k - 1)); }   int long long f[100000 + 1];   int32_t main() {     f[0] = 1;     f[1] = 1;     for (int i = 2; i <= 100000; i++) {       f[i] = ((i << 1) - 1ll) * f[i - 1] % 1000000007 * (i << 1) % 1000000007;     }     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       int long long n;       cin >> n;       cout << f[n] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[5];   char x[4];   long long res = 1e14;   inline long long calc(long long x, long long y, char xx) {     if (xx ==  * ) return x * y;     return x + y;   }   int main() {     register int i, j;     for (i = 1; i <= 4; i++) cin >> a[i];     for (i = 1; i <= 3; i++) cin >> x[i];     int f[5] = {0, 1, 2, 3, 4};     do {       long long A = calc(a[f[1]], a[f[2]], x[1]);       long long B = calc(A, a[f[3]], x[2]);       long long T1 = calc(B, a[f[4]], x[3]);       A = calc(a[f[1]], a[f[2]], x[1]);       B = calc(a[f[3]], a[f[4]], x[2]);       long long T2 = calc(A, B, x[3]);       res = min(res, min(T1, T2));     } while (next_permutation(f + 1, f + 5));     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int i;     string b, a;     cin >> b;     int size = b.size();     for (i = 0; i <= size - 1; i++) {       a += b[i];       i++;     }     a += b[size - 1];     cout << a <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     getline(cin, s);     int _t = stoi(s);     while (_t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 7;   int main() {     long long n;     cin >> n;     for (long long a = 0; a < n / 1234567 + 1; a++)       for (long long b = 0; b < n / 123456 + 1; b++) {         long long c = n - a * 1234567 - b * 123456;         if (c >= 0 && c % 1234 == 0) {           cout <<  YES  << endl;           return 0;         }       }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[100005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, k, x, y;     cin >> n >> k;     for (int i = 0; i < n - 1; ++i) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     int kol = 0;     for (int i = 1; i <= n; ++i) {       if ((int)g[i].size() == 1) kol++;     }     cout << fixed << setprecision(7) << k / (double)(kol)*2 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005, mod = 1e9 + 7;   inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; }   int n, cnt[maxn], vec[maxn], tot;   int main() {     scanf( %d , &n);     cnt[0] = 1;     for (int i = 1, x; i <= n; ++i) {       scanf( %d , &x), tot = 0;       for (int j = 1; j * j <= x; ++j) {         if (x % j == 0) {           vec[++tot] = j;           if (j * j != x && x / j <= n)             cnt[x / j] = Add(cnt[x / j], cnt[x / j - 1]);         }       }       for (int j = tot; j; --j)         if (vec[j] <= n) cnt[vec[j]] = Add(cnt[vec[j]], cnt[vec[j] - 1]);     }     int ans = 0;     for (int i = 1; i <= n; ++i) ans = Add(ans, cnt[i]);     printf( %d n , ans);   }


#include <bits/stdc++.h>   int main() {     int i = 1, n, m;     scanf( %d%d , &n, &m);     while (1) {       if (m < i) break;       m -= i;       i = i % n + 1;     }     printf( %d n , m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n, x;       cin >> n >> x;       long long int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a, a + n);       int j = 1, teams = 0;       for (int i = n - 1; i >= 0; i--) {         if (a[i] * j >= x) {           teams++;           j = 0;         }         j++;       }       cout << teams << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 1;   const long long LIMIT = 2e18 + 1;   int n;   long long k;   long long arr[N];   long long cnt[N];   bool isUnsafe(long long a, long long b) {     return a > LIMIT / b || b > LIMIT / a;   }   int solve(int i) {     int res = 0;     long long s, p;     if (arr[i] != 1) {       s = k * arr[i];       p = arr[i];       res += (s == p);     } else {       s = k * cnt[i];       p = 1;       res += (k == 1);     }     for (++i; i < n; ++i) {       if (arr[i] == 1) {         long long sOld = s;         s += k * cnt[i];         if (sOld < p && s >= p && (p - sOld) % k == 0) {           ++res;         }       } else {         if (isUnsafe(p, arr[i])) {           break;         }         p *= arr[i];         s += k * arr[i];         if (s == p) {           ++res;         }       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 0; i < n; ++i) {       cin >> arr[i];       if (i > 0 && arr[i] == 1 && arr[i - 1] == 1) {         --i;         --n;       }       ++cnt[i];     }     long long res = 0;     for (int i = 0; i < n; ++i) {       res += solve(i);       if (arr[i] == 1) {         --cnt[i];         if (cnt[i]) {           --i;         }       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long double pi = acos(-1.0);   const long double eps = 1e-9;   const int MAX_SIZE = 50;   const int dX[] = {-1, 1, 0, 0, -1, 1, -1, 1};   const int dY[] = {0, 0, -1, 1, 1, -1, -1, 1};   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& a) {     o << a.first <<     << a.second;     return o;   }   template <class S, class T>   ostream& operator<<(ostream& o, const map<S, T>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << (*it) <<   n ;     return o;   }   template <class S>   ostream& operator<<(ostream& o, const vector<S>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << *it <<    ;     return o;   }   template <class S>   ostream& operator<<(ostream& o, const vector<vector<S> >& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) {       o << (*it) <<   n ;     }     return o;   }   template <class S>   ostream& operator<<(ostream& o, const set<S>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << *it <<    ;     return o;   }   const long long MAX = 2005;   int a[MAX][MAX];   long long diagl[MAX * 2], diagr[MAX * 2];   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     ;     int i, j, n, x1, y1, x2, y2;     long long val, max1 = 0, max2 = 0;     scanf( %d , &n);     for (i = (0); (((1) > 0) - ((1) < 0)) * i < (((1) > 0) - ((1) < 0)) * (n);          i += (1)) {       for (j = (0); (((1) > 0) - ((1) < 0)) * j < (((1) > 0) - ((1) < 0)) * (n);            j += (1)) {         scanf( %d , &a[i][j]);         diagl[i + j] += a[i][j];         diagr[n - (i - j) - 1] += a[i][j];       }     }     for (i = (0); (((1) > 0) - ((1) < 0)) * i < (((1) > 0) - ((1) < 0)) * (n);          i += (1)) {       for (j = ((i % 2));            (((2) > 0) - ((2) < 0)) * j < (((2) > 0) - ((2) < 0)) * (n);            j += (2)) {         val = diagl[i + j] + diagr[n - (i - j) - 1] - a[i][j];         if (val >= max1) {           max1 = val;           x1 = i + 1;           y1 = j + 1;         }       }       for (j = ((1 - (i % 2)));            (((2) > 0) - ((2) < 0)) * j < (((2) > 0) - ((2) < 0)) * (n);            j += (2)) {         val = diagl[i + j] + diagr[n - (i - j) - 1] - a[i][j];         if (val >= max2) {           max2 = val;           x2 = i + 1;           y2 = j + 1;         }       }     }     printf( %I64 d n%d %d %d %d , max1 + max2, x1, y1, x2, y2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 260;   const int MOD = 1000000000 + 7;   int n, m, t;   int mid;   int v[N], mod[N];   bool g[10][N];   long long ans, f[10];   inline void Dfs(int k, int d) {     ans += f[d];     if (ans > MOD) ans -= MOD;     if (d > 5) return;     for (int i = k; i <= mid; i++)       if (v[i] && !g[d][i] && !g[d][m - i]) {         if (d < 5) {           for (int j = 0; j < m; j++) g[d + 1][j] = g[d][j];           for (int j = 0; j < m; j++)             if (g[d][j]) g[d + 1][mod[j + i]] = g[d + 1][mod[j + m - i]] = 1;         }         f[d + 1] = (f[d] * v[i]) % MOD;         Dfs(i + 1, d + 1);       }   }   int main() {     int x;     for (scanf( %d %d %d , &n, &m, &t); t--; v[((x) < (m - x) ? (x) : (m - x))]++)       scanf( %d , &x), x %= m;     mid = m / 2;     for (int i = 0; i <= 2 * m; i++) mod[i] = i % m;     f[0] = 1;     g[0][0] = 1;     Dfs(1, 0);     printf( %d n , (int)ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, h, l, r;   int a[2000 + 5], dp[2000 + 5][2000 + 5];   int solve(int curr, int idx) {     if (idx == n) {       if (l <= curr && curr <= r)         return 1;       else         return 0;     }     if (dp[curr][idx] != -1) return dp[curr][idx];     int p1 = 0, p2 = 0;     if (l <= curr && curr <= r) {       p1 = 1;       p2 = 1;     }     p1 += solve((curr + a[idx]) % h, idx + 1);     p2 += solve((curr + a[idx] - 1) % h, idx + 1);     return dp[curr][idx] = max(p1, p2);   }   int main(int argc, const char* argv[]) {     scanf( %d %d %d %d , &n, &h, &l, &r);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i <= h; i++) {       for (int j = 0; j <= n; j++) {         dp[i][j] = -1;       }     }     int k = solve(a[0], 1);     for (int i = 0; i <= h; i++) {       for (int j = 0; j <= n; j++) {         dp[i][j] = -1;       }     }     k = max(k, solve(a[0] - 1, 1));     printf( %d n , k);     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e6 + 10;   const int maxm = 1e5 + 10;   const long long int mod = 1e9 + 7;   const long long int INF = 1e18 + 100;   const int inf = 0x3f3f3f3f;   const double pi = acos(-1.0);   const double eps = 1e-8;   using namespace std;   int n, m;   int cas, tol, T;   struct Sam {     int node[maxn << 1][27], step[maxn << 1], fa[maxn << 1];     int dp[maxn << 1], tax[maxn << 1], gid[maxn << 1];     int vis[maxn << 1];     int last, sz;     int newnode() {       memset(node[++sz], 0, sizeof node[++sz]);       dp[sz] = step[sz] = fa[sz] = 0;       return sz;     }     void init() {       sz = 0;       last = newnode();     }     void insert(int k) {       int p = last, np = last = newnode();       dp[np] = 1;       step[np] = step[p] + 1;       for (; p && !node[p][k]; p = fa[p]) node[p][k] = np;       if (p == 0) {         fa[np] = 1;       } else {         int q = node[p][k];         if (step[q] == step[p] + 1) {           fa[np] = q;         } else {           int nq = newnode();           memcpy(node[nq], node[q], sizeof(node[q]));           fa[nq] = fa[q];           step[nq] = step[p] + 1;           fa[np] = fa[q] = nq;           for (; p && node[p][k] == q; p = fa[p]) node[p][k] = nq;         }       }     }     void handle() {       for (int i = 0; i <= sz; i++) tax[i] = 0;       for (int i = 1; i <= sz; i++) tax[step[i]]++;       for (int i = 1; i <= sz; i++) tax[i] += tax[i - 1];       for (int i = 1; i <= sz; i++) gid[tax[step[i]]--] = i;       for (int i = sz; i >= 1; i--) {         int u = gid[i];         dp[fa[u]] += dp[u];       }     }     int solve(char *s, int len, int id) {       int p = 1, res = 0;       int ans = 0;       for (int i = 1; i <= len + len; i++) {         int k = s[i] -  a  + 1;         while (p && !node[p][k]) {           p = fa[p];           res = step[p];         }         if (p == 0) {           p = 1;           res = 0;         } else {           p = node[p][k];           res++;           if (res >= len) {             int tmp = p;             while (tmp && vis[tmp] != id &&                    !(step[fa[tmp]] + 1 <= len && len <= step[tmp])) {               vis[tmp] = id;               tmp = fa[tmp];             }             if (vis[tmp] != id) {               vis[tmp] = id;               ans += dp[tmp];             }           }         }       }       return ans;     }   } sam;   char s[maxn], t[maxn << 1];   int main() {     scanf( %s , s + 1);     int slen = strlen(s + 1);     sam.init();     for (int i = 1; i <= slen; i++) {       sam.insert(s[i] -  a  + 1);     }     sam.handle();     scanf( %d , &T);     for (int tt = 1; tt <= T; tt++) {       scanf( %s , t + 1);       int tlen = strlen(t + 1);       for (int i = 1; i <= tlen; i++) {         t[i + tlen] = t[i];       }       t[tlen + tlen + 1] =   0 ;       int ans = sam.solve(t, tlen, tt);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   string tostring(T x) {     ostringstream out;     out << x;     return out.str();   }   long long toint(string s) {     istringstream in(s);     long long x;     in >> x;     return x;   }   int main() {     int n, x, tmp;     cin >> n >> x;     int ans = 0, zero = 0;     for (int i = 0; i < n; i++) {       cin >> tmp;       ans += tmp;     }     if (ans == 0) {       cout << 0 << endl;     } else {       ans = ((ans < 0) ? -(ans) : (ans));       if (ans % x == 0) {         cout << ans / x << endl;       } else {         cout << ans / x + 1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void past_code();   vector<int> v[100005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int x, y, j, k;     cin >> x;     for (int i = 1; i <= 2 * x; i++) {       cin >> y;       v[y].push_back(i);     }     long long sum = 0;     j = 1;     k = 1;     for (int i = 1; i <= x; i++) {       sum += abs(j - v[i][0]) + abs(k - v[i][1]);       j = v[i][0];       k = v[i][1];     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n = 0, s = 0, d = 0;   map<int, vector<int> > graph;   map<int, bool> vis;   bool stop = false;   void Dfs(int m) {     if (m == d || stop) {       cout << m;       stop = true;       return;     }     cout << m <<    ;     for (int i = 0; i < (int)graph[m].size(); i++) {       if (vis.find(graph[m][i]) == vis.end()) {         vis[graph[m][i]] = true;         Dfs(graph[m][i]);       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s >> d;       graph[s].push_back(d);       graph[d].push_back(s);     }     vector<int> k;     for (map<int, vector<int> >::iterator it = graph.begin(); it != graph.end();          it++) {       if ((*it).second.size() == 1) {         k.push_back((*it).first);       }     }     s = k[0], d = k[1];     vis[s] = true;     Dfs(s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(51);   const long long INF = 1e18;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       vector<vector<long long>> a(n, vector<long long>(m)),           pr(n, vector<long long>(m));       vector<vector<long long>> mn(m, vector<long long>(m, INF));       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           char c;           cin >> c;           a[i][j] = c -  0 ;           pr[i][j] = a[i][j] + (j > 0 ? pr[i][j - 1] : 0);         }       }       long long anss = 1e18;       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           for (long long jj = j; jj < m; jj++) {             if (jj - j + 1 >= 4) {               if (i > 0)                 mn[j][jj] += (a[i - 1][j] ^ 1) + (a[i - 1][jj] ^ 1) +                              (pr[i - 1][jj - 1] - pr[i - 1][j]);               if (i - 4 >= 0) {                 long long add = 0;                 for (long long f = i - 3; f < i; f++) {                   add +=                       (a[f][j] ^ 1) + (a[f][jj] ^ 1) + (pr[f][jj - 1] - pr[f][j]);                 }                 mn[j][jj] =                     min(mn[j][jj],                         (jj - j - 1) - (pr[i - 4][jj - 1] - pr[i - 4][j]) + add);               }               anss = min(anss,                          mn[j][jj] + (jj - j - 1) - (pr[i][jj - 1] - pr[i][j]));             }           }         }       }       cout << anss << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e6 + 7;   inline long long stoll(char* s, const long long& p, bool f = 0) {     long long a = 0;     bool fl = 0;     for (; *s; ++s) a = a * 10 + *s - 48, fl |= (f && a >= p), a %= p;     return a + fl * p;   }   long long b, n, p, phi;   char B[M], N[M];   inline long long fp(long long x, long long k, const long long& p) {     long long r = 1;     for (; k; k >>= 1, x = x * x % p)       if (k & 1) r = r * x % p;     return r;   }   inline long long getphi(long long n) {     long long r = n, nn = n;     for (long long i = 2; i * i <= nn; ++i)       if (!(n % i))         for (r = r / i * (i - 1); !(n % i); n /= i)           ;     if (n > 1) r = r / n * (n - 1);     return r;   }   inline long long mod(long long x, long long p) {     return x < p ? x : x % p + p;   };   inline char* N_1() {     register int s = strlen(N) - 1;     for (; s >= 0 && !N[s]; --s)       ;     --N[s];     return N;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     cin >> B >> N >> p;     phi = getphi(p);     b = stoll(B, p), n = stoll(N_1(), phi, 1);     long long ans = ((b - 1) * fp(b, n, p) + p) % p;     if (!(ans)) ans = p;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d;     pair<int, int> friendlist[100001];     scanf( %d%d , &n, &d);     for (int i = 0; i < n; i++)       scanf( %d%d , &((friendlist + i)->first), &((friendlist + i)->second));     sort(friendlist, friendlist + n);     int it_left = 0;     int it_right = 0;     long long maxfriendship = 0;     long long friendship = 0;     while (true) {       while (it_right < n &&              friendlist[it_right].first - friendlist[it_left].first < d) {         friendship += friendlist[it_right].second;         maxfriendship = max(maxfriendship, friendship);         it_right++;       }       if (it_right >= n) break;       while (friendlist[it_right].first - friendlist[it_left].first >= d) {         friendship -= friendlist[it_left].second;         it_left++;       }     }     cout << maxfriendship;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100 * 1000 + 7, MAX_LOG = 20;   int n, m;   vector<int> adj[MAXN];   bool isPassed[MAXN];   int par[MAXN][MAX_LOG], cycleCnt[MAXN], h[MAXN];   void pre(int root) {     isPassed[root] = true;     for (auto nei : adj[root]) {       if (!isPassed[nei]) {         par[nei][0] = root;         h[nei] = h[root] + 1;         pre(nei);         cycleCnt[root] = cycleCnt[root] + cycleCnt[nei];       } else if (par[root][0] != nei and h[nei] < h[root]) {         cycleCnt[nei]--;         cycleCnt[root]++;       }     }     return;   }   int nanCyCnt[MAXN][MAX_LOG];   void dfs(int root, int lstRt) {     par[root][0] = lstRt;     if (lstRt != -1 and cycleCnt[root] == 0) nanCyCnt[root][0] = 1;     for (int i = 1; i < MAX_LOG and h[root] >= (1 << i) and                     par[par[root][i - 1]][i - 1] != -1;          ++i) {       par[root][i] = par[par[root][i - 1]][i - 1];       nanCyCnt[root][i] =           nanCyCnt[root][i - 1] + nanCyCnt[par[root][i - 1]][i - 1];     }     isPassed[root] = true;     for (auto nei : adj[root]) {       if (!isPassed[nei]) {         h[nei] = h[root] + 1;         dfs(nei, root);       }     }     return;   }   int lca(int v, int u) {     int nancy = 0;     if (h[v] > h[u]) swap(v, u);     for (int i = 0; h[v] ^ h[u]; ++i) {       if ((h[u] - h[v]) & (1 << i)) {         nancy += nanCyCnt[u][i];         u = par[u][i];       }     }     if (v == u) return nancy;     for (int i = MAX_LOG - 1; i >= 0; --i) {       if (par[v][i] != par[u][i]) {         nancy += (nanCyCnt[v][i] + nanCyCnt[u][i]);         v = par[v][i], u = par[u][i];       }     }     return nancy + nanCyCnt[v][0] + nanCyCnt[u][0];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     memset(par, -1, sizeof par);     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int v, u;       cin >> v >> u, v--, u--;       adj[v].push_back(u);       adj[u].push_back(v);     }     memset(isPassed, 0, sizeof isPassed);     pre(0);     memset(isPassed, 0, sizeof isPassed);     memset(h, 0, sizeof h);     dfs(0, -1);     int mercsNum;     cin >> mercsNum;     while (mercsNum--) {       int sh, wa;       cin >> wa >> sh, wa--, sh--;       cout << lca(wa, sh) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool isPrime(uint64_t n) {     if (n == 1) return false;     if (n == 2) return true;     if (n % 2 == 0) return false;     for (uint64_t div = 3; div * div <= n; div += 2)       if (n % div == 0) return false;     return true;   }   inline std::vector<uint64_t> generatePrimesUntil(uint64_t upperBound) {     if (upperBound < 2) return {};     std::vector<uint64_t> result = {2};     for (uint64_t candidate = 3; candidate <= upperBound; candidate += 2) {       bool divisibleByPrevious = false;       for (size_t divisorIndex = 1;            divisorIndex < result.size() &&            result[divisorIndex] * result[divisorIndex] <= candidate;            ++divisorIndex) {         if (candidate % result[divisorIndex] == 0) {           divisibleByPrevious = true;           break;         }       }       if (!divisibleByPrevious) result.push_back(candidate);     }     return result;   }   inline std::vector<std::pair<uint64_t, uint64_t>> decomposeToPrimePowers(       uint64_t n) {     std::vector<std::pair<uint64_t, uint64_t>> result;     auto tryPrime = [&](uint64_t p) {       if (n % p == 0) {         result.push_back({p, 0});         while (n % p == 0) {           n /= p;           result.back().second++;         }       }     };     tryPrime(2);     for (uint64_t primeCandidate = 3; primeCandidate * primeCandidate <= n;          primeCandidate += 2)       tryPrime(primeCandidate);     if (n > 1) result.push_back({n, 1});     return result;   }   template <typename T>   void printVector(const std::vector<T>& out, std::string delimiter =    ) {     if (out.empty()) return;     for (size_t i = 0; i < out.size() - 1; ++i) std::cout << out[i] << delimiter;     std::cout << out.back();   }   class Solver576A {    public:     void run();   };   void Solver576A::run() {     int n;     cin >> n;     auto primes = generatePrimesUntil(n);     vector<int> ans;     for (auto p : primes)       for (int pp = p; pp <= n; pp *= p) ans.push_back(pp);     cout << ans.size() <<   n ;     printVector(ans);   }   using CurrentSolver = Solver576A;   int main() {     ios::sync_with_stdio(false);     CurrentSolver().run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1000000007, BIG = 0x3f3f3f3f;   long long p, k;   inline long long Mod(long long x, long long m) {     x %= m;     if (x < 0) x += m;     return x;   }   vector<long long> Process() {     vector<long long> ans;     while (p) {       const long long m = Mod(p, k);       ans.push_back(m);       p -= m;       p /= -k;     }     return ans;   }   void Output() {     const vector<long long> ans = Process();     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     while (cin >> p) {       cin >> k;       Output();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pow1(long long b, long long expo, long long md) {     if (!expo) return 1;     long long tmp = pow1(b, expo / 2, md);     tmp = (tmp * tmp) % md;     if (expo % 2) tmp = (tmp * b) % md;     return tmp;   }   long long w[1000005] = {};   long long apow[1000005] = {};   long long inv[1000005] = {};   vector<long long> v[1000005] = {};   long long get(long long a, long long m1, long long b, long long m2,                 long long x) {     long long ret = 0;     long long first, k;     k = (a - b) * inv[m2];     k %= m1;     k += m1;     k %= m1;     first = k * m2 + b;     first %= m1 * m2;     x -= first;     if (x >= 0) {       ret += 1 + x / (m1 * m2);     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(15);     long long md, a, b, x, per;     long long ans = 0;     cin >> a >> b >> md >> x;     apow[0] = 1;     v[1].push_back(0);     if (a != 1) {       apow[1] = a;       v[a].push_back(1);       for (long long f = (long long)2; f <= (long long)md; ++f) {         apow[f] = a * apow[f - 1];         apow[f] %= md;         if (apow[f] == 1) {           per = f;           break;         }         v[apow[f]].push_back(f);       }     } else {       per = 1;     }     for (long long f = (long long)1; f <= (long long)md - 1; ++f) {       inv[f] = pow1(f, md - 2, md);     }     for (long long f = (long long)1; f <= (long long)md - 1; ++f) {       w[f] = (inv[f] * b) % md;     }     for (long long f = (long long)1; f <= (long long)md - 1; ++f) {       if ((long)v[w[f]].size()) {         for (long long ff = (long long)0;              ff <= (long long)(long)v[w[f]].size() - 1; ++ff) {           ans += get(f, md, v[w[f]][ff], per, x);         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, long long> x, y;   map<pair<int, int>, long long> xy;   int main() {     int n;     cin >> n;     while (n--) {       int xx, yy;       cin >> xx >> yy;       x[xx]++;       y[yy]++;       xy[make_pair(xx, yy)]++;     }     long long ans = 0;     for (auto i : x) {       ans += i.second * (i.second - 1) / 2;     }     for (auto i : y) {       ans += i.second * (i.second - 1) / 2;     }     for (auto i : xy) {       ans -= i.second * (i.second - 1) / 2;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, ans;   int main() {     cin >> a >> b >> c;     long long pom = min(a, b);     a -= pom;     b -= pom;     ans = c + pom;     ans <<= 1;     if (a or b) ans++;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   vector<long long> v;   int main() {     if (fopen( a.txt ,  r )) {       freopen( a.txt ,  r , stdin);     } else       ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     if (k >= 1e6) {       cout << -1;       exit(0);     };     long long temp = (k * (k + 1)) / 2;     if (k == 1) {       cout << n;       return 0;     }     if (n == 1) {       cout << -1;       exit(0);     };     if (temp > n) {       cout << -1;       exit(0);     };     for (long long i = 1; i * i <= n; i++) {       if (n % i != 0) continue;       v.push_back(i);       if (i * i < n) v.push_back(n / i);     }     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     for (long long x : v) {       long long a = n / x;       if (a >= temp) {         for (int i = 1; i <= k - 1; i++) cout << i * x <<    , a -= i;         cout << a * x;         return 0;       }     }   }


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


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


#include <bits/stdc++.h>   using namespace std;   int n, m, u, v, s, to[5010], ne[5010], pre[5010], cnt, dfn[5010], low[5010],       len, f[5010], ss, q[5010], w[5010], d[5010], dis[5010];   bool us[5010];   void add(int u, int v) {     to[++s] = v;     ne[s] = pre[u];     pre[u] = s;   }   void dfs(int k) {     dfn[k] = low[k] = ++cnt;     f[++len] = k;     us[k] = true;     for (int i = pre[k]; i; i = ne[i]) {       int x = to[i];       if (!dfn[x]) {         dfs(x);         low[k] = min(low[k], low[x]);       } else if (us[x])         low[k] = min(low[k], low[x]);     }     if (dfn[k] == low[k]) {       ss++;       while (f[len] != k) us[f[len]] = false, w[ss]++, q[f[len--]] = ss;       us[k] = false;       w[ss]++;       q[f[len--]] = ss;     }   }   int bfs(int k, int u) {     int h = 0, t = 1;     f[1] = u;     memset(dis, 0, sizeof(dis));     while (h < t) {       int x = f[++h];       for (int i = pre[x]; i; i = ne[i])         if (q[to[i]] == k && !dis[to[i]]) {           int o = to[i];           dis[o] = dis[x] + 1;           if (o == u) return dis[o];           f[++t] = o;         }     }   }   int get(int k) {     if (w[k] == 1) return 0;     int mn = 1e9;     for (int i = 1; i <= n; i++)       if (q[i] == k) mn = min(mn, bfs(k, i));     return 1 + 998 * mn;   }   int main() {     cin >> n >> m;     while (m--) scanf( %d%d , &u, &v), add(u, v);     for (int i = 1; i <= n; i++)       if (!dfn[i]) dfs(i);     for (int i = 1; i <= n; i++)       for (int j = pre[i]; j; j = ne[j])         if (q[i] != q[to[j]]) d[q[i]]++;     int ans = n;     for (int i = 1; i <= ss; i++)       if (!d[i]) ans += get(i);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int L = 2e5;   vector<vector<int>> g(L);   vector<int> dp(L), d(L, 0), par(L, -1);   vector<bool> used(L);   int dist;   int ask1(int u) {     cout <<  d   << u + 1 << endl;     int t;     cin >> t;     if (t == -1) {       while (true) {       }     }     return t;   }   int ask2(int u) {     cout <<  s   << u + 1 << endl;     int t;     cin >> t;     --t;     if (t == -1) {       while (true) {       }     }     return t;   }   void dfs(int u) {     used[u] = true;     for (auto v : g[u]) {       if (!used[v]) {         d[v] = d[u] + 1;         par[v] = u;         dfs(v);         dp[u] += dp[v];       }     }     if (d[u] == dist) dp[u]++;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       --u;       --v;       g[u].push_back(v);       g[v].push_back(u);     }     dist = ask1(0);     dfs(0);     int q = 0;     if (dist == 0) {       cout <<  ! 1  << endl;       return 0;     } else {       while (d[q] != dist) {         int r = q;         while (d[r] != dist) {           int t = -1;           int ma = 0;           for (auto v : g[r]) {             if (d[v] > d[r]) {               if (dp[v] > ma) {                 ma = dp[v];                 t = v;               }             }           }           r = t;         }         int newdist = ask1(r);         if (newdist == 0) {           cout <<  !   << r + 1 << endl;           return 0;         } else {           while (d[r] * 2 != 2 * dist - newdist) {             r = par[r];           }           q = ask2(r);         }       }       cout <<  !   << q + 1 << endl;       return 0;     }     cin >> n;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const long long LINF = 1e18;   const int MOD = 1e9 + 7;   void test() {}   const int MAX = 1 << 19;   int tree[2 * MAX], prefMax[2 * MAX];   int killed = 0, m, n;   vector<pair<int, int> > play;   int query(int x, int y) {     int res = -1;     for (x += MAX, y += MAX; x < y; x >>= 1, y >>= 1) {       if (x & 1) res = max(tree[x++], res);       if (y & 1) res = max(tree[--y], res);     }     return res;   }   void update1(int x) {     x += MAX;     while (x) {       tree[x] = 0;       x /= 2;     }   }   void update2(int x, int val) {     x += MAX;     while (x) {       tree[x] = max(tree[x], val);       x /= 2;     }   }   bool check(int x) {     int l = -1, r = m;     int maks = query(killed, killed + x + 1);     while (l + 1 < r) {       int mid = (l + r) / 2;       if (play[mid].first >= maks)         r = mid;       else         l = mid;     }     if (r == m) return false;     if (prefMax[r] > x)       return true;     else       return false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       cin >> n;       for (int i = 0; i <= n; i++) update1(i);       for (int i = 0; i < n; i++) {         int x;         cin >> x;         update2(i, x);       }       cin >> m;       play = vector<pair<int, int> >(m);       for (int i = 0; i < m; i++) cin >> play[i].first >> play[i].second;       sort(play.begin(), play.end());       for (int i = m; i >= 0; i--) prefMax[i] = 0;       for (int i = m - 1; i >= 0; i--)         prefMax[i] = max(prefMax[i + 1], play[i].second);       killed = 0;       int res = 0;       bool czy = true;       while (killed < n) {         int l = -1, r = n - killed + 1;         while (l + 1 < r) {           int mid = (l + r) / 2;           if (check(mid))             l = mid;           else             r = mid;         }         if (l == -1) {           cout << -1 <<   n ;           czy = false;           break;         }         killed += (l + 1);         res++;       }       if (czy) cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 6e6;   const long long inf = 1e18;   const long long maxv = 2e8 + 100;   const int miniinf = 1e9;   const long double eps = 1e-6;   const long long flowconst = 1e9;   void init() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     init();     int n;     cin >> n;     vector<string> ans1;     vector<string> ans2;     if (n == 1) {       cout <<  ? 1 1  << endl;       string ans;       cin >> ans;       cout <<  !   << ans << endl;       return 0;     }     map<string, int> foo;     cout <<  ? 1   << n << endl;     for (int i = 0; i < n * (n + 1) / 2; i++) {       string x;       cin >> x;       sort(x.begin(), x.end());       foo[x]++;     }     cout <<  ? 1   << n - 1 << endl;     for (int i = 0; i < (n - 1) * (n) / 2; i++) {       string x;       cin >> x;       sort(x.begin(), x.end());       foo[x]--;     }     vector<string> diff;     for (auto ev : foo)       if (ev.second == 1) diff.push_back(ev.first);     sort(diff.begin(), diff.end(),          [&](string &a, string &b) { return a.size() < b.size(); });     string ans;     for (int i = 0; i < n; i++) {       vector<int> cnt(26, 0);       for (int j = 0; j < diff[i].size(); j++) {         cnt[diff[i][j] -  a ]++;       }       for (int j = 0; j < ans.size(); j++) {         cnt[ans[j] -  a ]--;       }       for (int j = 0; j < 26; j++) {         if (cnt[j] > 0) {           ans +=  a  + j;         }       }     }     reverse(ans.begin(), ans.end());     cout <<  !  ;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   const int M = 26;   const long long mod = 1e9 + 7;   const long long MOD = 998244353;   const int P = 1336;   const long double eps = 0.000000001;   const long long inf = 1e18 + 7;   mt19937 gen(time(0));   vector<int> g[N];   int sz[N], a[N];   int ask(int v, int u) {     cout <<  ?   << v + 1 <<     << u + 1 << endl;     int c;     cin >> c;     if (c == -1) exit(0);     c--;     return c;   }   void answ(int v) {     cout <<  !   << v + 1 << endl;     exit(0);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       g[x].push_back(y);       g[y].push_back(x);       sz[x]++;       sz[y]++;     }     for (int i = 0; i < n; i++) {       a[i] = 1;     }     int can = n / 2;     while (can--) {       int v = -1, u = -1;       for (int i = 0; i < n; i++) {         if (sz[i] == 1) {           if (v == -1)             v = i;           else if (u == -1)             u = i;         }       }       for (auto to : g[v]) {         sz[to]--;       }       for (auto to : g[u]) {         sz[to]--;       }       sz[v]--;       sz[u]--;       int c = ask(v, u);       if (c == v) answ(v);       if (c == u) answ(u);       a[v] = 0;       a[u] = 0;     }     for (int i = 0; i < n; i++) {       if (a[i]) answ(i);     }   }


#include <bits/stdc++.h>   using namespace std;   bool changed[222222];   pair<int, int> edges[222222];   set<pair<int, int> > adj[111111];   bool visited[111111];   deque<pair<int, int> > to_connect;   vector<int> Harry;   char outbuf[12000000];   char* out = outbuf;   int moves = 0;   void flush_harry() {     out += sprintf(out,  %d , Harry.size());     for (int v : Harry) {       out += sprintf(out,   %d , v);     }     out += sprintf(out,   n );     Harry.clear();   }   void move_stairs(int id, int u, int v) {     ++moves;     flush_harry();     adj[edges[id].first].erase(pair<int, int>(edges[id].second, id));     adj[edges[id].second].erase(pair<int, int>(edges[id].first, id));     edges[id].first = u, edges[id].second = v;     adj[u].insert(pair<int, int>(v, id));     adj[v].insert(pair<int, int>(u, id));     out += sprintf(out,  %d %d %d n , id, u, v);     changed[id] = true;   }   vector<int> in_component;   bool dfs1(int v, int parent_edge, bool connected_to_one) {     visited[v] = true;     in_component.push_back(v);     int edge_to_move = -1;     for (auto& p : adj[v]) {       if (!connected_to_one && visited[p.first] && parent_edge != p.second) {         edge_to_move = p.second;         connected_to_one = true;       } else if (!visited[p.first]) {         connected_to_one = dfs1(p.first, p.second, connected_to_one);       }     }     if (edge_to_move != -1) {       move_stairs(edge_to_move, v, 1);     }     return connected_to_one;   }   void dfs2(int v, int parent_edge) {     if (v != 1) Harry.push_back(v);     visited[v] = true;     for (auto& p : adj[v])       if (!visited[p.first]) {         dfs2(p.first, p.second);         Harry.push_back(v);       }     vector<pair<int, int> > to_change;     for (auto& p : adj[v])       if (p.second != parent_edge && !to_connect.empty() && !changed[p.second]) {         to_change.push_back(pair<int, int>(p.second, to_connect.back().second));         to_connect.pop_back();       }     for (auto& p : to_change) {       move_stairs(p.first, v, p.second);       dfs2(p.second, p.first);       Harry.push_back(v);     }   }   int main() {     int N, M;     scanf( %d%d , &N, &M);     for (int i = (0); i < (M); ++i) {       int a, b;       scanf( %d%d , &a, &b);       adj[a].insert(pair<int, int>(b, i + 1));       adj[b].insert(pair<int, int>(a, i + 1));       edges[i + 1].first = a, edges[i + 1].second = b;     }     Harry.push_back(1);     for (int i = (1); i < (N + 1); ++i)       if (!visited[i]) {         bool connected_to_one = dfs1(i, -1, i == 1);         if (!connected_to_one) {           for (int& v : in_component)             if (adj[v].size() <= 1) {               to_connect.push_back(pair<int, int>(in_component.size(), v));               break;             }         }         in_component.clear();       }     sort(to_connect.begin(), to_connect.end());     for (int i = (1); i < (N + 1); ++i) visited[i] = false;     dfs2(1, -1);     while (!to_connect.empty() && to_connect.back().first > 1) {       int v = to_connect.back().second;       to_connect.push_front(pair<int, int>(1, v));       to_connect.pop_back();       pair<int, int> n = *adj[v].begin();       move_stairs(n.second, 1, n.first);       dfs2(n.first, n.second);       Harry.push_back(1);     }     flush_harry();     if (!to_connect.empty()) {       printf( NO n );       return 0;     }     printf( YES n%d n , moves);     printf( %s n , outbuf);   }


/*    *    * Author: Hsueh-    * Date: 2021-03-05 21:34:52    *    * */      #include <bits/stdc++.h>      using namespace std;      #define dbg(x...)                                                                     do {                                                                                cout << #x <<   ->  ;                                                             err(x);                                                                       } while (0)   void err() { cout << endl; }   template <class T, class... Ts> void err(const T &arg, const Ts &...args) {       cout << arg <<    ;       err(args...);   }      #define endl   n    #define all(A) A.begin(), A.end()   using ll = long long;   using db = double;   using pII = pair<int, int>;      const int INF = 0x3f3f3f3f;   const ll INFLL = 0x3f3f3f3f3f3f3f3f;   const int N = 2e5 + 10;      int n, m;   int a[N];      void RUN() {       cin >> n >> m;       map<ll, int> mp;       ll sum = 0, Max = 0;       for (int i = 1; i <= n; ++i) {           cin >> a[i];           sum += a[i];           if (sum > Max) {               Max = sum;               mp[sum] = i;           }       }       for (int i = 1; i <= m; ++i) {           ll x;           cin >> x;           auto it = mp.lower_bound(x);           ll res = 0;           if (it == mp.end() && sum <= 0) {               res = -1;           } else if (it == mp.end()) {               auto last = mp.end();               last--;               ll need = (x - last->first + sum - 1) / sum;               it = mp.lower_bound(x - need * sum);               res = it->second + need * n - 1;           } else {               res = it->second - 1;           }           cout << res <<    n [i == m];       }   }      int main() {       ios::sync_with_stdio(false);       cin.tie(nullptr), cout.tie(nullptr);       cout << fixed << setprecision(20);          int T;       cin >> T;       while (T--) {           RUN();       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)2e18 + 5;   const long long MOD = (long long)1e9 + 7;   const int MAXN = 55;   long long mul(long long x, long long y) {     if (x == 0) return 0;     if (y > INF / x) return INF;     return x * y;   }   long long add(long long x, long long y) { return min(x + y, INF); }   long long n, k, cnt[MAXN], fac[MAXN], dp[MAXN][MAXN], way[MAXN];   vector<int> get_p(long long len, long long k) {     vector<int> res, from(len + 1);     res.push_back(len);     for (int i = 1; i <= len; i++) {       from[i] = i;     }     for (int i = 2; i <= len; i++) {       int src = i;       while (from[src] != src) {         src = from[src];       }       int tgt = k / fac[max(0LL, len - i - 1)];       for (int j = 1; j <= len; j++) {         if (j != src && from[j] == j) {           if (tgt == 0) {             from[j] = i;             res.push_back(j);             break;           } else {             tgt--;           }         }       }       k %= fac[max(0LL, len - i - 1)];     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     fac[0] = cnt[0] = cnt[1] = 1;     for (int i = 1; i < MAXN; i++) fac[i] = mul(fac[i - 1], i);     for (int i = 2; i < MAXN; i++) cnt[i] = fac[i - 2];     dp[0][0] = way[0] = 1;     for (int i = 1; i < MAXN; i++) {       for (int j = 1; j <= i; j++) {         for (int k = 1; k <= i; k++) {           dp[i][j] = add(dp[i][j], mul(dp[i - k][j - 1], cnt[k]));         }         way[i] = add(way[i], dp[i][j]);       }     }     int tc;     cin >> tc;     while (tc--) {       cin >> n >> k;       k--;       if (k >= way[n]) {         cout <<  -1 n ;         continue;       }       int put = 0;       vector<int> ans;       while (put < n) {         long long seg, rem = n - put;         for (seg = 1; seg <= rem; seg++) {           long long tot = mul(cnt[seg], way[rem - seg]);           if (k >= tot) {             k -= tot;           } else {             break;           }         }         vector<int> p = get_p(seg, k / way[rem - seg]);         k %= way[rem - seg];         for (int x : p) {           ans.push_back(x + put);         }         put += seg;       }       for (int x : ans) {         cout << x <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1025;   long long p[MAX_N];   int main() {     long long s, n, sum = 0;     scanf( %lld%lld , &n, &s);     for (int i = 1; i <= n; ++i) scanf( %lld , &p[i]), sum += p[i];     sort(p + 1, p + 1 + n);     if (sum < s) {       printf( -1 n );       return 0;     } else {       long long tmp = 0;       for (int i = 1; i <= n; ++i) {         tmp += p[i] - p[1];       }       if (tmp >= s) {         printf( %lld n , p[1]);         return 0;       } else {         tmp = s - tmp;         long long k = (tmp - 1) / n + 1;         printf( %lld n , p[1] - k);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int d1 = 7;   const int d2 = 29;   const int mod = 19260817;   char str[200005];   bool h1[mod + 5], h2[mod + 5];   int r1[200005], r2[200005];   int kf[4][200005];   int check[200005][2];   int fff[200005];   bool ban[200005];   int main() {     int t, n;     scanf( %d , &t);     while (t--) {       int cnt[4] = {0, 0, 0, 0};       int move[4] = {0, 0, 0, 0};       int n, len;       scanf( %d , &n);       int p1 = 0, p2 = 0;       for (int i = 1; i <= n; i++) {         scanf( %s , str + 1);         int len = strlen(str + 1);         int type = (str[1] -  0 ) * 2 + str[len] -  0 ;         kf[type][++cnt[type]] = i;         fff[i] = type;         int a = 0, b = 0;         for (int j = 1; j <= len; j++) {           a = (a * d1 + str[j] - 5) % mod;           b = (b * d2 + str[j] - 19) % mod;         }         h1[r1[++p1] = a] = h2[r2[++p2] = b] = 1;         a = b = 0;         for (int j = len; j >= 1; j--) {           a = (a * d1 + str[j] - 5) % mod;           b = (b * d2 + str[j] - 19) % mod;         }         check[i][0] = a;         check[i][1] = b;       }       for (int i = 1; i <= n; i++)         if (h1[check[i][0]] && h2[check[i][1]])           ban[i] = 1;         else           move[fff[i]]++;       if (cnt[1] == 0 && cnt[2] == 0 && cnt[3] != 0 && cnt[0] != 0)         printf( -1 n );       else if (cnt[1] == cnt[2])         printf( 0 n n );       else if (cnt[1] > cnt[2]) {         int ned = (cnt[1] - cnt[2]) / 2;         if (ned > move[1])           printf( -1 n );         else {           printf( %d n , ned);           for (int i = 1, ooo = 1; ooo <= ned; ooo++, i++) {             while (ban[kf[1][i]]) {               i++;             }             printf( %d  , kf[1][i]);           }           printf(  n );         }       } else if (cnt[2] > cnt[1]) {         int ned = (cnt[2] - cnt[1]) / 2;         if (ned > move[2])           printf( -1 n );         else {           printf( %d n , ned);           for (int i = 1, ooo = 1; ooo <= ned; ooo++, i++) {             while (ban[kf[2][i]]) {               i++;             }             printf( %d  , kf[2][i]);           }           printf(  n );         }       }       for (int i = 1; i <= n; i++) ban[i] = 0;       for (int i = 1; i <= p1; i++) h1[r1[i]] = 0;       for (int i = 1; i <= p2; i++) h2[r2[i]] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = 3.1415926535897932384626433;   const long long inf = 1e18;   const long long mod = 1e9 + 7;   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     vector<vector<long long> > cnt(100001);     long long me = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i];       cnt[a[i]].push_back(i);       me = max(me, a[i]);     }     bool ok = 1;     for (long long i = me; i >= 2; i--) {       if (cnt[i].size() > cnt[i - 1].size()) {         ok = 0;         break;       }     }     if (!ok) {       cout <<  -1 n ;       return;     }     vector<long long> ans(n);     long long c = 0;     for (long long i = 1; i <= me; i++) {       long long k = 1;       for (auto x : cnt[i]) {         ans[x] = k;         k++;         c = max(c, k);       }     }     cout << c - 1 <<   n ;     for (long long i = 0; i < n; i++) {       cout << ans[i] <<    ;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e5 + 5;   const long long mod = 1e9 + 7;   void solve() {     long long n;     cin >> n;     if (n == 1) {       cout << -1 << endl;       return;     }     cout << n <<     << n + 1 <<     << n * (n + 1) << endl;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, c0, c1, h;       cin >> n >> c0 >> c1 >> h;       string binary;       cin >> binary;       int count0 = 0, count1 = 0;       int ans = 0, arbans = 0;       for (auto i = 0; i < binary.length(); i++) {         if (binary[i] ==  1 ) count1++;         if (binary[i] ==  0 ) count0++;       }       arbans = count1 * c1 + count0 * c0;       if (c1 > c0 && c1 > h) {         ans = count1 * h + count0 * c0 + count1 * c0;       } else if (c1 < c0 && c0 > h) {         ans = count0 * h + count1 * c1 + count0 * c1;       } else {         ans = count0 * c0 + count1 * c1;       }       ans = (ans < arbans) ? ans : arbans;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000000 + 7;   long long int n, k, summ, tr, a[1000000], minn, maxx;   string s, s1;   map<int, int> been;   int compare(const void* a, const void* b) {     return ((*(long long int*)a) - (*(long long int*)b));   };   int gcd(int a, int b) {     while (b != 0) {       int t = b;       b = a % b;       a = t;     }     return a;   }   int NOK(int n1, int n2) { return n1 * n2 / gcd(n1, n2); }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       been.clear();       maxx =  a  - 1;       minn =  z  + 1;       cin >> s;       for (int j = 0; j < s.length(); j++) {         if (been[s[j]] == 1) {           minn = -1000;           break;         }         been[s[j]] = 1;         minn = min(minn, (long long int)s[j]);         maxx = max(maxx, (long long int)s[j]);       }       if (maxx - minn + 1 != s.length()) {         cout <<  No  << endl;       } else {         cout <<  Yes  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     long long t;     cin >> t;     while (t--) {       int a;       set<int> pr;       cin >> a;       int ar[a];       for (int i = 0; i < a; i++) {         cin >> ar[i];         pr.insert(ar[i]);       }       cout << pr.size() << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxl = 100000;   map<int, int> SG, num;   int n, x, tmp, ans = 0, prime[maxl], tot = 0;   bool notprime[maxl + 1];   inline void init() {     notprime[1] = 1;     for (int i = 2; i <= maxl; i++) {       if (!notprime[i]) prime[++tot] = i;       for (int j = 1; j <= tot && i * prime[j] <= maxl; j++) {         notprime[i * prime[j]] = 1;         if (i % prime[j] == 0) break;       }     }   }   inline int solve(int x) {     if (x == 0) return 0;     if (SG.count(x)) return SG[x];     map<int, int> vis;     int p = x, t = 0;     while (p) {       p /= 2;       t++;     }     for (int i = 1; i <= t; i++)       vis[solve((x >> i) | (x & ((1 << (i - 1)) - 1)))] = 1;     for (int i = 0;; i++)       if (!vis[i]) return SG[x] = i;   }   int main() {     init();     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       for (int pos = 1; prime[pos] * prime[pos] <= x; pos++) {         tmp = 0;         while (x % prime[pos] == 0) {           x /= prime[pos];           tmp++;         }         if (tmp != 0) num[prime[pos]] = num[prime[pos]] | (1 << (tmp - 1));       }       if (x != 1) num[x] |= 1;     }     for (map<int, int>::iterator it = num.begin(); it != num.end(); it++)       ans = ans ^ solve(it->second);     if (ans == 0)       puts( Arpa );     else       puts( Mojtaba );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pi = 1, sum = 0, n, k, a[200100], pos[200100];   const long long inf = (long long)1e19;   int main() {     scanf( %I64d %I64d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);       pos[i] = (a[i - 1] != 1 ? i - 1 : pos[i - 1]);     }     long long ans = 0;     for (long long i = 1; i <= n; i++) {       pi = sum = a[i];       ans += (k == 1);       for (long long now = pos[i], pre = i;; pre = now, now = pos[now]) {         long long mid = pi - sum * (long long)k;         if (mid > 0 && mid % k == 0 && mid / k < pre - now) ans++;         if (!now || inf / a[now] < pi) break;         pi *= (long long)a[now];         sum += a[now] + pre - now - 1;         ans += (pi == sum * (long long)k);       }     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   int n;   unordered_map<int, int> bit[MAX];   unordered_map<int, int> idx;   int inf = 1000000001;   void update(int pos, int t, int val) {     for (; t < inf; t += (t & -t)) {       bit[pos][t] += val;     }   }   int get(int pos, int t) {     int ans = 0;     for (; t > 0; t -= (t & -t)) ans += bit[pos][t];     return ans;   }   int main() {     int type, t, x;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d %d , &type, &t, &x);       if (!idx.count(x)) idx[x] = i;       int cur = idx[x];       if (type == 1) {         update(idx[x], t, 1);       }       if (type == 2) {         update(idx[x], t, -1);       }       if (type == 3) {         int ans = get(idx[x], t);         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   vector<int> graph[MAXN];   int values[MAXN];   long long dp[MAXN];   int leftValue[MAXN];   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       pq;   void dfs(int u, int prev) {     for (int v : graph[u]) {       if (v == prev || values[v] == 0) {         continue;       }       values[v]--;       dfs(v, u);     }     while (!pq.empty()) {       pq.pop();     }     for (int v : graph[u]) {       if (v == prev) {         continue;       }       pq.push({dp[v], v});     }     int currentLeft = values[u];     while (!pq.empty()) {       if (pq.size() <= values[u]) {         dp[u] += pq.top().first + 1;         values[pq.top().second] = leftValue[pq.top().second];         currentLeft--;       }       pq.pop();     }     for (int v : graph[u]) {       if (v == prev || values[v] == 0) {         continue;       }       int pick = min(currentLeft, values[v]);       currentLeft -= pick;       values[v] -= pick;       dp[u] += 2 * pick;     }     dp[u]++;     leftValue[u] = currentLeft;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> values[i];     }     for (int i = 1; i < n; i++) {       int in, out;       cin >> in >> out;       graph[in].push_back(out);       graph[out].push_back(in);     }     int root;     cin >> root;     dfs(root, -1);     cout << dp[root] - 1 << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long logn = 63;   const long long inf = 1e18;   void upd(long long& a, const long long& b) { a = min(a, b); }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<long long> a(n), b(n);     for (int i = 0; i < n; i++) cin >> a[i];     long long mx = *max_element(a.begin(), a.end());     for (int i = 0; i < n; i++) b[i] = mx - a[i];     vector<vector<long long> > dp(logn, vector<long long>(n + 1, inf));     dp[0][0] = 0;     vector<int> s;     for (int i = 0; i < n; i++) s.push_back(i);     for (long long bit = 0; bit + 1 < logn; bit++) {       if (bit) {         vector<int> nws;         for (int i : s)           if (!(b[i] & (1ll << (bit - 1)))) nws.push_back(i);         for (int i : s)           if (b[i] & (1ll << (bit - 1))) nws.push_back(i);         s = nws;       }       vector<long long> pf0(n + 1, 0), pf1(n + 1, 0);       for (int i = 0; i < n; i++) {         pf0[i + 1] = pf0[i], pf1[i + 1] = pf1[i];         if (b[s[i]] & (1ll << bit))           pf1[i + 1]++;         else           pf0[i + 1]++;       }       for (int sf = 0; sf <= n; sf++) {         int b0ones = pf1[n - sf] + (pf0[n] - pf0[n - sf]);         int b0carry = pf1[n] - pf1[n - sf];         upd(dp[bit + 1][b0carry], dp[bit][sf] + b0ones);         int b1ones = pf0[n - sf] + (pf1[n] - pf1[n - sf]);         int b1carry = n - pf0[n - sf];         upd(dp[bit + 1][b1carry], dp[bit][sf] + b1ones);       }     }     cout << dp[logn - 1][0] <<   n ;     return 0;   }


#include <bits/stdc++.h>   struct Edge {     Edge(int _from, int _to) : from(_from), to(_to) {}     int from, to;   };   enum GraphNodeColor { NODE_WHITE, NODE_GREY, NODE_BLACK };   class Graph {    public:     Graph(int nodes, std::vector<Edge> edges)         : neighb(nodes, std::vector<int>()) {       node_count = nodes;       for (int i = 0; i < edges.size(); ++i) {         neighb[edges[i].from].push_back(edges[i].to);       }     }     void dfs() {       timer = 0;       components.resize(node_count, 0);       tout.assign(node_count, 0);       colors.assign(node_count, NODE_WHITE);       nodeExitOrder.clear();       nodeExitOrder.reserve(node_count);       for (int i = 0; i < node_count; ++i) {         if (colors[i] == NODE_WHITE) {           dfs_traverse(i);         }       }     }     void count_components(const std::vector<int>& order) {       int label = 0;       timer = 0;       components.resize(node_count, 0);       tout.assign(node_count, 0);       colors.assign(node_count, NODE_WHITE);       nodeExitOrder.clear();       nodeExitOrder.reserve(node_count);       for (int i = 0; i < node_count; ++i) {         int v = order[i];         if (colors[v] == NODE_WHITE) {           dfs_traverse(v, label++);         }       }     }     void dfs_traverse(int v, int label = 0) {       colors[v] = NODE_GREY;       components[v] = label;       for (int neighbor : neighb[v]) {         if (colors[neighbor] == NODE_WHITE) {           dfs_traverse(neighbor, label);         }       }       colors[v] = NODE_BLACK;       tout[v] = timer++;       nodeExitOrder.push_back(v);     }     int node_count;     int timer;     std::vector<std::vector<int>> neighb;     std::vector<GraphNodeColor> colors;     std::vector<int> tout;     std::vector<int> nodeExitOrder;     std::vector<int> components;   };   int powModulo(int base, int power, int modulo) {     if (power == 0) {       return 1 % modulo;     } else {       long long ans = powModulo(base, power / 2, modulo);       ans = (ans * ans) % modulo;       ans = (power % 2 == 0) ? ans : ans * base % modulo;       return ans;     }   }   int main() {     const int modulo = 1e9 + 7;     int n;     std::cin >> n;     std::vector<Edge> edges, edges_tr;     edges.reserve(n);     edges_tr.reserve(n);     for (int i = 0; i < n; ++i) {       int ai;       std::cin >> ai;       edges.emplace_back(i, ai - 1);     }     Graph gr(n, edges);     gr.dfs();     std::vector<int> traverse_order = gr.nodeExitOrder;     std::reverse(traverse_order.begin(), traverse_order.end());     for (int i = 0; i < n; ++i) {       edges_tr.emplace_back(edges[i].to, edges[i].from);     }     Graph gr_tr(n, edges_tr);     gr_tr.count_components(traverse_order);     std::vector<int> comp_sizes;     comp_sizes.reserve(n);     for (int comp : gr_tr.components) {       if (comp >= comp_sizes.size()) {         comp_sizes.resize(comp + 1, 0);       }       ++comp_sizes[comp];     }     long long ans = 1;     int rest_edges = 0;     for (int comp_size : comp_sizes) {       if (comp_size > 1) {         int tmp = powModulo(2, comp_size, modulo);         if (tmp < 2) {           tmp += modulo;         }         ans = ans * (tmp - 2) % modulo;       } else {         ++rest_edges;       }     }     ans = ans * powModulo(2, rest_edges, modulo) % modulo;     std::cout << ans << std::endl;     return 0;   }


//EK ONKAR   #include <bits/stdc++.h>      using namespace std;      /******************************MACROS******************************/      #define int long long   #define ld long double   #define all(x) (x).begin(), (x).end()   #define rall(x) (x).rbegin(), (x).rend()   #define yes cout<< Yes n    #define no cout<< No n    #define vt vector   #define pb push_back   #define F first   #define S second   #define u_s unordered_set   #define u_m unordered_map   #define p_q priority_queue   #define m_p make_pair      /******************************TEMPLATE******************************/      #define sim template < class c   #define ris return * this   #define dor > debug & operator <<   #define eni(x) sim > typename      enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {   sim > struct rge { c b, e; };   sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }   sim > auto dud(c* x) -> decltype(cerr << *x, 0);   sim > char dud(...);   struct debug {   #ifndef ONLINE_JUDGE       ~debug() { cerr << endl; }       eni(!=) cerr << boolalpha << i; ris; }       eni(==) ris << range(begin(i), end(i)); }       sim, class b dor(pair < b, c > d) {           ris <<  (  << d.first <<  ,   << d.second <<  ) ;       }       sim dor(rge<c> d) {           *this <<  [ ;           for (auto it = d.b; it != d.e; ++it)               *this <<  ,   + 2 * (it == d.b) << *it;           ris <<  ] ;       }   #else       sim dor(const c&) { ris; }   #endif   };   #define deb(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]        /******************************ALGORITHMS******************************/      int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}   int __pow(int x, int y) {int res = 1;while(y>0){if(y&1) res = (res*x); y>>=1; x = (x*x);}return res;}   int mi(int x, int M) {return __pow(x, M-2, M);}   int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}   int add(int a, int b, int M) {return (a+b) % M;}   int mul(int a, int b, int M) {return (a*b) % M;}   int sub(int a, int b, int M) {return (a-b+M) % M;}   int div(int a, int b, int M) {return (a*mi(b, M)) % M;}   void umax(int &a, int b) {a = max(a, b);}   void umin(int &a, int b) {a = min(a, b);}      /******************************main*code******************************/      const int N = 5e3+2, inf = 1e18, M = 998244353;      int Test, n, m, k, dp[N][N];   bool ok = 1;   vt<int> g[N];      void dfs(int u, int par = 0)   {       bool is_leaf = 1;       for(auto v:g[u]) if(v != par)           {               dfs(v, u);               is_leaf = 0;           }       if(is_leaf)       {           for(int j = 0; j<=k; j++)               dp[u][j] = 1;           return;       }       for(int j = 0; j<=k/2; j++)       {           dp[u][j] = 1;           for(auto v:g[u]) if(v != par)                   dp[u][j] = (dp[u][j] * ((j == 0 ? 0 : dp[v][j-1]) + dp[v][k])) % M;       }       for(int j = k/2 + 1; j<=k; j++)       {           int last = 1;           int i = 1;           dp[u][j] = 0;           for(auto v:g[u]) if(v != par)               {                   int tmp = last;                   last = (tmp * (dp[v][k] + (j == k ? 0 : dp[v][k-j-1]))) % M;                   dp[u][j] = ((tmp * (j == 1 ? dp[v][j-1] : (dp[v][j-1] - dp[v][j-2] + M) % M)) % M + (dp[u][j] * (dp[v][k] + (j == k ? 0 : dp[v][k-j-1]))) % M) % M;                   i++;               }           dp[u][j] = (dp[u][j-1] + dp[u][j]) % M;       }   }      void test_case()   {       cin >> n >> k;       g[1].pb(0);       for(int i = 1; i<n; i++)       {           int u, v;           cin >> u >> v;           g[u].pb(v);           g[v].pb(u);       }       dfs(1);       cout << dp[1][k] << endl;   }      int32_t main()   {       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);       Test = 1;       //cin >> Test;       while(Test--)           test_case();   #ifndef ONLINE_JUDGE       cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s.  << endl;   #endif       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int limit = 453;   int n, m, q, i, x, y, col;   vector<pair<int, int> > edges[100011];   pair<int, int> Q[100011];   vector<int> cols[100011];   int sz[100011];   int id_big[100011], cnt_big;   int dad[100011];   vector<int> who[100011];   int big[limit + 11][100011];   bool us[100011];   vector<int> appear[100011];   int groups;   int Find(int x) {     if (dad[x] == x) return x;     dad[x] = Find(dad[x]);     return dad[x];   }   bool Merge(int x, int y) {     x = Find(x);     y = Find(y);     if (x == y) return false;     dad[x] = y;     return true;   }   void pre() {     int i;     for (i = 1; i <= n; i++) {       dad[i] = i;       sort(cols[i].begin(), cols[i].end());       cols[i].resize(unique(cols[i].begin(), cols[i].end()) - cols[i].begin());       sz[i] = cols[i].size();       if (sz[i] >= limit) id_big[i] = ++cnt_big;     }   }   void solve_color(int id) {     for (auto e : edges[id]) Merge(e.first, e.second);     for (auto e : edges[id]) {       if (!us[e.first]) who[Find(e.first)].push_back(e.first);       if (!us[e.second]) who[Find(e.second)].push_back(e.second);       us[e.first] = us[e.second] = true;     }     for (auto e : edges[id]) {       int node = e.first;       if (who[e.second].size() > 0) node = e.second;       if (who[node].size() > 0) {         groups++;         for (auto n1 : who[node]) {           appear[n1].push_back(groups);           if (sz[n1] < limit) continue;           for (auto n2 : who[node]) big[id_big[n1]][n2]++;         }         who[node].clear();       }     }     for (auto e : edges[id])       dad[e.first] = e.first, dad[e.second] = e.second,       us[e.first] = us[e.second] = false;   }   int main() {     scanf( %d%d , &n, &m);     for (i = 1; i <= m; i++) {       scanf( %d%d%d , &x, &y, &col);       edges[col].push_back(make_pair(x, y));       cols[x].push_back(col);       cols[y].push_back(col);     }     pre();     scanf( %d , &q);     for (i = 1; i <= q; i++) {       scanf( %d%d , &x, &y);       Q[i] = make_pair(x, y);     }     for (i = 1; i <= m; i++) {       if (edges[i].empty()) continue;       solve_color(i);     }     for (i = 1; i <= q; i++) {       if (sz[Q[i].first] > sz[Q[i].second]) swap(Q[i].first, Q[i].second);       if (sz[Q[i].second] < limit) {         int c0 = 0;         int c1 = 0;         int n1 = Q[i].first;         int n2 = Q[i].second;         int ans = 0;         while (c0 < appear[n1].size() && c1 < appear[n2].size()) {           if (appear[n1][c0] == appear[n2][c1]) {             ans++;             c0++;             c1++;             continue;           }           if (appear[n1][c0] < appear[n2][c1])             c0++;           else             c1++;         }         printf( %d n , ans);         continue;       }       printf( %d n , big[id_big[Q[i].second]][Q[i].first]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, m, i, a, b;     cin >> n >> d >> m;     for (i = 0; i < m; i++) {       cin >> a >> b;       if ((a - b + d) * (a - b - d) <= 0 &&           (a + b - d) * (a + b + d - 2 * n) <= 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long s;     scanf( %I64d , &s);     long long d = s;     int n = 0;     while (s > 0) {       d /= 10;       d *= 10;       int a = s - d;       if (a == 4 || a == 7) {         n++;       }       s /= 10;       d = s;     }     if (n == 0) {       printf( NO );       return 0;     }     d = n;     while (n > 0) {       d /= 10;       d *= 10;       int a = n - d;       if (a != 4 && a != 7) {         printf( NO );         return 0;       }       n /= 10;       d = n;     }     printf( YES );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a, b, k;     cin >> n >> a >> b >> k;     long long int skips_left = k;     long long int win = 0;     long long int h[n];     long long int skips_req[n];     for (long long int m = 0; m < n; m++) {       skips_req[m] = 0;     }     for (long long int j = 0; j < n; j++) {       cin >> h[j];       long long int rem = (h[j] % (a + b));       if ((0 < rem) && (rem <= a)) {         skips_req[j] = 0;       } else if ((a < rem) && (rem < (a + b))) {         long long int skips;         if ((rem - a) % a == 0) {           skips_req[j] = (rem - a) / a;         } else {           skips = (rem - a) / a + 1;           skips_req[j] = skips;         }       } else if (rem == 0) {         long long int skips;         if ((b) % a == 0) {           skips_req[j] = b / a;         } else {           skips = (b / a) + 1;           skips_req[j] = skips;         }       }     }     sort(skips_req, skips_req + n);     long long int d;     d = 0;     while ((skips_left > 0) && (d < n)) {       skips_left = skips_left - skips_req[d];       if (skips_left >= 0) {         win++;       }       d++;     }     cout << win << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 2147383647;   const double pi = 2 * acos(0.0);   const double eps = 1e-9;   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   inline long long gcd(long long a, long long b) {     a = ((a) < 0 ? -(a) : (a));     b = ((b) < 0 ? -(b) : (b));     while (b) {       a = a % b;       swap(a, b);     }     return a;   }   int ext_gcd(int a, int b, int& x, int& y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     int x1, y1;     int d = ext_gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   inline long long modInv(int a, int m) {     int x, y;     ext_gcd(a, m, x, y);     if (x < 0) x += m;     return x;   }   inline long long power(long long a, long long p) {     long long res = 1, x = a;     while (p) {       if (p & 1) res = (res * x);       x = (x * x);       p >>= 1;     }     return res;   }   inline long long bigmod(long long a, long long p, long long m) {     long long res = 1, x = a % m;     while (p) {       if (p & 1) res = (res * x) % m;       x = (x * x) % m;       p >>= 1;     }     return res;   }   vector<pair<int, int> > adj[100010];   pair<int, int> vis[100010];   int n;   struct road {     int u, v, w;   } rdd[100010];   pair<int, int> par[100010];   void bfs() {     pair<int, int> xyz = make_pair(inf, inf);     for (int i = (1); i <= (n); ++i) vis[i] = xyz;     queue<int> q;     q.push(1);     vis[1] = make_pair(0, 0);     while (!q.empty()) {       int s = q.front();       q.pop();       for (int i = (0); i <= (((int)(adj[s]).size()) - 1); ++i) {         xyz = adj[s][i];         int t = xyz.first;         int w = 1 - xyz.second;         if (vis[t].first == inf || vis[s].first + 1 < vis[t].first) {           vis[t] = make_pair(vis[s].first + 1, vis[s].second + w);           q.push(t);           par[t] = make_pair(s, i);         } else if (vis[s].first + 1 == vis[t].first) {           if (vis[s].second + w < vis[t].second) {             vis[t].second = vis[s].second + w;             par[t] = make_pair(s, i);           }         }       }     }   }   int mark[100010];   set<pair<int, int> > st;   int main() {     int m;     scanf( %d %d , &n, &m);     int good = 0;     for (int i = (0); i <= (m - 1); ++i) {       int u, v, w;       scanf( %d %d %d , &u, &v, &w);       rdd[i].u = u;       rdd[i].v = v;       rdd[i].w = w;       adj[u].push_back(make_pair(v, w));       adj[v].push_back(make_pair(u, w));       if (w == 1) good++;     }     bfs();     pair<int, int> temp;     int cur = n;     while (cur != 1) {       temp = par[cur];       int s = temp.first;       int t = cur;       st.insert(make_pair(s, t));       st.insert(make_pair(t, s));       cur = s;     }     int x = vis[n].second;     int k = 2 * x + good - vis[n].first;     printf( %d n , k);     for (int i = (0); i <= (m - 1); ++i) {       int u = rdd[i].u;       int v = rdd[i].v;       int w = rdd[i].w;       if (w == 1 && st.find(make_pair(u, v)) == st.end()) {         printf( %d %d %d n , u, v, 1 - w);       } else if (w == 0 && st.find(make_pair(u, v)) != st.end()) {         printf( %d %d %d n , u, v, 1 - w);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 400007;   const int mod = (int)1e9 + 7;   int a, b, k, t, N;   int ans, f[MaxN], s[MaxN];   int main() {     int i, j;     scanf( %d%d%d%d , &a, &b, &k, &t);     f[2 * k * t + 1] = 1;     N = 4 * k * t + 1;     for (i = 1; i <= t + t; i++) {       for (j = 1; j <= N; j++) {         s[j] = (s[j - 1] + f[j]) % mod;       }       for (j = 1; j <= N; j++) {         f[j] = (s[min(N - 1, j + k)] - s[max(1, j - k - 1)]) % mod;         if (f[j] < 0) f[j] += mod;       }     }     ans = 0;     for (i = 2 * k * t + b - a + 2; i <= N; i++) ans = (ans + f[i]) % mod;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool DEB = 0;   void __start__() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   mt19937_64 rnd(time(0));   using ll = long long;   using vi = vector<int>;   using ld = long double;   using pii = pair<int, int>;   void test_case() {     ll n, k, two, five, c = 1;     cin >> n >> k;     two = k;     five = k;     while (n % 2 == 0 && two) n /= 2, two--;     while (n % 5 == 0 && five) n /= 5, five--;     cout << n << string(k,  0 );   }   signed main() {     __start__();     int Q = 1;     while (Q--) test_case();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> c(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &c[i]);     }     vector<int> ans(n + 1, 0);     for (int i = 0; i < n; ++i) {       vector<int> mx(n + 1, 0);       int p = 0;       for (int k = i; k < n; ++k) {         mx[c[k]] += 1;         if (mx[c[k]] > mx[p]) {           p = c[k];         } else if (mx[c[k]] == mx[p]) {           p = min(p, c[k]);         }         ans[p] += 1;       }     }     for (int i = 1; i <= n; ++i) {       printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     long long res = 1;     long long mdl = 1000000000 + 7;     long long s1 = s.size();     vector<long long> fibo(s1 + 1);     fibo[0] = 1;     fibo[1] = 1;     for (long long i = 0; i < s1; i++) {       if (s[i] ==  m  || s[i] ==  w ) {         cout <<  0 ;         return 0;       }     }     for (long long j = 1; j < s1; j++) {       long long z = 1;       while ((j < s1) && ((s[j] == s[j - 1] && s[j] ==  u ) ||                           (s[j] == s[j - 1] && s[j] ==  n ))) {         z++;         j++;       }       long long i = 2;       while (i <= z) {         fibo[i] = (fibo[i - 1] + fibo[i - 2]) % mdl;         i++;       }       res = (res * fibo[z]) % mdl;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a[5005], b[5005];   int la, lb;   int memo[5005][5005];   int dp(int x, int y) {     if (x == la || y == lb)       return 0;     else if (memo[x][y] != -1) {       return memo[x][y];     } else {       int ans1 = dp(x, y + 1) % 1000000007;       int ans2 = 0;       if (a[x] == b[y]) {         ans2 = (dp(x + 1, y + 1) + 1) % 1000000007;       }       int ret = (ans1 + ans2) % 1000000007;       return memo[x][y] = ret;     }   }   int main() {     scanf( %s %s , a, b);     memset(memo, -1, sizeof(memo));     int i;     la = strlen(a);     lb = strlen(b);     int ans = 0;     for (i = 0; i < la; i++) {       ans = (ans + dp(i, 0)) % 1000000007;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const int MAXN = 2005;   int n, m;   char maze[MAXN][MAXN];   void Dfs(int x, int y) {     if (x >= n - 1 || y >= m - 1 || x < 0 || y < 0) return;     int xx, yy, s = 0;     for (int i = 0; i < 2; ++i)       for (int j = 0; j < 2; ++j)         if (maze[x + i][y + j] ==  * ) {           s++;           xx = x + i;           yy = y + j;         }     if (s == 1) {       maze[xx][yy] =  . ;       for (int i = -1; i < 1; ++i)         for (int j = -1; j < 1; ++j) Dfs(xx + i, yy + j);     }   }   int main(int argc, char const *argv[]) {     scanf( %d %d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %s , maze[i]);     for (int i = 0; i < n - 1; ++i)       for (int j = 0; j < m - 1; ++j) Dfs(i, j);     for (int i = 0; i < n; ++i) printf( %s n , maze[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[200005];   int visited[200005];   int dfs(int u) {     visited[u] = 1;     int iscycle = ((adj[u].size() == 2) ? (1) : (0));     for (int i = 0; i < adj[u].size(); ++i) {       if (visited[adj[u][i]] == 0 and dfs(adj[u][i]) == 0) iscycle = 0;     }     return iscycle;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     for (int i = 0; i <= n; ++i) visited[i] = 0;     int ans = 0;     for (int i = 1; i <= n; ++i)       if (visited[i] == 0) ans += dfs(i);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   long long MOD1 = 1e9 + 123;   long long MOD2 = 1e9 + 321;   vector<int> fun(string &s) {     int i;     vector<int> v;     for (i = 0; i + 1 < s.length(); i++)       if (s[i] != s[i + 1]) v.push_back(i + 1);     if (s[s.length() - 1] ==  1 ) v.push_back(s.length());     return v;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t, a, n, q, k, i, l, m, c, u, f, j, p, r, x, y, s, b, d;     cin >> t;     while (t--) {       cin >> n;       string S, P;       cin >> S >> P;       vector<int> v1, v2;       v1 = fun(S);       v2 = fun(P);       reverse(v2.begin(), v2.end());       cout << v1.size() + v2.size() <<    ;       for (auto h : v1) cout << h <<    ;       for (auto h : v2) cout << h <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {}   int main() {     long long a, b, c, d;     cin >> a >> b >> c >> d;     long long ans = 0;     long long l = max(c, a + b);     long long r = c + d + 1;     for (long long i = l; i < r; i++) {       long long che = min(i - c, d - c + 1);       long long e1 = max(0LL, i - 2 * b);       long long e2 = max(0LL, i - a - c);       long long e3 = max(0LL, i - b - c - 1);       long long e4 = (i - a - b + 1);       ans += (che * (e4 - e1 - e2 + e3));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000006];   int n, k;   int max_a;   int has[20000007];   bool judge(int num) {     if (num == 1) return true;     int cnt = 0;     for (int i = 0; i < num * 2; i++) {       has[i] = i / num;     }     for (int i = num * 2; i <= max_a; i++) {       has[i] = has[i / 2] + has[i - i / 2];     }     for (int i = 0; i < n; i++) {       cnt += has[a[i]];       if (cnt >= k) {         return true;       }     }     return false;   }   int main() {     while (scanf( %d%d , &n, &k) != EOF) {       for (int i = 0; i < n; i++) {         scanf( %d , &a[i]);       }       long long sum = 0;       max_a = 0;       for (int i = 0; i < n; i++) {         sum += a[i];         if (a[i] > max_a) {           max_a = a[i];         }       }       if (sum < k) {         printf( -1 n );       } else {         int l = 1, r = max_a + 1;         while (l < r - 1) {           int m = (l + r) / 2;           if (judge(m)) {             l = m;           } else {             r = m;           }         }         while (judge(l)) {           l++;         }         cout << l - 1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, cnt;   char x[55];   int main() {     scanf( %s , x);     n = strlen(x);     for (int i = 0; i < n; i++) cnt += x[i] ==  a ;     printf( %d n , min(n, cnt * 2 - 1));   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       vector<long long int> arr;       long long int num;       for (int i = 0; i < n; i++) {         cin >> num;         arr.push_back(num);       }       sort(arr.begin(), arr.end());       long long int sum = 0;       for (int i = 0; i < n - 1; i++) {         if (arr[i] != arr[i + 1]) {           sum++;         }       }       cout << sum + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const inline int __GET_INT() {     int ret;     scanf( %d , &ret);     return ret;   }   int main() {     long long A = __GET_INT(), B = __GET_INT(), n = __GET_INT();     for (int i = 0; i < n; ++i) {       long long l = __GET_INT(), t = __GET_INT(), m = __GET_INT();       long long first = B * (l - 1) + A;       if (first > t) {         puts( -1 );       } else {         int r = -1;         long long low = 1, high = 1000000, mid;         while (low <= high) {           mid = (low + high) >> 1;           long long val = ((mid * (mid - 1)) / 2) * B + mid * first;           long long second = B * (l + mid - 2) + A;           if (second > t || val > (t * m)) {             high = mid - 1;           } else {             low = mid + 1;             r = l + mid - 1;           }         }         printf( %d n , r);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1005], b[1005];   char c[1005][1005];   int main() {     int n, m;     cin >> n >> m;     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> c[i][j];         if (c[i][j] ==  * ) {           a[i]++;           b[j]++;           cnt++;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (c[i][j] ==  * ) {           if (a[i] + b[j] - 1 == cnt) return cout <<  YES n  << i <<     << j, 0;         } else {           if (a[i] + b[j] == cnt) return cout <<  YES n  << i <<     << j, 0;         }       }     }     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   long long n, A, B, C = 1;   const long long E = 1e9 + 7;   int main() {     cin >> n;     if (n <= 1) {       cout << 0 << endl;       return 0;     }     for (long long i = 2; i < n; i++) {       A = B;       B = C;       C = 2 * B + 3 * A;       C %= E;     }     long long ans = (3 * C) % E;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   int main() {     long long int i, t, a = 0, k, b = 0, j = 0, l = 0, n, m, r = 0, c = 0, ct = 0,                         pos = 0, neg = 0, flag = 0, x, y, space, maxi,                         ans = 999999, ans2 = -100, avg, sum = 0;     double d;     long long int ar1[100005] = {0}, ar2[100005] = {0};     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int ch, temp = 0;     vector<long long int> v;     string st, st2, st3;     stack<char> s;     vector<pair<string, string> > vp;     queue<pair<long long int, long long int> > q;     cin >> n;     cout << 2 * n - 1 <<     << 2 << endl << 1 <<     << 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> s[300000];   int ans[300000];   bool vis[300000];   int nr[500000];   int main() {     int n, m;     scanf( %d %d , &n, &m);     priority_queue<pair<int, int> > pq;     for (int i = 0; i <= 2 * m; i++) nr[i] = -1;     for (int i = 0; i < n; i++) {       int k, x;       scanf( %d , &k);       while (k--) {         scanf( %d , &x);         nr[x + m] = i;         s[i].insert(x);       }       pq.push(make_pair(-s[i].size(), i));     }     while (!pq.empty()) {       int k = pq.top().second;       pq.pop();       if (vis[k]) continue;       vis[k] = true;       if (s[k].empty()) {         puts( NO );         return 0;       }       int x = *s[k].begin();       if (x > 0) ans[x] = 1;       if (nr[m - x] != -1) {         s[nr[m - x]].erase(-x);         pq.push(make_pair(-s[nr[m - x]].size(), nr[m - x]));       }     }     puts( YES );     for (int i = 1; i <= m; i++) printf( %d , ans[i]);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     if (m == 3 && n >= 5)       cout << -1 << endl;     else {       if (m == 3 && n == 4)         puts( -1 0 n1 0 n0 2 n0 1 );       else {         for (int i = 1; i <= m; i++) cout << i <<     << i * i + 343231 << endl;         n -= m;         for (int i = 1; i <= n; i++)           cout << i <<     << -(i * i + 343231) << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int s[n];     int f[10] = {0};     for (int i = 0; i < n; i++) {       cin >> s[i];     }     int tmp;     for (int j = 0; j < m; j++) {       cin >> tmp;       f[tmp] = 1;     }     int emp = 1;     for (int i = 0; i < n; i++) {       if (f[s[i]] == 1) {         emp = 0;         cout << s[i] <<    ;       }     }     if (emp == 1) cout <<   n ;   }


#include <bits/stdc++.h>   const int maxn = 3e5 + 3;   int n, a[maxn], b[maxn], m, l[maxn], r[maxn];   void solve() {     scanf( %d , &n);     m = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);       b[m++] = a[i];     }     std::sort(b, b + m);     m = std::unique(b, b + m) - b;     for (int i = 0; i < n; ++i) {       a[i] = std::lower_bound(b, b + m, a[i]) - b;     }     std::fill(l, l + m, 0x7fffffff);     std::fill(r, r + m, 0x80000000);     for (int i = 0; i < n; ++i) {       l[a[i]] = std::min(l[a[i]], i);       r[a[i]] = std::max(r[a[i]], i);     }     int ans = m - 1;     for (int i = 0, j; i < m; i = j) {       for (j = i + 1; j < m && r[j - 1] < l[j]; ++j)         ;       ans = std::min(ans, i + (m - j));     }     printf( %d n , ans);   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int Q = 9, R = 5, B = 3, N = 3, P = 1, q = 9, r = 5, b = 3, n = 3, p = 1;     int sum2 = 0, Sum2 = 0;     char c[64];     for (int i = 0; i < 64; i++) {       cin >> c[i];     }     for (int i = 0; i < 64; i++) {       switch (c[i]) {         case  q :           sum2 += q;           break;         case  r :           sum2 += r;           break;         case  b :           sum2 += b;           break;         case  n :           sum2 += n;           break;         case  p :           sum2 += p;           break;         case  Q :           Sum2 += Q;           break;         case  R :           Sum2 += R;           break;         case  B :           Sum2 += B;           break;         case  N :           Sum2 += N;           break;         case  P :           Sum2 += P;           break;       }     }     if (sum2 > Sum2)       cout <<  Black ;     else if (sum2 < Sum2)       cout <<  White ;     else       cout <<  Draw ;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 10;   const long long MN = 5e2 + 10;   long long n, m, a[MN], b[MN];   vector<pair<long long, char> > ans;   long long lz = 0;   bool fl = false;   void fail() {     cout <<  NO n ;     exit(0);   }   long long get(long long l, long long r) {     long long mx = *max_element(a + l, a + r + 1);     if (a[l] == mx) {       fl = true;       long long ret = l;       for (; a[ret] == mx; ++ret)         ;       return ret - 1;     }     long long ret = l;     for (; a[ret] != mx; ++ret)       ;     return ret;   }   int32_t main() {     ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (long long i = 0; i < n; ++i) cin >> a[i];     cin >> m;     for (long long i = 0; i < m; ++i) cin >> b[i];     long long cur = 0;     long long p = 0;     long long lst = 0;     for (long long i = 0; i < n; ++i) {       cur += a[i];       if (cur == b[p] && p < m) {         if (i == lst) {           ++p, cur = 0, lst = i + 1;           continue;         }         if (*max_element(a + lst, a + i + 1) == *min_element(a + lst, a + i + 1))           fail();         ++p;         fl = false;         long long ind = get(lst, i);         if (fl) {           for (long long j = 0; j < (i - ind); ++j)             ans.push_back(make_pair(ind - lz,  R ));           for (long long j = ind - 1; j >= lst; --j) {             ans.push_back(make_pair(ind - lz,  L ));             ++lz;           }           lz += (i - ind);         } else {           for (long long j = ind - 1; j >= lst; --j) {             ans.push_back(make_pair(ind - lz,  L ));             ++lz;           }           for (long long j = 0; j < (i - ind); ++j)             ans.push_back(make_pair(ind - lz,  R ));           lz += i - ind;         }         cur = 0;         lst = i + 1;       }     }     if (cur || p < m) fail();     cout <<  YES n ;     for (auto v : ans) cout << v.first + 1 <<     << v.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return a * b / gcd(a, b);   }   long long int fexp(long long int a, long long int b) {     long long int ans = 1;     while (b) {       if (b & 1) ans = ans * a % 998244353;       b /= 2;       a = a * a % 998244353;     }     return ans;   }   long long int inverse(long long int a, long long int p) {     return fexp(a, p - 2);   }   void solve() {     string s;     cin >> s;     long long int n = (long long int)s.size();     long long int ans = 1e9 + 5;     set<long long int> st;     long long int cnt = 0;     vector<long long int> vec(4, 0);     long long int fir = s[0] -  0 , num = 0;     for (long long int i = 0; i < n; i++) {       cnt = 0;       long long int idx = s[i] -  0 ;       if (idx == fir && i != num) {         vec[fir] = 1;         st.insert(idx);         fir = s[i - 1] -  0 ;         continue;       }       st.insert(idx);       vec[idx]++;       if ((long long int)st.size() == 3) {         cnt = vec[s[i - 1] -  0 ] + 2;         ans = min(ans, cnt);         st.erase(fir);         vec[fir] = 0;         vec[s[i - 1] -  0 ] = 1;         fir = s[i - 1] -  0 ;         num = i - 1;       }     }     if (ans == 1e9 + 5) {       cout << 0 <<   n ;       return;     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   vector<pair<int, int> > v[N * N];   bool flag[N * N], a[N][N], vis[N][N];   pair<int, int> dis[N * N];   int id[N][N], q[N * N], ID, n, m, r, c, sx, sy, ans;   char s[N];   void bfs(int r, int c) {     int L = 0, R = 0;     q[R++] = id[r][c];     flag[id[r][c]] = true;     dis[id[r][c]] = make_pair(0, 0);     while (L < R) {       int u = q[L++], sz = v[u].size();       for (int i = 0; i < sz; i++) {         int x = v[u][i].first, y = v[u][i].second;         if (y > 1 && a[x][y - 1] && !flag[id[x][y - 1]]) {           q[R++] = id[x][y - 1];           flag[id[x][y - 1]] = true;           dis[id[x][y - 1]] = make_pair(dis[u].first + 1, dis[u].second);         }         if (y < m && a[x][y + 1] && !flag[id[x][y + 1]]) {           q[R++] = id[x][y + 1];           flag[id[x][y + 1]] = true;           dis[id[x][y + 1]] = make_pair(dis[u].first, dis[u].second + 1);         }       }     }   }   void dfs(int x, int y, int z) {     vis[x][y] = true;     id[x][y] = z;     v[z].push_back(make_pair(x, y));     if (x > 1 && a[x - 1][y] && !vis[x - 1][y]) dfs(x - 1, y, z);     if (x < n && a[x + 1][y] && !vis[x + 1][y]) dfs(x + 1, y, z);   }   int main() {     memset(dis, 0x3f, sizeof(dis));     scanf( %d%d%d%d%d%d , &n, &m, &r, &c, &sx, &sy);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= m; j++) a[i][j] = (s[j] ==  . );     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && !vis[i][j]) ID++, dfs(i, j, ID);     bfs(r, c);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j]) {           int u = id[i][j];           if (dis[u].first <= sx && dis[u].second <= sy) ans++;         }     printf( %d n , ans);     return 0;   }