#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   long long int a, b, c, r; n   while (t--) { n     long long int ans = 0, mx = 0, mn = 0; n     cin >> a >> b >> c >> r; n     if (a >= b) { n       mx = a; n       mn = b; n     } else { n       mx = b; n       mn = a; n     } n     if (c == mx || c == mn) { n       ans = mx - mn - r; n       if (ans < 0) ans = 0; n       cout << ans << endl; n     } else if (c > mn && c < mx) { n       if (abs(c - mn) < r) n         ans = mx - mn - (r + abs(c - mn)); n       else if (abs(mx - c) < r) n         ans = mx - mn - (r + abs(mx - c)); n       else n         ans = mx - mn - 2 * r; n       if (ans < 0) ans = 0; n       cout << ans << endl; n     } else { n       if (mn > c && abs(mn - c) < r) n         ans = mx - mn - (r - abs(mn - c)); n       else if (c > mx && abs(c - mx) < r) n         ans = mx - mn - (r - abs(c - mx)); n       else n         ans = mx - mn; n       if (ans < 0) ans = 0; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t[45]; n int n, i, k, m, kol, ans, ans2, j, l, ans3, ans1, sum; n void summ(int x) { n   for (j = 0; j < k; j++) { n     l = n - x; n     while (ans2 <= m - t[j] and l > 0) { n       l--; n       ans2 += t[j]; n       kol++; n     } n   } n } n int main() { n   cin >> n >> k >> m; n   for (i = 0; i < k; i++) { n     cin >> t[i]; n     sum += t[i]; n   } n   i = 0; n   sort(t, t + k); n   for (i = 0; i <= n; i++) { n     ans2 = 0; n     kol = 0; n     ans3 = 0; n     if (sum * i > m) { n       break; n     } n     if (sum * i == m) { n       ans = max(ans, i * (k + 1)); n     } n     ans2 = sum * i; n     ans3 = (k + 1) * i; n     summ(i); n     ans3 += kol; n     ans = max(ans, ans3); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   long long int temp; n   while (b > 0) { n     temp = b; n     b = a % b; n     a = temp; n   } n   return a; n } n int main() { n   long long int N, m; n   cin >> N >> m; n   long long int a[N], B[m]; n   for (long long int i = 0; i < N; i++) { n     cin >> a[i]; n   } n   long long int n = N - 1; n   long long int A[n]; n   for (long long int i = 1; i < N; i++) { n     A[i - 1] = a[i] - a[i - 1]; n   } n   for (long long int i = 0; i < m; i++) { n     cin >> B[i]; n   } n   long long int result = A[0]; n   for (long long int i = 1; i < n; i++) { n     result = gcd(result, A[i]); n   } n   long long int j = 0; n   while (j < m && result % B[j] != 0) { n     j++; n   } n   if (j < m) { n     cout <<  YES  << endl; n     cout << a[0] <<     << j + 1 << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n vector<int> G[maxn + 5]; n int dp[maxn + 5], d[maxn + 5]; n int main() { n   int n, x, ans = 0; n   scanf( %d , &n); n   for (int i = 2; i <= maxn; i++) n     for (int j = i; j <= maxn; j += i) G[j].push_back(i); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     for (int j = 0; j < G[x].size(); j++) dp[x] = max(dp[x], d[G[x][j]] + 1); n     for (int j = 0; j < G[x].size(); j++) d[G[x][j]] = dp[x]; n     ans = max(ans, dp[x]); n   } n   if (x == 1) n     printf( 1 n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1111; n int n; n string a[N], b[N]; n int t[N]; n int d; n std::set<pair<string, string> > res; n int main() { n   cin >> n >> d; n   for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> t[i]; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) { n       if (!(a[i] == b[j] && a[j] == b[i])) continue; n       int m = min(t[i], t[j]); n       int M = max(t[i], t[j]); n       if (M - m <= 0 || M - m > d) continue; n       res.insert(make_pair(a[i], b[i])); n       res.insert(make_pair(b[i], a[i])); n     } n   cout << res.size() / 2 << endl; n   for (set<pair<string, string> >::iterator it = res.begin(); it != res.end(); n        ++it) { n     string s1 = it->first, s2 = it->second; n     if (s1 < s2) cout << s1 <<     << s2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 500; n long long dp[N]; n long long n; n void init() { n   dp[0] = 1ll; n   dp[1] = 2ll; n   for (int i = 2; i < N; i++) { n     dp[i] = dp[i - 2] + dp[i - 1]; n   } n } n int main() { n   init(); n   while (cin >> n) { n     int cur = 0; n     while (dp[cur] <= n) cur++; n     cout << cur - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[100005]; n   cin >> str; n   int flag = 0, i; n   char checkString[100005]; n   for (i = 0; str[i] !=   0 ; i++) { n     checkString[i] =  a ; n   } n   checkString[i] =   0 ; n   if (strcmp(str, checkString) == 0) { n     checkString[strlen(checkString) - 1] =  z ; n     cout << checkString; n   } else { n     for (i = 0; str[i] !=   0 ; i++) { n       if (!flag && str[i] ==  a ) continue; n       if (flag && str[i] ==  a ) n         break; n       else { n         flag = 1; n         str[i]--; n       } n     } n     cout << str; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e7 + 50; n const double eps = 1e-7; n const int inf = 0x3f3f3f3f; n const long long lnf = 0x3f3f3f3f3f3f3f3f; n const long long mod = 1e9 + 7; n const double pi = 3.141592653589; n int vis[maxn], cnt = 0, h[maxn]; n int sum1[maxn], sum2[maxn]; n int spr[maxn]; n void ol() { n   memset(vis, 1, sizeof(vis)); n   vis[0] = vis[1] = 0; n   for (long long i = 2; i < maxn; i++) { n     if (vis[i]) spr[cnt++] = i; n     for (long long j = 0; i * spr[j] < maxn && j < cnt; j++) { n       vis[i * spr[j]] = 0; n       if (i % spr[j] == 0) break; n     } n   } n } n int f(int x) { n   int ans = 0; n   for (int i = 1; i <= cnt; i++) { n     if (spr[i] <= x) { n       ans = i; n       break; n     } n   } n   return ans; n } n vector<int> v(12); n bool g(int x) { n   v.clear(); n   while (x) { n     v.push_back(x % 10); n     x /= 10; n   } n   bool flage = true; n   int len = v.size(); n   for (int i = 0; i < len / 2; i++) { n     if (v[i] != v[len - i - 1]) { n       flage = false; n       break; n     } n   } n   return flage; n } n bool is(int x) { n   int xx = x, s = 0; n   while (x) { n     s = s * 10 + (x % 10); n     x /= 10; n   } n   return s == xx; n } n int main() { n   long long p, q; n   scanf( %lld%lld , &p, &q); n   ol(); n   for (int i = 1; i < maxn - 20; i++) { n     if (vis[i]) n       sum1[i] = sum1[i - 1] + 1; n     else n       sum1[i] = sum1[i - 1]; n   } n   for (int i = 1; i < 10000000; i++) { n     if (g(i)) { n       sum2[i] = sum2[i - 1] + 1; n     } else { n       sum2[i] = sum2[i - 1]; n     } n   } n   int ans = 0; n   for (int i = 1; i < 1e7; i++) { n     if ((long long)sum1[i] * q <= (long long)sum2[i] * p) { n       ans = i; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll MOD = 1000000007; n ll INF = 1LL << 60; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> tree[n + 1]; n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     tree[u].push_back(v); n     tree[v].push_back(u); n   } n   bool ans = true; n   for (int i = 1; i <= n; i++) { n     if (tree[i].size() == 2) { n       ans = false; n       break; n     } n   } n   cout << (ans ?  YES  :  NO ) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   long long int n, a, b, t, ans, g; n   cin >> t >> a >> b; n   if (a > b) swap(a, b); n   n = (b / gcd(a, b)); n   if (n > LLONG_MAX / a) { n     ans = min(a - 1, t); n   } else { n     n *= a; n     ans = (t / n) * a + min(t % n, a - 1); n   } n   g = gcd(ans, t); n   cout << (ans / g) <<  /  << (t / g) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500500; n bool Ans[N]; n int Weight[N]; n pair<int, int> Edges1[N]; n vector<int> Edges2[N]; n vector<pair<int, vector<int> > > Query[N]; n int P[N]; n stack<pair<int, int> > Was; n void Clear() { n   while (!Was.empty()) Was.pop(); n } n void Restore() { n   while (!Was.empty()) { n     int x = Was.top().first; n     int p = Was.top().second; n     P[x] = p; n     Was.pop(); n   } n } n int F(int x) { n   if (x == P[x]) return x; n   Was.push(make_pair(x, P[x])); n   return P[x] = F(P[x]); n } n bool Un(int x, int y) { n   if ((x = F(x)) == (y = F(y))) return false; n   if (rand() & 1) swap(x, y); n   Was.push(make_pair(x, P[x])); n   P[x] = y; n   return true; n } n int main() { n   srand(time(0)); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     Weight[i] = w; n     Edges1[i] = pair<int, int>(x, y); n     Edges2[w].push_back(i); n   } n   int q; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 0; j < k; j++) { n       int Idx; n       scanf( %d , &Idx); n       int w = Weight[Idx]; n       if (Query[w].empty() || Query[w].back().first != i) n         Query[w].push_back(make_pair(i, vector<int>())); n       Query[w].back().second.push_back(Idx); n     } n   } n   for (int i = 1; i <= q; i++) Ans[i] = true; n   for (int i = 1; i <= n; i++) P[i] = i; n   for (int w = 1; w < N; w++) { n     for (int i = 0; i < Query[w].size(); i++) { n       int Idx = Query[w][i].first; n       vector<int>& v = Query[w][i].second; n       for (int j : v) { n         int x = Edges1[j].first; n         int y = Edges1[j].second; n         Ans[Idx] &= Un(x, y); n       } n       Restore(); n     } n     for (int i : Edges2[w]) { n       int x = Edges1[i].first; n       int y = Edges1[i].second; n       Un(x, y); n     } n     Clear(); n   } n   for (int i = 1; i <= q; i++) puts(Ans[i] ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n using INT = long long; n using pii = pair<int, int>; n using pi3 = pair<int, pii>; n int n, m; n int nxt[100010]; n int a[100010], b[100010]; n set<pii> A, B; n set<pi3> ans; n int calc_max(int u, set<pair<int, int> > &st) { n   auto it = st.upper_bound({m - 1 - u, n + 100}); n   if (it == st.begin()) return 0; n   return (*--it).second; n } n int update_A(int u) { n   int id2 = calc_max(a[u], B); n   if (id2 == 0) return 0; n   int id1 = calc_max(b[id2], A); n   if (id1 == 0) return 0; n   ans.insert(pi3(a[id1] + b[id2], pii(id1, id2))); n } n int update_B(int u) { n   int id2 = calc_max(b[u], A); n   if (id2 == 0) return 0; n   int id1 = calc_max(a[id2], B); n   if (id1 == 0) return 0; n   ans.insert(pi3(a[id2] + b[id1], pii(id2, id1))); n } n int del_A(int u) { n   int id = calc_max(a[u], B); n   if (id == 0) return 0; n   if (calc_max(b[id], A) == u) { n     auto p = pi3(a[u] + b[id], pii(u, id)); n     if (ans.find(p) != ans.end()) ans.erase(p); n   } n } n int del_B(int u) { n   int id = calc_max(b[u], A); n   if (id == 0) return 0; n   if (calc_max(b[id], B) == u) { n     auto p = pi3(b[u] + a[id], pii(id, u)); n     if (ans.find(p) != ans.end()) ans.erase(p); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int u; n     scanf( %d , &u); n     a[i] = u; n     A.insert({u, i}); n   } n   for (int i = 1; i <= n; i++) { n     int u; n     scanf( %d , &u); n     b[i] = u; n     B.insert({u, i}); n   } n   for (int i = 1; i <= n; i++) { n     update_A(i); n     update_B(i); n   } n   while (!A.empty()) { n     int u = 0, v = 0; n     pi3 p; n     if (!ans.empty()) p = *--ans.end(); n     if (ans.empty() || n         p.first < ((*--A.end()).first + (*--B.end()).first) % m) { n       u = (*--A.end()).second, v = (*--B.end()).second; n     } else n       u = p.second.first, v = p.second.second; n     printf( %d  , (a[u] + b[v]) % m); n     del_A(u); n     del_B(v); n     A.erase({a[u], u}); n     B.erase({b[v], v}); n     auto it = A.lower_bound({a[u], u}); n     if (it != A.begin()) update_A((*--it).second); n     it = B.lower_bound({b[v], v}); n     if (it != B.begin()) update_B((*--it).second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int dp[200001][18]; n void solve() { n   long long int n, m, rc, cc, rb, cb, tim = 0; n   cin >> n >> m >> rc >> cc >> rb >> cb; n   long long int dx = 1, dy = 1; n   while ((rc != rb) && (cc != cb)) { n     if ((rc == n) || (rc == 0)) dx = -dx; n     if ((cc == m) || (cc == 0)) dy = -dy; n     rc = rc + dx; n     cc = cc + dy; n     tim++; n   } n   cout << tim <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long int t = 1; n   for (long long int i = 1; i <= 200000; i++) { n     for (long long int j = 0; j < 18; j++) { n       dp[i][j] = dp[i - 1][j] + (((1 << j) & i) == 0); n     } n   } n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[2005]; n long long sum[10005], D[5005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) D[abs(a[i] - a[j])]++; n   sum[5000] = 0; n   double ans = 0; n   for (int i = 4999; i >= 0; i--) sum[i] = sum[i + 1] + D[i]; n   for (int i = 1; i <= 5000; i++) n     for (int j = 1; i + j <= 5000; j++) ans += D[i] * D[j] * sum[i + j + 1]; n   double match = n * (n - 1) / 2.0; n   printf( %.10lf n , ans / match / match / match); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n, x, y; n   cin >> n >> x >> y; n   long long l = y - (n - 1); n   if ((n - 1) + l * l >= x && l > 0) { n     for (long long i = 0; i < n - 1; i++) cout <<  1  ; n     cout << l << endl; n   } else { n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <typename T> n void chmin(T &t, const T &f) { n   if (t > f) t = f; n } n template <typename T> n void chmax(T &t, const T &f) { n   if (t < f) t = f; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n int M, N, V, W; n char A[1010][20][20]; n int D[1010][1010]; n int ds[1010]; n int prv[1010]; n bool used[1010]; n int ansLen; n pair<int, int> ans[1010]; n int main() { n   for (; ~scanf( %d%d%d%d , &M, &N, &V, &W);) { n     for (int u = 0; u < V; ++u) { n       for (int x = 0; x < M; ++x) { n         scanf( %s , A[u][x]); n       } n     } n     for (int u = 0; u < V; ++u) n       for (int v = u + 1; v < V; ++v) { n         int cnt = 0; n         for (int x = 0; x < M; ++x) n           for (int y = 0; y < N; ++y) { n             if (A[u][x][y] != A[v][x][y]) { n               ++cnt; n             } n           } n         D[u][v] = D[v][u] = cnt * W; n       } n     fill_n(ds, V, M * N); n     fill_n(used, V, false); n     fill_n(prv, V, -1); n     int total = 0; n     ansLen = 0; n     for (;;) { n       int um = -1; n       for (int u = 0; u < V; ++u) n         if (!used[u]) { n           if (!~um || ds[um] > ds[u]) { n             um = u; n           } n         } n       if (!~um) { n         break; n       } n       used[um] = true; n       total += ds[um]; n       ans[ansLen++] = make_pair(um, prv[um]); n       for (int v = 0; v < V; ++v) { n         if (ds[v] > D[um][v]) { n           ds[v] = D[um][v]; n           prv[v] = um; n         } n       } n     } n     printf( %d n , total); n     for (int i = 0; i < ansLen; ++i) { n       printf( %d %d n , ans[i].first + 1, ans[i].second + 1); n     } n   } n   return 0; n } n


