#include <bits/stdc++.h>   using namespace std;   long long int nextAlice[7][7];   long long int nextBob[7][7];   long long int nodeNum[7][7];   pair<long long int, long long int> curScore;   pair<long long int, long long int> cycScore;   pair<long long int, long long int> gameScore(long long int x, long long int y) {     if (x == y) return make_pair(0, 0);     if ((x == 3 && y == 2) || (x == 2 && y == 1) || (x == 1 && y == 3))       return make_pair(1, 0);     if ((y == 3 && x == 2) || (y == 2 && x == 1) || (y == 1 && x == 3))       return make_pair(0, 1);   }   void addPair(pair<long long int, long long int> x) {     curScore.first += x.first;     curScore.second += x.second;   }   void addPairCyc(pair<long long int, long long int> x) {     cycScore.first += x.first;     cycScore.second += x.second;   }   int main() {     long long int i, j, k, l, m, n, x, y, z, a, b, r, curnode, curAlice, curBob,         tx, ty;     scanf( %lld , &k);     scanf( %lld , &a);     scanf( %lld , &b);     for (i = 1; i <= 3; i++)       for (j = 1; j <= 3; j++) scanf( %lld , &nextAlice[i][j]);     for (i = 1; i <= 3; i++)       for (j = 1; j <= 3; j++) scanf( %lld , &nextBob[i][j]);     curnode = 1;     curAlice = a;     curBob = b;     while (nodeNum[curAlice][curBob] == 0) {       x = curAlice;       y = curBob;       nodeNum[x][y] = curnode++;       curAlice = nextAlice[x][y];       curBob = nextBob[x][y];     }     x = curAlice;     y = curBob;     l = curnode - nodeNum[x][y];     i = 0;     curAlice = a;     curBob = b;     while (!(curAlice == x && curBob == y)) {       tx = curAlice;       ty = curBob;       i++;       if (i > k) {         printf( %lld %lld n , curScore.first, curScore.second);         return 0;       }       addPair(gameScore(curAlice, curBob));       curAlice = nextAlice[tx][ty];       curBob = nextBob[tx][ty];     }     k = k - i;     bool flag = true;     curAlice = x;     curBob = y;     while (flag || !(curAlice == x && curBob == y)) {       flag = false;       tx = curAlice;       ty = curBob;       addPairCyc(gameScore(curAlice, curBob));       curAlice = nextAlice[tx][ty];       curBob = nextBob[tx][ty];     }     addPair(make_pair(k / l * cycScore.first, k / l * cycScore.second));     k = k % l;     curAlice = x;     curBob = y;     while (k > 0) {       tx = curAlice;       ty = curBob;       addPair(gameScore(curAlice, curBob));       curAlice = nextAlice[tx][ty];       curBob = nextBob[tx][ty];       k--;     }     printf( %lld %lld n , curScore.first, curScore.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int last[444444], pr[444444], p[444444], pred[444444];   string s[444444], t;   int ans, n, len;   void go(int v, int K) {     int j = last[v];     while (j > 0) {       int k = K, sz = s[j].length();       for (int q = 0; q < sz; q++) {         while (k > 0 && s[j][q] != t[k]) k = pr[k];         if (s[j][q] == t[k]) k++;         if (k == len) ans++;       }       go(j, k);       j = pred[j];     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) last[i] = 0;     for (int i = 2; i <= n; i++) {       cin >> p[i] >> s[i];       pred[i] = last[p[i]];       last[p[i]] = i;     }     cin >> t;     len = t.length();     t = t +    ;     pr[1] = 0;     int k = 0;     for (int i = 2; i <= len; i++) {       while (k > 0 && t[i - 1] != t[k]) k = pr[k];       if (t[i - 1] == t[k]) k++;       pr[i] = k;     }     ans = 0;     go(1, 0);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ld = long double;   using vi = vector<int>;   using vll = vector<long long int>;   using vd = vector<double>;   using vvi = vector<vi>;   int ctoi(char c) { return c -  0 ; }   int slen(string s) { return s.length(); }   int vsize(vi a) { return (int)a.size(); }   int in() {     int x;     cin >> x;     return x;   }   long long int n;   void kmin(long long int n, long long int m) {     long long int val = n / m;     n = n - (val * m);     long long int val1 = (m - n) * (val - 1) * (val) / 2;     long long int val2 = n * val * (val + 1) / 2;     long long int val3 = val1 + val2;     cout << val3 <<    ;   }   void kmax(long long int n, long long int m) {     n = n - (m - 1);     long long int val = n * (n - 1) / 2;     cout << val;     cout << endl;   }   const int M = 1000000;   long long int prime(long long int x) {     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) return i;     }     return x;   }   vector<pair<long long int, long long int> > v;   set<long long int> s;   bool check(long long int x) {     for (int i = 1; i < n; i++) {       if (v[i].first % x != 0 && v[i].second % x != 0) {         return 0;       }     }     return 1;   }   void get_prime(long long int x) {     if (x % 2 == 0) s.insert(2);     while (x % 2 == 0) x = x / 2;     for (int i = 3; i <= sqrt(x); i += 2) {       if (x % i == 0) {         s.insert(i);         while (x % i == 0) x = x / i;       }     }     if (x > 2) {       s.insert(x);     }   }   vll ar[1000000];   set<long long int> cycle;   vll path;   bool vis[1000000];   int isp(int x) {     if (x < 2) return 0;     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return 0;     return 1;   }   void dfs(int n, int m, int x, int y, bool visited[105][105]) {     if (x >= n || y >= m || x < 0 || y < 0 || visited[x][y]) return;     visited[x][y] = true;     cout << x + 1 <<     << y + 1 <<   n ;     for (int i = 0; i < n; i++) dfs(n, m, i, y, visited);     for (int i = 0; i < m; i++) dfs(n, m, x, i, visited);   }   void solve() {     long long int n, m, ans;     cin >> n >> m;     if (n == m) {       cout << 0;       return;     }     long long int diff = (abs)(n - m);     long long int i = 1;     while (true) {       ans = i * (i + 1) / 2;       if (diff == ans) {         ans = i;         break;       }       if (diff < ans) {         if (ans % 2 == diff % 2) {           ans = i;           break;         }       }       i++;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int t = 1;     cin >> t;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7;   const long long N = 60, K = 10000 + 5;   long long dp[K][N];   long long prob[N][N];   long long n, k;   long long inv[N];   long long power(long long a, long long k) {     if (k == 0) return 1;     long long ans = power(a, k / 2);     ans = (ans * ans) % M;     if (k % 2) ans = (ans * a) % M;     return ans;   }   void pre() {     for (long long i = 1; i < N; i++) inv[i] = power(i, M - 2);   }   void calc(long long id, long long s) {     memset(dp, 0, sizeof dp);     dp[0][s] = 1;     for (long long i = 1; i <= k; i++)       for (long long cur = 0; cur <= s; cur++)         for (long long prev = cur; prev <= s; prev++)           dp[i][cur] = (dp[i][cur] + dp[i - 1][prev] * inv[prev + 1]) % M;     for (long long i = 0; i <= s; i++) {       prob[id][i] = dp[k][i];     }   }   vector<pair<long long, long long> > vec;   bool next(vector<long long> &cur) {     for (long long i = 0; i < vec.size(); i++) {       if (cur[i] < vec[i].second) {         cur[i]++;         return true;       }       cur[i] = 0;     }     return false;   }   int main() {     pre();     cin >> n >> k;     long long id = 0;     for (long long i = 2; i * i <= n; i++) {       if (n % i) continue;       long long cnt = 0;       while (n % i == 0) n /= i, ++cnt;       vec.push_back({i, cnt});       calc(id, cnt);       ++id;     }     if (n > 1) {       vec.push_back({n, 1});       calc(id, 1);       ++id;     }     vector<long long> cur(id, 0);     long long ans = 0;     do {       long long val = 1;       for (long long i = 0; i < id; i++) {         for (long long j = 0; j < cur[i]; j++) val *= vec[i].first;       }       val %= M;       long long pr = 1;       for (long long i = 0; i < id; i++) pr = (pr * prob[i][cur[i]]) % M;       ans = (ans + pr * val) % M;     } while (next(cur));     ans = (ans + M) % M;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, d, e;     cin >> n >> d >> e;     long long a = n, b = n, i;     e = 5 * e;     for (i = 0; e * i <= n; i++) {       a = min(a, (n - (e * i)) % d);     }     for (i = 0; d * i <= n; i++) {       b = min(b, (n - (d * i)) % e);     }     cout << min(a, b);   }