#include <iostream> n #include<cmath>> n #include<algorithm> n #include <string> n using namespace std; n int arr[101]; n int arrstr[200]; n bool prime(int n) n { n     if(n<=1) n         return false; n     if(n<=3) n         return true; n     for(int i=2;i<=sqrt(n);i++) n     { n         if(n%i==0) n             return false; n  n     } n     return true; n } n bool have(int n,int d) n { n  n     if(n<d) n         return false; n     int x=n; n     while(x) n     { n         if(x%10==d) n         { n             return true; n         } n         x=x/10; n     } n     return false; n } n bool check(int n,int d) n { n     bool a=false; n     int c=n%d; n     int v=n/d; n     for(int i=1;i<=v;i++) n     { n         if(have(d*i+c,d)) n         { n             return true; n         } n  n  n     } n         return false; n } n void solve() n { n long long  n,k,x; n cin>>n>>k; n x=k; n if(n>k) n { n     if(n%k==0) n      cout<<1<<  n ; n else n  cout<<2<<  n ; n return; n } n cout<<k/n+(k%n!=0)<<  n ; n } n  n string sp(int n) n { n     string str=  ; n     while(n>0) n     { n         str+=   ; n         n--; n     } n     return str; n } n string st(int n) n { n     string str=  ; n     while(n>0) n     { n         str+= * ; n         n--; n     } n     return str; n } n int main() n { n     ios::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n  n    int t; n    cin>>t; n    while(t--) n     solve(); n  n  n     return 0; n  n }


#include <bits/stdc++.h> n int n; n struct mjj { n   int x, y; n } a[500001], b[500001]; n int fa[500001]; n int head[500001], end[1000001], len[1000001], next[1000001], tp; n int head2[500001], end2[1000001], len2[1000001], next2[1000001], tp2; n int dad[500001], db[500001], dad2[500001], db2[500001], wz[500001], gs; n int ans; n int cmp(mjj x, mjj y) { return x.x < y.x || x.x == y.x && x.y < y.y; } n int find(int p) { return p == fa[p] ? p : fa[p] = find(fa[p]); } n void dfs(int p) { n   int i, j, k; n   wz[++gs] = p; n   for (i = head[p]; i; i = next[i]) n     if (end[i] != dad[p]) { n       dad[end[i]] = p; n       db[end[i]] = len[i]; n       dfs(end[i]); n     } n } n void dfs2(int p) { n   int i, j, k; n   for (i = head2[p]; i; i = next2[i]) n     if (end2[i] != dad2[p]) { n       dad2[end2[i]] = p; n       db2[end2[i]] = len2[i]; n       dfs2(end2[i]); n     } n } n int main() { n   int i, j, k; n   scanf( %d , &n); n   for (i = 1; i < n; i++) { n     scanf( %d%d , &a[i].x, &a[i].y); n     if (a[i].x > a[i].y) { n       int t = a[i].x; n       a[i].x = a[i].y; n       a[i].y = t; n     } n   } n   for (i = 1; i < n; i++) { n     scanf( %d%d , &b[i].x, &b[i].y); n     if (b[i].x > b[i].y) { n       int t = b[i].x; n       b[i].x = b[i].y; n       b[i].y = t; n     } n   } n   std::sort(a + 1, a + n, cmp); n   std::sort(b + 1, b + n, cmp); n   for (i = 1; i <= n; i++) fa[i] = i; n   ans = n - 1; n   for (i = 1, j = 1; i < n && j < n;) { n     if (cmp(a[i], b[j])) n       i++; n     else if (cmp(b[j], a[i])) n       j++; n     else { n       if (find(a[i].x) != find(a[i].y)) { n         fa[fa[a[i].x]] = fa[a[i].y]; n         ans--; n       } n       i++; n       j++; n     } n   } n   for (i = 1; i <= n; i++) find(i); n   for (i = 1; i < n; i++) n     if (fa[a[i].x] != fa[a[i].y]) { n       int x = fa[a[i].x], y = fa[a[i].y]; n       next[++tp] = head[x]; n       head[x] = tp; n       end[tp] = y; n       len[tp] = i; n       next[++tp] = head[y]; n       head[y] = tp; n       end[tp] = x; n       len[tp] = i; n     } n   for (i = 1; i < n; i++) n     if (fa[b[i].x] != fa[b[i].y]) { n       int x = fa[b[i].x], y = fa[b[i].y]; n       next2[++tp2] = head2[x]; n       head2[x] = tp2; n       end2[tp2] = y; n       len2[tp2] = i; n       next2[++tp2] = head2[y]; n       head2[y] = tp2; n       end2[tp2] = x; n       len2[tp2] = i; n     } n   dfs(fa[1]); n   dfs2(fa[1]); n   printf( %d n , ans); n   for (i = gs; i > 1; i--) n     printf( %d %d %d %d n , a[db[wz[i]]].x, a[db[wz[i]]].y, b[db2[wz[i]]].x, n            b[db2[wz[i]]].y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 12; n int rows, cols, k, n; n char mat[MAXN][MAXN]; n int sol = 0; n int Calc(int a, int b, int c, int d) { n   int ret = 0; n   for (int i = a; i <= c; i++) { n     for (int j = b; j <= d; j++) ret += (mat[i][j] ==  # ); n   } n   return ret; n } n int main() { n   scanf( %d %d %d %d , &rows, &cols, &n, &k); n   for (int i = 0; i <= rows; i++) n     for (int j = 0; j <= cols; j++) mat[i][j] =  . ; n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     mat[x][y] =  # ; n   } n   for (int i = 1; i <= rows; i++) { n     for (int j = 1; j <= cols; j++) { n       for (int ii = i; ii <= rows; ii++) { n         for (int jj = j; jj <= cols; jj++) { n           int sum = Calc(i, j, ii, jj); n           sol += (sum >= k); n         } n       } n     } n   } n   printf( %d n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using db = double; n using str = string; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using pd = pair<db, db>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vd = vector<db>; n using vs = vector<str>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vpd = vector<pd>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n const int MOD = 1e9 + 7; n const ll INF = 1e18; n const ll infl = 1e18 + 5; n const int infi = 1e9 + 5; n const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n constexpr int pct(int x) { return __builtin_popcount(x); } n constexpr int bits(int x) { return 31 - __builtin_clz(x); } n ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } n ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n T fstTrue(T lo, T hi, U f) { n   hi++; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = lo + (hi - lo) / 2; n     f(mid) ? hi = mid : lo = mid + 1; n   } n   return lo; n } n template <class T, class U> n T lstTrue(T lo, T hi, U f) { n   lo--; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = lo + (hi - lo + 1) / 2; n     f(mid) ? lo = mid : hi = mid - 1; n   } n   return lo; n } n template <class T> n void remDup(vector<T>& v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n } n template <class T, class U> n void erase(T& t, const U& u) { n   auto it = t.find(u); n   assert(it != end(t)); n   t.erase(u); n } n template <class T> n void re(T& x) { n   cin >> x; n } n void re(db& d) { n   str t; n   re(t); n   d = stod(t); n } n void re(ld& d) { n   str t; n   re(t); n   d = stold(t); n } n template <class T, class... U> n void re(T& t, U&... u) { n   re(t); n   re(u...); n } n template <class T> n void re(complex<T>& c) { n   T a, b; n   re(a, b); n   c = {a, b}; n } n template <class T, class U> n void re(pair<T, U>& p) { n   re(p.f, p.s); n } n template <class T> n void re(vector<T>& x) { n   for (auto& a : x) re(a); n } n template <class T, size_t SZ> n void re(AR<T, SZ>& x) { n   for (auto& a : x) re(a); n } n str to_string(char c) { return str(1, c); } n str to_string(const char* s) { return (str)s; } n str to_string(str s) { return s; } n str to_string(bool b) { return to_string((int)b); } n template <class T> n str to_string(complex<T> c) { n   stringstream ss; n   ss << c; n   return ss.str(); n } n str to_string(vector<bool> v) { n   str res =  { ; n   for (int i = (0); i < ((int)(v).size()); ++i) res += char( 0  + v[i]); n   res +=  } ; n   return res; n } n template <size_t SZ> n str to_string(bitset<SZ> b) { n   str res =   ; n   for (int i = (0); i < (SZ); ++i) res += char( 0  + b[i]); n   return res; n } n template <class T> n str to_string(T v) { n   bool fst = 1; n   str res =   ; n   for (const auto& x : v) { n     if (!fst) res +=    ; n     fst = 0; n     res += to_string(x); n   } n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p) { n   return to_string(p.f) +     + to_string(p.s); n } n template <class T> n void pr(T x) { n   cout << to_string(x); n } n template <class T, class... U> n void pr(const T& t, const U&... u) { n   pr(t); n   pr(u...); n } n void ps() { pr(  n ); } n template <class T, class... U> n void ps(const T& t, const U&... u) { n   pr(t); n   if (sizeof...(u)) pr(   ); n   ps(u...); n } n void pdbg(ll t) { cerr << t; } n void pdbg(int t) { cerr << t; } n void pdbg(str t) { cerr << t; } n void pdbg(char t) { cerr << t; } n void pdbg(ld t) { cerr << t; } n void pdbg(db t) { cerr << t; } n void pdbg(ull t) { cerr << t; } n template <class T, class V> n void pdbg(pair<T, V> p) { n   cerr <<  { ; n   pdbg(p.ff); n   cerr <<  , ; n   pdbg(p.ss); n   cerr <<  } ; n } n template <class T> n void pdbg(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     pdbg(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void pdbg(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     pdbg(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void pdbg(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     pdbg(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void pdbg(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     pdbg(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n void solve() { n   ll n; n   re(n); n   str s; n   re(s); n   str t; n   re(t); n   ll a1 = 0, b1 = 0; n   ll a2 = 0, b2 = 0; n   for (int i = (0); i < (n); ++i) { n     if (s[i] ==  a ) { n       a1 += 1; n     } else { n       b1 += 1; n     } n     if (t[i] ==  a ) { n       a2 += 1; n     } else { n       b2 += 1; n     } n   } n   if ((a1 + a2) % 2 == 0 && (b1 + b2) % 2 == 0) { n     ll ans = 0; n     ll ab = 0, ba = 0; n     vl aa1; n     vl aa2; n     vl bb1; n     vl bb2; n     for (int i = (0); i < (n); ++i) { n       if (s[i] ==  a  && t[i] ==  b ) { n         ab += 1; n         aa1.push_back(i + 1); n         bb2.push_back(i + 1); n       } else if (s[i] ==  b  && t[i] ==  a ) { n         ba += 1; n         bb1.push_back(i + 1); n         aa2.push_back(i + 1); n       } n     } n     ans += ab / 2; n     ans += ba / 2; n     if (ab % 2 == 1) { n       ans += 2; n     } n     ps(ans); n     if (aa1.size() != 0 || aa2.size() != 0) { n       while (aa1.size() > 1) { n         cout << aa1[aa1.size() - 1] <<     << bb2[aa1.size() - 2] << endl; n         aa1.pop_back(); n         bb2.pop_back(); n         aa1.pop_back(); n         bb2.pop_back(); n       } n       while (aa2.size() > 1) { n         cout << aa2[aa2.size() - 1] <<     << bb1[aa2.size() - 2] << endl; n         aa2.pop_back(); n         bb1.pop_back(); n         aa2.pop_back(); n         bb1.pop_back(); n       } n       if (aa1.size() == 1) { n         cout << aa1[0] <<     << bb2[0] << endl; n         cout << aa1[0] <<     << bb1[0] << endl; n         aa1.pop_back(); n         bb1.pop_back(); n         aa2.pop_back(); n         bb2.pop_back(); n       } n     } n   } else { n     cout << -1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ll T = 1; n   for (int i = (0); i < (T); ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10; n const int INF = 1e9 + 9; n const int B = 1e9 + 7; n int n, p[N]; n set<pair<int, int> > s[N]; n int d[N]; n int main() { n   scanf( %d , &n); n   ++n; n   for (int i = int(2); i <= int(n); ++i) { n     scanf( %d , &p[i]); n     d[i] = -1; n     for (int x = i; x > 1; x = p[x]) { n       int old = d[x]; n       s[p[x]].erase(pair<int, int>(d[x], x)); n       if (s[x].size() == 0) n         d[x] = 0; n       else { n         d[x] = s[x].rbegin()->first; n         if (s[x].size() > 1) { n           auto it = s[x].rbegin(); n           ++it; n           d[x] = max(d[x], it->first + 1); n         } n       } n       s[p[x]].insert(pair<int, int>(d[x], x)); n       while (s[p[x]].size() > 2) s[p[x]].erase(s[p[x]].begin()); n       if (d[x] == old) break; n     } n     printf( %d  , s[1].rbegin()->first + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100000]; n int main() { n   int m, n, j, k, l, t, ll; n   bool is = false; n   cin >> t; n   cin >> s; n   if (t < 5) { n     cout <<  no  << endl; n     return 0; n   } n   l = t / 5; n   ll = t % 5; n   for (j = 0; j < t && !is; j++) n     for (k = 1; k < t && !is; k++) n       if (j + 4 * k < t && s[j] ==  *  && s[j + 2 * k] ==  *  && n           s[j + k] ==  *  && s[j + 3 * k] ==  *  && s[j + 4 * k] ==  * ) { n         is = true; n       } n   if (is) n     cout <<  yes  << endl; n   else n     cout <<  no  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, g[2010], a[100003], f[2010], ansn, sum1, sum2; n bool make[200]; n int main() { n   cin >> n >> m; n   bool see = false; n   for (int i = m; i > n; i--) { n     for (int j = 2; j <= min((long long)100000, n) and !see; j++) n       if (i % j == 0) { n         see = true; n       } n     if (!see) return cout << i, 0; n     see = false; n   } n   cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n using INT = long long; n const int NN = 202020; n int a[NN], b[NN]; n map<int, int> mp; n int main() { n   int n, y; n   cin >> n >> y; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int m, yy; n   cin >> m >> yy; n   for (int i = 1; i <= m; i++) cin >> b[i]; n   int ans = 2; n   for (int bt = 0; bt < 30; bt++) { n     int d = (1 << bt); n     mp.clear(); n     for (int i = 1; i <= n; i++) { n       int ha = a[i] % (2 * d); n       mp[ha]++; n       ans = max(ans, mp[ha]); n     } n     for (int i = 1; i <= m; i++) { n       int ha = (b[i] + d) % (2 * d); n       mp[ha]++; n       ans = max(ans, mp[ha]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n const long long MOD = 1e9 + 7; n const long long INF = LLONG_MAX; n const long long SIZE = 100000; n void solve() { n   long long n, s; n   cin >> n >> s; n   long long sum = 0, dig = 0, fir; n   long long k = n; n   vector<long long> v; n   while (k != 0) { n     long long rem = k % 10; n     sum += rem; n     fir = rem; n     v.push_back(rem); n     k /= 10; n     dig++; n   } n   if (sum <= s) { n     cout << 0 << endl; n   } else { n     reverse(v.begin(), v.end()); n     long long sum2 = 0, fin; n     long long pos = -1; n     for (long long i = 0; i < (long long)v.size(); i++) { n       sum2 += v[i]; n       if (sum2 > s) { n         pos = i; n         break; n       } n     } n     sum2 -= v[pos]; n     long long yup = -1; n     for (long long i = pos - 1; i >= 0; i--) { n       v[i] += 1; n       sum2 += 1; n       if (sum2 <= s) { n         yup = i; n         break; n       } n       sum2 -= v[i]; n     } n     if (yup == -1) { n       long long fin = pow(10, dig); n       cout << fin - n << endl; n     } else { n       for (long long i = yup + 1; i < (long long)v.size(); i++) { n         v[i] = 0; n       } n       long long fin = 0; n       long long len = v.size() - 1; n       for (long long i = 0; i < (long long)v.size(); i++) { n         fin += v[i] * pow(10, len); n         len--; n       } n       cout << fin - n << endl; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 1e5 + 5; n int n, a[N]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   sort(a, a + n); n   for (int i = 0, eq = 0; i < n - 1; ++i) { n     if (a[i] == a[i + 1]) { n       if ((++eq) > 1 || (i > 0 && a[i] - 1 == a[i - 1]) || a[i] == 0) { n         puts( cslnb ); n         return 0; n       } n     } n   } n   ll sum = 0; n   for (int i = 0; i < n; ++i) sum += a[i] - i; n   if ((sum & 1) == 0) n     puts( cslnb ); n   else n     puts( sjfnb ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int M, N, s, x, l, c; n   vector<pair<int, int>> val; n   vector<int> seq; n   cin >> M; n   for (int i = 0; i < M; i++) { n     cin >> s; n     if (s == 1) { n       cin >> x; n       val.push_back(make_pair(0, x)); n       if (seq.size() < 1e6) { n         seq.push_back(x); n       } n     } else { n       cin >> l >> c; n       val.push_back(make_pair(l, c)); n       for (int j = 0; j < c; j++) { n         if (seq.size() < 1e6) { n           seq.insert(seq.end(), seq.begin(), seq.begin() + l); n         } else { n           break; n         } n       } n     } n   } n   vector<long long> q; n   long long v; n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> v; n     q.push_back(--v); n   } n   vector<int> res(N, 0); n   int i = 0; n   long long cc = 0; n   for (int j = 0; j < val.size() && i < N; j++) { n     if (val[j].first == 0) { n       if (cc++ == q[i]) { n         res[i++] = val[j].second; n       } n     } else { n       long long next = cc + ((long long)val[j].second) * val[j].first; n       while (i < N && q[i] < next) { n         v = (q[i] - cc) % val[j].first; n         res[i] = seq[v]; n         i++; n       } n       cc += ((long long)val[j].second) * val[j].first; n     } n   } n   cout << res[0]; n   for (int i = 1; i < N; i++) { n     cout <<     << res[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<vector<int> > v(26); n   for (int i = 0; i < n; i++) { n     char a = s[i]; n     char k = a; n     k++; n     for (char j = k; j <=  z ; j++) { n       int x = j -  a ; n       if (v[x].size() > 0) { n         cout <<  YES  << endl; n         cout << v[x].back() + 1 <<     << i + 1 << endl; n         return; n       } n     } n     v[a -  a ].push_back(i); n   } n   cout <<  NO  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int tests = 1; n   while (tests--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[200005]; n pair<long long int, long long int> even[200005]; n pair<long long int, long long int> odd[200005]; n int main() { n   long long int n; n   cin >> n; n   for (long long int i = 1; i <= n; i++) cin >> arr[i]; n   even[2] = make_pair(0, 2); n   odd[2] = make_pair(abs(arr[2] - arr[1]), 1); n   for (long long int i = 3; i <= n; i++) { n     long long int preve = even[i - 1].first; n     long long int prevel = even[i - 1].second; n     long long int prevo = odd[i - 1].first; n     long long int prevol = odd[i - 1].second; n     if (i % 2 == 0) { n       odd[i] = make_pair(prevo + abs(arr[i] - arr[i - 1]), prevol); n       if (preve - abs(arr[i] - arr[i - 1]) <= 0) n         even[i] = make_pair(0, i); n       else n         even[i] = make_pair(preve - abs(arr[i] - arr[i - 1]), prevel); n     } else { n       even[i] = make_pair(preve + abs(arr[i] - arr[i - 1]), prevel); n       if (prevo - abs(arr[i] - arr[i - 1]) <= 0) n         odd[i] = make_pair(0, i); n       else n         odd[i] = make_pair(prevo - abs(arr[i] - arr[i - 1]), prevol); n     } n   } n   long long int m = 0; n   for (long long int i = 1; i <= n; i++) { n     m = max(m, max(even[i].first, odd[i].first)); n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n std::mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n inline int sj(int n) { n   unsigned long long x = rnd(); n   x = x << 32 | rnd(); n   return x % n + 1; n } n const int p = 998244353; n inline void inc(int &x, const int y) { n   if ((x += y) >= p) x -= p; n } n inline void dec(int &x, const int y) { n   if ((x -= y) < 0) x += p; n } n inline int ksm(int x, int y) { n   int r = 1; n   while (y) { n     if (y & 1) r = (long long)r * x % p; n     x = (long long)x * x % p; n     y >>= 1; n   } n   return r; n } n priority_queue<int> ONLYYFORRCOPYY; n priority_queue<int, vector<int>, greater<int>> ONLYYFORRCOPYY__; n const int N = 1e6 + 2, M = 4e6 + 2; n struct Q { n   int a, s; n   bool operator<(const Q &o) const { n     return max(a, s) < max(o.a, o.s) || n            max(a, s) == max(o.a, o.s) && (s < o.s || s == o.s && a < o.a); n   } n }; n Q a[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << setiosflags(ios::fixed) << setprecision(15); n   int n, d, i, ans = 0; n   cin >> n >> d; n   for (i = 1; i <= n; i++) cin >> a[i].s >> a[i].a; n   sort(a + 1, a + n + 1); n   for (i = 1; i <= n; i++) n     if (d <= a[i].s) d = max(d, a[i].a), ++ans; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> g[100005]; n vector<pair<pair<int, int>, int> > coord; n int visited[100005]; n long long ans = 1; n const long long pmod = 1000000007; n long long expsize; n set<int> x, y; n bool cycle; n int mapback[100005]; n long long qpmod(long long b, long long e) { n   long long cur = b; n   long long res = 1; n   while (e) { n     if (e % 2 == 1) { n       res = res * cur % pmod; n     } n     e /= 2; n     cur = cur * cur % pmod; n   } n   return res; n } n pair<pair<int, int>, int> m3(int a, int b, int c) { n   pair<pair<int, int>, int> res; n   res.first.first = a; n   res.first.second = b; n   res.second = c; n   return res; n } n bool cmpx(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) { n   if (x.first.first < y.first.first) return true; n   if ((x.first.first == y.first.first) && (x.first.second < y.first.second)) n     return true; n   return false; n } n bool cmpy(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) { n   if (x.first.second < y.first.second) return true; n   if ((x.first.second == y.first.second) && (x.first.first < y.first.first)) n     return true; n   return false; n } n void dfs(int last, int cur) { n   expsize++; n   visited[cur] = 1; n   x.insert(coord[mapback[cur]].first.first); n   y.insert(coord[mapback[cur]].first.second); n   int i; n   for (i = 0; i < g[cur].size(); i++) { n     if (g[cur][i] == last) continue; n     if (visited[g[cur][i]]) { n       cycle = true; n       continue; n     } n     dfs(cur, g[cur][i]); n   } n } n int main() { n   scanf( %d , &n); n   int i, j, k; n   for (i = 0; i < n; i++) { n     scanf( %d %d , &j, &k); n     coord.push_back(m3(j, k, i)); n   } n   sort(coord.begin(), coord.end(), cmpx); n   for (i = 1; i < n; i++) { n     if (coord[i].first.first == coord[i - 1].first.first) { n       g[coord[i].second].push_back(coord[i - 1].second); n       g[coord[i - 1].second].push_back(coord[i].second); n     } n   } n   sort(coord.begin(), coord.end(), cmpy); n   for (i = 1; i < n; i++) { n     if (coord[i].first.second == coord[i - 1].first.second) { n       g[coord[i].second].push_back(coord[i - 1].second); n       g[coord[i - 1].second].push_back(coord[i].second); n     } n   } n   for (i = 0; i < n; i++) { n     mapback[coord[i].second] = i; n   } n   for (i = 0; i < n; i++) { n     if (visited[i] == 0) { n       x.clear(); n       y.clear(); n       cycle = false; n       dfs(i, i); n       expsize = x.size() + y.size(); n       if (cycle) { n         ans = ans * qpmod(2, expsize) % pmod; n       } else { n         ans = ans * ((qpmod(2, expsize) + pmod - 1) % pmod) % pmod; n       } n     } n   } n   printf( %d n , (int)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int incoming[110]; n int outgoing[110]; n int main(int argc, const char* argv[]) { n   int n, m; n   cin >> n >> m; n   int a, b, c; n   for (int i = 0; i < m; i++) { n     cin >> a >> b >> c; n     incoming[b] += c; n     outgoing[a] += c; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (outgoing[i] - incoming[i] > 0) ans += outgoing[i] - incoming[i]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, n, m, z, c; n   string s; n   cin >> s; n   c = 0, z = 0; n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  4 ) n       c++; n     else if (s[i] ==  7 ) n       z++; n   } n   if (c == z && z == 0) n     cout << -1 << endl; n   else if (c >= z) n     cout << 4 << endl; n   else n     cout << 7 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, INF = 1e9; n vector<int> g[N], r[N], out; n int cnt[N], ans = INF, sum = 0; n int dfs(int v, int par) { n   for (int i = 0; i < g[v].size(); i++) n     if (g[v][i] != par) cnt[v] += dfs(g[v][i], v); n   for (int i = 0; i < r[v].size(); i++) n     if (r[v][i] != par) cnt[v] += 1 + dfs(r[v][i], v); n   return cnt[v]; n } n void dfs2(int v, int par, int a, int b) { n   for (int i = 0; i < g[v].size(); i++) n     if (g[v][i] != par) dfs2(g[v][i], v, a + 1, b); n   for (int i = 0; i < r[v].size(); i++) n     if (r[v][i] != par) dfs2(r[v][i], v, a, b + 1); n   int cur = sum - b + a; n   if (cur < ans) { n     ans = cur; n     out.clear(); n     out.push_back(v + 1); n   } else if (cur == ans) n     out.push_back(v + 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a].push_back(b); n     r[b].push_back(a); n   } n   dfs(0, -1); n   sum = cnt[0]; n   dfs2(0, -1, 0, 0); n   cout << ans <<   n ; n   sort(out.begin(), out.end()); n   for (int i = 0; i < out.size(); i++) n     cout << out[i] <<    n [i == out.size() - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> E[1010000]; n int n, m, T[1010000]; n bool v[1010000]; n void DFS(int a) { n   v[a] = true; n   int i; n   for (i = 0; i < E[a].size(); i++) { n     if (!v[E[a][i]]) DFS(E[a][i]); n   } n } n long long Res; n int main() { n   int i, a, b, ck = 0, cc = 0; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     if (a == b) { n       cc++; n       T[a]++; n     } n     E[a].push_back(b); n     E[b].push_back(a); n   } n   Res = 1ll * cc * (m - cc) + 1ll * cc * (cc - 1) / 2; n   for (i = 1; i <= n; i++) { n     if (!E[i].empty()) { n       if (!v[i] && ck) { n         printf( 0 n ); n         return 0; n       } n       ck = 1; n       DFS(i); n     } n   } n   for (i = 1; i <= n; i++) { n     int t = E[i].size() - T[i] * 2; n     Res += 1ll * t * (t - 1) / 2; n   } n   printf( %lld n , Res); n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n const int maxn = 3e3+5; n const int mod = 0x3f3f3f3f; n int n,d,q; n  n void solve(int n){ n  if(n>=10*d){ n   cout<< YES <<endl; n   return; n  } n  for(int i=1;i<=9;i++){ n   if(n>=d*i&&(n-i*d)%10==0){ n    cout<< YES <<endl; n    return; n   } n  } n  cout<< NO <<endl; n } n  n int main(){ n  int t; n  cin>>t; n  while(t--){ n    n   cin>>q>>d; n   for(int i=0;i<q;i++){ n    cin>>n; n    solve(n); n   } n  }  n   n  return 0; n }  n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 110, mod = 1e9 + 7; n const long long inf = 1e18; n long long a[maxn]; n int b[maxn]; n long long ans1, ans2, ans3; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int tot = 2; tot <= 2 * m; tot++) { n     int pos = max(1, tot - m), pos2 = 0, pos3 = 0, mxp = -1, n         lim = (tot + 1) / 2, cnt = 0; n     for (int j = 0; j < n; j++) { n       b[j] = a[j] % tot; n     } n     for (int j = 0; j < n; j++) { n       cnt += (b[j] >= lim); n       if (b[j] >= lim) n         pos = max(pos, tot - b[j]); n       else n         pos = max(pos, b[j] + 1); n       if (mxp == -1 || b[mxp] < b[j]) mxp = j; n     } n     for (int j = 0; j < n; j++) { n       if (j != mxp) pos2 = max(pos2, (b[j] / 2) + 1); n       pos3 = max(pos3, (b[j] / 2) + 1); n     } n     if (cnt & 1) n       ans2 += max(0, tot - 2 * max(pos, pos2) + 1); n     else n       ans3 += max(0, tot - 2 * max(pos, pos3) + 1); n   } n   ans1 = (1ll * m * m - ans2 - ans3) >> 1; n   return cout << ans1 <<     << ans1 <<     << ans2 <<     << ans3 << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n const int N = 200005; n const int MAX = 250; n int cnt[N * (MAX + 5)]; n int pref[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   lint ans = 0; n   for (int i = 0; i < n; i++) { n     pref[i + 1] = s[i] -  0 ; n     pref[i + 1] += pref[i]; n   } n   for (int k = 1; k <= MAX; k++) { n     for (int i = 0; i <= n; i++) { n       ans += cnt[k * pref[i] - i + n]++; n     } n     for (int i = 0; i <= n; i++) { n       cnt[k * pref[i] - i + n]--; n     } n   } n   vector<int> ones; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       ones.emplace_back(i + 1); n     } n   } n   ones.emplace_back(n + 1); n   int i = 0; n   int num = (int)ones.size() - 1; n   for (int l = 1; l <= n; l++) { n     while (i < num && ones[i] < l) { n       i++; n     } n     for (int j = i; j < num; j++) { n       if ((j - i + 1) * MAX > n) { n         break; n       } n       int high = (ones[j + 1] - l) / (j - i + 1); n       int low = max(MAX, (ones[j] - l) / (j - i + 1)); n       ans += max(high - low, 0); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100007]; n int main() { n   int n, kq; n   cin >> n; n   if (n == 1) { n     cout << 1; n     return 0; n   } n   kq = 2; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n - 2; ++i) { n     int j = 0; n     for (j = i + 2; j <= n; ++j) n       if (a[j] != a[j - 1] + a[j - 2]) break; n     kq = max(kq, j - i); n     i = j - 2; n   } n   cout << kq; n   return 0; n } n