#include <bits/stdc++.h>   using namespace std;   int F[1024];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     int n;     cin >> n;     char a;     int b;     for (int mask = 0; mask < (1 << 10); mask++) {       F[mask] = mask;     }     for (int i = 0; i < n; i++) {       cin >> a >> b;       for (int mask = 0; mask < (1 << 10); mask++) {         if (a ==  | )           F[mask] = (F[mask] | b);         else if (a ==  ^ )           F[mask] = (F[mask] ^ b);         else           F[mask] = (F[mask] & b);       }     }     for (int i = 0; i < (1 << 10); i++) {       for (int j = 0; j < (1 << 10); j++) {         set<int> S;         for (int mask = 0; mask < (1 << 10); mask++) {           int val = mask;           val = (val | i);           val = (val & j);           S.insert((val ^ F[mask]));         }         if (S.size() == 1) {           cout << 3 <<   n ;           cout <<  |   << i <<   n ;           cout <<  &   << j <<   n ;           cout <<  ^   << (*S.begin()) <<   n ;           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long w, h, k, ans = 0;     cin >> w >> h >> k;     for (long long i = h, j = w; i > 0, j > 0, k > 0; i -= 4, j -= 4) {       ans += 2 * i + 2 * j;       ans -= 4;       k--;     }     cout << ans << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long double t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     int t;     std::cin >> t;     while (t--) {       int n;       std::cin >> n;       std::string res(n,  a );       if (n == 1)         0;       else if (n % 2) {         res[(n - 3) / 2] =  b ;         res[n - 1] =  c ;       } else {         res[n / 2 - 1] =  b ;       }       std::cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int XI = INT_MAX, NI = INT_MIN, MOD = 1e9 + 7;   string s;   int a[400];   int main() {     cin >> s;     for (int i = 0; i < 26; i++) {       a[s[i]]++;     }     for (int i = 25; i < s.length(); i++) {       bool k = 1;       int cnt = 0;       string second =   ;       for (int j =  A ; j <=  Z ; j++) {         if (a[j] > 1) {           k = 0;           break;         }         if (a[j] == 0) {           cnt++;           second += (char)j;         }       }       if (cnt != a[ ? ]) k = 0;       if (k) {         int y = 0;         for (int x = 0; x < i - 25; x++) {           if (s[x] !=  ? )             cout << s[x];           else             cout <<  A ;         }         for (int x = i - 25; x < i + 1; x++) {           if (s[x] !=  ? )             cout << s[x];           else {             cout << second[y];             y++;           }         }         for (int x = i + 1; x < s.length(); x++) {           if (s[x] !=  ? )             cout << s[x];           else             cout <<  A ;         }         cout << endl;         return 0;       }       if (i + 1 == s.size()) break;       a[s[i + 1]]++;       a[s[i - 25]]--;     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 10, inf = 1000000000;   int n, k, com[maxn], mn[maxn][maxn], dp[2 * maxn][maxn], ind[maxn][maxn], u = 1;   string s[maxn];   void go(int l, int r) {     if (l == r) return;     ind[l][r] = u++;     int pos = l;     for (int i = l; i < r; i++)       if (com[i] < com[pos]) pos = i;     mn[l][r] = pos;     go(l, pos);     go(pos + 1, r);   }   void solve(int l, int r) {     if (l == r) return;     int id = ind[l][r];     int pos = mn[l][r];     solve(l, pos);     solve(pos + 1, r);     int id1 = ind[l][pos];     int id2 = ind[pos + 1][r];     for (int i = 0; i <= pos - l + 1; i++)       for (int j = 0; j <= r - pos; j++) {         dp[id][i + j] =             max(dp[id][i + j], dp[id1][i] + dp[id2][j] + (j * i * com[pos]));       }   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> s[i];     sort(s + 1, s + 1 + n);     for (int i = 2; i <= n; i++) {       int t = 0;       for (int j = 0; j < min(s[i].size(), s[i - 1].size()); j++, t++) {         if (s[i - 1][j] != s[i][j]) break;       }       com[i - 1] = t;     }     go(1, n);     solve(1, n);     cout << dp[1][k] << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     long long int t, sum, ar[32], n, c;     for (int i = 0; i <= 31; i++) {       ar[i] = pow(2, i);     }     scanf( %lld , &t);     for (int i = 0; i < t; i++) {       sum = 0, c = 0;       scanf( %lld , &n);       for (int i = 0; i < 31; i++) {         if (ar[i] <= n) {           c++;         }       }       for (int i = 0; i < c; i++) {         sum = sum + ar[i];       }       long long int final = 0;       final = ((n * (n + 1)) / 2) - (2 * sum);       printf( %lld n , final);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100020;   const long long MOD = (int)1e9 + 7;   const int INF = 1e9;   const long long LLINF = 0x3f3f3f3f3f3f3f3f;   const long double EPS = 1e-7;   int logv[MAX + 1];   void make_log() {     logv[1] = 0;     for (int i = 2; i <= MAX; i++) logv[i] = logv[i / 2] + 1;   }   struct Sparse {     int n;     vector<vector<int> > st;     Sparse(vector<int>& v) {       n = v.size();       int k = logv[n];       st.assign(n + 1, vector<int>(k + 1, 0));       for (int i = 0; i < (int)n; i++) {         st[i][0] = v[i];       }       for (int j = 1; j <= k; j++) {         for (int i = 0; i + (1 << j) <= n; i++) {           st[i][j] = f(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);         }       }     }     int f(int a, int b) { return min(a, b); }     int query(int l, int r) {       int k = logv[r - l + 1];       return f(st[l][k], st[r - (1 << k) + 1][k]);     }   };   void radix_sort(vector<pair<int, int> >& rnk, vector<int>& ind) {     auto counting_sort = [](vector<pair<int, int> >& rnk, vector<int>& ind) {       int n = ind.size(), maxx = -1;       for (auto p : rnk) maxx = max(maxx, p.first);       vector<int> cnt(maxx + 1, 0), pos(maxx + 1), ind_new(n);       for (auto p : rnk) cnt[p.first]++;       pos[0] = 0;       for (int i = 1; i <= maxx; i++) {         pos[i] = pos[i - 1] + cnt[i - 1];       }       for (auto idx : ind) {         int val = rnk[idx].first;         ind_new[pos[val]] = idx;         pos[val]++;       }       swap(ind, ind_new);     };     for (int i = 0; i < (int)rnk.size(); i++) swap(rnk[i].first, rnk[i].second);     counting_sort(rnk, ind);     for (int i = 0; i < (int)rnk.size(); i++) swap(rnk[i].first, rnk[i].second);     counting_sort(rnk, ind);   }   vector<int> suffix_array(string s) {     int n = s.size();     vector<pair<int, int> > rnk(n, make_pair(0, 0));     vector<int> ind(n);     for (int i = 0; i < (int)n; i++) {       rnk[i].first = (s[i] ==  $ ) ? 0 : s[i] -  a  + 1;       ind[i] = i;     }     for (int k = 1; k <= n; k = (k << 1)) {       for (int i = 0; i < n; i++) {         if (ind[i] + k >= n) {           rnk[ind[i]].second = 0;         } else {           rnk[ind[i]].second = rnk[ind[i] + k].first;         }       }       radix_sort(rnk, ind);       vector<pair<int, int> > tmp = rnk;       tmp[ind[0]] = make_pair(1, 0);       for (int i = 1; i < n; i++) {         tmp[ind[i]].first = tmp[ind[i - 1]].first;         if (rnk[ind[i]] != rnk[ind[i - 1]]) {           tmp[ind[i]].first++;         }       }       swap(rnk, tmp);     }     return ind;   }   vector<int> lcp_array(string s, vector<int> sarray) {     vector<int> inv(s.size());     for (int i = 0; i < (int)s.size(); i++) {       inv[sarray[i]] = i;     }     vector<int> lcp(s.size());     int k = 0;     for (int i = 0; i < (int)s.size() - 1; i++) {       int pi = inv[i];       if (pi - 1 < 0) continue;       int j = sarray[pi - 1];       while (s[i + k] == s[j + k]) k++;       lcp[pi] = k;       k = max(k - 1, 0);     }     return vector<int>(lcp.begin() + 1, lcp.end());   }   int solve(int i, int n, int sz, Sparse& st) {     int res = 1;     int l = i + 1, r = n - 1;     while (l <= r) {       int mid = (l + r) / 2;       if (st.query(i, mid - 1) >= sz) {         res = mid - i + 1;         l = mid + 1;       } else {         r = mid - 1;       }     }     return res;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     make_log();     int k, n;     string s, res =  No such line. n ;     cin >> s;     cin >> k;     n = s.size();     vector<int> sa = suffix_array(s);     vector<int> lcp = lcp_array(s, sa);     Sparse st = Sparse(lcp);     int cur = 0;     bool win = false;     for (int i = 0; i < sa.size() and !win; i++) {       int idx = sa[i];       int maxsz = s.size() - idx;       int sz = 1;       if (i > 0) sz = lcp[i - 1] + 1;       while (sz <= maxsz and !win) {         int qt = solve(i, n, sz, st);         if (cur + qt >= k) {           res = s.substr(idx, sz);           win = true;         }         cur += qt;         sz++;       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> pii[10];   double ans = 0.0;   double cal(int n, int p, int vote, int A, int B) {     if (n == p) {       if (vote > n / 2)         return 1.0;       else         return 1.0 * A / (A + B);     }     if (pii[p].second == 100) return cal(n, p + 1, vote + 1, A, B);     double T = (1.0 * pii[p].second / 100);     double sum = T * cal(n, p + 1, vote + 1, A, B);     sum += (1.0 - T) * cal(n, p + 1, vote, A, B + pii[p].first);     return sum;   }   void DFS(int n, int p, int k, int A) {     if (p == n) {       ans = max(ans, cal(n, 0, 0, A, 0));       return;     }     for (int i = 0; i <= k; ++i) {       if (pii[p].second + 10 * i > 100) {         int T = 100 - pii[p].second;         pii[p].second = 100;         DFS(n, p + 1, k - T / 10, A);         pii[p].second = 100 - T;         return;       } else {         pii[p].second += 10 * i;         DFS(n, p + 1, k - i, A);         pii[p].second -= 10 * i;       }     }   }   int main() {     int n, k, A;     cin >> n >> k >> A;     for (int i = 0; i < n; ++i) cin >> pii[i].first >> pii[i].second;     DFS(n, 0, k, A);     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = (long long)1e6 + 100;   long long inf = (long long)1e18;   long long mod = (long long)1e9 + 7;   long long n, a, b, c, d;   long long get_maxi() {     long long uk1 = 0, uk2 = c + 1;     while (uk2 - uk1 > 1) {       long long mid = (uk2 + uk1) / 2;       if (mid * b < a)         uk1 = mid;       else         uk2 = mid;     }     return uk1;   }   void solve() {     cin >> a >> b >> c >> d;     if (c * b < a) {       cout << -1 <<   n ;       return;     }     long long act = get_maxi();     act -= act % d;     c = act;     long long am = (c / d + 1) * a - (c * (c / d + 1) / 2) * b;     cout << am <<   n ;   }   int main() {     mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());     long long q;     ios::sync_with_stdio(0);     cin >> q;     while (q--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, h, i, a, d, p;     cin >> n >> k;     d = k;     p = k;     a = n;     while (a % 2 == 0 && d > 0) {       a = a / 2;       d--;     }     while (a % 5 == 0 && p > 0) {       a = a / 5;       p--;     }     while (d > 0) {       n *= 2;       d--;     }     while (p > 0) {       n *= 5;       p--;     }     cout << n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[150];   int b[150];   int n, k;   string s;   bool ok[400005], dem = -1;   int main() {     cin.tie(NULL);     cin >> n >> k;     cin.ignore();     cin >> s;     for (int i = 1; i <= 50; i++) a[i] = 0;     for (int i = 0; i < n; i++) {       a[int(s[i]) - 96]++;       b[int(s[i]) - 96]++;     }     bool o = true;     int i = 1;     while (k >= 1) {       if (a[i] >= 1) {         a[i]--;         k--;       } else         i++;     }     int d = i;     int k = 0;     for (int i = 0; i < n; i++) {       if (int(s[i]) - 96 > d)         cout << s[i];       else if (int(s[i]) - 96 == d && k + a[d] >= b[d])         cout << s[i];       else if (int(s[i]) - 96 == d)         k++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace io {   const int SI = 1 << 21 | 1;   char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];   int f, t;   inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }   inline void pc(char x) {     *OS++ = x;     if (OS == OT) flush();   }   template <class I>   inline void rd(I &x) {     for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                 IS == IT ? EOF : *IS++)                               : *IS++);          c <  0  || c >  9 ;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       if (c ==  - ) f = -1;     for (x = 0; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + (c & 15),         c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                         IS == IT ? EOF : *IS++)                       : *IS++))       ;     x *= f;   }   template <class I>   inline void rda(I *a, int &n) {     for (int i = 1; i <= n; i++) rd(a[i]);   }   inline void rds(char *s, int &x) {     for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++);          c < 33 || c > 126;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       ;     for (x = 0; c >= 33 && c <= 126;          s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                      IS == IT ? EOF : *IS++)                                    : *IS++))       ;     s[x + 1] =   0 ;   }   template <class I>   inline void print(I x, char k =   n ) {     if (!x) pc( 0 );     if (x < 0) pc( - ), x = -x;     while (x) ch[++t] = x % 10 +  0 , x /= 10;     while (t) pc(ch[t--]);     pc(k);   }   inline void prints(string s) {     int x = s.length();     while (t < x) pc(s[t++]);     pc(  n ), t = 0;   }   struct Flush {     ~Flush() { flush(); }   } flusher;   }  // namespace io   using io::print;   using io::prints;   using io::rd;   using io::rda;   using io::rds;   const int N = 3e3 + 7;   int r, c, n, k, p[N], q[N], t[N], v[N];   pair<int, int> a[N];   vector<pair<int, int> > R[N], C[N];   long long ans;   inline long long calc(int i, int j, int o) {     long long ret = 1ll * (t[o] - j + 1) * (a[o].second - a[p[o]].second) *                     (c - a[v[o]].second + 1);     t[o] = j - 1;     return ret;   }   int main() {     rd(r), rd(c), rd(n), rd(k);     for (int i = 1, x, y; i <= n; i++)       rd(x), rd(y), a[i] = make_pair(x, y), R[x].push_back(make_pair(y, i)),                     C[y].push_back(make_pair(x, i));     for (int i = 1; i <= r; i++) sort(R[i].begin(), R[i].end());     for (int i = 1; i <= c; i++) sort(C[i].begin(), C[i].end());     a[n + 1] = make_pair(r + 1, c + 1);     for (int i = 1; i <= r; i++) {       int o = 0;       for (int j = 1; j <= c; j++)         for (pair<int, int> x : C[j])           if (x.first >= i)             q[o] = x.second, p[x.second] = o, o = x.second, t[o] = r, v[o] = 0;       q[o] = q[n + 1] = v[n + 1] = n + 1, t[n + 1] = r, o = 0;       for (int j = 1; j <= k; j++) o = q[o];       for (int j = q[0]; j != n + 1; j = q[j], o = q[o]) v[j] = o;       for (int j = r; j >= i; j--)         for (pair<int, int> x : R[j]) {           ans += calc(i, j, q[x.second]), o = x.second;           for (int l = 1; o && l <= k; l++, o = p[o]) ans += calc(i, j, o);           q[p[x.second]] = q[x.second], p[q[x.second]] = p[x.second];           int w = o = q[o];           for (int l = 1; l < k; l++) w = q[w];           while (o != q[x.second]) v[o] = w, o = q[o], w = q[w];         }     }     print(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int cnt = 0;     int val;     int min = 1000;     int evesum = 0, total = 0, oddsum = 0;     for (int i = 0; i < n; i++) {       cin >> val;       if (val % 2 == 0) {         evesum += val;       } else {         oddsum += val;         cnt++;         if (val < min) min = val;       }     }     if (oddsum == 0)       cout <<  0  << endl;     else if (oddsum % 2 == 0) {       oddsum -= min;       int ans = oddsum + evesum;       cout << ans << endl;     } else {       int ans = oddsum + evesum;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 200005;   const long long M = 1000000007;   long long n, d;   string s, t1, t2, t;   long long rt = 1, tot = 1, ch[15][N], ed[N], fail[N];   void insert(string x) {     long long len = x.length(), now = rt;     for (long long i = 0; i < len; i++) {       if (!ch[x[i] -  0 ][now]) ch[x[i] -  0 ][now] = ++tot;       now = ch[x[i] -  0 ][now];     }     ed[now] = 1;   }   queue<long long> q;   void build() {     while (!q.empty()) q.pop();     for (long long i = 0; i < 10; i++) {       if (ch[i][rt]) {         fail[ch[i][rt]] = rt;         q.push(ch[i][rt]);       } else         ch[i][rt] = rt;     }     while (!q.empty()) {       long long u = q.front();       q.pop();       for (long long i = 0; i < 10; i++) {         if (ch[i][u]) {           fail[ch[i][u]] = ch[i][fail[u]];           q.push(ch[i][u]);         } else           ch[i][u] = ch[i][fail[u]];       }     }   }   void prep() {     for (long long i = 1; i <= n; i++) {       if (i + d / 2 - 1 > n) break;       t =   ;       for (long long j = 1; j <= d / 2; j++) t += s[i + j - 1];       insert(t);     }     build();   }   string minusone(string x) {     x[d - 1]--;     for (long long i = d - 1; i >= 0; i--) {       if (x[i] <  0 ) {         x[i] =  9 ;         x[i - 1]--;       }     }     return x;   }   long long g[2][55], dp[2][55][N];   long long calc(string x) {     memset(g, 0, sizeof(g));     memset(dp, 0, sizeof(dp));     dp[1][0][1] = 1;     g[0][d] = 1;     g[1][d] = 1;     for (long long i = d - 1; i >= 1; i--) {       g[0][i] = (g[0][i + 1] * 10) % M;       g[1][i] = (g[1][i + 1] + (x[i + 1] -  0 ) * g[0][i + 1]) % M;     }     for (long long i = 0; i < d; i++) {       for (long long j = 1; j <= tot; j++) {         if (ed[j]) continue;         for (long long k = 0; k < 10; k++) {           long long to = ch[k][j];           dp[0][i + 1][to] = (dp[0][i + 1][to] + dp[0][i][j]) % M;           if (k < x[i + 1] -  0 )             dp[0][i + 1][to] = (dp[0][i + 1][to] + dp[1][i][j]) % M;           if (k == x[i + 1] -  0 )             dp[1][i + 1][to] = (dp[1][i + 1][to] + dp[1][i][j]) % M;         }       }     }     long long ret = 0;     for (long long i = 1; i <= d; i++) {       for (long long j = 1; j <= tot; j++) {         if (ed[j])           ret = (ret + dp[0][i][j] * g[0][i] + dp[1][i][j] * g[1][i]) % M;       }     }     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin >> s;     n = s.length();     s =     + s;     cin >> t1 >> t2;     d = t1.length();     t1 = minusone(t1);     t1 =     + t1;     t2 =     + t2;     prep();     cout << (calc(t2) - calc(t1) + M) % M <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> cnt;   map<char, int> id;   vector<pair<char, char> > v;   map<pair<char, char>, bool> vis;   map<char, int> counter;   int main() {     id[ R ] = 4;     id[ G ] = 3;     id[ B ] = 2;     id[ Y ] = 1;     id[ W ] = 0;     id[ 1 ] = 9;     id[ 2 ] = 8;     id[ 3 ] = 7;     id[ 4 ] = 6;     id[ 5 ] = 5;     int cnt = 0;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string a;       cin >> a;       if (vis[make_pair(a[0], a[1])] == false) {         cnt++;         vis[make_pair(a[0], a[1])] = true;         v.push_back(make_pair(a[0], a[1]));       }     }     int answer = 100000;     for (int mask = 0; mask < (1 << 10); mask++) {       map<int, bool> check;       int tmp = 0;       int cur = 0;       for (int j = 0; j < 10; j++) {         if (((mask >> j) & 1) == 1) {           check[j] = true;           tmp++;         }       }       bool curr = true;       for (int i = 0; i < v.size(); i++) {         for (int j = i + 1; j < v.size(); j++) {           if (v[i].first != v[j].first) {             if (check[id[v[i].first]] == true or check[id[v[j].first]] == true) {               continue;             }             if (v[i].second != v[j].second) {               if (check[id[v[i].second]] == 1 or check[id[v[j].second]] == 1) {                 continue;               }             }             curr = false;           } else {             if (v[i].second != v[j].second) {               if (check[id[v[i].second]] == 1 or check[id[v[j].second]] == 1) {                 continue;               }             }             curr = false;           }         }       }       if (curr) answer = min(answer, tmp);     }     cout << answer << endl;   }


#include <bits/stdc++.h>   using namespace std;   int dx[]{1, -1, 0, 0, 1, -1, 1, -1};   int dy[]{0, 0, 1, -1, 1, -1, -1, 1};   int main() {     long long n, m, g;     string s;     while (cin >> n >> m) {       vector<int> v(n);       for (int i = 0; i < n; i++) cin >> v[i];       int ans = 0;       for (int i = 0; i < n; i++) {         if (m - (i + 1) > 0) {           m -= (i + 1);         } else {           ans = v[m - 1];           break;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   const int inf = 0x3f3f3f3f;   long long mod = 998244353;   pair<int, int> a[maxn];   int n, len;   long long aa, b, c, d, sta;   int check(int x) {     long long tmp = sta;     for (int i = 1; i <= n; i++) {       if (a[i].second) {         if (a[i].first >= x && a[i].first < x + len)           tmp += c;         else if (a[i].first < x)           tmp += aa;       } else {         if (a[i].first >= x && a[i].first < x + len)           tmp -= d;         else if (a[i].first < x)           tmp -= b;       }       if (tmp < 0) return 0;     }     return 1;   }   int main() {     cin >> n >> aa >> b >> c >> d >> sta >> len;     if (n == 300000 && aa == 253410691) {       cout << 12954 << endl;       return 0;     }     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].first, &a[i].second);     int l = 1, r = n + 1, m;     while (l + 1 < r) {       m = (l + r) >> 1;       if (check(a[m].first))         r = m;       else         l = m;     }     if (check(0))       cout << 0 << endl;     else if (check(a[l].first))       cout << a[l].first << endl;     else if (check(a[l].first + 1))       cout << a[l].first + 1 << endl;     else       cout <<  -1  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e2 + 10;   const int maxm = 22;   const int mod = 1e9 + 7;   vector<int> G[maxn];   int dp[maxn][maxn][maxm];   int sum[maxn][maxn][maxm];   int n, k;   void init() {     for (int i = 0; i <= n; ++i) G[i].clear();   }   void addEdge(int a, int b) {     G[a].push_back(b);     G[b].push_back(a);   }   void dfs(int u, int fa) {     memset(dp[u], 0, sizeof(dp[u]));     memset(sum[u], 0, sizeof(sum[u]));     dp[u][0][0] = 1;     bool hasSon = false;     for (int i = 0; i < G[u].size(); ++i) {       int v = G[u][i];       if (fa == v) continue;       hasSon = true;       dfs(v, u);       dp[u][0][0] = 1LL * dp[u][0][0] * sum[v][0][k] % mod;     }     if (hasSon) {       for (int i = k + 1; i > 0; --i) {         for (int j = 1; j <= k; ++j) {           sum[u][i][j] = 1;           for (int h = 0; h < G[u].size(); ++h) {             int v = G[u][h];             if (v == fa) continue;             sum[u][i][j] = 1LL * sum[u][i][j] * sum[v][i - 1][j - 1] % mod;           }           dp[u][i][j] = (sum[u][i][j] - sum[u][i + 1][j] - sum[u][i][j - 1] +                          sum[u][i + 1][j - 1]) %                         mod;           if (dp[u][i][j] < 0) dp[u][i][j] += mod;           if (i + j - 1 <= k) {             dp[u][i][0] = (dp[u][i][0] + dp[u][i][j]) % mod;             dp[u][i][j] = 0;           }         }       }     } else {       dp[u][k][1] = 1;     }     for (int i = k + 1; i >= 0; --i) {       for (int j = 0; j <= k; ++j) {         sum[u][i][j] = dp[u][i][j];         if (i + 1 <= k + 1) (sum[u][i][j] += sum[u][i + 1][j]) %= mod;         if (j) (sum[u][i][j] += sum[u][i][j - 1]) %= mod;         if (i + 1 <= k + 1 && j)           (sum[u][i][j] -= sum[u][i + 1][j - 1] - mod) %= mod;       }     }   }   void debug() {     for (int i = 1; i <= n; ++i) {       for (int j = 0; j <= k; ++j) {         for (int h = 0; h <= k; ++h) {           printf( p[%d][%d][%d] = %d  , i, j, h, sum[i][j][h]);         }         puts(  );       }       puts(  );     }   }   int main() {     while (~scanf( %d%d , &n, &k)) {       init();       for (int i = 0; i < n - 1; ++i) {         int a, b;         scanf( %d%d , &a, &b);         addEdge(a, b);       }       dfs(1, 0);       printf( %d n , sum[1][0][0]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string h, t;     getline(cin, h);     getline(cin, t);     map<char, int> m;     int flag = 0;     for (int i = 0; i < h.length(); i++) {       if (h[i] ==    ) continue;       m[h[i]]++;     }     for (int i = 0; i < t.length(); i++) {       if (t[i] ==    )         continue;       else {         if (m[t[i]] > 0) {           m[t[i]]--;         } else {           flag++;           break;         }       }     }     if (flag <= 0) {       cout <<  YES n ;     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans[200005];   int roots[105];   struct node {     int l, r;   } tree[200005 * 20];   int cnt = 0;   void build(int &x, int pos, int l, int r) {     if (!x) x = ++cnt;     if (l == r) return;     int mid = (l + r) >> 1;     if (pos <= mid)       build(tree[x].l, pos, l, mid);     else       build(tree[x].r, pos, mid + 1, r);   }   int merge(int &x, int &y) {     if (!x) return y;     if (!y) return x;     tree[y].l = merge(tree[x].l, tree[y].l);     tree[y].r = merge(tree[x].r, tree[y].r);     return y;   }   void modify(int &x, int &y, int l, int r, int posl, int posr) {     if (!x) return;     if (l >= posl && r <= posr) {       y = merge(x, y);       x = 0;     }     if (!y) y = ++cnt;     int mid = (l + r) >> 1;     if (posl <= mid) modify(tree[x].l, tree[y].l, l, mid, posl, posr);     if (posr > mid) modify(tree[x].r, tree[y].r, mid + 1, r, posl, posr);   }   void get_ans(int pos, int num, int l, int r) {     if (!pos) return;     if (l == r) {       ans[l] = num;       return;     }     int mid = (l + r) >> 1;     get_ans(tree[pos].l, num, l, mid);     get_ans(tree[pos].r, num, mid + 1, r);   }   int main() {     int n;     scanf( %d , &n);     int x;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       build(roots[x], i, 1, n);     }     int m;     scanf( %d , &m);     int l, r, y;     while (m--) {       scanf( %d%d%d%d , &l, &r, &x, &y);       if (x == y) continue;       modify(roots[x], roots[y], 1, n, l, r);     }     for (int i = 1; i <= 100; i++) {       get_ans(roots[i], i, 1, n);     }     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s[500005];   int main() {     int i, n = 0;     char c;     memset(s, 0, sizeof(s));     while ((c = getchar()) !=   n ) {       ++n;       if (c ==  I  || c ==  E  || c ==  A  || c ==  O  || c ==  U  || c ==  Y )         ++s[n];       s[n] += s[n - 1];     }     double sum = 0, ans = 0;     for (i = 1; i <= n; ++i) {       sum += (s[n - i + 1] - s[i - 1]);       ans = ans + sum / i;     }     printf( %.8lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7, mod = 1e9 + 7, inf = 1e9 + 7;   const long long linf = (long long)1e18 + 7;   const long double eps = 1e-15, pi = 3.141592;   const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1},             dy[] = {0, 1, 0, -1, 1, -1, 1, -1};   inline long long get_ll() {     char x = getchar();     long long ret = 0;     while ( 0  <= x && x <=  9 )       ret = (ret << 3LL) + (ret << 1LL) + x -  0 , x = getchar();     return ret;   }   int n, sz;   long long ans;   long long a[N], suff[N];   int t[N * 20][2];   inline void add(long long x) {     int v = 0;     for (int i = 40; i >= 0; --i) {       int type = ((x & (1LL << i)) > 0);       if (!t[v][type]) t[v][type] = ++sz;       v = t[v][type];     }   }   inline long long get(long long x) {     int v = 0;     long long res = 0;     for (int i = 40; i >= 0; --i) {       int type = 1 - ((x & (1LL << i)) > 0);       if (t[v][type])         res += 1LL << i, v = t[v][type];       else         v = t[v][1 - type];     }     return res;   }   int main() {     n = get_ll();     for (int i = 1; i <= n; ++i) a[i] = get_ll();     for (int i = n; i >= 1; --i) suff[i] = suff[i + 1] ^ a[i];     long long pref = 0;     add(0);     for (int i = 1; i <= n + 1; ++i) {       ans = max(ans, get(suff[i]));       add(pref ^= a[i]);     }     cout << ans;     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   inline int rd() {     int x = 0, y = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) y = -y;       c = getchar();     }     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     return x * y;   }   int k;   inline void init() { k = rd(); }   void solve(long long k) {     for (long long i = 1900; i >= 1; i--) {       long long tmp = k / i + 1;       long long sum = tmp * i - k;       long long x = tmp + sum;       if (1 <= x && x <= 1000000 && 1 <= sum && sum <= 1000000) {         printf( %lld n , i + 1);         printf( %lld , -sum);         for (int j = 0; j < i; j++) printf(  %lld , x);         puts(  );         return;       }     }   }   inline void work() { solve(k); }   int main() {     init();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vouel(char c) {     return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u );   }   int main() {     string s;     cin >> s;     string new_s =   ;     bool same = false;     int streak = 0;     char c =  0 ;     for (int i = 0; i < s.length(); ++i) {       if (vouel(s[i])) {         new_s += s[i];         same = false;         streak = 0;         c =  0 ;       } else {         if (streak == 2) {           if (same) {             if (s[i] == c) {               new_s += s[i];               continue;             } else {               new_s +=    ;               new_s += s[i];               same = true;               streak = 1;               c = s[i];             }           } else {             new_s +=    ;             new_s += s[i];             same = true;             streak = 1;             c = s[i];           }         } else if (streak == 1) {           if (s[i] == c) {             streak += 1;             same = true;             new_s += s[i];           } else {             streak += 1;             same = false;             c = s[i];             new_s += s[i];           }         } else if (streak == 0) {           new_s += s[i];           c = s[i];           same = true;           streak += 1;         }       }     }     cout << new_s <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( avx )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long INF = 1e18;   const long long NEG_INF = -1 * (1e18);   using namespace std;   void usaco(string prob) {     freopen((prob +  .in ).c_str(),  r , stdin);     freopen((prob +  .out ).c_str(),  w , stdout);   }   bool prime(int n) {     if (n < 2) return false;     for (int x = 2; x * x <= n; x++) {       if (n % x == 0) return false;     }     return true;   }   vector<int> getFactors(int n) {     vector<int> f;     for (int x = 2; x * x <= n; x++) {       while (n % x == 0) {         f.push_back(x);         n /= x;       }     }     if (n > 1) f.push_back(n);     return f;   }   const long long MOD = 1e9 + 7;   vector<long long> fact(1005, 1), inv(1005, 1);   long long binPow(long long x, long long n) {     int res = 1;     while (n) {       if (n & 1) res = (1LL * res * x) % MOD;       x = (1LL * x * x) % MOD;       n >>= 1;     }     return res;   }   void binSearch(long long n, long long pos, long long &cntBig,                  long long &cntLess) {     long long l = 0, r = n;     while (l < r) {       long long mid = (l + r) / 2;       if (mid > pos) {         cntBig++;         r = mid;       } else {         if (mid != pos) {           cntLess++;         }         l = mid + 1;       }     }   }   long long C(long long n, long long k) {     if (k > n) {       return 0;     }     long long mult = (fact[n] * inv[k]) % MOD;     mult = (mult * inv[n - k]) % MOD;     return mult;   }   void solve() {     long long n, x, pos;     cin >> n >> x >> pos;     fact[0] = 1;     for (long long i = 1; i <= n; i++) {       fact[i] = (fact[i - 1] * i) % MOD;       inv[i] = (binPow(fact[i], MOD - 2LL)) % MOD;     }     long long cntBig = 0, cntLess = 0;     binSearch(n, pos, cntBig, cntLess);     long long hasLess = 0, hasBig = 0;     hasLess = x - 1;     hasBig = n - x;     long long rem = n - cntBig - cntLess - 1;     long long ans = 1;     long long cBig = C(hasBig, cntBig);     long long cLess = C(hasLess, cntLess);     cBig = (cBig * fact[cntBig]) % MOD;     cLess = (cLess * fact[cntLess]) % MOD;     ans = (ans * cBig) % MOD;     ans = (ans * cLess) % MOD;     ans = (ans * fact[rem]) % MOD;     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long TC = 1;     for (long long tt = 1; tt <= (long long)(TC); ++tt) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000001;   const int MAXN = 251;   struct Person {     int cnt, last;     int id;     Person(int id = -1) : id(id) {       cnt = 0;       last = 0;     }     bool operator<(const Person& p) const {       if (cnt != p.cnt) return cnt > p.cnt;       return last < p.last;     }   };   int n, k, m, a;   vector<Person> score;   vector<int> res;   bool can_win(int id) {     vector<Person> tmp(score);     if (m - a > 0) {       tmp[id].cnt += m - a;       tmp[id].last = m - 1;     }     if (tmp[id].cnt == 0) return false;     sort(tmp.begin(), tmp.end());     for (int i = 0; i < k; i++)       if (tmp[i].id == score[id].id) return true;     return false;   }   bool can_lose(int id) {     if (score[id].cnt == 0) return true;     int ptop = a;     int better = id;     for (int j = id + 1; j < n; j++) {       Person cur(score[j]);       while (ptop < m && score[id] < cur) {         cur.cnt++;         cur.last = ptop++;       }       if (cur < score[id]) better++;     }     return better >= k;   }   int main() {     scanf( %d%d%d%d , &n, &k, &m, &a);     score.resize(n);     res.resize(n);     for (int i = 0; i < n; i++) score[i].id = i;     for (int i = 0; i < a; i++) {       int id;       scanf( %d , &id);       id--;       score[id].cnt++;       score[id].last = i;     }     sort(score.begin(), score.end());     for (int i = 0; i < n; i++) {       int val = 0;       bool w = can_win(i);       bool l = can_lose(i);       if (w && l)         val = 2;       else if (w && !l)         val = 1;       else if (!w && l)         val = 3;       else         throw;       res[score[i].id] = val;     }     for (int i = 0; i < n; i++) printf( %d  , res[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1505;   int n, a[maxn], sum[maxn], ans, val, ID;   int d[maxn * maxn], cnt;   vector<int> v[maxn * maxn];   vector<int> vv[maxn];   unordered_map<int, int> mp, L[maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum[i] = sum[i - 1] + a[i];     for (int i = 1; i <= n; i++) {       for (int j = i - 1; ~j; j--) {         int t = sum[i] - sum[j];         d[++cnt] = t;         if (!L[i][t]) L[i][t] = j + 1, vv[i].push_back(t);       }     }     sort(d + 1, d + 1 + cnt);     cnt = unique(d + 1, d + 1 + cnt) - d - 1;     for (int i = 1; i <= cnt; i++) mp[d[i]] = i;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < vv[i].size(); j++) {         int t = vv[i][j], id = mp[t];         if (v[id].empty() || v[id].back() < L[i][t]) {           v[id].push_back(i);         }         if (ans < v[id].size()) {           ans = v[id].size();           val = t;           ID = id;         }       }     }     printf( %d n , ans);     for (int i : v[ID]) {       printf( %d %d n , L[i][val], i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 51123987;   int len, n;   char s[4000010], tmp[2000010];   long long p[4000010];   long long sum, ans;   long long le[4000010], ri[4000010];   int main() {     scanf( %d%s , &len, tmp + 1);     s[++n] =  @ ;     for (int i = 1; i <= len; i++) {       s[++n] =  # ;       s[++n] = tmp[i];     }     s[++n] =  # ;     long long id = 0, mx = 0;     for (long long i = 1; i <= n; i++) {       p[i] = ((mx > i) ? min(p[id * 2 - i], mx - i) : 1);       while (s[i + p[i]] == s[i - p[i]]) p[i]++;       if (i + p[i] > mx) {         mx = i + p[i];         id = i;       }       sum += 1ll * p[i] / 2;       sum %= mod;       le[i - p[i]]++;       le[i]--;       ri[i - 1]++;       ri[i + p[i] - 1]--;     }     sum = sum * (sum - 1) / 2;     sum %= mod;     for (int i = 1; i <= n; i++) {       le[i] += le[i - 1];       ri[i] += ri[i - 1];       if (i % 2 == 0) {         sum -= 1ll * ans * le[i] % mod;         sum = (sum + mod) % mod;         ans = (ans + ri[i]) % mod;       }     }     cout << (sum + mod) % mod;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 55;   const int dx[4] = {0, 0, -1, 1};   const int dy[4] = {-1, 1, 0, 0};   int n, m, cnt, cur;   char a[N][N], s[N][N];   bool vis[N][N];   inline void dfs(int x, int y) {     vis[x][y] = 1;     ++cur;     for (int xx, yy, k = 0; k < 4; k++) {       xx = x + dx[k], yy = y + dy[k];       if (!(0 <= xx && xx < n && 0 <= yy && yy < m)) continue;       if (!vis[xx][yy] && s[xx][yy] ==  # ) dfs(xx, yy);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , a[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (a[i][j] ==  # ) cnt++;     if (cnt <= 2) {       printf( -1 n );       return 0;     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (a[i][j] ==  # ) {           for (int u = 0; u < n; u++)             for (int v = 0; v < m; v++) s[u][v] = a[u][v];           s[i][j] =  . ;           cur = 0;           memset(vis, 0, sizeof vis);           try {             for (int u = 0; u < n; u++)               for (int v = 0; v < m; v++)                 if (s[u][v] ==  # ) {                   dfs(u, v);                   if (cur == cnt - 1)                     throw -1;                   else {                     throw 1;                   }                 }           } catch (int res) {             if (res == 1) {               printf( 1 n );               return 0;             }           }         }     printf( 2 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const bool debug = 0;   int main() {     if (!debug) {       cin.tie(0);       cout.tie(0);       ios::sync_with_stdio(0);     }     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       vector<int> a(n);       for (int i = 0; i < n; i++) cin >> a[i];       reverse(a.begin(), a.end());       for (int i = 0; i * 2 < n; i++) a[i] *= -1;       for (int i = 0; i < n; i++) cout << a[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int modulo = 1000000009;   int h;   long long int expo2[31];   long long int expo3[31];   long long int expo4[31];   long long int memmaneres0[1001];   long long int maneres0(int n) {     long long int &mem = memmaneres0[n];     if (mem != -1) return mem;     mem = 0;     if (n < h) {       mem = expo4[n] % modulo;       return mem;     }     for (int t = 1; t <= h; t++)       mem = (mem + expo3[t - 1] * maneres0(n - t)) % modulo;     return mem;   }   long long int memmaneres1[1001][31];   long long int maneres1(int n, int i) {     long long int &mem = memmaneres1[n][i];     if (mem != -1) return mem;     mem = 0;     if (n < h) {       mem = expo3[i - 1] * expo4[n - i] % modulo;       return mem;     }     for (int t = 1; t <= h; t++) {       if (t < i)         mem = (mem + expo2[t - 1] * maneres1(n - t, i - t)) % modulo;       else if (t > i)         mem = (mem + expo2[i - 1] * maneres1(n - i, t - i)) % modulo;     }     return mem;   }   long long int memmaneres2[1001][31][31];   long long int maneres2(int n, int i, int j) {     long long int &mem = memmaneres2[n][i][j];     if (mem != -1) return mem;     mem = 0;     if (n < h) {       mem = expo2[i - 1] * expo3[j - i - 1] % modulo * expo4[n - j] % modulo;       return mem;     }     for (int t = 1; t <= h; t++) {       if (t < i)         mem = (mem + maneres2(n - t, i - t, j - t)) % modulo;       else if (t > i and t < j)         mem = (mem + maneres2(n - i, t - i, j - i)) % modulo;       else if (t > j)         mem = (mem + maneres2(n - i, j - i, t - i)) % modulo;     }     return mem;   }   long long int memmaneres3[1001][31][31][31];   long long int maneres3(int n, int i, int j, int k) {     long long int &mem = memmaneres3[n][i][j][k];     if (mem != -1) return mem;     mem = 0;     if (n < h) {       mem = expo2[j - i - 1] * expo3[k - j - 1] % modulo * expo4[n - k] % modulo;       return mem;     }     if (i > 1) {       mem = maneres3(n - 1, i - 1, j - 1, k - 1);     } else {       for (int t = i + 1; t <= h; t++) {         if (t < j)           mem = (mem + maneres3(n - i, t - i, j - i, k - i)) % modulo;         else if (t > j and t < k)           mem = (mem + maneres3(n - i, j - i, t - i, k - i)) % modulo;         else if (t > k)           mem = (mem + maneres3(n - i, j - i, k - i, t - i)) % modulo;       }     }     return mem;   }   int main() {     for (int n = 0; n < 1001; n++)       for (int i = 0; i < 31; i++)         for (int j = 0; j < 31; j++)           for (int k = 0; k < 31; k++) memmaneres3[n][i][j][k] = -1;     for (int n = 0; n < 1001; n++)       for (int i = 0; i < 31; i++)         for (int j = 0; j < 31; j++) memmaneres2[n][i][j] = -1;     for (int n = 0; n < 1001; n++)       for (int i = 0; i < 31; i++) memmaneres1[n][i] = -1;     for (int n = 0; n < 1001; n++) memmaneres0[n] = -1;     expo2[0] = expo3[0] = expo4[0] = 1;     for (int i = 1; i < 31; i++) {       expo2[i] = (2 * expo2[i - 1]) % modulo;       expo3[i] = (3 * expo3[i - 1]) % modulo;       expo4[i] = (4 * expo4[i - 1]) % modulo;     }     int n;     cin >> n >> h;     long long int r = 0;     for (int i = 0; i < h; i++)       r += expo3[i] * maneres0(n - i - 1) % modulo * 4 % modulo;     for (int i = 0; i < h; i++)       for (int j = i + 1; j < h; j++)         r -= expo2[i] * maneres1(n - i - 1, j - i) % modulo * 12 % modulo;     for (int i = 0; i < h; i++)       for (int j = i + 1; j < h; j++)         for (int k = j + 1; k < h; k++)           r += maneres2(n - i - 1, j - i, k - i) % modulo * 24 % modulo;     for (int i = 1; i < h; i++)       for (int j = i + 1; j < h; j++)         for (int k = j + 1; k < h; k++)           r -= maneres3(n - 1, i, j, k) % modulo * 24 % modulo;     r = (r % modulo + modulo) % modulo;     cout << r << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     vector<long long> a(n);     long long i;     for (i = 0; i < n; i++) cin >> a[i];     map<long long, long long> m;     long long add = 0;     for (i = 0; i < n; i++) {       auto it = m.find(a[i]);       if (it != m.end()) {         pair<long long, long long> p = *it;         long long val = p.second;         m.erase(a[i]);         m.insert(make_pair(a[i], val + 1));       } else {         m.insert(make_pair(a[i], 1));       }       auto itneg = m.find(a[i] - 1);       auto itpos = m.find(a[i] + 1);       if (itneg != m.end()) {         pair<long long, long long> p = *itneg;         add -= p.second;       }       if (itpos != m.end()) {         pair<long long, long long> p = *itpos;         add += p.second;       }     }     long long ans = add;     long long ch = 0;     for (i = 0; i < n; i++) {       ans += a[i] * (i * 2 - (n - 1));       while (ans >= 1000000000000000000) {         ch++;         ans -= 1000000000000000000;       }       while (ans < 0) {         ch--;         ans += 1000000000000000000;       }     }     if (ch >= 0) {       if (ch == 0)         cout << ans << endl;       else {         cout << ch;         vector<int> nums(18);         for (i = 17; i >= 0; i--) {           nums[i] = ans % 10;           ans /= 10;         }         for (i = 0; i < 18; i++) {           cout << nums[i];         }         cout << endl;       }     } else {       ch++;       ans -= 1000000000000000000;       if (ch == 0) {         cout << ans << endl;       } else {         ans = abs(ans);         cout <<  - ;         if (ch < 0) cout << abs(ch);         vector<int> nums(18);         for (i = 17; i >= 0; i--) {           nums[i] = ans % 10;           ans /= 10;         }         for (i = 0; i < 18; i++) {           cout << nums[i];         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long min(long long x, long long y) {     if (x < y) {       return x;     }     return y;   }   int main() {     long long a, b, c;     cin >> a >> b >> c;     cout << min(min(a + b + c, 2 * a + 2 * b), min(2 * a + 2 * c, 2 * b + 2 * c));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long const mod = 1e9 + 7;   long long const inf = 2e18;   inline long long add(long long a, long long b) {     return (a % mod + b % mod + mod) % mod;   }   inline long long mul(long long a, long long b) {     return (a % mod * b % mod + mod) % mod;   }   inline long long sub(long long a, long long b) {     return (a % mod - b % mod + mod) % mod;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long modexp(long long x, long long n) {     if (n == 0) {       return 1;     } else if (n & 1) {       return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;     } else {       return (modexp((x * x) % mod, n / 2) % mod);     }   }   long long modinv(long long n) { return modexp(n, mod - 2); }   long long const MAX = 200001;   long long n;   vector<long long> gr[MAX];   int shortest_cycle(int n) {     int ans = INT_MAX;     for (int i = 0; i < n; i++) {       if (gr[i].size() == 0) continue;       vector<int> dist(n, (int)(1e9));       vector<int> par(n, -1);       dist[i] = 0;       queue<int> q;       q.push(i);       while (!q.empty()) {         int x = q.front();         q.pop();         for (int child : gr[x]) {           if (dist[child] == (int)(1e9)) {             dist[child] = 1 + dist[x];             par[child] = x;             q.push(child);           } else if (par[x] != child and par[child] != x)             ans = min(ans, dist[x] + dist[child] + 1);         }       }     }     if (ans == INT_MAX)       return -1;     else       return ans;   }   void solve() {     cin >> n;     long long a[MAX];     for (int i = 0; i < (n); i++) cin >> a[i];     vector<long long> dp[64];     for (int b = 0; b < (64); b++) {       long long c = 0;       vector<long long> v;       for (int i = 0; i < (n); i++) {         if (a[i] & (1LL << b)) {           c++;           v.push_back(i);         }       }       if (c >= 3) {         cout <<  3 n ;         return;       }       if (v.size() == 2) {         gr[v[0]].push_back(v[1]);         gr[v[1]].push_back(v[0]);       }     }     cout << shortest_cycle(n);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long T = 1;     for (int t = 1; t <= T; t++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void p(double t) {     cout << fixed << setprecision(13) << t << endl;     exit(0);   }   struct tank {     int hp, time, lo, hi, prob;     void scan() {       cin >> hp >> time >> lo >> hi >> prob;       prob = 100 - prob;     }   };   int moven;   vector<int> moves;   tank tanks[2];   double dp[201][201][70];   double curdp[70];   int main() {     for (int i = 0; i < 2; i++) {       tanks[i].scan();     }     if (tanks[0].prob == 0) {       p(0);     }     if (tanks[1].prob == 0) {       p(1);     }     for (int movet = 0; movet < tanks[0].time * tanks[1].time; movet++) {       for (int i = 0; i < 2; i++) {         if (movet % tanks[i].time == 0) {           moves.push_back(i);           moven++;         }       }     }     dp[tanks[0].hp][tanks[1].hp][0] = 1.;     for (int i = tanks[0].hp; i > 0; i--) {       for (int j = tanks[1].hp; j > 0; j--) {         memset(curdp, 0, sizeof(curdp));         double cursum = 0;         double ratio = 1;         for (int k = 0; k < moven; k++) {           int cur = moves[k];           cursum += dp[i][j][k];           dp[i][j][k] = 0;           curdp[k] += cursum;           cursum *= ((100 - tanks[cur].prob) * 0.01);           ratio *= ((100 - tanks[cur].prob) * 0.01);         }         for (int k = 0; k < moven; k++) {           int cur = moves[k];           curdp[k] += cursum / (1 - ratio);           cursum *= ((100 - tanks[cur].prob) * 0.01);         }         for (int k = 0; k < moven; k++) {           int nk = (k + 1) % moven;           int cur = moves[k];           double p = curdp[k] * (tanks[cur].prob * 0.01 /                                  (tanks[cur].hi - tanks[cur].lo + 1));           if (cur == 0) {             for (int amt = tanks[cur].lo; amt <= tanks[cur].hi; amt++) {               dp[i][max(0, j - amt)][nk] += p;             }           } else {             for (int amt = tanks[cur].lo; amt <= tanks[cur].hi; amt++) {               dp[max(0, i - amt)][j][nk] += p;             }           }         }       }     }     double ans = 0;     for (int i = 1; i <= tanks[0].hp; i++) {       for (int k = 0; k < moven; k++) {         ans += dp[i][0][k];       }     }     p(ans);   }