// n // Created by Abhishek Bansal on 3/18/2021 n // n //#pragma GCC optimize ( O3 ) n //#pragma GCC optimize( Ofast,no-stack-protector ) n //#pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,mmx,tune=native ) n #include <bits/stdc++.h> n #define ll long long n #define ld long double n using namespace std; n clock_t startTime; n double getCurrentTime() { n     return (double)(clock() - startTime) / CLOCKS_PER_SEC; n } n /*------------------------------------------------IO_OPERATORS---------------------------------------------*/ n template<typename T, typename U> inline ostream &operator << (ostream &_out, const pair<T, U> &_p) { _out << _p.first <<     << _p.second; return _out; } n template<typename T, typename U> inline istream &operator >> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; } n template<typename T> inline ostream &operator << (ostream &_out, const vector<T> &_v) { if (_v.empty()) return _out; _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out <<     << *_it; return _out; } n template<typename T> inline istream &operator >> (istream &_in, vector<T> &_v) { for (auto &_i : _v) _in >> _i; return _in; } n template<typename T> inline ostream &operator << (ostream &_out, const set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; return _out; } n template<typename T> inline ostream &operator << (ostream &_out, const multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; return _out; } n template<typename T> inline ostream &operator << (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; return _out; } n template<typename T> inline ostream &operator << (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; return _out; } n template<typename T, typename U> inline ostream &operator << (ostream &_out, const map<T, U> &_m) { if (_m.empty()) return _out; _out <<  {   << _m.begin()->first <<   ,    << _m.begin()->second <<   } ; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out <<  , {    << _it->first <<   ,    << _it->second <<   } ; return _out; } n template<typename T, typename U> inline ostream &operator << (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) return _out; _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out <<  , (  << _it->first <<  :   << _it->second <<  ) ; return _out; } n //For 1 based indexing n template<typename T> n void out(const vector<T> &a, int n) { n     for (int i = 1; i <= n; ++i) {cout << a[i] <<    ;} cout <<   n ; n } n template<typename T> n void out(const vector<vector<T>> &a, int n, int m) { n     for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {cout << a[i][j] <<    ;}cout <<   n ;} n } n  n const int N = 20000005; n int lp[N+1]; n vector<int> pr; n  n void Precompute() { n     for (int i=2; i<=N; ++i) { n         if (lp[i] == 0) { n             lp[i] = i; n             pr.push_back (i); n         } n         for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) n             lp[i * pr[j]] = pr[j]; n     } n } n  n void solve() { n     int c, d, x;cin >> c >> d >> x; n     ll ans = 0; n     auto calc = [&](int i) { n         if ((d + x / i) % c) return; n         int prod = (d + x / i) / c; n         int cnt = 0; n         while (prod != 1) { n             int  u= lp[prod]; n             cnt++; n             while (prod % u == 0) prod /= u; n         } n         ans += (1 << cnt); n     }; n  n     for (int i = 1; i * i <= x; ++i) { n         if (x % i != 0) continue; n         calc(i); n         if (i * i != x) calc(x / i); n     } n     cout << ans <<   n ; n } n  n int main() { n     ios_base::sync_with_stdio(false);cin.tie(nullptr); n  n     startTime = clock(); n // freopen( input.txt ,  r , stdin); n // freopen( output.txt ,  w , stdout); n //    cout << fixed << setprecision(20); n     int T = 1; n     Precompute(); n     cin >> T; n     while (T--) { n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n const long long maxn = 1e3 + 5; n const long long w = 1e8; n using namespace std; n int m, n, p, kq; n char a[maxn][maxn]; n string s; n char dir[4] = { D ,  L ,  U ,  R }; n int tdx, tdy; n int x, y; n inline void init() { n   cin >> n >> m; n   cin.ignore(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  S ) { n         x = j; n         y = i; n       } n     } n   cin >> s; n   p = s.length(); n } n inline void solve() { n   sort(dir, dir + 4); n   do { n     tdx = x; n     tdy = y; n     for (int i = 0; i < p; i++) { n       if (dir[s[i] - 48] ==  D ) tdy++; n       if (dir[s[i] - 48] ==  U ) tdy--; n       if (dir[s[i] - 48] ==  L ) tdx--; n       if (dir[s[i] - 48] ==  R ) tdx++; n       if (a[tdy][tdx] ==  #  || tdx < 1 || tdy < 1 || tdx > m || tdy > n) break; n       if (a[tdy][tdx] ==  E ) { n         kq++; n         break; n       } n     } n   } while (next_permutation(dir, dir + 4)); n   cout << kq; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a; n   int N[2005], A[2005]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     N[i] = a; n   } n   for (int i = 0; i < n; i++) { n     int cont = 0; n     for (int j = 0; j < n; j++) { n       if (i != j && N[i] < N[j]) cont++; n     } n     A[i] = 1 + cont; n   } n   for (int i = 0; i < n; i++) { n     if (i != n - 1) n       cout << A[i] <<    ; n     else n       cout << A[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, a, P, len, rev[N]; n int Lim; n int f[2][N], g[2][N]; n const long double PI = acos(-1.0); n struct Complex { n   long double x, y; n   Complex() {} n   Complex(long double _x, long double _y) { n     x = _x; n     y = _y; n   } n   Complex conj() { return Complex(x, -y); } n   Complex operator+(const Complex rhs) const { n     return Complex(x + rhs.x, y + rhs.y); n   } n   Complex operator-(const Complex rhs) const { n     return Complex(x - rhs.x, y - rhs.y); n   } n   Complex operator*(const Complex rhs) const { n     return Complex(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x); n   } n } w[N], w2[N]; n inline void FFT(Complex *F, int len, int type) { n   for (int i = 0; i < len; i++) n     if (i < rev[i]) swap(F[i], F[rev[i]]); n   for (int k = 2; k <= len; k <<= 1) n     for (int j = 0; j < len; j += k) { n       for (int i = j; i < j + (k / 2); i++) { n         Complex cur = type == 1 ? w[(k / 2) + (i - j)] : w2[(k / 2) + (i - j)]; n         Complex u = F[i], v = cur * F[i + (k / 2)]; n         F[i] = u + v; n         F[i + (k / 2)] = u - v; n       } n     } n   if (type == -1) n     for (int i = 0; i < len; i++) F[i].x /= len, F[i].y /= len; n } n Complex A[N], B[N], C[N], D[N]; n inline void MTT(int *F, int *G, int k) { n   for (int i = 0; i < len; i++) A[i] = B[i] = C[i] = D[i] = Complex(0, 0); n   Lim = int(sqrt(P) + 1); n   for (int i = 0; i < k; i++) { n     A[i] = Complex(F[i] / Lim, F[i] % Lim); n     C[i] = Complex(G[i] / Lim, G[i] % Lim); n   } n   FFT(A, len, 1); n   FFT(C, len, 1); n   for (int i = 1; i < len; i++) B[len - i] = A[i].conj(); n   B[0] = A[0].conj(); n   for (int i = 1; i < len; i++) D[len - i] = C[i].conj(); n   D[0] = C[0].conj(); n   for (int i = 0; i < len; i++) { n     Complex aa = (A[i] + B[i]) * Complex(0.5, 0), n             bb = (A[i] - B[i]) * Complex(0, -0.5), n             cc = (C[i] + D[i]) * Complex(0.5, 0), n             dd = (C[i] - D[i]) * Complex(0, -0.5); n     A[i] = (aa * cc) + Complex(0, 1) * (aa * dd + bb * cc), B[i] = bb * dd; n   } n   FFT(A, len, -1); n   FFT(B, len, -1); n   for (int i = 0; i < k; i++) { n     long long aa = (long long)(A[i].x + 0.5) % P, n               bb = (long long)(A[i].y + 0.5) % P, n               cc = (long long)(B[i].x + 0.5) % P; n     F[i] = (1ll * aa * Lim * Lim % P + 1ll * bb * Lim % P + cc % P) % P; n     F[i] = (F[i] + P) % P; n   } n } n int ans = 0; n inline void solve(int a, int k) { n   if (a == 1) { n     f[1][1] = ans = 1; n     return; n   } n   solve(a >> 1, k); n   memset(g, 0, sizeof g); n   g[0][0] = g[1][0] = 1; n   for (int i = 1; i < k; i++) g[0][i] = g[1][i] = (f[0][i] + f[1][i]) % P; n   MTT(g[0], f[(a >> 1) & 1], k); n   MTT(g[1], f[((a >> 1) & 1) ^ 1], k); n   for (int i = 0; i < k; i++) n     f[0][i] = (f[0][i] + g[0][i]) % P, f[1][i] = (f[1][i] + g[1][i]) % P; n   if (a & 1) { n     for (int i = k - 1; i; i--) n       f[1][i] = (f[1][i] + f[0][i - 1] + f[1][i - 1]) % P; n     f[1][1] = (f[1][1] + 1) % P; n   } n   for (int i = 1; i < k; i += 2) ans = (ans + f[1][i]) % P; n } n int main() { n   scanf( %d%d%d , &n, &a, &P); n   if (a == 1) return puts( 0 ), 0; n   len = 1; n   while (len <= n * 2) len <<= 1; n   for (int i = 0; i < len; i++) { n     rev[i] = rev[i >> 1] >> 1; n     if (i & 1) rev[i] |= len >> 1; n   } n   for (int i = 1; i < len; i <<= 1) { n     w[i] = Complex(1, 0); n     for (int j = 1; j < i; j++) n       w[i + j] = (((j & 31) == 1) ? Complex(cos(PI * j / i), sin(PI * j / i)) n                                   : w[i + j - 1] * w[i + 1]); n   } n   for (int i = 0; i < len; i++) w2[i] = w[i].conj(); n   solve(a >> 1, n + 1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000 + 1, inf = 2e8 + 1; n int a, m, n; n bool d[N]; n int p[N]; n int dp[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   cin >> a >> n >> m; n   for (int i = 0; i < n; ++i) { n     int l, r; n     cin >> l >> r; n     for (int i = l; i < r; ++i) d[i] = true; n   } n   for (int i = 0; i <= a; ++i) p[i] = inf; n   for (int i = 0; i < m; ++i) { n     int x, px; n     cin >> x >> px; n     p[x] = min(p[x], px); n   } n   for (int i = a - 1; i >= 0; --i) { n     dp[i] = inf; n     if (!d[i]) dp[i] = dp[i + 1]; n     if (p[i] != inf) n       for (int j = i + 1; j <= a; ++j) n         if (dp[j] != inf) dp[i] = min(dp[i], p[i] * (j - i) + dp[j]); n   } n   cout << (dp[0] == inf ? -1 : dp[0]) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ppi = pair<pii, int>; n char s[200100]; n int rk[200100], sa[200100], ht[200100]; n int mn[200100][22]; n namespace SA { n int head[200100 * 2]; n ppi pos[200100 * 2], buf[200100 << 1], tbuf[200100 << 1]; n int nxt[200100 * 2]; n void bucket_sort(int ok, int n) { n   int i, u, j, deg, E, K; n   for (i = E = K = 0; i < max(222, n + 10); i++) head[i] = -1; n   for (i = 0; i < n; i++) { n     if (ok) n       u = pos[i].first.first + 1; n     else n       u = pos[i].first.second + 1; n     buf[E] = pos[i]; n     nxt[E] = head[u]; n     head[u] = E++; n   } n   for (int i = 0; i < max(n + 10, 222); i++) { n     for (deg = 0, j = head[i]; ~j; j = nxt[j]) { n       tbuf[deg++] = buf[j]; n     } n     while (deg) pos[K++] = tbuf[--deg]; n   } n } n void LCP(int n) { n   for (int i = 1; i <= n; i++) mn[i][0] = ht[i]; n   for (int j = 1; j < 22; j++) { n     for (int i = 1; i <= n; i++) { n       mn[i][j] = min(mn[i][j - 1], mn[min(n, i + (1 << j - 1))][j - 1]); n     } n   } n } n void height(int n) { n   int h = 0, i; n   for (i = 0; i < n; i++) sa[rk[i]] = i; n   for (i = 0; i < n; i++) { n     if (rk[i] == 1) continue; n     h = max(0, h - 1); n     for (int j = sa[rk[i] - 1]; s[i + h] == s[j + h]; h++) n       ; n     ht[rk[i]] = h; n   } n } n void buildSA(int n) { n   int i, j, cnt, u; n   for (i = 0; i < n; i++) rk[i] = s[i]; n   rk[n] = 0; n   for (i = 0; i < 20; i++) { n     for (j = 0; j < n; j++) n       pos[j] = ppi(pii(rk[j], rk[min(n, j + (1 << i))]), j); n     bucket_sort(0, n); n     bucket_sort(1, n); n     pos[n] = ppi(pii(-1, -1), -1); n     for (cnt = j = 0; j < n; j++) { n       rk[pos[j].second] = cnt + 1; n       cnt += (pos[j].first != pos[j + 1].first); n     } n     if (cnt == n) break; n   } n   height(n); n   LCP(n); n } n int calc(int u, int v) { n   if (u == v) return 100000000; n   u = rk[u], v = rk[v]; n   if (u > v) swap(u, v); n   u++; n   int l = 31 - __builtin_clz(v - u + 1); n   return min(mn[u][l], mn[v - (1 << l) + 1][l]); n } n }  // namespace SA n using namespace SA; n using INT = long long; n char vst[200100]; n int cnt[200100]; n int main() { n   int n; n   cin >> n; n   cin >> s; n   cin >> vst; n   reverse(s, s + n); n   buildSA(n); n   INT ans = 0; n   for (int i = 1; i <= n; i++) { n     int u = sa[i]; n     cnt[i] = cnt[i - 1]; n     u = n - sa[i] - 1; n     if (vst[u] ==  0 ) { n       ans = max(ans, (INT)n - sa[i]); n       cnt[i]++; n     } n   } n   for (int i = 2; i <= n; i++) { n     int st = 0, ed = i; n     while (st + 1 < ed) { n       int md = (st + ed) >> 1; n       if (calc(sa[md], sa[i]) >= ht[i]) n         ed = md; n       else n         st = md; n     } n     int l = ed; n     st = i, ed = n + 1; n     while (st + 1 < ed) { n       int md = (st + ed) >> 1; n       if (calc(sa[md], sa[i]) >= ht[i]) n         st = md; n       else n         ed = md; n     } n     int r = st; n     ans = max(ans, (INT)ht[i] * (cnt[r] - cnt[l - 1])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int BSZ1 = 1010; n const int BSZ2 = 200; n int N; n char A[202020]; n int B[40404040]; n int C[202020]; n long long ans; n int R[202020]; n void calc(int i, int r, int zero) { n   int t = (zero + R[r]) / i; n   int u = zero / i; n   if (zero % i != 0) u++; n   if (t > BSZ2) { n     u = max(u, BSZ2 + 1); n     ans += (t - u + 1); n   } n } n int main() { n   scanf( %s , A + 1); n   N = strlen(A + 1); n   for (int i = N - 1; i >= 1; i--) { n     if (A[i + 1] ==  0 ) n       R[i] = R[i + 1] + 1; n     else n       R[i] = 0; n   } n   for (int i = 1; i <= BSZ1; i++) { n     int l = 1, r = 1; n     int zero = 0, cnt = 0; n     while (r <= N && cnt < i) { n       if (A[r] ==  1 ) n         cnt++; n       else n         zero++; n       r++; n     } n     if (cnt < i) continue; n     r--; n     while (1) { n       while (A[l] ==  0 ) { n         calc(i, r, zero); n         l++; n         zero--; n       } n       calc(i, r, zero); n       l++; n       r++; n       while (r <= N) { n         if (A[r] ==  1 ) n           break; n         else n           zero++; n         r++; n       } n       if (r > N) break; n     } n   } n   for (int i = 0; i <= BSZ2; i++) { n     B[N]++; n     C[0] = N; n     int c = N; n     for (int j = 1; j <= N; j++) { n       if (A[j] ==  1 ) n         c += i; n       else n         c--; n       ans += B[c]; n       B[c]++; n       C[j] = c; n     } n     for (int j = 0; j <= N; j++) B[C[j]] = 0; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string num1, num2, num3; n   int i, j, k, l, m, n, last, flag = 0; n   cin >> num1; n   num2 = num1; n   num3 = num1; n   l = num1.length(); n   last = num1.at(l - 1) - 48; n   for (i = 0; i < l - 1; i++) { n     m = num2.at(i) - 48; n     if (m % 2 == 0 && m < last) { n       swap(num2.at(i), num2.at(l - 1)); n       flag = 1; n       cout << num2 << endl; n       return 0; n     } n   } n   if (flag == 0) { n     for (i = l - 2; i >= 0; i--) { n       m = num3.at(i); n       if (m % 2 == 0 && last < m) { n         swap(num3.at(i), num3.at(l - 1)); n         cout << num3 << endl; n         return 0; n       } n     } n   } n   cout <<  -1  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double ep = 1e-6; n const double eps = 4e-5; n const double pi = acos(-1); n int tmpx, tmpy; n struct pnt { n   double x, y; n   pnt(double x = 0, double y = 0) : x(x), y(y) {} n   pnt operator+(const pnt &b) const { return pnt(x + b.x, y + b.y); } n   pnt operator-(const pnt &b) const { return pnt(x - b.x, y - b.y); } n   pnt operator*(const double &b) const { return pnt(x * b, y * b); } n   pnt operator/(const double &b) const { return pnt(x / b, y / b); } n   double operator&(const pnt &b) const { return x * b.x + y * b.y; } n   double operator^(const pnt &b) const { return x * b.y - y * b.x; } n   double dist() { return sqrt(x * x + y * y); } n   void input() { scanf( %d %d , &tmpx, &tmpy), x = tmpx, y = tmpy; } n   void print() { printf( (%.4lf %.4lf) n , x, y); } n }; n int n, Q; n pnt p[10101 << 1], q; n double s[10101 << 1], area; n void prep() { n   for (int i = 0, j = n - 1; i < j; i++, j--) swap(p[i], p[j]); n   for (int i = 0; i <= n; i++) p[i + n] = p[i]; n   s[0] = 0; n   for (int i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + (p[i - 1] ^ p[i]); n   area = s[n]; n } n double dist(pnt &p, double &a, double &b, double &c) { n   return p.x * a + p.y * b + c; n } n pnt intersect(pnt &a, pnt &b, double &A, double &B, double &C) { n   double a_ = dist(a, A, B, C); n   double b_ = dist(b, A, B, C); n   return (a * b_ - b * a_) / (b_ - a_); n } n double calc(double alpha) { n   double A = sin(alpha), B = -cos(alpha), C = -A * q.x - B * q.y; n   int st, en, u, v, mid; n   double st_, en_, u_, v_, mid_; n   st = 0, en = n - 1; n   st_ = dist(p[st], A, B, C); n   en_ = dist(p[en], A, B, C); n   while (st < en) { n     u = (st + en - 1) >> 1; n     v = u + 1; n     u_ = dist(p[u], A, B, C); n     v_ = dist(p[v], A, B, C); n     if ((st_ <= en_ && st_ <= v_) || (u_ <= en_ && u_ <= v_)) n       en = u; n     else n       st = v; n   } n   int mn = st; n   st = 0, en = n - 1; n   st_ = dist(p[st], A, B, C); n   en_ = dist(p[en], A, B, C); n   while (st < en) { n     u = (st + en - 1) >> 1; n     v = u + 1; n     u_ = dist(p[u], A, B, C); n     v_ = dist(p[v], A, B, C); n     if ((st_ >= en_ && st_ >= v_) || (u_ >= en_ && u_ >= v_)) n       en = u; n     else n       st = v; n   } n   int mx = st; n   st = mn, en = mx + n; n   while (st < en - 1) { n     mid = (st + en) >> 1; n     mid_ = dist(p[mid], A, B, C); n     if (mid_ < 0) n       st = mid; n     else n       en = mid; n   } n   int ans1 = st % n; n   pnt crs1 = intersect(p[ans1], p[ans1 + 1], A, B, C); n   st = mx, en = mn + n; n   while (st < en - 1) { n     mid = (st + en) >> 1; n     mid_ = dist(p[mid], A, B, C); n     if (mid_ > 0) n       st = mid; n     else n       en = mid; n   } n   int ans2 = st % n; n   pnt crs2 = intersect(p[ans2], p[ans2 + 1], A, B, C); n   if (ans1 >= ans2) ans2 += n; n   double rlt = (p[ans2] ^ crs2) + (crs2 ^ crs1) + (crs1 ^ p[ans1 + 1]) + n                s[ans2] - s[ans1 + 1]; n   if (((crs1 - crs2) & pnt(cos(alpha), sin(alpha))) < 0) rlt = area - rlt; n   return rlt; n } n int main() { n   scanf( %d %d , &n, &Q); n   for (int i = 0; i < n; i++) p[i].input(); n   prep(); n   while (Q--) { n     q.input(); n     double st = 0, en = pi, mid, tmp; n     bool stt = (calc(st) < area / 2), enn = !stt, midd; n     for (int step = 1; step <= 42; step++) { n       mid = (st + en) / 2; n       tmp = calc(mid); n       midd = (tmp < area / 2); n       if (fabs(tmp * 2 - area) / area < eps) break; n       if (midd == stt) n         st = mid; n       else n         en = mid; n     } n     printf( %.20lf n , (st + en) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(long long a, long long b) { n   if (a > b) { n     return solve(b, a); n   } n   if (a == 0) { n     return false; n   } n   bool next = solve(b % a, a); n   if (!next) { n     return true; n   } n   long long c = (b / a); n   if ((a % 2 == 1)) { n     return (c % 2 == 0); n   } else { n     return (((b / a) % (a + 1)) % 2 == 0); n   } n } n inline void init() {} n int main() { n   init(); n   bool prev = false; n   int T; n   while (cin >> T) { n     if (prev) { n       cout << endl; n     } n     prev = true; n     long long a, b; n     for (int i = 0; i < T; i++) { n       cin >> a >> b; n       cout << (solve(a, b) ?  First  :  Second ) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool visited[2000]; n vector<int> G[2000]; n vector<int> parent; n int dfs(int u) { n   if (visited[u]) { n     return 0; n   } n   visited[u] = true; n   int ans = 0; n   for (int i = 0; i < G[u].size(); i++) { n     ans = max(ans, dfs(G[u][i])); n   } n   return ans + 1; n } n int ans(int n) { n   int a = 0; n   for (int i = 0; i < parent.size(); i++) { n     a = max(a, dfs(parent[i])); n   } n   return a; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     visited[i] = false; n     int u; n     scanf( %d , &u); n     if (u != -1) { n       u--; n       G[u].push_back(i); n     } else { n       parent.push_back(i); n     } n   } n   printf( %d n , ans(n)); n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[101], N, T; n void Input() { n   cin >> N; n   memset(arr, 0, sizeof arr); n   for (int i = 0; i < N; i++) { n     long long tmp; n     cin >> tmp; n     arr[tmp]++; n   } n } n void Solve() { n   int one = 0, two = 0; n   for (int i = 0; i <= 100; i++) { n     if (arr[i] >= 2 && one == two) { n       one++; n       two++; n     } else if (arr[i] >= 1) { n       one++; n     } else { n       break; n     } n   } n   cout << one + two <<   n ; n } n int main() { n   cin >> T; n   for (int i = 0; i < T; i++) { n     Input(); n     Solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n int N; n char S[2 * MAXN]; n int ord[2 * MAXN]; n pair<pair<int, int>, int> E[2 * MAXN]; n int step = 1; n int pre[MAXN]; n int minp[MAXN]; n int mins[MAXN]; n char ans[MAXN]; n void build() { n   for (int i = 0; i < N; i++) ord[i] = (S[i] ==  ) ); n   for (int jump = 1; jump < N; step++, jump *= 2) { n     for (int i = 0; i < N; i++) n       E[i] = make_pair( n           pair<int, int>(ord[i], (i + jump < N) ? ord[i + jump] : N), i); n     sort(E, E + N); n     int ind = -1; n     pair<int, int> prev(-1, -1); n     for (int i = 0; i < N; i++) { n       int idx = E[i].second; n       pair<int, int> cur = E[i].first; n       if (cur != prev) { n         ind++; n         prev = cur; n       } n       ord[idx] = ind; n     } n   } n } n int main() { n   scanf( %s , &S); n   N = (int)(strlen(S)); n   memcpy(S + N, S, N); n   N += N; n   S[N] =  | ; n   S[N + 1] = 0; n   build(); n   N /= 2; n   for (int i = 1; i <= N; i++) n     pre[i] = pre[i - 1] + ((S[i - 1] ==  ( ) ? 1 : -1); n   minp[0] = pre[0] + pre[N]; n   for (int i = 1; i < N; i++) minp[i] = min(minp[i - 1], pre[i] + pre[N]); n   mins[N - 1] = pre[N - 1]; n   for (int i = N - 2; i >= 0; i--) mins[i] = min(mins[i + 1], pre[i]); n   int best = -1, x = min(0, pre[N]); n   for (int i = 0; i < N; i++) n     if (min(minp[i], mins[i]) - pre[i] >= x) n       if (best == -1 || ord[i] < ord[best]) best = i; n   int ind = 0; n   for (int i = 0; i < -pre[N]; i++) ans[ind++] =  ( ; n   for (int i = 0; i < N; i++) ans[ind++] = S[best + i]; n   for (int i = 0; i < pre[N]; i++) ans[ind++] =  ) ; n   ans[ind] = 0; n   printf( %s n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string findSum(string str1, string str2) { n   if (str1.length() > str2.length()) swap(str1, str2); n   string str =   ; n   int n1 = str1.length(), n2 = str2.length(); n   reverse(str1.begin(), str1.end()); n   reverse(str2.begin(), str2.end()); n   int carry = 0; n   for (int i = 0; i < n1; i++) { n     int sum = ((str1[i] -  0 ) + (str2[i] -  0 ) + carry); n     str.push_back(sum % 10 +  0 ); n     carry = sum / 10; n   } n   for (int i = n1; i < n2; i++) { n     int sum = ((str2[i] -  0 ) + carry); n     str.push_back(sum % 10 +  0 ); n     carry = sum / 10; n   } n   if (carry) str.push_back(carry +  0 ); n   reverse(str.begin(), str.end()); n   return str; n } n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   int idx = (n + 1) / 2; n   while (idx < n && s[idx] ==  0 ) idx++; n   if (idx == n) { n     idx--; n     while (s[idx] ==  0 ) idx--; n     if (idx == 0) { n       cout << s << endl; n     } else { n       string s1 = s.substr(0, idx); n       string s2 = s.substr(idx); n       cout << findSum(s1, s2) << endl; n     } n   } else { n     string s1 = findSum(s.substr(0, idx), s.substr(idx)); n     idx--; n     while (s[idx] ==  0 ) idx--; n     string s2 = findSum(s.substr(0, idx), s.substr(idx)); n     if (s1.size() > s2.size()) { n       cout << s2 << endl; n     } else if (s1.size() < s2.size()) { n       cout << s1 << endl; n     } else { n       cout << min(s1, s2) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::string; n int main() { n   bool newline = true; n   bool found; n   for (string s; getline(cin, s);) { n     found = false; n     for (string::size_type index = 0; index < s.size(); ++index) n       if (s[index] !=    ) n         if (s[index] ==  # ) n           found = true; n         else n           break; n     if (found) { n       if (!newline) cout << std::endl; n       cout << s << std::endl; n       newline = true; n     } else { n       for (string::size_type index = 0; index < s.size(); ++index) n         if (s[index] !=    ) cout << s[index]; n       newline = false; n     } n   } n   if (!found) cout << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, c[55]; n int dp[500010]; n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n >> d; n   for (int i = 0; i < n; i++) cin >> c[i]; n   sort(c, c + n); n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 500000 - c[i]; j >= 0; j--) n       if (dp[j] != -1) dp[j + c[i]] = max(dp[j + c[i]], dp[j] + 1); n   } n   int l, r; n   for (l = 0, r = 0; r <= 500000 && r - l <= d; r++) { n     if (dp[r] != -1) l = r; n   } n   int days = 0; n   for (int i = 1, prev = 0, j = 0; i <= l; i++) { n     if (dp[i] != -1) { n       if (i - prev > d) { n         days++; n         prev = j; n       } n       j = i; n     } n   } n   cout << l <<     << days + (l != 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<long long, int> p[400005]; n   int n, m; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first; n     p[i].second = 1; n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> p[i + n].first; n     p[i + n].second = 2; n   } n   int x = n * 3; n   int y = m * 3; n   sort(p, p + n + m); n   int u = x, v = y; n   for (int i = 0; i < n + m; i++) { n     if (p[i].second == 1) n       x--; n     else n       y--; n     if (x - y > u - v) { n       u = x; n       v = y; n     } n   } n   cout << u <<  :  << v << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100100; n const int Mod = 1e9 + 7; n long long N, S; n long long p[MaxN]; n long long deg[MaxN]; n long long pref[MaxN]; n long long depth[MaxN]; n vector<long long> child[MaxN]; n void solve(long long branch) { n   for (int j = (2); j < (N + 1); j++) { n     p[j] = j - 1; n     deg[j - 1] = 1; n     child[j - 1].push_back(j); n     depth[j] = j; n   } n   deg[N] = 0; n   if (branch == 1) { n     cout <<  Yes  << endl; n     for (int j = (2); j < (N + 1); j++) { n       cout << p[j] <<    ; n     } n     exit(0); n   } n   long long lo = 2 * N - 1; n   long long hi = N * (N + 1) / 2; n   if (S < lo || S > hi) { n     cout <<  No  << endl; n     exit(0); n   } n   long long x = N - 2; n   long long i = N; n   long long z = 0; n   vector<long long> anchor; n   anchor.push_back(1); n   depth[1] = 1; n   while (S < hi) { n     while (deg[anchor[z]] >= branch) { n       for (auto w : child[anchor[z]]) { n         anchor.push_back(w); n         ; n         depth[w] = depth[anchor[z]] + 1; n       } n       z++; n       if (z > (long long)anchor.size()) { n         assert(false); n         cin.ignore(); n       } n       x = (i - 1) - depth[anchor[z]]; n     }; n     ; n     if (S + x <= hi) { n       deg[p[i]]--; n       child[p[i]].pop_back(); n       p[i] = anchor[z]; n       deg[anchor[z]]++; n       assert(anchor[z] != i); n       child[anchor[z]].push_back(i); n       S += x; n       x--; n       i--; n     } else { n       x = (hi - S); n       p[i] = (i - 1) - x; n       S += x; n     } n   } n   cout <<  Yes  << endl; n   for (int j = (2); j < (N + 1); j++) { n     cout << p[j] <<    ; n   } n } n long long check() { n   for (int i = (1); i < (N + 1); i++) pref[i] = 1; n   for (long long i = N; i >= 2; i--) { n     pref[p[i]] += pref[i]; n   } n   long long sum = 0; n   for (int i = (1); i < (N + 1); i++) { n     sum += pref[i]; n   } n   return sum; n } n long long f(long long branch) { n   if (branch == 1) { n     return (N * (N + 1)) / 2; n   } n   for (long long i = 1; i <= N; i++) { n     deg[i] = 0; n   } n   long long cur = 1; n   for (long long i = 2; i <= N; i++) { n     while (deg[cur] >= branch) { n       cur++; n     } n     p[i] = cur; n     deg[cur]++; n   } n   long long ret = check(); n   return ret; n } n void load() { n   cin >> N >> S; n   long long lo = 2 * N - 1; n   long long hi = N * (N + 1) / 2; n   if (S < lo || S > hi) { n     cout <<  No  << endl; n     exit(0); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   load(); n   long long lo = 1, hi = N - 1; n   while (lo < hi) { n     long long mid = (lo + hi) / 2; n     if (f(mid) <= S) { n       hi = mid; n     } else { n       lo = mid + 1; n     } n   }; n   solve(lo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, b, sum[200005], sm[200005]; n struct node { n   long long x, y, z; n } val[200005]; n bool comp(node a, node b) { return a.z > b.z; } n int main(void) { n   long long ans = 0; n   scanf( %lld%lld%lld , &n, &a, &b); n   b = min(b, n); n   for (int i = 1; i <= n; i++) n     scanf( %lld%lld , &val[i].x, &val[i].y), val[i].z = val[i].x - val[i].y; n   sort(val + 1, val + n + 1, comp); n   for (int i = 1; i <= n; i++) { n     sum[i] = sum[i - 1] + max(val[i].x, val[i].y); n     sm[i] = sm[i - 1] + val[i].y; n   } n   ans += sum[b] + sm[n] - sm[b]; n   if (b == 0) { n     printf( %lld n , sm[n]); n     return 0; n   } n   if (a == 0) { n     printf( %lld n , ans); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     int num = 0; n     long long t = val[i].x; n     for (int j = 1; j <= a; j++) t *= 2ll; n     if (i <= b - 1) n       t = t - max(val[i].x, val[i].y), t += sum[b], t += sm[n] - sm[b]; n     else n       t += sum[b - 1], t += sm[n] - sm[b - 1] - val[i].y; n     ans = max(ans, t); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n const int mod = 10007; n void add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n int mul(int x, int y) { return x * y % mod; } n int pw(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = mul(ret, x); n     x = mul(x, x); n     y >>= 1; n   } n   return ret; n } n namespace berlekamp_massey { n vector<int> bm(vector<int> x) { n   vector<int> rel, cur; n   int lf, ld; n   for (int i = 0; i < x.size(); ++i) { n     int sum = 0; n     for (int j = 0; j < cur.size(); ++j) add(sum, mul(x[i - j - 1], cur[j])); n     if (sum == x[i]) continue; n     if (!cur.size()) { n       cur.resize(i + 1); n       lf = i; n       ld = sum - x[i]; n       add(ld, mod); n       continue; n     } n     int coef = mul(sum - x[i] + mod, pw(ld, mod - 2)); n     vector<int> new_cur(i - lf - 1); n     new_cur.push_back(coef); n     for (int j = 0; j < rel.size(); ++j) { n       new_cur.push_back(mul(mod - rel[j], coef)); n     } n     if (new_cur.size() < cur.size()) new_cur.resize(cur.size()); n     for (int j = 0; j < cur.size(); ++j) add(new_cur[j], cur[j]); n     if (i - lf + rel.size() >= cur.size()) { n       rel = cur, lf = i, ld = sum - x[i]; n       add(ld, mod); n     } n     cur = new_cur; n   } n   return cur; n } n int sz; n int a[5005], rel[5005], tmp[5005]; n int ff[5005], gg[5005]; n void mulmod(int *p, int *q) { n   for (int i = 0; i < sz + sz; ++i) tmp[i] = 0; n   for (int i = 0; i < sz; ++i) n     if (p[i]) { n       for (int j = 0; j < sz; ++j) { n         add(tmp[i + j], mul(p[i], q[j])); n       } n     } n   for (int i = sz + sz - 1; i >= sz; --i) n     if (tmp[i]) { n       for (int j = sz - 1; j >= 0; --j) { n         add(tmp[i - j - 1], mul(tmp[i], rel[j])); n       } n     } n   for (int i = 0; i < sz; ++i) p[i] = tmp[i]; n } n int solve(vector<int> x, int n) { n   if (n < x.size()) return x[n]; n   vector<int> cur = bm(x); n   sz = cur.size(); n   for (int i = 0; i < sz; ++i) rel[i] = cur[i], a[i] = x[i]; n   for (int i = 0; i < sz; ++i) ff[i] = gg[i] = 0; n   ff[0] = 1; n   if (sz > 1) n     gg[1] = 1; n   else n     gg[0] = rel[0]; n   while (n) { n     if (n & 1) mulmod(ff, gg); n     mulmod(gg, gg); n     n >>= 1; n   } n   int ret = 0; n   for (int i = 0; i < sz; ++i) add(ret, mul(ff[i], a[i])); n   return ret; n } n }  // namespace berlekamp_massey n using namespace berlekamp_massey; n int n, m; n string s; n int f[805][N][N], ans[1405]; n vector<int> g; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s >> n; n   m = s.size(); n   s =  ~  + s; n   f[0][0][0] = 1; n   int len = m + 1400 >> 1; n   for (int i = 1; i <= len; ++i) { n     for (int j = 0; j <= m; ++j) { n       for (int k = 0; k <= m; ++k) { n         if (f[i - 1][j][k] == 0) continue; n         if (j == m && k == m) n           add(f[i][j][k], mul(26, f[i - 1][j][k])); n         else if (j == m) { n           add(f[i][j][k], mul(25, f[i - 1][j][k])); n           add(f[i][j][k + 1], f[i - 1][j][k]); n         } else if (k == m) { n           add(f[i][j][k], mul(25, f[i - 1][j][k])); n           add(f[i][j + 1][k], f[i - 1][j][k]); n         } else { n           if (s[j + 1] == s[m + 1 - k - 1]) { n             add(f[i][j + 1][k + 1], f[i - 1][j][k]); n             add(f[i][j][k], mul(25, f[i - 1][j][k])); n           } else { n             add(f[i][j + 1][k], f[i - 1][j][k]); n             add(f[i][j][k + 1], f[i - 1][j][k]); n             add(f[i][j][k], mul(24, f[i - 1][j][k])); n           } n         } n       } n     } n   } n   for (int i = 0; i < 1400; ++i) { n     int l = m + i >> 1; n     for (int j = 0; j <= m; ++j) { n       for (int k = 0; k <= m; ++k) { n         if (j + k >= m) { n           if (m + i & 1) n             add(ans[i], mul(26, f[l][j][k])); n           else n             add(ans[i], f[l][j][k]); n         } n         if (j + k == m - 1) { n           if (m + i & 1) add(ans[i], f[l][j][k]); n         } n       } n     } n   } n   for (int i = 0; i < 1400; ++i) g.push_back(ans[i]); n   cout << solve(g, n) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, A, B; n   while (cin >> n) { n     vector<int> a, b; n     while (n--) { n       int p; n       cin >> p; n       a.push_back(p); n     } n     int ans = 0; n     cin >> A >> B; n     sort(a.begin(), a.end()); n     a.resize(unique(a.begin(), a.end()) - a.begin()); n     while (A != B) { n       int t1 = A - 1; n       int len = a.size(); n       b.clear(); n       for (int i = 0; i < len; i++) { n         int t = A / a[i] * a[i]; n         if (t >= B) { n           t1 = min(t, t1); n           b.push_back(a[i]); n         } n       } n       a = b; n       A = t1; n       ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 5e5 + 2; n const int M = 1e5 + 2; n const int inf = 2e9 + 2; n const long long linf = 4e18 + 2; n long long add(long long a, long long b) { n   a += b; n   return a >= mod ? a - mod : a; n } n long long sub(long long a, long long b) { n   a += mod - b; n   return a >= mod ? a - mod : a; n } n long long mul(long long a, long long b) { return (long long)a * b % mod; } n long long a[N], bit[2][N]; n long long id[N]; n bool cmp(long long c, long long d) { return a[c] < a[d]; } n void upd(int x, int val, int s) { n   for (; x < N; x += x & (-x)) bit[s][x] += val; n } n long long get(int x, int s) { n   long long ans = 0; n   for (; x > 0; x -= x & (-x)) ans += bit[s][x]; n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i], id[i] = i; n   sort(id + 1, id + 1 + n, cmp); n   long long ans = 0; n   for (int j = 1; j <= n; j++) { n     int i = id[j]; n     ans = add(ans, mul(a[i], mul(i, n - i + 1))); n     long long l = get(i, 0); n     long long r = get(n, 1) - get(i, 1); n     ans = add(ans, mul(a[i], mul(l, n - i + 1))); n     ans = add(ans, mul(a[i], mul(r, i))); n     upd(i, i, 0); n     upd(i, n - i + 1, 1); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int n; n int a[5005], pos[5005]; n long long dp[5005], f[5055], inv[5005]; n void prework() { n   f[0] = 1; n   f[1] = 1; n   inv[1] = 1; n   inv[0] = 1; n   for (int i = 2; i <= n; i++) { n     f[i] = f[i - 1] * i % mod; n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n   } n   for (int i = 2; i <= n; i++) { n     inv[i] = inv[i] * inv[i - 1] % mod; n   } n } n long long A(long long a, long long b) { return f[a] * inv[a - b] % mod; } n int main() { n   cin >> n; n   prework(); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   int head = 0; n   for (int i = 1; i <= n; i++) { n     while (a[head + 1] * 2 <= a[i]) head++; n     pos[i] = head; n   } n   dp[0] = 1; n   pos[0] = -1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= pos[i]; j++) { n       dp[i] = n           (dp[i] + dp[j] * A(n - 2 - pos[j], pos[i] - pos[j] - 1) % mod) % mod; n     } n   } n   if (pos[n] == n - 1) n     cout << dp[n] << endl; n   else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int a[1010], b[1010]; n int n, k, inf = 0x0f0f0f0f; n int test(int cur) { n   int i, ans = 0; n   if (cur <= 0) return inf; n   for (i = 0; i < n; i++) { n     if (a[i] != cur) ans++; n     cur += k; n   } n   return ans; n } n int main() { n   int i, j, cur; n   while (~scanf( %d%d , &n, &k)) { n     for (i = 0; i < n; i++) scanf( %d , &a[i]); n     for (i = 0; i < n; i++) b[i] = test(a[i] - i * k); n     for (i = 1, j = 0; i < n; i++) n       if (b[i] < b[j]) j = i; n     printf( %d n , b[j]); n     cur = a[j] - k * j; n     for (i = 0; i < n; i++) { n       if (a[i] != cur) { n         if (a[i] < cur) n           printf( + %d %d n , i + 1, cur - a[i]); n         else n           printf( - %d %d n , i + 1, a[i] - cur); n       } n       cur += k; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n struct Event { n   int t, L, R, on; n   Event(int t = 0, int L = 0, int R = 0, int on = 0) n       : t(t), L(L), R(R), on(on) {} n   bool operator<(const Event &rh) const { return t < rh.t; } n } E[maxn * 2]; n int V[maxn * 2], cnt; n int Pre[maxn * 2], P[maxn * 2]; n long long Ans[maxn]; n int ID(int v) { n   int L = 1, R = cnt; n   while (L < R) { n     int mid = (L + R) >> 1; n     if (V[mid] < v) n       L = mid + 1; n     else n       R = mid; n   } n   return L; n } n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     static int x, y; n     scanf( %d%d , &x, &y); n     E[2 * i - 1] = Event(x - k + 1, y, y + k - 1, 1); n     E[2 * i] = Event(x + 1, y, y + k - 1, -1); n     V[++cnt] = y; n     V[++cnt] = y + k; n   } n   sort(V + 1, V + 1 + cnt); n   cnt = unique(V + 1, V + 1 + cnt) - V - 1; n   sort(E + 1, E + 1 + 2 * n); n   for (int i = 1; i <= 2 * n; i++) { n     int id = ID(E[i].L); n     for (; id < cnt && V[id] <= E[i].R; id++) { n       Ans[P[id]] += ((long long)(E[i].t - Pre[id])) * (V[id + 1] - V[id]); n       P[id] += E[i].on; n       Pre[id] = E[i].t; n     } n   } n   for (int i = 1; i <= n; i++) cout << Ans[i] << (i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int f[105]; n int v[105]; n bool cmp(int i, int j) { return a[i] > a[j]; } n int main() { n   int n, w; n   while (cin >> n >> w) { n     int sum = 0; n     int sum2 = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sum += a[i]; n       if (a[i] % 2 == 0) n         f[i] = a[i] / 2; n       else n         f[i] = (a[i] + 1) / 2; n       sum2 += f[i]; n     } n     for (int i = 0; i < n; i++) v[i] = i; n     sort(v, v + n, cmp); n     if (sum2 > w || sum < w) { n       cout << -1 << endl; n       continue; n     } n     w -= sum2; n     for (int i = 0; i < n; i++) { n       if (w > a[v[i]] - f[v[i]]) { n         w -= (a[v[i]] - f[v[i]]); n         f[v[i]] = a[v[i]]; n       } else if (w <= a[v[i]] - f[v[i]]) { n         f[v[i]] += w; n         break; n       } n     } n     for (int i = 0; i < n; i++) { n       cout << f[i]; n       if (i != n - 1) cout <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n class BIT { n   std::vector<int> bit; n  n  public: n   BIT(int size) : bit(size + 1, 0) {} n   void add(int i, int x) { n     i++; n     while (i < (int)bit.size()) { n       bit[i] += x; n       i += i & -i; n     } n   } n   int sum(int a) { n     a++; n     int res = 0; n     while (0 < a) { n       res += bit[a]; n       a -= a & -a; n     } n     return res; n   } n   int sum(int a, int b) { return sum(b) - sum(a - 1); } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   int n; n   cin >> n; n   BIT xs(s.size()), ys(s.size()), zs(s.size()); n   for (int i = 0; i < (s.size()); i++) { n     if (s[i] ==  x ) n       xs.add(i, 1); n     else if (s[i] ==  y ) n       ys.add(i, 1); n     else n       zs.add(i, 1); n   } n   for (int i = 0; i < (n); i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     int x, y, z; n     x = xs.sum(l, r); n     y = ys.sum(l, r); n     z = zs.sum(l, r); n     string ans =  NO ; n     if (abs(x - y) < 2 && abs(x - z) < 2 && abs(y - z) < 2 || r - l < 2) n       ans =  YES ; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1000001; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, k; n   string s; n   cin >> n >> k >> s; n   m = s.size(); n   int last[200]; n   memset(last, -1, sizeof last); n   for (int i = 0; i < m; i++) { n     last[s[i]] = i; n   } n   pair<int, char> a[k]; n   for (int i = 0; i < k; i++) { n     a[i] = make_pair(last[ a  + i],  a  + i); n   } n   sort(a, a + k); n   int ind = 0; n   for (int i = 0; i < n; i++) { n     s += a[ind].second; n     ind = (ind + 1) % k; n   } n   long long int dp[200]; n   memset(dp, 0, sizeof dp); n   long long int t = 0; n   for (char c : s) { n     long long int h = dp[c]; n     dp[c] = (t + 1 + 1000000007) % 1000000007; n     t = (t - h + dp[c] + 1000000007) % 1000000007; n   } n   cout << (t + 1) % 1000000007; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:640000000 ) n using namespace std; n const double EPS = 1e-9; n const int INF = 0x7f7f7f7f; n const double PI = acos(-1.0); n template <class T> n inline T _abs(T n) { n   return ((n) < 0 ? -(n) : (n)); n } n template <class T> n inline T _max(T a, T b) { n   return (!((a) < (b)) ? (a) : (b)); n } n template <class T> n inline T _min(T a, T b) { n   return (((a) < (b)) ? (a) : (b)); n } n template <class T> n inline T _swap(T& a, T& b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n template <class T> n inline T gcd(T a, T b) { n   return (b) == 0 ? (a) : gcd((b), ((a) % (b))); n } n template <class T> n inline T lcm(T a, T b) { n   return ((a) / gcd((a), (b)) * (b)); n } n template <typename T> n string NumberToString(T Number) { n   ostringstream second; n   second << Number; n   return second.str(); n } n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   while (cin >> n) { n     priority_queue<pair<int, string> > pq; n     for (int i = (int)(1); i <= (int)(n); i++) { n       string st; n       int no; n       cin >> st >> no; n       pq.push(make_pair(-no, st)); n     } n     vector<pair<int, string> > v; n     int hh = 1; n     int flg = 1; n     while (!pq.empty()) { n       pair<int, string> p = pq.top(); n       pq.pop(); n       p.first *= -1; n       if (p.first == 0) { n         v.push_back(make_pair(hh, p.second)); n         hh++; n         continue; n       } n       vector<int> tmp; n       for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) { n         tmp.push_back(v[i].first); n       } n       sort(tmp.rbegin(), tmp.rend()); n       if (tmp.size() < p.first) { n         flg = 0; n         break; n       } n       int tmpNo = tmp[p.first - 1]; n       for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) { n         if (v[i].first >= tmpNo) v[i].first += 1; n       } n       v.push_back(make_pair(tmpNo, p.second)); n     } n     if (flg) { n       for (int i = (int)(0); i <= (int)(n - 1); i++) { n         cout << v[i].second <<     << v[i].first <<   n ; n       } n     } else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 0x3f3f3f3f; n const long long linf = 0x3f3f3f3f3f3f3f3f; n const int N = 1e4, M = 1e5; n int n, m, p, a[N], b[M]; n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n int mypow(int a, int first = p - 2, int res = 1) { n   for (; first; first >>= 1, a = 1ll * a * a % p) n     (first & 1) && (res = 1ll * res * a % p); n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n >> m >> p; n   int phi = p - 1, g = phi; n   for (int i(0); i < (n); ++i) cin >> a[i]; n   for (int i(0); i < (m); ++i) cin >> b[i], g = gcd(g, b[i]); n   vector<int> c; n   for (int d = 1; d * d <= phi; ++d) n     if (phi % d == 0) n       c.push_back(d), d *d < phi ? c.push_back(phi / d) : void(); n   sort(c.begin(), c.end()); n   vector<int> mb; n   for (int i(0); i < (n); ++i) { n     int mn_c = -1; n     for (int &u : c) n       if (mypow(a[i], 1ll * g * u % phi) == 1) { n         mb.push_back(phi / u); n         break; n       } n   } n   sort(mb.begin(), mb.end()); n   mb.erase(unique(mb.begin(), mb.end()), mb.end()); n   vector<int> f(int((mb).size()), 0); n   int ns = 0; n   for (int i((int((mb).size())) - 1); i >= 0; --i) { n     f[i] = phi / mb[i]; n     for (int j = i + 1; j < int((mb).size()); ++j) n       if (mb[j] % mb[i] == 0) f[i] -= f[j]; n     ns += f[i]; n   } n   cout << ns <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p[101], f[101], ans = 0, last = 1; n double Calc(int x) { return 1 - (1 - f[x]) * (1 - p[x]); } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     p[i] = f[i] = x / 100.0; n     last *= p[i]; n   } n   ans += last * n; n   for (int i = n + 1; i <= 300000; i++) { n     int x = 1; n     for (int j = 1; j <= n; j++) { n       if (Calc(j) / f[j] > Calc(x) / f[x]) x = j; n     } n     double now = last * Calc(x) / f[x]; n     f[x] = Calc(x); n     ans += (now - last) * i; n     last = now; n   } n   printf( %.8f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n pair<double, double> p[100100]; n double dt[100100]; n double db[100100]; n double da[100100]; n int main() { n   double ax, ay, bx, by, tx, ty; n   int n; n   cin >> ax >> ay >> bx >> by >> tx >> ty; n   cin >> n; n   double ans = 0; n   scanf( %lf %lf , &p[0].first, &p[0].second); n   ans += sqrt((p[0].first - tx) * (p[0].first - tx) + n               (p[0].second - ty) * (p[0].second - ty)); n   dt[0] = ans; n   for (int i = 1; i < n; i++) { n     scanf( %lf %lf , &p[i].first, &p[i].second); n     dt[i] = sqrt((p[i].first - tx) * (p[i].first - tx) + n                  (p[i].second - ty) * (p[i].second - ty)); n     ans += dt[i]; n   } n   ans *= 2; n   for (int i = 0; i < n; i++) { n     da[i] = sqrt((p[i].first - ax) * (p[i].first - ax) + n                  (p[i].second - ay) * (p[i].second - ay)); n     db[i] = sqrt((p[i].first - bx) * (p[i].first - bx) + n                  (p[i].second - by) * (p[i].second - by)); n   } n   double p1, p2, p3, p4; n   double Da = 1.7976931348623158e+308, Db = 1.7976931348623158e+308; n   int indexofa = -1; n   int indexofb = -1; n   for (int i = 0; i < n; i++) { n     if (da[i] - dt[i] < Da) { n       Da = da[i] - dt[i]; n       indexofa = i; n     } n   } n   for (int i = 0; i < n; i++) { n     if (db[i] - dt[i] < Db && i != indexofa) { n       Db = db[i] - dt[i]; n       indexofb = i; n     } n   } n   p1 = Da + Db; n   Da = 1.7976931348623158e+308, Db = 1.7976931348623158e+308; n   indexofa = -1; n   indexofb = -1; n   for (int i = 0; i < n; i++) { n     if (db[i] - dt[i] < Db) { n       Db = db[i] - dt[i]; n       indexofb = i; n     } n   } n   for (int i = 0; i < n; i++) { n     if (da[i] - dt[i] < Da && i != indexofb) { n       Da = da[i] - dt[i]; n       indexofa = i; n     } n   } n   p2 = Da + Db; n   Db = 1.7976931348623158e+308; n   indexofb = -1; n   for (int i = 0; i < n; i++) { n     if (db[i] - dt[i] < Db) { n       Db = db[i] - dt[i]; n       indexofb = i; n     } n   } n   p3 = Db; n   Da = 1.7976931348623158e+308; n   indexofa = -1; n   for (int i = 0; i < n; i++) { n     if (da[i] - dt[i] < Da) { n       Da = da[i] - dt[i]; n       indexofa = i; n     } n   } n   p4 = Da; n   printf( %.12lf , min(min(p1, p2), min(p3, p4)) + ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, n; n   while (~scanf( %d%d%d%d , &a, &b, &c, &n)) { n     if (c > a || c > b || n - (a + b - c) < 1) n       printf( -1 n ); n     else n       printf( %d n , n - (a + b - c)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int count = 0, wc = 0; n     bool flag = 0; n     int m = n; n     while (n--) { n       string s; n       cin >> s; n       int l = s.size(); n       if (l % 2 == 1) flag = 1; n       int zeroc = 0, onec = 0; n       for (int i = 0; i < l; i++) { n         if (s[i] ==  0 ) { n           zeroc++; n         } else { n           onec++; n         } n       } n       if (onec % 2 == 1 && zeroc % 2 == 1) { n         wc++; n       } else { n         count++; n       } n     } n     if (flag == 1) n       cout << m << endl; n     else { n       count = count + 2 * (wc / 2); n       cout << count << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   cin >> n >> k; n   long long int cost = 0, ans = 0, a[n]; n   vector<pair<long long int, long long int>> pr(n); n   for (long long int(i) = 0; (i) < (n); (i)++) { n     long long int a; n     cin >> a; n     pr[i] = make_pair(a, i + 1); n   } n   sort(pr.begin(), pr.end()); n   vector<long long int> v; n   for (long long int(i) = 0; (i) < (n); (i)++) { n     if ((cost > k) || (cost + pr[i].first > k)) n       break; n     else { n       cost += pr[i].first; n       ans++; n       v.push_back(pr[i].second); n     } n   } n   cout << ans <<   n ; n   sort(v.begin(), v.end()); n   for (long long int(i) = 0; (i) < (ans); (i)++) { n     cout << v[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   string ans; n   for (int i = 0; i < n; i++) ans +=  U ; n   for (int i = 0; i < m; i++) ans +=  L ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m - 1; j++) { n       ans += (i % 2 ?  L  :  R ); n     } n     if (i < n - 1) ans +=  D ; n   } n   cout << ans.size() <<   n  << ans <<   n ; n } n