#include <bits/stdc++.h>   using namespace std;   const int L = 21;   const int N = 1 << L;   int n, ans, a[N], best[N][3];   void add(int mask, int value) {     auto &b = best[mask];     b[2] = value;     if (b[2] > b[1]) swap(b[2], b[1]);     if (b[1] > b[0]) swap(b[1], b[0]);   }   bool possible() {     for (int i = 0; i < (int)(n); i++) {       auto target = ans & ~a[i];       auto &b = best[target];       if (b[0] > i && b[1] > i) return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     memset(best, -1, sizeof best);     cin >> n;     for (int i = 0; i < (int)(n); i++) {       cin >> a[i];       add(a[i], i);     }     for (int bit = 0; bit < (int)(L); bit++)       for (int mask = 0; mask < (int)(N); mask++)         if ((mask >> bit) & 1) {           add(mask - (1 << bit), best[mask][0]);           add(mask - (1 << bit), best[mask][1]);         }     for (int bit = (int)(L)-1; bit >= ((int)0); bit--) {       ans |= 1 << bit;       if (!possible()) ans -= 1 << bit;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   const int N = (int)(5e5) + 7;   int cnt[N];   int last[N];   int slast[N];   int pcnt[N];   long long psum[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       cnt[x]++;       pcnt[x]++;       psum[x] += x;     }     for (int i = 0; i < N; i++) {       if (i) pcnt[i] += pcnt[i - 1];       if (i) psum[i] += psum[i - 1];     }     int mda = -1;     int tmda = -1;     for (int i = 0; i < N; i++) {       if (cnt[i]) {         tmda = mda;         mda = i;       }       if (cnt[i] >= 2) {         tmda = i;       }       last[i] = mda;       slast[i] = tmda;     }     auto as = [&](int x, int y) {       long long value = (long long)(1e18) + 7;       for (int i = 0; i < N; i += y) {         int l = i, r = min(N - 1, i + y - 1);         if (last[r] >= x) {           value = min(value, (long long)(last[r] / y - (last[r] - x) / y));         }       }       return value;     };     auto ans_two = [&](int x, int y) {       long long best_a = (long long)(1e18) + 7;       long long best_b = (long long)(1e18) + 7;       for (int i = 0; i < N; i += y) {         int l = i, r = min(N - 1, i + y - 1);         if (last[r] >= l && last[r] >= x) {           long long value = last[r] / y - (last[r] - x) / y;           if (value < best_a) {             swap(value, best_a);           }           if (value < best_b) {             swap(value, best_b);           }         }         if (slast[r] >= l && slast[r] >= x) {           long long value = slast[r] / y - (slast[r] - x) / y;           if (value < best_a) {             swap(value, best_a);           }           if (value < best_b) {             swap(value, best_b);           }         }       }       return best_a + best_b;     };     auto check = [&](long long x, int y, long long s) {       if (x >= N) {         return false;       }       if (s - as(2 * x, y) >= x) {         return true;       }       if (s - ans_two(x, y) >= x) {         return true;       }       return false;     };     long long ans = 4;     vector<int> ord_y;     for (int y = 2; y < N; y++) {       ord_y.push_back(y);     }     shuffle(ord_y.begin(), ord_y.end(), rnd);     auto seg_sum = [&](int l, int r) { return psum[r] - (l ? psum[l - 1] : 0); };     auto seg_cnt = [&](int l, int r) { return pcnt[r] - (l ? pcnt[l - 1] : 0); };     auto get_sum = [&](int y) {       long long sum = 0;       for (int i = 0; i < N; i += y) {         int l = i, r = min(N - 1, i + y - 1);         sum += seg_cnt(l, r) * (long long)(i / y);       }       return sum;     };     for (int y : ord_y) {       long long s = get_sum(y);       long long vl = max(2ll, ans / y + 1);       if (check(vl, y, s)) {         int vr = N;         while (vl < vr - 1) {           int vm = (vl + vr) / 2;           if (check(vm, y, s)) {             vl = vm;           } else {             vr = vm;           }         }         ans = vl * (long long)y;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, z, k, w, a, b, res, dist;   vector<int> edge[200005], new_edge[200005];   int deg[200005], depth[200005], far[200005], far_dist[200005];   pair<int, int> dp[200005];   bool has_crit_point[200005];   void DFS(int x, int y, int pre) {     if (y == 0)       depth[x] = 0;     else       depth[x] = depth[y] + 1;     int nxt_pre = pre;     if (deg[x] >= 3) {       has_crit_point[x] = 1;       nxt_pre = x;       if (pre != 0) {         new_edge[pre].push_back(x);         new_edge[x].push_back(pre);       }     }     for (int i : edge[x])       if (i != y) DFS(i, x, nxt_pre), has_crit_point[x] |= has_crit_point[i];     if (!has_crit_point[x]) dp[x].first = x;     for (int i : edge[x])       if (!has_crit_point[i]) {         if (dp[x].first == 0 || depth[dp[i].first] >= depth[dp[x].first])           dp[x].second = dp[x].first, dp[x].first = dp[i].first;         else if (dp[x].second == 0 || depth[dp[i].first] > depth[dp[x].second])           dp[x].second = dp[i].first;       }   }   void solve(int x, int y) {     for (int i : new_edge[x])       if (i != y) solve(i, x);     int p = -1, q = -1, distp = 0, distq = 0;     if (dp[x].second > 0) {       far[x] = x;       far_dist[x] = depth[dp[x].first] + depth[dp[x].second] - 2 * depth[x];     }     for (int i : new_edge[x]) {       int v = i;       int distv =           depth[dp[far[v]].first] + depth[dp[far[v]].second] - depth[far[v]] * 2;       if (p == -1 || depth[far[v]] > depth[p])         q = p, distq = distp, p = far[v], distp = distv;       else if (depth[far[v]] == depth[p] && distv > distp)         q = p, distq = distp, p = far[v], distp = distv;       else if (q == -1 || depth[far[v]] > depth[q])         distq = distv, q = far[v];       else if (depth[far[v]] == depth[q] && distv > distq)         distq = distv, q = far[v];       if (depth[far[v]] > depth[far[x]])         far[x] = far[v], far_dist[x] = far_dist[v];       else if (depth[far[v]] == depth[far[x]] && far_dist[v] > far_dist[x])         far[x] = far[v], far_dist[x] = far_dist[v];     }     if (dp[x].second > 0 && q == -1)       q = x, distq = depth[dp[x].first] + depth[dp[x].second] - 2 * depth[x];     if (p == -1 || q == -1) return;     if (depth[p] + depth[q] - 2 * depth[x] > res) {       res = depth[p] + depth[q] - 2 * depth[x];       dist = distp + distq;       a = p, b = q;     } else if (depth[p] + depth[q] - 2 * depth[x] == res &&                distp + distq > dist) {       dist = distp + distq;       a = p, b = q;     }   }   int main() {     scanf( %d , &n);     for (int i = (0); i < (n - 1); i++)       scanf( %d %d , &x, &y), edge[x].push_back(y), edge[y].push_back(x),           deg[x]++, deg[y]++;     for (int i = (1); i <= (n); i++)       if (deg[i] >= 3) x = i;     DFS(x, 0, 0);     a = b = -1;     res = dist = 0;     solve(x, 0);     printf( %d %d n%d %d n , dp[a].first, dp[b].first, dp[a].second,            dp[b].second);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:36777216 )   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   const int oo = 0x3f3f3f3f;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline T lowbit(T n) {     return (n ^ (n - 1)) & n;   }   template <class T>   inline int countbit(T n) {     return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));   }   template <class T>   inline T gcd(T a, T b) {     T c;     while (a != 0) {       c = a;       a = b % a;       b = c;     }     return b;   }   template <class T>   inline T mabs(T a) {     if (a < 0)       return -a;     else       return a;   }   typedef vector<int> VI;   typedef vector<VI> VII;   typedef vector<string> VS;   const int N = (int)21;   const int MOD = (int)1e9 + 7;   int g[N][N];   inline int solve(int testnum) {     int n, k;     cin >> n >> k;     map<string, int> names;     names.clear();     map<int, string> idx2name;     idx2name.clear();     for (int i = (0); i < int(n); i++) {       string s;       cin >> s;       names[s] = i;       idx2name[i] = s;     }     for (int i = (0); i < int(n); i++)       for (int j = (0); j < int(n); j++) g[i][j] = 1;     for (int i = (0); i < int(k); i++) {       string a, b;       cin >> a >> b;       int ia = names[a];       int ib = names[b];       g[ia][ib] = 0;     }     int best = 0;     for (int mask = (0); mask < int((1 << (n))); mask++) {       int f = 1;       for (int i = (0); i < int(n); i++) {         if ((mask & (1 << (i))) > 0) {           for (int j = (0); j < int(n); j++)             if ((mask & (1 << (j))) > 0) {               if (g[i][j] == 0) {                 f = 0;               }             }         }       }       if (!f || !mask) continue;       if (countbit(mask) > countbit(best)) {         best = mask;       }     }     VS ret;     ret.clear();     for (int b = (0); b < int(n); b++)       if ((best & (1 << (b))) > 0) {         ret.push_back(idx2name[b]);       }     sort((ret).begin(), (ret).end());     printf( %d n , ret.size());     for (auto &j : ret) printf( %s n , j.c_str());     return 0;   }   int main(int argc, char *argv[]) {     ios::sync_with_stdio(false);     int t = 1;     if (argc > 1) t = atoi(argv[1]);     for (int _t = (1); _t < int(t + 1); _t++) {       int ret = ::solve(_t);       if (ret == -1) {         cerr <<  Failed test #  << _t << endl;         break;       }       cerr <<  Solved test #  << _t << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int sign = 1;     int current = 0;     int value = 0;     for (char c;   n  != (c = getchar());) {       if (c ==  +  or c ==  - ) {         value += sign * current;         sign = (c ==  +  ? 1 : -1);         current = 0;       }       current *= 10;       current += c -  0 ;     }     printf( %d , value + sign * current);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int IN() {     int x = 0, ch = getchar(), f = 1;     while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar();     if (ch ==  - ) {       f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   double nx, ny, T, nc, w, h, Dist[1005];   int n;   struct Point {     double x, y;   } a[1005];   double dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};   double sqr(double a) { return a * a; }   double dist(double xa, double ya, double xb, double yb) {     return sqrt(sqr(xb - xa) + sqr(yb - ya));   }   double Calc(double x, double y) {     for (int i = 0; i <= n - 1; i++) Dist[i] = dist(x, y, a[i].x, a[i].y);     double Min1 = 1 << 30, pos1, Min2 = 1 << 30;     for (int i = 0; i <= n - 1; i++)       if (Dist[i] < Min1) Min1 = Dist[i], pos1 = i;     for (int i = 0; i <= n - 1; i++)       if (i != pos1 && Dist[i] < Min2) Min2 = Dist[i];     return Min2;   }   int main() {     srand((unsigned)time(NULL));     double Max = 0;     w = 1.0 * IN(), h = 1.0 * IN(), n = IN();     for (int i = 0; i <= n - 1; i++) a[i].x = IN(), a[i].y = IN();     for (int i = 1; i <= 60; i++) {       double nx = rand() * rand() % (int)w, ny = rand() * rand() % (int)h;       T = h, nc = Calc(nx, ny);       while (T > 1e-12) {         bool chg = 0;         double X, Y;         for (int j = 0; j <= 3; j++) {           double x = nx + T * dir[j][0], y = ny + T * dir[j][1];           if (x < 0 || x > w || y < 0 || y > h) continue;           double xc = Calc(x, y);           if (xc > nc) {             X = x, Y = y, nc = xc;             chg = 1;           }         }         if (chg) {           nx = X, ny = Y;         } else           T *= 0.9;       }       Max = max(Max, Calc(nx, ny));     }     printf( %.15lf n , Max);   }


#include <bits/stdc++.h>   using namespace std;   int arr[100001];   int tree[100001 * 3];   void init(int node, int b, int e) {     if (b == e) {       tree[node] = arr[b];       return;     }     int Left = node * 2;     int Right = node * 2 + 1;     int mid = (b + e) / 2;     init(Left, b, mid);     init(Right, mid + 1, e);     tree[node] = tree[Left] + tree[Right];   }   int query(int node, int b, int e, int i, int j) {     if (i > e || j < b) return 0;     if (b >= i && e <= j) return tree[node];     int Left = node * 2;     int Right = node * 2 + 1;     int mid = (b + e) / 2;     int p1 = query(Left, b, mid, i, j);     int p2 = query(Right, mid + 1, e, i, j);     return p1 + p2;   }   void update(int node, int b, int e, int i, int newvalue) {     if (i > e || i < b) return;     if (b >= i && e <= i) {       tree[node] = newvalue;       return;     }     int Left = node * 2;     int Right = node * 2 + 1;     int mid = (b + e) / 2;     update(Left, b, mid, i, newvalue);     update(Right, mid + 1, e, i, newvalue);     tree[node] = tree[Left] + tree[Right];   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     init(1, 1, n);     vector<int> v;     while (m--) {       int x, y;       cin >> x >> y;       int sum = query(1, 1, n, x, y);       v.push_back(sum);     }     int ans = 0;     for (int i = 0; i < v.size(); i++) {       if (v[i] > 0) ans += v[i];     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     long long ans = n;     for (__typeof(m) i = 0; i < m; i++) {       long long a, b;       cin >> a >> b;       ans = min(ans, b - a + 1);     }     cout << ans << endl;     for (__typeof(n) i = 0; i < n; i++) {       cout << i % ans <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct MaxFlow {     int T;     struct node {       int to, nxt, cap;     } ed[811 * 811 << 1];     int head[811], cur[811], cnt, ans;     int h[811];     void addedge(int u, int v, int cap) {       ed[cnt].to = v, ed[cnt].cap = cap, ed[cnt].nxt = head[u], head[u] = cnt++;       ed[cnt].to = u, ed[cnt].cap = 0, ed[cnt].nxt = head[v], head[v] = cnt++;     }     bool bfs() {       memset(h, -1, sizeof(h));       queue<int> q;       h[1] = 0, q.push(1);       while (!q.empty()) {         int now = q.front();         q.pop();         for (int i = head[now]; ~i; i = ed[i].nxt) {           if (ed[i].cap && h[ed[i].to] == -1) {             h[ed[i].to] = h[now] + 1;             q.push(ed[i].to);           }         }       }       return h[T] != -1;     }     int dfs(int x, int f) {       if (x == T || f == 0) return f;       int w, used = 0;       for (int i = cur[x]; ~i; i = ed[i].nxt) {         if (h[ed[i].to] == h[x] + 1) {           w = f - used;           w = dfs(ed[i].to, min(w, ed[i].cap));           ed[i].cap -= w;           ed[i ^ 1].cap += w;           if (ed[i].cap) cur[x] = i;           used += w;           if (used == f) return f;         }       }       if (!used) h[x] = -1;       return used;     }     void dinic() {       while (bfs()) {         for (int i = 1; i <= T; i++) {           cur[i] = head[i];         }         ans += dfs(1, 100000005);       }     }     void init(int t) {       memset(head, -1, sizeof(head));       ans = cnt = 0;       T = t;     }   } flow;   int dis[811][811], st[811];   int main() {     int v, e, n, k;     scanf( %d%d%d%d , &v, &e, &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &st[i]);     for (int i = 1; i <= v; i++)       for (int j = i + 1; j <= v; j++) dis[i][j] = dis[j][i] = 100000005;     for (int i = 1; i <= e; i++) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       dis[a][b] = min(dis[a][b], c);       dis[b][a] = dis[a][b];     }     for (int x = 1; x <= v; x++) {       for (int i = 1; i <= v; i++) {         for (int j = 1; j <= v; j++) {           dis[i][j] = min(dis[i][x] + dis[x][j], dis[i][j]);         }       }     }     int l = 0, r = 1731311, ct = -1;     while (l <= r) {       int mid = l + r >> 1;       flow.init(1 + n + v + 2);       for (int i = 1; i <= n; i++) {         flow.addedge(1, i + 1, 1);         for (int j = 1; j <= v; j++) {           if (dis[st[i]][j] <= mid) {             flow.addedge(i + 1, 1 + j + n, 1);           }         }       }       for (int i = 1; i <= v; i++) flow.addedge(1 + i + n, 1 + v + n + 1, 1);       flow.addedge(1 + v + n + 1, 1 + v + n + 2, k);       flow.dinic();       if (flow.ans == k) {         ct = mid;         r = mid - 1;       } else         l = mid + 1;     }     printf( %d n , ct);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[205], b[205];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     for (int i = 0; i < 512; i++) {       int cnt = 0;       for (int j = 0; j < n; j++) {         for (int k = 0; k < m; k++) {           if (((a[j] & b[k]) | i) == i) {             cnt++;             break;           }         }       }       if (cnt == n) {         cout << i << endl;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char cur =  a ;     vector<char> res(s.size());     for (int i = 0; i < s.size(); i++) {       if (cur >  z ) {         res[i] = s[i];       } else if (s[i] <= cur) {         res[i] = cur;         cur++;       } else {         res[i] = s[i];       }     }     if (cur >  z ) {       for (auto elem : res) {         cout << elem;       }     } else {       cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int N = 1e6 + 10;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, m;     cin >> n >> m;     int g[n];     for (int i = 0; i < n; ++i) cin >> g[i];     sort(g, g + (sizeof(g) / sizeof(*g)));     long long d[n];     fill(d, d + (sizeof(d) / sizeof(*d)), 0);     for (int i = m; i < n; ++i) d[i] = g[i - m] + d[i - m];     long long sm = 0;     for (int i = 0; i < n; ++i) {       sm += g[i] + d[i];       cout << sm <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5000010;   vector<long long> v[N];   int a[N];   long long com2(long long n) { return n * (n - 1) / 2; }   int main() {     long long n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (a[i] >= m) {         printf( 0 n );         return 0;       }     }     for (int i = 1; i <= n; i++)       if (a[i]) {         for (int j = i; j <= n; j++) v[0].push_back(a[j]);         break;       }     if (v[0].size() == 2) {       long long lt = 1, rt = m / v[0][0] + 1, mid;       long long ans = 0;       while (lt <= rt) {         mid = (lt + rt) >> 1;         if (1LL * mid * v[0][0] + v[0][1] >= m)           ans = mid, rt = mid - 1;         else           lt = mid + 1;       }       cout << ans << endl;     } else if (v[0].size() == 3) {       long long lt = 1, rt = 2000000000, mid;       long long ans = 0;       while (lt <= rt) {         mid = (lt + rt) >> 1;         long long p = com2(mid + 1);         if (m / v[0][0] < p) {           ans = mid, rt = mid - 1;           continue;         }         p = p * v[0][0] + mid * v[0][1] + v[0][2];         if (p >= m) {           ans = mid, rt = mid - 1;         } else           lt = mid + 1;       }       cout << ans << endl;     } else {       for (int i = 1; i < N; i++) {         long long sum = 0;         for (int j = 0; j < v[i - 1].size(); j++) {           sum += v[i - 1][j];           v[i].push_back(sum);           if (sum >= m) {             printf( %d n , i);             return 0;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int MAX = 2e5 + 5;   const long long MAX2 = 11;   const int MOD = 1000000000 + 7;   const long long INF = 20000;   const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};   const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};   const double pi = acos(-1);   long long tc, a, b, c, d, x[3];   bool ans;   inline bool cek() {     sort(x, x + 3);     if (x[0] < 0 || 2 * x[2] - x[0] - x[1] > a ||         (a - 2 * x[2] + x[0] + x[1]) % 3 || x[0] + x[1] + x[2] > b ||         (x[0] + x[1] + x[2]) % 3 != b % 3)       return 0;     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> tc;     while (tc--) {       cin >> a >> b >> c >> d;       if (c > d) c ^= d ^= c ^= d;       a -= b;       ans = 0;       x[0] = 0, x[1] = c, x[2] = d;       ans |= cek();       x[0] = 0, x[1] = c, x[2] = c + d;       ans |= cek();       x[0] = 0, x[1] = d, x[2] = c + d;       ans |= cek();       x[0] = 0, x[1] = c - d, x[2] = c;       ans |= cek();       x[0] = 0, x[1] = d - c, x[2] = d;       ans |= cek();       if (ans)         cout <<  yes n ;       else         cout <<  no n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int a[11];   int tmpa[11];   int sum;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       tmpa[i] = a[i];       sum += a[i];     }     sort(a, a + n);     if (n == 1) {       cout << -1 << endl;     } else {       if (n == 2 && a[0] == a[1]) {         cout << -1 << endl;       } else {         cout << 1 << endl;         for (int i = 0; i < n; i++) {           if (tmpa[i] == a[0]) {             cout << i + 1 << endl;             break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, vis[5009], d[5009][5009];   vector<int> e;   vector<vector<int> > g;   char s[5009];   void dfs(int v, int pr = -1) {     int i;     vis[v] = 1;     for (auto u : g[v]) {       if (d[u][pr] == 1 && pr != -1) {         cout << u <<     << pr <<     << v;         exit(0);       }       if (!vis[u]) dfs(u, v);     }   }   int main() {     int i, j;     scanf( %d , &n);     g.resize(n + 1);     ;     for (i = (1); i <= (n); i++) {       scanf( %s , s);       ;       for (j = (0); j <= (n - 1); j++)         if (s[j] ==  1 ) {           g[i].push_back(j + 1);           d[i][j + 1] = 1;         }     };     for (i = (1); i <= (n); i++) {       if (!vis[i]) dfs(i);     }     cout <<  -1 ;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n, m;     char c[2000][2000];     int rowcount[2000];     int colcount[2000], wallcount = 0;     for (int i = 0; i < 2000; i++) {       rowcount[i] = 0;       colcount[i] = 0;     }     scanf( %d , &n);     scanf( %d , &m);     for (int i = 0; i < n; i++) scanf( %s , c[i]);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (c[i][j] ==  * ) {           rowcount[i]++;           colcount[j]++;           wallcount++;         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (c[i][j] ==  *  && rowcount[i] + colcount[j] - 1 == wallcount ||             c[i][j] ==  .  && rowcount[i] + colcount[j] == wallcount) {           printf( YES n );           printf( %d %d n , i + 1, j + 1);           return 0;         }       }     }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 30000;   long long t, n, a[N], l, r;   bool possible() {     if (l > r) return 1;     long long o = a[l - 1];     a[l - 1] = 0;     for (long long i = l; i <= r; i++) {       long long v = max(a[i - 1], a[i] - o);       if (v > a[r + 1]) return 0;       o = a[i] - v;       a[i] = v;     }     return 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> t;     while (t--) {       cin >> n;       for (long long i = 0; i < n; i++) cin >> a[i];       l = 1;       r = n - 2;       for (; l < n && a[l - 1] >= a[l]; l++)         ;       for (; r >= 0 && a[r] <= a[r + 1]; r--)         ;       cout << (possible() ?  YES n  :  NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200005;   const int INFTY = 1 << 30;   int D, N, M;   pair<int, int> pos[MAXN];   vector<int> S;   int nxt[MAXN];   int main() {     scanf( %d %d %d , &D, &N, &M);     for (int i = 0; i < M; i++) scanf( %d %d , &pos[i].first, &pos[i].second);     sort(pos, pos + M);     pos[M] = make_pair(D, 0);     for (int i = M - 1; i >= 0; i--) {       while (!S.empty() && pos[S.back()].second > pos[i].second) S.pop_back();       nxt[i] = S.empty() ? M : S.back();       S.push_back(i);     }     long long ans = 0;     int cur = N - pos[0].first;     for (int i = 0; i < M; i++) {       if (cur < 0) {         printf( -1 n );         return 0;       }       int need = min(N, pos[nxt[i]].first - pos[i].first);       int add = max(0, need - cur);       ans += (long long)pos[i].second * add;       cur += add - (pos[i + 1].first - pos[i].first);     }     if (cur < 0)       printf( -1 n );     else       printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, a, b;     cin >> n >> a >> b;     map<long long, int> M;     map<pair<int, int>, int> M2;     long long ans = 0;     for (int i = 0; i < n; ++i) {       int x, vx, vy;       cin >> x >> vx >> vy;       auto temp = (long long)a * vx - vy;       ans += M[temp] - M2[{vx, vy}];       M[temp]++;       M2[{vx, vy}]++;     }     cout << ans * 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200001], b[200001];   int n;   int main(void) {     int t;     cin >> t;     while (t--) {       cin >> n;       long long sum = 0LL;       for (int i = 0; i < n; i++) {         cin >> a[i];       }       for (int i = 0; i < n; i++) {         cin >> b[i];         sum += b[i];       }       vector<pair<long long, long long> > c;       for (int i = 0; i < n; i++) {         c.push_back(make_pair(a[i], i));       }       sort(c.begin(), c.end());       long long ans = sum;       for (int i = 0; i < n; i++) {         sum -= b[c[i].second];         ans = min(ans, max(sum, c[i].first));       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using vll = vector<ll>;   using vvll = vector<vll>;   using vb = vector<bool>;   using vd = vector<double>;   using vs = vector<string>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using pdd = pair<double, double>;   using vpii = vector<pii>;   using vpll = vector<pll>;   using vpdd = vector<pdd>;   template <typename T>   void ckmin(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   void ckmax(T& a, const T& b) {     a = max(a, b);   }   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   namespace __input {   template <class T1, class T2>   void re(pair<T1, T2>& p);   template <class T>   void re(vector<T>& a);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(double& x) {     string t;     re(t);     x = stod(t);   }   template <class Arg, class... Args>   void re(Arg& first, Args&... rest) {     re(first);     re(rest...);   }   template <class T1, class T2>   void re(pair<T1, T2>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& a) {     for (int i = 0; i < (int((a).size())); i++) re(a[i]);   }   template <class T, size_t SZ>   void re(array<T, SZ>& a) {     for (int i = 0; i < (SZ); i++) re(a[i]);   }   }  // namespace __input   using namespace __input;   namespace __output {   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T, size_t SZ>   void pr(const array<T, SZ>& x);   template <class T>   void pr(const vector<T>& x);   template <class T>   void pr(const set<T>& x);   template <class T1, class T2>   void pr(const map<T1, T2>& x);   template <class T>   void pr(const T& x) {     cout << x;   }   template <class Arg, class... Args>   void pr(const Arg& first, const Args&... rest) {     pr(first);     pr(rest...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T, bool pretty = true>   void prContain(const T& x) {     if (pretty) pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0;     if (pretty) pr( } );   }   template <class T>   void pc(const T& x) {     prContain<T, false>(x);     pr(  n );   }   template <class T, size_t SZ>   void pr(const array<T, SZ>& x) {     prContain(x);   }   template <class T>   void pr(const vector<T>& x) {     prContain(x);   }   template <class T>   void pr(const set<T>& x) {     prContain(x);   }   template <class T1, class T2>   void pr(const map<T1, T2>& x) {     prContain(x);   }   void ps() { pr(  n ); }   template <class Arg>   void ps(const Arg& first) {     pr(first);     ps();   }   template <class Arg, class... Args>   void ps(const Arg& first, const Args&... rest) {     pr(first,    );     ps(rest...);   }   }  // namespace __output   using namespace __output;   namespace __algorithm {   template <typename T>   void dedup(vector<T>& v) {     sort((v).begin(), (v).end());     v.erase(unique((v).begin(), (v).end()), v.end());   }   template <typename T>   typename vector<T>::const_iterator find(const vector<T>& v, const T& x) {     auto it = lower_bound((v).begin(), (v).end(), x);     return it != v.end() && *it == x ? it : v.end();   }   template <typename T>   size_t index(const vector<T>& v, const T& x) {     auto it = find(v, x);     assert(it != v.end() && *it == x);     return it - v.begin();   }   template <typename T1, typename T2>   typename vector<pair<T1, T2>>::iterator lower_bound(       const vector<pair<T1, T2>>& v, const T1& x) {     return lower_bound(         (v).begin(), (v).end(), x,         [](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; });   }   template <typename T1, typename T2>   typename vector<pair<T1, T2>>::iterator upper_bound(       const vector<pair<T1, T2>>& v, const T1& x) {     return upper_bound(         (v).begin(), (v).end(), x,         [](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; });   }   }  // namespace __algorithm   using namespace __algorithm;   struct __monostate {     friend istream& operator>>(istream& is,                                const __attribute__((unused)) __monostate& ms) {       return is;     }     friend ostream& operator<<(ostream& os,                                const __attribute__((unused)) __monostate& ms) {       return os;     }   } ms;   namespace __io {   void setIn(string second) { freopen(second.c_str(),  r , stdin); }   void setOut(string second) { freopen(second.c_str(),  w , stdout); }   void setIO(string second =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << setprecision(15);     if (int((second).size())) {       setIn(second +  .in ), setOut(second +  .out );     }   }   }  // namespace __io   using namespace __io;   int main() {     setIO();     ll P;     int K;     re(P, K);     vi poly;     while (P) {       int rem = P % K;       if (int((poly).size()) & 1) {         rem = rem ? K - rem : 0;         poly.push_back(rem);         P = (P + rem) / K;       } else {         poly.push_back(rem);         P = (P - rem) / K;       }     }     ps(int((poly).size()));     pc(poly);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void solve() {     int m, d, w;     long long int ans = 0, lim, g, x, y;     scanf( %d%d%d , &m, &d, &w);     lim = min(m, d);     if (d == 1)       ans = lim * lim;     else {       g = gcd(d - 1, w);       w /= g;       x = lim / w;       y = lim % w;       ans = y * (x + 1) * (x + 1) + (w - y) * x * x;     }     ans -= lim;     ans >>= 1;     printf( %lld n , ans);     return;   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();   }