#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long double pi = acos(-1.0); n const int inf = 1e+9; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n int xabs(int a) { return a >= 0 ? a : -a; } n int sign(int a) { return (a < 0) ? -1 : 1; } n int sqr(int a) { return a * a; } n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   stack<pair<char, int> > q; n   set<pair<char, char> > sp; n   sp.insert(make_pair( [ ,  ] )); n   sp.insert(make_pair( ( ,  ) )); n   for (int i = 0; i < n; i++) { n     if (q.size() == 0) { n       q.push(make_pair(s[i], i)); n     } else { n       if (sp.count(make_pair(q.top().first, s[i]))) { n         q.pop(); n       } else { n         q.push(make_pair(s[i], i)); n       } n     } n   } n   vector<int> ans; n   while (q.size() != 0) { n     ans.push_back(q.top().second); n     q.pop(); n   } n   reverse(ans.begin(), ans.end()); n   ans.push_back(n); n   int cnt = 0; n   int j = 0; n   int ma = 0; n   string res; n   while (j < ans.size()) { n     int cnt = 0; n     int start = (j > 0) ? ans[j - 1] + 1 : 0; n     for (int i = start; i < ans[j]; i++) { n       if (s[i] ==  [ ) cnt++; n     } n     if (cnt > ma) { n       ma = cnt; n       res = s.substr(start, ans[j] - start); n     } n     j++; n   } n   cout << ma << endl; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos((double)-1); n const double Eps = 1E-6; n int read() { n   int $x; n   scanf( %d , &$x); n   return $x; n } n long long sqr(long long $x) { return $x * $x; } n int x[100000], N, A, B; n int main() { n   A = read(), B = read(); n   N = A + B + 1; n   x[0] = 1; n   int i = 0, t; n   t = 1; n   while (A--) x[++i] = ++t; n   t = 1; n   while (B--) x[++i] = --t; n   for (i = 0; i < N; ++i) printf( %d  , x[i] - t + 1); n   return 0; n } n


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


#include <bits/stdc++.h> n const int N = 100010; n char s[N], str[3][N]; n int len; n int solve(int type, int ret) { n   int ans = 0; n   memcpy(str[type], s, sizeof(s)); n   for (int i = len - 1; i >= 0; --i) { n     if ((str[type][i] -  0 ) % 3 == type) { n       str[type][i] =   0 ; n       ++ans; n       ret = (ret - type + 3) % 3; n       if (!ret) { n         break; n       } n     } n   } n   for (int i = 0; i < len; ++i) { n     if (str[type][i] == 0) { n       continue; n     } n     if (str[type][i] !=  0  || len - ans == 1) { n       break; n     } n     str[type][i] =   0 ; n     ++ans; n   } n   return ret ? INT_MAX : ans; n } n int main() { n   scanf( %s , s); n   int sum = 0; n   len = strlen(s); n   for (int i = 0; i < len; ++i) { n     sum += s[i] -  0 ; n   } n   if (!(sum % 3)) { n     return printf( %s , s), 0; n   } n   int ans1 = solve(1, sum % 3), ans2 = solve(2, sum % 3); n   int type = ans1 < ans2 ? 1 : 2; n   int cnt = 0; n   for (int i = 0; i < len; ++i) { n     if (str[type][i] != 0) { n       ++cnt; n       putchar(str[type][i]); n     } n   } n   if (!cnt) { n     return printf( -1 n ), 0; n   } n   return printf(  n ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h1, a1, c1, i; n   int h2, a2; n   scanf( %d%d%d%d%d , &h1, &a1, &c1, &h2, &a2); n   vector<int> v; n   while (true) { n     if (h1 - a2 <= 0 && h2 - a1 <= 0) { n       h1 -= a2; n       h2 -= a1; n       v.push_back(1); n       break; n     } else if (h1 - a2 > 0) { n       h2 = h2 - a1; n       h1 = h1 - a2; n       v.push_back(1); n       if (h2 <= 0 || h1 <= 0) { n         break; n       } n     } else { n       h1 += c1; n       h1 -= a2; n       v.push_back(0); n     } n   } n   int n = v.size(); n   printf( %d n , n); n   for (i = 0; i < n; i++) { n     if (v[i] == 0) n       printf( HEAL n ); n     else n       printf( STRIKE n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   long long int A; n   long long int B; n }; n bool edgecompare(edge lhs, edge rhs) { n   if (lhs.B != rhs.B) n     return lhs.B < rhs.B; n   else n     return lhs.A < rhs.A; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m, i, j, k, flag = 0, a[100005]; n   string str; n   map<long long int, long long int> Map; n   set<long long int> Set; n   set<long long int, greater<long long int> >::iterator itr; n   cin >> n >> k; n   long long int ma = 0; n   i = 0; n   while (pow(2, i) <= n) { n     i++; n   } n   ma = pow(2, i) - 1; n   if (k == 1) n     cout << n; n   else n     cout << ma; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> times; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     times.push_back(x); n   } n   int ans = 0; n   for (int i = 1; i < n; ++i) { n     if (times[i] + times[i - 1] < k) { n       ans += k - (times[i] + times[i - 1]); n       times[i] += k - (times[i] + times[i - 1]); n     } n   } n   cout << ans << endl; n   for (int i = 0; i < n; ++i) cout << times[i] <<    ; n   cout << endl; n   return 0; n } n


#include<iostream> n #include<string.h> n #include <fstream> n #include <string> n #include <vector> n #include <iomanip>  n #include <cmath> n #include <cstdlib> n #include<map> n #include <ctime> n using namespace std;  n int main(){  n  long long int t; n  cin>>t; n  for(long long int mat=0;mat<t;mat=mat+1){ n   long long int total; n   cin>>total; n   long long int median; n   vector<long long int> array; n   for(long long int i=0;i<total;i=i+1){ n    char a; n    cin>>a; n    if(a== * ){ n     array.push_back(i); n    } n   } n   long long int n=array.size(); n   if(n%2==0){ n    median=n/2-1; n   } n   else{ n    median=n/2; n   } n   long long int ans=0; n   for(long long int i=0;i<array.size();i=i+1){ n    ans+=abs(array[i]-array[median])-abs(i-median); n   }  n   cout<<ans<<endl; n    n  n  n  }    n return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int main() { n   int n, r; n   cin >> n >> r; n   double deg = (1.0 / n) * pi; n   double bi = sin(deg); n   cout << setiosflags(ios::fixed) << setprecision(10) << (bi / (1 - bi) * r) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ghost { n   long long vx, vy; n   long long magic; n   ghost(long long x, long long y, long long a) n       : vx(x), vy(y), magic((long long)a * x - y) {} n }; n bool cmp1(const ghost& x, const ghost& y) { return x.magic < y.magic; } n bool cmp2(const ghost& x, const ghost& y) { return x.vx < y.vx; } n vector<ghost> G; n int main() { n   long long n, a, b; n   cin >> n >> a >> b; n   for (long long i = 0; i < n; i++) { n     long long x, vx, vy; n     cin >> x >> vx >> vy; n     ghost tmp(vx, vy, a); n     G.push_back(tmp); n   } n   stable_sort(G.begin(), G.end(), cmp2); n   stable_sort(G.begin(), G.end(), cmp1); n   long long res = 0; n   long long t_m = G[0].magic; n   long long t_b = 0; n   for (long long i = 0; i < G.size(); i++) { n     if (G[i].magic != t_m) { n       if (i - t_b > 1) { n         res += ((i - t_b) * (i - t_b - 1)); n         long long t_vx = G[t_b].vx; n         long long tt_b = t_b; n         for (long long k = tt_b; k < i; k++) { n           if (G[k].vx != t_vx) { n             res -= ((k - tt_b) * (k - tt_b - 1)); n             tt_b = k; n             t_vx = G[k].vx; n           } n         } n         res -= (i - tt_b) * (i - tt_b - 1); n       } n       t_b = i; n       t_m = G[i].magic; n     } n   } n   res += ((n - t_b) * (n - t_b - 1)); n   long long t_vx = G[t_b].vx; n   long long tt_b = t_b; n   for (long long k = tt_b; k < n; k++) { n     if (G[k].vx != t_vx) { n       res -= ((k - tt_b) * (k - tt_b - 1)); n       tt_b = k; n       t_vx = G[k].vx; n     } n   } n   res -= (n - tt_b) * (n - tt_b - 1); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct problem { n   long long ts, tl, ss, sl, p; n } pro[1005]; n pair<long long, double> f[1005][1566]; n int n, t; n bool cmp(problem a, problem b) { n   return a.tl * a.p * (1000000 - b.p) < b.tl * b.p * (1000000 - a.p); n } n bool better(pair<long long, double> &a, pair<long long, double> &b) { n   if (a.first != b.first) return a.first > b.first; n   return a.second < b.second; n } n void dp() { n   for (int i = (0); i <= (n + 1); i++) n     for (int j = (0); j <= (t); j++) f[i][j].first = -1; n   f[1][0] = make_pair(0, 0); n   for (int i = (1); i <= (n); i++) n     for (int j = (0); j <= (t); j++) n       if (f[i][j].first > -1) { n         if (better(f[i][j], f[i + 1][j])) f[i + 1][j] = f[i][j]; n         pair<long long, double> P; n         long long nj; n         if (j + pro[i].ts <= t) { n           nj = j + pro[i].ts; n           P = make_pair(f[i][j].first + pro[i].ss, n                         f[i][j].second + pro[i].ts * 1000000); n           if (better(P, f[i + 1][nj])) f[i + 1][nj] = P; n         } n         if (j + pro[i].ts + pro[i].tl <= t) { n           nj = j + pro[i].ts + pro[i].tl; n           P.first = n               f[i][j].first + pro[i].ss + pro[i].sl * (1000000 - pro[i].p); n           P.second = pro[i].ts * 1000000 + n                      (1000000 - pro[i].p) * (pro[i].tl + j) + n                      f[i][j].second * pro[i].p / 1.0 / 1000000; n           if (better(P, f[i + 1][nj])) f[i + 1][nj] = P; n         } n       } n   pair<long long, double> res = make_pair(0, 0); n   for (int j = (0); j <= (t); j++) n     if (better(f[n + 1][j], res)) { n       res = f[n + 1][j]; n     } n   printf( %.10lf %.10lf n , res.first / 1000000.0, res.second / 1000000.0); n } n long long str_ll(string &s) { n   long long x = 0; n   int pos = -1; n   for (int i = 0; i < (s.length()); i++) { n     if (s[i] ==  . ) { n       pos = i + 1; n     } else n       x = x * 10 + s[i] -  0 ; n   } n   if (pos != -1) n     for (int i = 0; i < (6 - s.length() + pos); i++) x = x * 10; n   return x; n } n int main() { n   cin >> n >> t; n   for (int i = (1); i <= (n); i++) { n     double p; n     cin >> pro[i].ss >> pro[i].sl >> pro[i].ts >> pro[i].tl >> p; n     pro[i].ss *= 1000000; n     pro[i].p = (p * 1000000 + 0.5); n   } n   sort(pro + 1, pro + n + 1, cmp); n   dp(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool Check(long long n, long long pos) { return (n & (1 << pos)); } n long long Set(long long n, long long pos) { return (n | (1 << pos)); } n string s; n long long n, mod; n long long dp[(1 << 18) + 2][102]; n long long call(long long mask, long long val) { n   if (mask == (1 << n) - 1) { n     if (val) n       return 0; n     else n       return 1; n   } n   long long &ret = dp[mask][val]; n   if (ret != -1) return ret; n   ret = 0; n   unordered_map<int, bool> mp; n   for (int i = 0; i < n; i++) { n     int digit = s[i] -  0 ; n     if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue; n     mp[digit] = 1; n     ret += call(Set(mask, i), (val * 10 + digit) % mod); n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> s >> mod; n   n = s.size(); n   memset(dp, -1, sizeof dp); n   cout << call(0, 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int MAXN = 1000005; n char a[MAXN], b[MAXN]; n int mi[MAXN], ma[MAXN]; n long long ans = 0; n int trs(char a, char b) { n   int s = 0; n   if (a ==  R ) n     s += 6; n   else if (a ==  B ) n     s += 3; n   if (b ==  R ) n     s += 2; n   else if (b ==  B ) n     s += 1; n   return s; n } n int s[MAXN][10]; n int main() { n   scanf( %s , a + 1); n   scanf( %s , b + 1); n   n = strlen(a + 1), m = strlen(b + 1); n   for (int i = 2; i <= m; i++) { n     for (int j = 0; j <= 9; j++) s[i][j] = s[i - 1][j]; n     if (b[i] != b[i - 1]) s[i][trs(b[i], b[i - 1])]++; n   } n   for (int i = 1; i <= n; i++) { n     if (i == 1) n       mi[i] = ma[i] = 1; n     else n       mi[i] = mi[i - 1], ma[i] = ma[i - 1]; n     if (i != 1 && a[i - 1] == b[mi[i]]) mi[i]++; n     if (i != 1 && ma[i] != m) ma[i]++; n     while (ma[i] != m && b[ma[i]] != a[i]) ma[i]++; n     ans += ma[i] - mi[i] + 1; n     if (i != 1) { n       ans -= s[ma[i]][trs(a[i - 1], a[i])] - s[mi[i] - 1][trs(a[i - 1], a[i])]; n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T &x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T &x, L y) { n   return y < x ? (x = y, 1) : 0; n } n const int maxn = 20, maxm = 1e5 + 17; n char c[maxm]; n int n, m, col[maxm]; n int dp[maxn + 1][1 << maxn], ans[1 << maxn]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> c; n     for (int j = 0; j < m; j++) col[j] |= c[j] -  0  << i; n   } n   for (int i = 0; i < m; i++) dp[0][col[i]]++; n   for (int i = 0; i < n; i++) n     for (int k = n; k; k--) n       for (int mask = 0; mask < 1 << n; mask++) n         dp[k][mask] += dp[k - 1][mask ^ (1 << i)]; n   int po = 1 << n; n   for (int i = 0; i <= n; i++) { n     int x = min(i, n - i); n     for (int mask = 0; mask < po; mask++) ans[mask] += x * dp[i][mask]; n   } n   cout << *min_element(ans, ans + po) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, l, k = 0, maxi; n   scanf( %d , &n); n   long long int count = 0; n   int a[15]; n   l = n; n   while (l) { n     a[k] = l % 10; n     l = l / 10; n     k++; n   } n   while (k > 0) { n     maxi = 0; n     for (i = 0; i < k; i++) n       if (a[i] > maxi) maxi = a[i]; n     n = n - maxi; n     k = 0; n     l = n; n     while (l) { n       a[k] = l % 10; n       l = l / 10; n       k++; n     } n     count++; n   } n   if (n == 0) n     cout << count << endl; n   else n     cout << count + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 1e4; n int a[maxn], mod; n int b[maxn]; n int n; n int pow(int a, int b) { n   if (b == 1) return a; n   int t = pow(a, b >> 1); n   t = 1LL * t * t % mod; n   if (b & 1) t = 1LL * t * a % mod; n   return t; n } n int ny(int a) { return pow(a, mod - 2); } n int ans, dd; n void solve(int a[], int n) { n   if (n == 1) { n     ans = a[1]; n     dd = 1; n     return; n   } n   int cc = a[2] - a[1]; n   int s = 0; n   for (int i = (1); i <= (n); ++i) n     if (!binary_search(a + 1, a + n + 1, (a[i] + cc) % mod)) ++s; n   cc = 1LL * cc * ny(s) % mod; n   ans = -1, dd = mod - cc; n   for (int i = (1); i <= (n); ++i) n     if (!binary_search(a + 1, a + n + 1, (a[i] + cc) % mod)) { n       if (ans == -1) n         ans = a[i]; n       else { n         ans = -1; n         break; n       } n     } n } n void init() { n   scanf( %d%d , &mod, &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); n   if (n == mod) { n     printf( %d %d n , 0, 1); n     return; n   } n   sort(a + 1, a + n + 1); n   if (n * 2 > mod) { n     int t = 0; n     for (int i = (0); i <= (mod - 1); ++i) n       if (!binary_search(a + 1, a + n + 1, i)) b[++t] = i; n     solve(b, t); n     if (ans != -1) (ans += 1LL * dd * t % mod) %= mod; n   } else n     solve(a, n); n   if (ans == -1) n     printf( %d n , ans); n   else n     printf( %d %d n , ans, dd); n } n int main() { n   init(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 5; n int seg[N], ans[N]; n vector<pair<int, int> > beg[N], fin[N]; n set<pair<int, int> > pending; n set<pair<int, int> >::iterator it1, it2; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> seg[i]; n   for (int q = 0; q < m; q++) { n     int l, r, x; n     cin >> l >> r >> x; n     beg[l].emplace_back(x, q); n     fin[r].emplace_back(x, q); n   } n   for (int i = 1; i <= n; i++) { n     for (pair<int, int> xq : beg[i]) pending.insert(xq); n     it2 = pending.lower_bound(make_pair(seg[i], 0)); n     it1 = pending.begin(); n     while (it1 != it2) { n       ans[it1->second] = i; n       pending.erase(it1++); n     } n     it1 = pending.lower_bound(make_pair(seg[i] + 1, 0)); n     while (it1 != pending.end()) { n       ans[it1->second] = i; n       pending.erase(it1++); n     } n     for (pair<int, int> xq : fin[i]) n       if ((it1 = pending.find(xq)) != pending.end()) { n         ans[xq.second] = -1; n         pending.erase(it1); n       } n   } n   for (int q = 0; q < m; q++) cout << ans[q] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m, a, b; n     cin >> n >> m >> a >> b; n     long long int arr[n + 5][m + 5]; n     long long int i, j; n     memset(arr, 0, sizeof(arr)); n     for (i = 0; i < n; i++) { n       long long int x = a; n       for (j = m - 1; j >= 0; j--) { n         if (x > 0) { n           arr[i][j] = 1; n           x--; n         } else { n           break; n         } n       } n     } n     long long int flag = 0; n     for (j = 0; j < m; j++) { n       long long int count = 0; n       for (i = 0; i < n; i++) { n         if (arr[i][j] == 1) { n           count++; n         } n       } n       if (count > b) { n         flag = 1; n         break; n       } n       long long int y = count; n       while (y < b) { n         for (i = 0; i < n; i++) { n           if (arr[i][j] == 0) { n             for (int h = j + 1; h < m; h++) { n               if (arr[i][h] == 1) { n                 arr[i][h] = 0; n                 arr[i][j] = 1; n                 y++; n                 break; n               } n             } n           } n           if (y == b || i == n - 1) { n             break; n           } n         } n         if (y == b || i == n - 1) { n           break; n         } n       } n       if (y != b) { n         flag = 1; n         break; n       } n     } n     if (flag == 1) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n       for (i = 0; i < n; i++) { n         for (j = 0; j < m; j++) { n           cout << arr[i][j]; n         } n         cout << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n const int maxn = 200005, maxm = maxn << 1; n int n, m, e, s1, s2, s, maxx, col, flg, mid; n int start[maxn], to[maxm], then[maxm], dep[maxn], f[maxn], ans[maxn], n     flen[maxn], slen[maxn]; n inline void add(int x, int y) { then[++e] = start[x], start[x] = e, to[e] = y; } n void dfs(int x, int last) { n   f[x] = last, dep[x] = dep[last] + 1; n   flen[x] = slen[x] = 0; n   if (dep[x] > maxx) maxx = dep[x], s = x; n   for (int i = start[x]; i; i = then[i]) { n     int y = to[i]; n     if (y == last) continue; n     dfs(y, x); n   } n } n void getlen(int x, int last) { n   for (int i = start[x]; i; i = then[i]) { n     int y = to[i]; n     if (y == last || ans[y]) continue; n     getlen(y, x); n     if (flen[y] + 1 > flen[x]) n       slen[x] = flen[x], flen[x] = flen[y] + 1; n     else if (flen[y] + 1 > slen[x]) n       slen[x] = flen[y] + 1; n     if (slen[y] + 1 > slen[x]) slen[x] = slen[y] + 1; n   } n } n void solve(int x, int c, int d, int t) { n   ans[x] = c; n   c = (c + t + m - 1) % m + 1; n   for (int i = start[x]; i; i = then[i]) { n     int y = to[i]; n     if (ans[y] != 0) continue; n     solve(y, c, d + 1, t); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y), add(y, x); n   } n   maxx = -1000000000, dfs(1, 0), s1 = s; n   maxx = -1000000000, dfs(s, 0), s2 = s; n   if (m == 2) { n     puts( Yes ); n     for (int i = 1; i <= n; i++) n       printf( %d%c , dep[i] % 2 + 1, i == n ?   n  :    ); n     return 0; n   } n   for (int i = s2; i != 0; i = f[i]) { n     col = col % m + 1; n     if ((dep[s1] + dep[s2]) / 2 == dep[i]) mid = i; n     ans[i] = col; n   } n   for (int i = s2; i != 0; i = f[i]) { n     getlen(i, 0); n     if (ans[i] && flen[i] && dep[i] + flen[i] >= m && n         (dep[s2] - dep[i] + 1) + flen[i] >= m) { n       puts( No ); n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) n     if (flen[i] + slen[i] + 1 >= m) { n       puts( No ); n       return 0; n     } n   for (int i = s2; i != mid; i = f[i]) solve(i, ans[i], 1, -1); n   for (int i = mid; i != 0; i = f[i]) solve(i, ans[i], 1, 1); n   puts( Yes ); n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 100000; n int n, mxdepth, MOD; n int INV2; n int inv[2 * MAXN + 1]; n vector<int> parts; n void rec(int l, int r, int h) { n   if (l > r) return; n   if (h == 1 || l == r) { n     parts.push_back(r - l + 1); n     return; n   } n   int m = (l + r) / 2; n   rec(l, m, h - 1); n   rec(m + 1, r, h - 1); n } n int cntpairs(int sz) { return (long long)sz * (sz - 1) / 2 % MOD; } n int calc(int sz) { return (long long)cntpairs(sz) * INV2 % MOD; } n int calc(int sza, int szb) { n   int ret = 0; n   for (int den = (2); den <= (sza + szb); ++den) { n     int lo = max(1, den - szb), hi = min(sza, den - 1), cnt = hi - lo + 1; n     if (lo > hi) continue; n     int cur = (long long)(den - 2) % MOD * inv[den] % MOD * INV2 % MOD; n     ret = (ret + (long long)cnt * cur) % MOD; n   } n   return ret; n } n int solve() { n   INV2 = (MOD + 1) / 2; n   inv[1] = 1; n   for (int i = (2); i <= (2 * n); ++i) n     inv[i] = (long long)(MOD - MOD / i) * inv[MOD % i] % MOD; n   parts.clear(); n   rec(1, n, mxdepth); n   int sz1 = -1, cnt1 = 0, sz2 = -1, cnt2 = 0; n   for (int i = (0); i < (((int)(parts).size())); ++i) { n     int x = parts[i]; n     if (x == sz1) n       ++cnt1; n     else if (x == sz2) n       ++cnt2; n     else if (sz1 == -1) n       sz1 = x, ++cnt1; n     else if (sz2 == -1) n       sz2 = x, ++cnt2; n     else n       assert(false); n   } n   int ret = 0; n   if (cnt1 != 0) ret = (ret + (long long)cnt1 * calc(sz1)) % MOD; n   if (cnt2 != 0) ret = (ret + (long long)cnt2 * calc(sz2)) % MOD; n   if (cnt1 != 0) ret = (ret + (long long)cntpairs(cnt1) * calc(sz1, sz1)) % MOD; n   if (cnt1 != 0 && cnt2 != 0) n     ret = (ret + (long long)cnt1 * cnt2 % MOD * calc(sz1, sz2)) % MOD; n   if (cnt2 != 0) ret = (ret + (long long)cntpairs(cnt2) * calc(sz2, sz2)) % MOD; n   return ret; n } n void run() { n   scanf( %d%d%d , &n, &mxdepth, &MOD); n   printf( %d n , solve()); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n int a[maxn], s[maxn], f[maxn], n, ans; n int pmin, pmax; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i), f[i] = -1; n   sort(a, a + n); n   swap(a[0], a[n - 1]); n   for (int i = 0; i < n; i++) printf( %d  , a[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10]; n int main() { n   for (int i = 1; i <= 6; i++) cin >> a[i]; n   cout << (a[1] + a[2] + a[3]) * (a[1] + a[2] + a[3]) - a[1] * a[1] - n               a[3] * a[3] - a[5] * a[5]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 210, INF = 0x3f3f3f3f, VMAX = 20010; n int N, A[NMAX], Source, Sink, Flow[NMAX][NMAX], Cap[NMAX][NMAX], Father[NMAX], n     Even, Odd; n bool Used[NMAX], Prime[VMAX]; n vector<int> G[NMAX], FlowG[NMAX], CurrentTable; n vector<vector<int> > Ans; n bool BFS() { n   queue<int> Q; n   for (int i = 0; i <= N + 1; ++i) Used[i] = Father[i] = 0; n   Used[Source] = 1; n   Q.push(Source); n   while (!Q.empty()) { n     int Node = Q.front(); n     Q.pop(); n     if (Node == Sink) return 1; n     for (int i = 0; i < G[Node].size(); ++i) { n       int Vec = G[Node][i]; n       if (!Used[Vec] && Cap[Node][Vec] > Flow[Node][Vec]) n         Used[Vec] = 1, Father[Vec] = Node, Q.push(Vec); n     } n   } n   return Used[Sink]; n } n void DFS(int Node, int StartNode) { n   if (Node == StartNode && CurrentTable.size() >= 3) return; n   CurrentTable.push_back(Node); n   Used[Node] = 1; n   for (int i = 0; i < FlowG[Node].size(); ++i) n     if (!Used[FlowG[Node][i]]) DFS(FlowG[Node][i], StartNode); n } n void BuildGraph() { n   Source = 0; n   Sink = N + 1; n   for (int i = 1; i <= N; ++i) n     if (A[i] % 2 == 1) { n       G[Source].push_back(i); n       G[i].push_back(Source); n       Cap[Source][i] = 2; n     } else { n       G[i].push_back(Sink); n       G[Sink].push_back(i); n       Cap[i][Sink] = 2; n     } n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= N; ++j) n       if (A[i] % 2 == 1 && A[j] % 2 == 0 && Prime[A[i] + A[j]]) { n         G[i].push_back(j); n         G[j].push_back(i); n         Cap[i][j] = 1; n       } n } n void MaxFlow() { n   int CntFlow = 0; n   while (BFS()) { n     for (int i = 0; i < G[Sink].size(); ++i) { n       int Vec = G[Sink][i]; n       if (Used[Vec] && Cap[Vec][Sink] != Flow[Vec][Sink]) { n         int MinFlow = INF; n         Father[Sink] = Vec; n         for (int Node = Sink; Node != Source; Node = Father[Node]) n           MinFlow = n               min(MinFlow, Cap[Father[Node]][Node] - Flow[Father[Node]][Node]); n         for (int Node = Sink; Node != Source; Node = Father[Node]) { n           Flow[Father[Node]][Node] += MinFlow; n           Flow[Node][Father[Node]] -= MinFlow; n         } n         CntFlow += MinFlow; n       } n     } n   } n   if (CntFlow != N) n     printf( Impossible n ); n   else { n     for (int i = 1; i <= N; ++i) n       for (int j = 1; j <= N; ++j) n         if (A[i] % 2 == 1 && A[j] % 2 == 0 && Prime[A[i] + A[j]] && n             Flow[i][j] == 1) n           FlowG[i].push_back(j), FlowG[j].push_back(i); n     for (int i = 1; i <= N; ++i) Used[i] = 0; n     for (int i = 1; i <= N; ++i) n       if (!Used[i]) { n         CurrentTable.clear(); n         DFS(i, i); n         Ans.push_back(CurrentTable); n       } n     printf( %i n , Ans.size()); n     for (int i = 0; i < Ans.size(); ++i) { n       printf( %i  , Ans[i].size()); n       for (int j = 0; j < Ans[i].size(); ++j) printf( %i  , Ans[i][j]); n       printf(  n ); n     } n     return; n   } n } n int main() { n   for (int i = 2; i < VMAX; ++i) Prime[i] = 1; n   for (int i = 2; i < VMAX; ++i) n     if (Prime[i]) n       for (int j = i + i; j < VMAX; j += i) Prime[j] = 0; n   scanf( %i , &N); n   for (int i = 1; i <= N; ++i) { n     scanf( %i , &A[i]); n     Even += (A[i] % 2 == 0); n     Odd += (A[i] % 2 == 1); n   } n   if (Even != Odd) { n     printf( Impossible n ); n     return 0; n   } n   BuildGraph(); n   MaxFlow(); n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-3; n const double PI = 3.1415926535897932384626433832795; n const int maxn = 9999999; n using namespace std; n char name[1001][40]; n int grade[1001]; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     map<string, int> mp; n     for (int i = 1; i <= n; i++) { n       scanf( %s %d , name[i], &grade[i]); n       mp[name[i]] += grade[i]; n     } n     int max = -9999999; n     for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) n       if (it->second > max) max = it->second; n     map<string, int> mp2; n     for (int i = 1; i <= n; i++) { n       mp2[name[i]] += grade[i]; n       if (mp[name[i]] == max && mp2[name[i]] >= max) { n         puts(name[i]); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n #define all(x) (x).begin(), (x).end() n typedef vector<bool> vb; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vector<bool>> vvb; n typedef vector<vector<int>> vvi; n typedef vector<vector<ll>> vvl; n typedef pair<int,int> pii; n typedef pair<ll,ll> pll; n void debugF(ostream &os) {os << endl;} n template <typename Head, typename... Tail> n void debugF(ostream &os, Head H, Tail... T) { os <<     << H; debugF(os, T...); } n #define debug(...) cerr <<  [  << #__VA_ARGS__ <<  ]: , debugF(cerr, __VA_ARGS__) n template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } n template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); os << (i>0 ?    :  ) << v[i++]); return os;} n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mod = 998244353; n  n ll expMod(ll a, ll b, ll mod){ n     ll ans = 1; n     while(b){ n         if(b&1) ans = (ans*a)%mod; n         a = (a*a)%mod; n         b >>= 1; n     } n     return ans; n } n  n int calc(vector<string> &M, vi &dp2){ n     int n = M.size(); int m = M[0].size(); n     int i,j; n     int ans = 0; n     for(i=0;i<n;i++){ n         int cc = 0; n         for(j=0;j<m;j++){ n             if(M[i][j] ==  o ){ n                 cc++; n                 if(j+1 >= m || M[i][j+1] !=  o ){ n                     ans += dp2[cc-1]; n                     ans %= mod; n                 } n             } n             else  n                 cc = 0; n         } n     } n     return ans; n } n  n void transpose(vector<string> &M){ n     int n = M.size(); int m = M[0].size(); n     vector<string> M2(m, string(n, . )); n     int i,j; n     for(i=0;i<n;i++){ n         for(j=0;j<m;j++){ n             M2[j][i] = M[i][j]; n         } n     } n     M = M2; n } n  n void solve(){ n     int n, m, i, j; n     cin >> n >> m; n     vector<string> M(n); n     cin >> M; n     int nw = 0; n     for(i=0;i<n;i++){ n         nw += count(all(M[i]),  o ); n     } n     if(nw < 2){ n         cout << 0 <<   n ; n         return; n     } n  n     int N = max(n,m); n  n     vi p2(nw+1); n     p2[0] = 1; n     for(i=1;i<=nw;i++){ n         p2[i] = (p2[i-1]*2)%mod; n     } n     ll div4 = expMod(4, mod-2, mod); n      n     vi dp(N); n     vi dp2(N); //dp2[i] = dp[0] + ... + dp[i-1] n     dp[0] = p2[nw-2]; n     dp2[0] = 0; n     for(i=1;i<N;i++){ n         dp[i] = p2[nw-3]; n         if(i-2 >= 0){ n             dp[i] += (dp[i-2]*div4)%mod; n             dp[i] %= mod;   n         } n         dp2[i] = (dp2[i-1] + dp[i-1])%mod; n     } n  n     auto ans = calc(M, dp2); n     transpose(M); n     ans += calc(M, dp2); n     ans %= mod; n     cout << ans <<   n ; n } n  n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n     int nt = 1; n     for(int tt=1;tt<=nt;tt++){ n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n int main() { n   int n, k, i, j; n   int hols, arr[1000]; n   int temp, loop, flag = 0; n   scanf( %d%d , &n, &k); n   scanf( %d , &hols); n   for (i = 0; i <= n; i++) { n     arr[i] = 0; n   } n   for (i = 0; i < hols; i++) { n     scanf( %d , &temp); n     arr[temp] = 2; n   } n   for (i = 0; i <= n;) { n     for (j = i; j < i + k; j++) { n       if (arr[j] == 2) { n         flag = 1; n         loop = j; n       } n     } n     if (flag == 1) { n       flag = 0; n       i = loop; n     } else { n       i = i + k; n     } n     arr[i] = 1; n   } n   int min = 0; n   for (i = 1; i <= n; i++) { n     if (arr[i] == 1 || arr[i] == 2) min++; n   } n   printf( %d , min); n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n const double pi = acos(-1); n using namespace std; n struct sx {}; n bool cmp(int a, int b) { return a > b; } n long long t1, t2, t3; n int main() { n   int n, m; n   cin >> n >> m; n   int x = (n + m) / 3; n   while (t1 < x && ((n >= 1 && m >= 2) || (n >= 2 && m >= 1))) { n     if (n > m) { n       n -= 2; n       m -= 1; n     } else { n       m -= 2; n       n -= 1; n     } n     t1++; n   } n   cout << t1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str1, str2; n   cin >> str1 >> str2; n   for (int i = 0; i < str1.size(); i++) { n     str1[i] = tolower(str1[i]); n     str2[i] = tolower(str2[i]); n   } n   if (str1 == str2) n     cout <<  0 ; n   else if (str1 < str2) n     cout <<  -1 ; n   else n     cout <<  1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> v; n set<int> g[100005]; n vector<pair<int, int> > f; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       if (i * i != n && i != 1) v.push_back(n / i); n     } n   } n   int a, b; n   for (int i = 1; i <= m; i++) { n     cin >> a >> b; n     g[a].insert(a); n     g[b].insert(a); n     f.push_back(make_pair(a, b)); n   } n   for (auto x : v) { n     bool flag = 1; n     for (auto y : f) { n       int l = y.first, r = y.second; n       l += x, r += x; n       if (l > n) l -= n; n       if (r > n) r -= n; n       if (g[l].count(r) || g[r].count(l)) n         continue; n       else { n         flag = 0; n         break; n       } n     } n     if (flag) { n       cout <<  Yes  << endl; n       exit(0); n     } n   } n   cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n using ull = unsigned long long; n using ll = long long; n using ld = long double; n const int mod = 1e9 + 7; n const double pi = acos(-1.0); n const int inf = INT_MAX; n const int N = (1 << 22); n int h, g, a[N]; n bool ok(int i) { n   while (true) { n     if (a[i << 1] == 0 && a[i << 1 | 1] == 0) { n       break; n     } n     if (a[i << 1] > a[i << 1 | 1]) { n       i = i << 1; n     } else { n       i = i << 1 | 1; n     } n   } n   int msb = 32 - __builtin_clz(i) - 1; n   return msb >= g; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     cin >> h >> g; n     for (int i = 1; i < (1 << h); i++) cin >> a[i]; n     fill(a + (1 << h), a + (1 << (h + 1)), 0); n     vector<int> ops; n     for (int i = 1; i < (1 << g); i++) { n       while (ok(i)) { n         ops.push_back(i); n         int j; n         for (j = i; a[j << 1] || a[j << 1 | 1];) { n           if (a[j << 1] > a[j << 1 | 1]) { n             a[j] = a[j << 1]; n             j = j << 1; n           } else { n             a[j] = a[j << 1 | 1]; n             j = j << 1 | 1; n           } n         } n         a[j] = 0; n       } n     } n     ll ans = 0; n     for (int i = 1; i < (1 << g); i++) { n       ans += a[i]; n     } n     cout << ans <<   n ; n     for (int i : ops) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[1000000]; n int buggy_sorting(int n) { n   if (n == 1 || n == 2) return 0; n   v[0] = 5; n   v[1] = 6; n   v[2] = 1; n   for (int i = 3; i < n; i++) v[i] = i; n   return 1; n } n void median() { n   int t, n, k, pas, cnt; n   long long int s; n   cin >> t; n   for (int z = 0; z < t; z++) { n     cin >> n >> k; n     pas = n / 2; n     for (int i = 0; i < n * k; i++) cin >> v[i]; n     cnt = 0; n     s = 0; n     for (int i = n * k - 1 - pas; i >= 0; i -= pas + 1) n       if (cnt < k) { n         s += v[i]; n         cnt++; n       } n     cout << s <<   n ; n   } n } n int main() { n   int n; n   cin >> n; n   if (buggy_sorting(n)) n     for (int i = 0; i < n; i++) cout << v[i] <<    ; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool used[100001]; n int ans[100001], a[100001]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] <= n && !used[a[i]]) { n       used[a[i]] = 1; n       ans[i] = a[i]; n     } n   } n   int first = 1; n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 0) { n       while (used[first]) first++; n       ans[i] = first; n       first++; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * f; n } n const int maxn = 1e5 + 10; n int n, tot, head[maxn], d[maxn], a[maxn], ans, sz[maxn]; n struct data { n   int to, next, w; n } edge[maxn]; n void calc(int x) { n   sz[x] = 1; n   for (int i = head[x]; i; i = edge[i].next) { n     int y = edge[i].to; n     calc(y); n     sz[x] += sz[y]; n   } n } n void dfs(int x) { n   if (d[x] > a[x]) { n     ans += sz[x]; n     return; n   } n   for (int i = head[x]; i; i = edge[i].next) { n     int y = edge[i].to; n     d[y] = d[x] + edge[i].w; n     if (d[y] < 0) d[y] = 0; n     dfs(y); n   } n } n inline void make(int u, int v, int w) { n   edge[++tot] = (data){v, head[u], w}; n   head[u] = tot; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 2; i <= n; i++) { n     int x = read(), y = read(); n     make(x, i, y); n   } n   calc(1); n   dfs(1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, p, d, x; n   cin >> n >> k >> d; n   p = n - k; n   for (int i = 0; i < d; i++) { n     cin >> x; n     x--; n     if (p <= k) { n       if (x % 2 == 0 && x / 2 < p) { n         cout <<  . ; n       } else { n         cout <<  X ; n       } n     } else { n       if (n % 2 == 0) { n         if ((x % 2 == 1) && (n - 1 - x) / 2 < k) { n           cout <<  X ; n         } else { n           cout <<  . ; n         } n       } else { n         if (x != n - 1) { n           if ((x % 2 == 1) && (n - 2 - x) / 2 < (k - 1)) { n             cout <<  X ; n           } else { n             cout <<  . ; n           } n         } else { n           if (k > 0) n             cout <<  X ; n           else n             cout <<  . ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   string s; n   vector<long long int> arr(n + 1); n   for (long long int i = 1; i <= n; i++) cin >> arr[i]; n   cin >> s; n   vector<long long int> pre(n, 0); n   if (s.back() ==  1 ) pre[n - 1] = 1; n   for (int i = n - 2; i >= 0; i--) { n     if (s[i] ==  1 ) n       pre[i] = 1 + pre[i + 1]; n     else n       pre[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     if (i > arr[i]) { n       if (arr[i] + pre[arr[i] - 1] < i) { n         cout <<  NO n ; n         return; n       } n     } else if (i < arr[i]) { n       if (i + pre[i - 1] < arr[i]) { n         cout <<  NO n ; n         return; n       } n     } n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned char G[301][301]; n unsigned char bv[301]; n int ints[301]; n int main() { n   int n; n   cin >> n; n   for (register int i = 1; i <= n; i++) cin >> ints[i]; n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= n; j++) { n       char c; n       cin >> c; n       G[i][j] = c -  0 ; n     } n   } n   for (register int k = 1; k <= n; k++) { n     for (register int i = 1; i <= n; i++) { n       for (register int j = 1; j <= n; j++) { n         G[i][j] = G[i][j] || (G[i][k] && G[k][j]); n       } n     } n   } n   for (register int i = 1; i <= n; i++) { n     G[i][i] = 1; n   } n   ints[0] = 0x7fffffff; n   for (register int i = 1; i <= n; i++) { n     int minn = 0; n     for (register int j = 1; j <= n; j++) { n       if (ints[j] < ints[minn] && G[i][j] && bv[j] == 0) minn = j; n     } n     printf( %d  , ints[minn]); n     bv[minn] = 1; n   } n   return (0); n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 0x3f3f3f3f; n const long long MOD = 1e9 + 7; n long long n, sum[100002][2], sqsum[100002][2], cnt[100002][2], all_7[100002], n     s[100002], ten[200002]; n long long solve(string str) { n   n = str.length(); n   memset(all_7, 0, sizeof(all_7)); n   memset(sum, 0, sizeof(sum)), memset(sqsum, 0, sizeof(sqsum)), n       memset(cnt, 0, sizeof(cnt)); n   cnt[0][1] = 1; n   all_7[n + 1] = 1; n   for (long long i = n; i >= 1; --i) n     all_7[i] = all_7[i + 1] & (str[i - 1] ==  7 ); n   long long rest = 0; n   for (long long i = 1; i <= n; ++i) { n     long long num = str[i - 1] ^ 48; n     for (long long j = 0; j < 2; ++j) { n       for (long long jj = 4; jj <= 7; jj += 3) { n         if (j && (jj > num)) break; n         long long new_j = j & (jj == num); n         (cnt[i][new_j] += cnt[i - 1][j]) %= MOD; n         (sum[i][new_j] += n          (1ll * cnt[i - 1][j] * jj % MOD + 10ll * sum[i - 1][j])) %= MOD; n         (sqsum[i][new_j] += n          (100ll * sqsum[i - 1][j] + 20ll * jj * sum[i - 1][j] + n           1ll * jj * jj * cnt[i - 1][j])) %= MOD; n       } n     } n     long long tmp = 0, A, B; n     bool smaller = (str[i - 1] ==  7 ); n     A = 7ll * ten[n - i] % MOD + 4ll * s[n - i] % MOD; n     A %= MOD; n     B = 4ll * ten[n - i] % MOD + 7ll * s[n - i] % MOD; n     B %= MOD; n     long long Sum = sum[i - 1][0], Sqsum = sqsum[i - 1][0], Cnt = cnt[i - 1][0]; n     if (smaller) { n       Sum += sum[i - 1][1], Sqsum += sqsum[i - 1][1], Cnt += cnt[i - 1][1]; n       Sum %= MOD, Sqsum %= MOD, Cnt %= MOD; n     } n     (tmp += (1ll * Sqsum * ten[(n - i + 1) * 2] % MOD)) %= MOD; n     (tmp += n      (1ll * Sum * A % MOD + 1ll * Sum * B % MOD) * ten[n - i + 1] % MOD) %= MOD; n     (tmp += 1ll * A * B % MOD * Cnt % MOD) %= MOD; n     (rest += tmp) %= MOD; n   } n   return rest; n } n signed main() { n   ios::sync_with_stdio(false); n   string l, r; n   s[0] = 0; n   s[1] = 1; n   ten[0] = 1; n   ten[1] = 10; n   for (long long i = 2; i <= 100000; ++i) s[i] = (10ll * s[i - 1] + 1) % MOD; n   for (long long i = 2; i <= 200000; ++i) ten[i] = (10ll * ten[i - 1]) % MOD; n   cin >> l >> r; n   cout << (solve(r) - solve(l) + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, i, j, x, k; n   cin >> n >> k; n   string s, t; n   cin >> s; n   t = s; n   for (i = 0; i < n; i++) { n     if (i - k >= 0) t[i] = t[i - k]; n   } n   if (t > s) { n     cout << n <<   n  << t; n     return 0; n   } n   for (i = 0; i < n; i++) { n     if (s[i] > t[i]) { n       for (j = k - 1; j >= 0; j--) { n         if (t[j] !=  9 ) break; n       } n       for (; j < n; j += k) t[j]++; n       break; n     } n   } n   for (j = 0; j < k; j++) n     if (s[j] < t[j]) break; n   for (i = j + 1; i < k; i++) { n     for (long long p = i; p < n; p += k) t[p] =  0 ; n   } n   cout << n <<   n  << t; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n const int N = 1000 + 5, mod = 1000000007; n int n, edges; n vector<pair<int, int>> graph[N]; n bool cycle; n bool vis[N]; n int mxmDepth, indx, mnmDiameter; n void dfs(int node, int dep) { n   vis[node] = true; n   if (mxmDepth < dep) { n     mxmDepth = dep; n     indx = node; n   } n   for (auto child : graph[node]) { n     if (vis[child.first]) { n       cycle = true; n       return; n     } n     mnmDiameter = min(mnmDiameter, child.second); n     dfs(child.first, dep + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> edges; n   set<int> st; n   for (int i = 1; i <= n; i++) st.insert(i); n   for (int i = 1; i <= edges; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     graph[u].push_back({v, c}); n     st.erase(v); n   } n   vector<pair<int, pair<int, int>>> ans; n   for (auto par : st) { n     if (graph[par].empty()) continue; n     cycle = false; n     mxmDepth = 0; n     mnmDiameter = 1e9; n     indx = -1; n     dfs(par, 0); n     if (cycle) continue; n     ans.push_back({par, {indx, mnmDiameter}}); n   } n   cout << ans.size() <<   n ; n   for (auto it : ans) { n     cout << it.first <<     << it.second.first <<     << it.second.second n          <<   n ; 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     long long int n, r, an; n     cin >> n >> r; n     if (r < n) n       an = (r) * (r + 1) / 2; n     else { n       an = (n) * (n - 1) / 2; n       an++; n     } n     cout << an; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int n; n long long seg[4 * N], lazy[4 * N]; n void update(int st, int en, long long v, int idx = 1, int L = 0, n             int R = n - 1) { n   if (L > en || R < st) { n     return; n   } n   if (L >= st && R <= en) { n     seg[idx] += v; n     lazy[idx] += v; n     return; n   } n   seg[2 * idx] += lazy[idx]; n   seg[2 * idx + 1] += lazy[idx]; n   lazy[2 * idx] += lazy[idx]; n   lazy[2 * idx + 1] += lazy[idx]; n   lazy[idx] = 0; n   update(st, en, v, 2 * idx, L, (L + R) / 2); n   update(st, en, v, 2 * idx + 1, (L + R) / 2 + 1, R); n   seg[idx] = min(seg[2 * idx], seg[2 * idx + 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   int a[n], c[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i], --a[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> c[a[i]]; n   } n   long long s = 0; n   for (int i = 0; i < n; ++i) { n     update(i, i, s); n     s += c[i]; n   } n   long long ans = 9e18; n   for (int i = 0; i < n - 1; ++i) { n     update(0, a[i], c[a[i]]); n     update(a[i], n - 1, -c[a[i]]); n     ans = min(ans, seg[1]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   bool val = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int a; n       scanf( %d , &a); n       if (i == j) { n         val ^= a; n       } n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 3) { n       printf( %d , val); n     } else { n       int i; n       scanf( %d , &i); n       val ^= 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long BINF = 1e18 + 10; n signed main() { n   ios_base::sync_with_stdio(false); n   long long T; n   cin >> T; n   while (T--) { n     long long n, a, b; n     cin >> n >> a >> b; n     vector<long long> c(n); n     for (auto &it : c) { n       char kek; n       cin >> kek; n       it = kek -  0 ; n     } n     vector<vector<long long>> dp(n + 1, vector<long long>(2, BINF)); n     dp[0][0] = b; n     for (long long i = 0; i < n; i++) { n       dp[i + 1][1] = min(dp[i][0] + 2 * (a + b), dp[i][1] + a + 2 * b); n       if (!c[i] && (i + 1 == n || !c[i + 1])) { n         dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + 2 * a + b); n       } n     } n     cout << dp[n][0] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n typedef struct Nodo { n   long long int v, l; n } Nodo; n int S; n Nodo segtree[2000024]; n inline void st_init(int N) { n   if (__builtin_popcount(N) <= 1) { n     S = N; n   } else { n     for (; __builtin_popcount(N) > 1; N -= N & -N) n       ; n     S = N << 1; n   } n   memset(segtree, 0, (sizeof(Nodo) * S) << 1); n } n inline void st_deflag(int p) { n   long long int l = segtree[p].l; n   if (!l) { n     return; n   } n   if (p < S) { n     long long int &a = segtree[p << 1].l; n     long long int &b = segtree[(p << 1) + 1].l; n     a = l; n     b = l; n   } n   segtree[p].v = l; n   segtree[p].l = 0; n } n inline void st_update(int n) { n   long long int l = segtree[n << 1].v; n   long long int r = segtree[(n << 1) + 1].v; n   segtree[n].v = (l > r ? l : r); n } n void st_set(int n, int a, int b, int i, int f, long long int v) { n   st_deflag(n); n   if (i >= a && f <= b) { n     segtree[n].l = v; n     st_deflag(n); n     return; n   } else if (f < a || i > b) { n     return; n   } n   int d = ((f - i) >> 1) + 1; n   st_set(n << 1, a, b, i, f - d, v); n   st_set((n << 1) + 1, a, b, i + d, f, v); n   st_update(n); n } n long long int st_query(int n, int a, int b, int i, int f) { n   st_deflag(n); n   if (i >= a && f <= b) { n     return segtree[n].v; n   } else if (f < a || i > b) { n     return -0x3f3f3f3f3f3f3f3fll; n   } n   int d = ((f - i) >> 1) + 1; n   long long int r1 = st_query(n << 1, a, b, i, f - d); n   long long int r2 = st_query((n << 1) + 1, a, b, i + d, f); n   return (r1 > r2 ? r1 : r2); n } n int M; n int W, H; n int main() { n   int i, a; n   long long R; n   scanf( %d , &N); n   st_init(N); n   for (i = 0; i < N; i++) { n     scanf( %d , &a); n     st_set(1, i, i, 0, S - 1, (long long)a); n   } n   scanf( %d , &M); n   for (i = 0; i < M; i++) { n     scanf( %d %d , &W, &H); n     R = st_query(1, 0, (W - 1 < N - 1 ? W - 1 : N - 1), 0, S - 1); n     printf( %I64d n , R); n     st_set(1, 0, (W - 1 < N - 1 ? W - 1 : N - 1), 0, S - 1, R + (long long)H); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const long double PI = 3.141592653589793238462643383279502884197; n priority_queue<int, vector<int>, greater<int> > pq; n vector<int> v; n int cnt[512]; n pair<int, int> vec[512]; n pair<int, int> sec[512]; n int main() { n   memset(vec, 0x3f, sizeof(vec)); n   memset(vec, 0x3f, sizeof(vec)); n   int n, m; n   scanf( %d %d , &n, &m); n   int t; n   for (int i = 0; i < n; i++) { n     scanf( %d , &t); n     vector<int> kk; n     int k, bit = 0; n     while (t--) { n       scanf( %d , &k); n       k--; n       bit |= 1 << k; n     } n     for (int j = 0; j < 512; j++) n       if ((bit & j) == bit) cnt[j]++; n   } n   for (int i = 0; i < m; i++) { n     int p, t, bit = 0; n     scanf( %d %d , &p, &t); n     while (t--) { n       int k; n       scanf( %d , &k); n       k--; n       bit |= (1 << k); n     } n     if (vec[bit].first > p) { n       sec[bit] = vec[bit]; n       vec[bit] = {p, i + 1}; n     } else if (sec[bit].first > p) { n       sec[bit] = {p, i + 1}; n     } n   } n   int mav = -1, mivp = 1e9, u, v; n   for (int i = 0; i < 512; i++) { n     if (vec[i].first > 1e9) continue; n     for (int j = i + 1; j < 512; j++) { n       if (i == j || vec[j].first > 1e9) continue; n       int k = i | j; n       if (mav < cnt[k]) { n         mav = cnt[k]; n         mivp = vec[i].first + vec[j].first; n         u = vec[i].second; n         v = vec[j].second; n       } else if (mav == cnt[k] && mivp > vec[i].first + vec[j].first) { n         mivp = vec[i].first + vec[j].first; n         u = vec[i].second; n         v = vec[j].second; n       } n     } n     if (sec[i].first > 1e9) continue; n     if (mav < cnt[i]) { n       mav = cnt[i]; n       mivp = vec[i].first + sec[i].first; n       u = vec[i].second, v = sec[i].second; n     } else if (mav == cnt[i] && mivp > vec[i].first + sec[i].first) { n       mivp = vec[i].first + sec[i].first; n       u = vec[i].second, v = sec[i].second; n     } n   } n   printf( %d %d , u, v); n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n struct Level { n   int lo[10 + 5], hi[10 + 5], sz; n   bool operator<(const Level &l) const { n     for (int i = 0; i < K; i++) { n       if (hi[i] >= l.lo[i]) return false; n     } n     return true; n   } n }; n Level s[50000 + 5]; n set<Level> ps; n void merge(Level &tar, const Level &src) { n   for (int i = 0; i < K; i++) { n     tar.lo[i] = min(tar.lo[i], src.lo[i]); n     tar.hi[i] = max(tar.hi[i], src.hi[i]); n   } n   tar.sz += src.sz; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> N >> K; n   for (int i = 0; i < N; i++) { n     s[i].sz = 1; n     for (int j = 0; j < K; j++) { n       cin >> s[i].lo[j]; n       s[i].hi[j] = s[i].lo[j]; n     } n     auto it1 = ps.lower_bound(s[i]), it2 = ps.upper_bound(s[i]); n     while (!ps.empty() && it1 != it2) { n       merge(s[i], *it1); n       ps.erase(it1); n       it1 = ps.lower_bound(s[i]); n       it2 = ps.upper_bound(s[i]); n     } n     ps.insert(s[i]); n     cout << (--ps.end())->sz <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100005], lazy[4 * 100005], num, value; n bool flag; n struct Node { n   int maxi, mini, one, two; n } tree[4 * 100005]; n Node merge(Node a, Node b) { n   Node temp; n   int num = max(a.maxi, b.maxi); n   temp.maxi = num, temp.one = num == a.maxi ? a.one : b.one; n   num = min(a.mini, b.mini); n   temp.mini = num, temp.two = num == a.mini ? a.two : b.two; n   return temp; n } n void build(int node, int a, int b) { n   if (a > b) return; n   if (a == b) { n     tree[node] = (Node){arr[a], arr[a], a, a}; n     return; n   } n   int left, right; n   left = 2 * node + 1, right = 2 * node + 2; n   build(left, a, (a + b) / 2); n   build(right, (a + b) / 2 + 1, b); n   tree[node] = merge(tree[left], tree[right]); n } n void lazy_update(int node, int a, int b) { n   int left, right; n   left = 2 * node + 1, right = 2 * node + 2; n   tree[node].maxi -= lazy[node]; n   tree[node].mini -= lazy[node]; n   if (a != b) { n     lazy[left] += lazy[node]; n     lazy[right] += lazy[node]; n   } n   lazy[node] = 0; n } n void update(int node, int a, int b, int i, int j, int val) { n   if (lazy[node]) lazy_update(node, a, b); n   if (a > b || a > j || b < i) return; n   if (a >= i && b <= j) { n     lazy[node] = val; n     lazy_update(node, a, b); n     return; n   } n   int left, right; n   left = 2 * node + 1, right = 2 * node + 2; n   update(left, a, (a + b) / 2, i, j, val); n   update(right, (a + b) / 2 + 1, b, i, j, val); n   tree[node] = merge(tree[left], tree[right]); n } n void query(int node, int a, int b, int i, int j, int val) { n   if (lazy[node]) lazy_update(node, a, b); n   if (flag) return; n   if (a > b || a > j || b < i) return; n   if (val > tree[node].mini) { n     if (a == b) { n       flag = true; n       num = a; n       return; n     } n     int left, right; n     left = 2 * node + 1, right = 2 * node + 2; n     query(left, a, (a + b) / 2, i, j, val); n     query(right, (a + b) / 2 + 1, b, i, j, val); n   } n } n void find(int node, int a, int b, int i, int j) { n   if (lazy[node]) lazy_update(node, a, b); n   if (a > b || a > j || b < i) return; n   if (a == b) { n     value = tree[node].mini; n     return; n   } n   int left, right; n   left = 2 * node + 1, right = 2 * node + 2; n   find(left, a, (a + b) / 2, i, j); n   find(right, (a + b) / 2 + 1, b, i, j); n } n int main() { n   int n, a, b, one, two, k = 0, diff; n   scanf( %d , &n); n   set<int> zero; n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (!arr[i]) zero.insert(i); n   } n   build(0, 0, n - 1); n   zero.insert(n); n   vector<pair<int, int> > vec; n   while (true) { n     if (lazy[0]) lazy_update(0, 0, n - 1); n     if (tree[0].maxi == 0) break; n     int index = tree[0].one, x, y; n     y = *zero.upper_bound(index); n     flag = false; n     query(0, 0, n - 1, index + 1, y - 1, tree[0].maxi); n     if (flag) n       a = index, b = num - 1; n     else n       a = index, b = min(n - 1, y - 1); n     update(0, 0, n - 1, a, b, 1); n     vec.push_back(make_pair(a + 1, b + 1)); n   } n   printf( %d n , vec.size()); n   for (int i = 0; i < vec.size(); i++) n     printf( %d %d n , vec[i].first, vec[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50; n int x[N][N], y[N], True[N], False[N], ans; n int n, m; n bool flag = false; n void dfs(int pos) { n   if (pos > n) { n     ans++; n     return; n   } n   bool f = true; n   for (int i = 1; i <= m; i++) { n     if (!x[i][pos]) n       True[i]++; n     else n       False[i]++; n     if (True[i] > y[i] || False[i] > n - y[i]) f = false; n   } n   if (f) dfs(pos + 1); n   f = true; n   for (int i = 1; i <= m; i++) { n     if (!x[i][pos]) n       True[i]--, False[i]++; n     else n       False[i]--, True[i]++; n     if (True[i] > y[i] || False[i] > n - y[i]) f = false; n   } n   if (f) dfs(pos + 1); n   for (int i = 1; i <= m; i++) { n     if (!x[i][pos]) n       False[i]--; n     else n       True[i]--; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) scanf( %1d , &x[i][j]); n     scanf( %d , &y[i]); n   } n   dfs(1); n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010, mod = 998244353; n int a[N]; n int n; n int fac[N], inv[N]; n inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } n inline int del(int a, int b) { return a < b ? a - b + mod : a - b; } n inline int mul(int a, int b) { return a * 1ll * b % mod; } n inline int qpow(int a, int b) { n   int s = 1; n   while (b) { n     if (b & 1) s = mul(s, a); n     a = mul(a, a); n     b >>= 1; n   } n   return s; n } n int main() { n   scanf( %d , &n); n   int m = 2 * n; n   fac[0] = fac[1] = 1; n   for (int i = 2; i <= m; ++i) fac[i] = mul(fac[i - 1], i); n   inv[m] = qpow(fac[m], mod - 2); n   for (int i = m - 1; i >= 1; --i) inv[i] = mul(inv[i + 1], i + 1); n   inv[0] = 1; n   for (int i = 1; i <= m; ++i) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + m); n   for (int i = 1; i <= m; ++i) n     if (a[i] >= mod) a[i] %= mod; n   int ans = 0; n   for (int i = 1; i <= n; ++i) ans = del(ans, a[i]); n   for (int i = n + 1; i <= m; ++i) ans = add(ans, a[i]); n   printf( %d n , mul(ans, mul(fac[m], mul(inv[n], inv[n])))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> return_digit(long long num) { n   vector<long long> arr; n   while (num != 0) { n     long long rem = num % 10; n     arr.push_back(rem); n     num /= 10; n   } n   return arr; n } n long long product_minmax(vector<long long> v) { n   long long max_val = v[0]; n   long long min_val = v[0]; n   for (long long i = 1; i < v.size(); i++) { n     max_val = max(max_val, v[i]); n     min_val = min(min_val, v[i]); n   } n   return max_val * min_val; n } n long long compute(long long num) { n   return num + product_minmax(return_digit(num)); n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a1, k; n     cin >> a1 >> k; n     long long temp = a1; n     long long i = 0; n     long long prev = a1; n     while (i < k - 1) { n       prev = temp; n       temp = compute(temp); n       if (prev == temp) { n         break; n       } n       i += 1; n     } n     cout << temp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, T, a, b; n     cin >> n >> T >> a >> b; n     vector<pair<int, int>> qstn(n); n     int totA = 0, totB = 0; n     for (auto& i : qstn) { n       cin >> i.second; n       totB += i.second; n     } n     totA = n - totB; n     for (auto& i : qstn) { n       cin >> i.first; n     } n     sort(qstn.begin(), qstn.end()); n     int sol = 0, ctA = 0, ctB = 0, i = -1; n     do { n       if (i >= 0 && i < n) { n         if (qstn[i].second) { n           ++ctB; n         } else { n           ++ctA; n         } n       } n       int t = (i + 1 < n ? qstn[i + 1].first - 1 : T); n       if ((long long)ctA * a + (long long)ctB * b > t) continue; n       t -= ctA * a + ctB * b; n       int xtra = min(totA - ctA, t / a); n       t -= xtra * a; n       xtra += min(totB - ctB, t / b); n       sol = max(sol, xtra + ctA + ctB); n     } while (++i < n); n     cout << sol <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int s[1000 * 1000]; n int main() { n   int cnt = 1; n   for (int i = 1; i <= 1005; i++) { n     int t = i; n     int a[10]; n     int cnt1 = 1; n     while (t) { n       int tmp = t % 10; n       a[cnt1++] = tmp; n       t = t / 10; n     } n     for (cnt1--; cnt1; cnt1--) s[cnt++] = a[cnt1]; n   } n   int n; n   while (scanf( %d , &n) != EOF) printf( %d , s[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100001]; n int n, f = 1, x, ans; n int main() { n   cin >> a; n   n = strlen(a); n   for (int i = 0; i < n; i++) { n     if (a[i] <  0  || a[i] >  9 ) { n       ans += x * f; n       x = 0; n       if (a[i] ==  - ) f = -1; n       if (a[i] ==  + ) f = 1; n       x = x * 10 + a[i] -  0 ; n     } n     if (a[i] >=  0  && a[i] <=  9 ) { n       x *= 10; n       x += a[i] -  0 ; n     } n   } n   ans += x * f; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void solve() { n   int n, m; n   cin >> n >> m; n   map<vector<int>, int> tiles; n   for (int i = 0; i < n; i++) { n     vector<int> temp; n     for (int j = 0; j < 4; j++) { n       int a; n       cin >> a; n       temp.push_back(a); n     } n     tiles[temp]++; n   } n   if (m & 1) { n     cout <<  NO n ; n     return; n   } n   for (auto &i : tiles) { n     if (i.first[1] == i.first[2]) { n       cout <<  YES n ; n       return; n     } n   } n   cout <<  NO n ; n } n int main() { n   fast(); n   int t; n   cin >> t; n   while (t--) solve(); 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     long long ar[n]; n     for (int i = 0; i < n; i++) cin >> ar[i]; n     int maxi = -1000000001; n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       if (ar[i] > maxi) maxi = ar[i]; n       sum += ar[i]; n     } n     float ans = (sum - maxi) / (float)(n - 1) + maxi; n     printf( %.9f , ans); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int m = 1; n   while (m * (m - 1) <= 2 * n) m++; n   m = m - 1; n   cout << m <<   n ; n   int arr[m + 4][m + 3]; n   long long k = 1; n   for (int i = 0; i < m; i++) { n     long long count = 0; n     for (int j = i; j < m - 1; j++) { n       arr[i][j] = k + count++; n     } n     count = 0; n     for (long long j = i + 1; j < m; j++) { n       arr[j][i] = k + count++; n     } n     k = k + m - i - 1; n   } n   for (long long i = 0; i < m; i++) { n     for (long long j = 0; j < m - 1; j++) { n       cout << arr[i][j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename F, typename S> n ostream &operator<<(ostream &os, const pair<F, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   typename vector<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   typename set<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename F, typename S> n ostream &operator<<(ostream &os, const map<F, S> &v) { n   os <<  [ ; n   typename map<F, S>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n int dx[4] = {1, -1, 0, 0}; n int dy[4] = {0, 0, 1, -1}; n const long long N = 1e6 + 1, M = 1e5 + 5, mod = 1e9 + 7, MOD = 1e9 + 7, n                 inf = 5e18, MAX = 2e5; n string alphabet =  abcdefghijklmnopqrstuvwxyz , n        capital =  ABCDEFGHIJKLMNOPQRSTUVWXYZ , dig =  0123456789 ; n vector<bool> taken(N, false); n vector<int> ar; n void solve(int tt) { n   int n, i, h, j, k, m, a, b; n   cin >> n; n   int sm = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     ar.push_back(a); n     taken[a] = true; n     sm += a - 1; n   } n   vector<int> ans; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     int cur = ar[i]; n     if (!taken[N - cur]) n       ans.push_back(N - cur); n     else n       ++cnt; n     taken[N - cur] = true; n   } n   for (int i = 1; i < N && cnt > 0; i++) { n     if (taken[i]) continue; n     int now = N - i; n     if (!taken[now]) { n       taken[i] = taken[now] = true; n       ans.push_back(i); n       ans.push_back(N - i); n       cnt -= 2; n     } n   } n   cout << ans.size() <<   n ; n   for (auto z : ans) { n     sm -= (N - z - 1); n     cout << z <<    ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i; n   long long T = 1, K; n   for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); n        blockTime.second; n        fprintf(stderr,  %s: %d ms n ,  Exec Time :  , n                (int)chrono::duration_cast<chrono::milliseconds>( n                    chrono::high_resolution_clock::now() - blockTime.first) n                    .count()), n             fflush(stderr), blockTime.second = false) { n     long long x = 1; n     while (x <= T) { n       solve(x); n       x++; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #pragma region atcoder n /*#include <atcoder/all> n using namespace atcoder; n //using mint = modint998244353; n //using mint = modint1000000007;*/ n #pragma endregion n #pragma region macros n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vi = vector<int>; n using vs = vector<string>; n using vl = vector<ll>; n using vb = vector<bool>; n using vvi = vector<vector<int>>; n using vvl = vector<vector<ll>>; n #define rep(i, n) for(int i = 0; i < n; i++) n #define REP(i, a, b) for(int i = a; i < b; i++) n #define rrep(i, n) for(int i = n - 1; i >= 0; i--) n #define RREP(i, a, b) for(int i = b - 1; i >= a; i--) n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define sz(x) ((int)(x).size()) n #define pb push_back n #define lb lower_bound n #define ub upper_bound n #define fi first n #define se second n #pragma endregion n #pragma region debug for var, v, vv n #define debug(var)  do{std::cout << #var <<   :  ;view(var);}while(0) n template<typename T> void view(T e){std::cout << e << std::endl;} n template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e <<    ; } std::cout << std::endl;} n template<typename T> void view(const std::vector<std::vector<T> >& vv){cout << endl;int cnt = 0;for(const auto& v : vv){cout << cnt <<  th :  ; view(v); cnt++;} cout << endl;} n #pragma endregion n /*#pragma region int128 n std::ostream &operator<<(std::ostream &dest, __int128_t value) { n   std::ostream::sentry s(dest); n   if (s) { n     __uint128_t tmp = value < 0 ? -value : value; n     char buffer[128]; n     char *d = std::end(buffer); n     do { n       --d; n       *d =  0123456789 [tmp % 10]; n       tmp /= 10; n     } while (tmp != 0); n     if (value < 0) { n       --d; n       *d =  - ; n     } n     int len = std::end(buffer) - d; n     if (dest.rdbuf()->sputn(d, len) != len) { n       dest.setstate(std::ios_base::badbit); n     } n   } n   return dest; n } n #pragma endregion*/ n const ll mod = 998244353; n const int inf = 1001001001; n const ll INF = 1001001001001001001; n   n int dx[]={1,0,-1,0}; n int dy[]={0,1,0,-1}; n template<class T>bool chmax(T &a, const T b) { if (a<b) { a=b; return 1; } return 0; } n template<class T>bool chmin(T &a, const T b) { if (b<a) { a=b; return 1; } return 0; } n ll rudiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } //  20 / 3 == 7 n ll rddiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // -20 / 3 == -7 n ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;} n ll modpow(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;} n /*--------------------------------------------------------------------------------------------------------------------------------*/ n int main() { n     cin.tie(nullptr); n     ios::sync_with_stdio(false); n     //cout << fixed << setprecision(15); n     int t; cin >> t; n      while(t--){ n          int x, y; cin >> x >> y; n          if(x == y) cout << (x + y) << endl; n          else{ n              cout << min(x, y) * 2 + ((max(x, y) - min(x, y)) - 1) * 2 + 1  << endl; n          } n      } n } n /* n    * review you code when you get WA (typo? index?) n    * int overflow, array bounds n    * special cases (n=1?) n */


#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long n) { n   if (n <= 1) return false; n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n long long factorial(long long n) { n   if (n == 0) n     return 1; n   else n     return n * factorial(n - 1); n } n bool primeFactors(int n, int l, int r, bool notFound) { n   for (int i = l; i <= r; i++) { n     if (i == 1) i++; n     if (n % i == 0) { n       cout << i <<    ; n       notFound = false; n     } n     while (n % i == 0) { n       n = n / i; n     } n   } n   return notFound; n } n bool IsInBinarySequence(long long number) { n   long long numbertocheck = 1; n   do { n     if (number == numbertocheck) return true; n     numbertocheck *= 2; n   } while (numbertocheck <= number); n   return false; n } n long long nextPowerOf2(long long n) { n   if (n && !(n & (n - 1))) return n; n   long long cnt = 0; n   while (n != 0) { n     n >>= 1; n     cnt++; n   } n   long long x = 1; n   x = x << cnt; n   return x; n } n long long highestPowerof2(long long n) { n   long long res = 0; n   for (long long i = n; i >= 1; i--) { n     if ((i & (i - 1)) == 0) { n       res = i; n       break; n     } n   } n   return res; n } n long long fib(long long n) { n   if (n <= 1) return 1; n   return fib(n - 1) + fib(n - 2); n } n long long binets_formula(long long n) { n   double sqrt5 = sqrt(5); n   int F_n = (pow((1 + sqrt5), n) - pow((1 - sqrt5), n)) / (pow(2, n) * sqrt5); n   return F_n; n } n void first50fib() { n   long long arr[] = { n       1,          1,           2,          3,          5,          8, n       13,         21,          34,         55,         89,         144, n       233,        377,         610,        987,        1597,       2584, n       4181,       6765,        10946,      17711,      28657,      46368, n       75025,      121393,      196418,     317811,     514229,     832040, n       1346269,    2178309,     3524578,    5702887,    9227465,    14930352, n       24157817,   39088169,    63245986,   102334155,  165580141,  267914296, n       433494437,  701408733,   1134903170, 1836311903, 2971215073, 4807526976, n       7778742049, 12586269025, 20365011074}; n } n int binarySearch(int a[], int first, int last, int search_num) { n   int middle; n   if (last >= first) { n     middle = (first + last) / 2; n     if (a[middle] == search_num) { n       return middle + 1; n     } else if (a[middle] < search_num) { n       return binarySearch(a, middle + 1, last, search_num); n     } else { n       return binarySearch(a, first, middle - 1, search_num); n     } n   } n   return -1; n } n void factorize(long long n) { n   int count = 0; n   while (!(n % 2)) { n     n /= 2; n     count++; n   } n   if (count) cout << 2 <<  ^  << count << endl; n   for (long long i = 3; i <= sqrt(n); i += 2) { n     count = 0; n     while (n % i == 0) { n       count++; n       n = n / i; n     } n     if (count) cout << i <<  ^  << count << endl; n   } n   if (n > 2) cout << n <<  ^  << 1 << endl; n } n void primeFactors(long long num) { n   long long fac = 2; n   while (num > 1) { n     if (num % fac == 0) { n       cout <<  (  << fac <<  ^ ; n       num /= fac; n       long long pow = 1; n       while (num % fac == 0) { n         num /= fac; n         pow++; n       } n       cout << pow; n       if (num != 1) cout <<  )* ; n     } else { n       fac++; n     } n   } n } n bool onePrimeFactor(long long num) { n   long long fac = 2; n   int cnt = 0; n   while (num > 1) { n     if (num % fac == 0) { n       cnt++; n       while (num % fac == 0) num /= fac; n     } else { n       fac++; n     } n     if (cnt >= 2) { n       return false; n     } n   } n   return true; n } n bool isPerfect(long long d) { n   double temp = d; n   d = sqrt(d); n   temp = sqrt(temp); n   if (temp == d) return true; n   return false; n } n void sort(string s[], int n) { n   for (int i = 1; i < n; i++) { n     string temp = s[i]; n     int j = i - 1; n     while (j >= 0 && temp.length() < s[j].length()) { n       s[j + 1] = s[j]; n       j--; n     } n     s[j + 1] = temp; n   } n } n bool IsPowerOfTwo(long long x) { return (x & (x - 1)) == 0; } n int binarySearchCount(int arr[], int n, int key) { n   int left = 0, right = n; n   int mid; n   while (left < right) { n     mid = (right + left) >> 1; n     if (arr[mid] == key) { n       while (mid + 1 < n && arr[mid + 1] == key) mid++; n       break; n     } else if (arr[mid] > key) n       right = mid; n     else n       left = mid + 1; n   } n   while (mid > -1 && arr[mid] > key) mid--; n   return mid + 1; n } n bool is(int x, int y) { n   set<int> s; n   if (x < 10000) { n     s.insert(0); n   } n   if (y < 10000) { n     s.insert(0); n   } n   while (x > 0) { n     int d = x % 10; n     s.insert(d); n     x /= 10; n   } n   while (y > 0) { n     int d = y % 10; n     s.insert(d); n     y /= 10; n   } n   return s.size() == 10; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long i, n; n   set<long long> k; n   string s[] = { 47 ,  4477 ,  444777 ,  44447777 ,  4444477777 }; n   for (i = 0; i < 5; i++) { n     do { n       k.insert(stoll(s[i])); n     } while (next_permutation(s[i].begin(), s[i].end())); n   } n   cin >> n; n   cout << *k.lower_bound(n); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2510; n int sum[maxn][maxn]; n char s[maxn][maxn]; n int n, m, K; n long long ans; n int p1[10], p2[10]; n int cal(int x1, int y1, int x2, int y2) { n   return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]; n } n void div(int x1, int y1, int x2, int y2, bool flag) { n   if (x1 > x2 || y1 > y2) return; n   if (x1 == x2 && y1 == y2) { n     ans += (cal(x1, y1, x2, y2) == K); n     return; n   } n   if (flag) { n     int mid = (x1 + x2) >> 1; n     div(x1, y1, mid, y2, !flag); n     div(mid + 1, y1, x2, y2, !flag); n     for (int i = y1; i <= y2; i++) { n       for (int k = 0; k <= K; k++) { n         p1[k] = mid, p2[k] = mid + 1; n       } n       for (int j = y2; j >= i; j--) { n         for (int k = 0; k <= K; k++) { n           while (p1[k] >= x1 && cal(p1[k], i, mid, j) <= k) p1[k]--; n           while (p2[k] <= x2 && cal(mid + 1, i, p2[k], j) <= k) p2[k]++; n         } n         for (int k = 1; k < K; k++) { n           ans += (p1[k - 1] - p1[k]) * (p2[K - k] - p2[K - k - 1]); n         } n         if (K > 0) { n           ans += (mid - p1[0]) * (p2[K] - p2[K - 1]); n           ans += (p2[0] - mid - 1) * (p1[K - 1] - p1[K]); n         } else if (K == 0) { n           ans += (mid - p1[0]) * (p2[0] - mid - 1); n         } n       } n     } n   } else { n     int mid = (y1 + y2) >> 1; n     div(x1, y1, x2, mid, !flag); n     div(x1, mid + 1, x2, y2, !flag); n     for (int i = x1; i <= x2; i++) { n       for (int k = 0; k <= K; k++) { n         p1[k] = mid, p2[k] = mid + 1; n       } n       for (int j = x2; j >= i; j--) { n         for (int k = 0; k <= K; k++) { n           while (p1[k] >= y1 && cal(i, p1[k], j, mid) <= k) p1[k]--; n           while (p2[k] <= y2 && cal(i, mid + 1, j, p2[k]) <= k) p2[k]++; n         } n         for (int k = 1; k < K; k++) { n           ans += (p1[k - 1] - p1[k]) * (p2[K - k] - p2[K - k - 1]); n         } n         if (K > 0) { n           ans += (mid - p1[0]) * (p2[K] - p2[K - 1]); n           ans += (p2[0] - mid - 1) * (p1[K - 1] - p1[K]); n         } else if (K == 0) { n           ans += (mid - p1[0]) * (p2[0] - mid - 1); n         } n       } n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &K); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       sum[i][j] = n           sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (s[i][j] ==  1 ); n     } n   div(1, 1, n, m, 0); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int main() { n   cin >> n >> k; n   int ans = 1; n   while ((k & 1LL) == 0) { n     ++ans; n     k = k >> 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n long long q,n,c[400005],z,ans,mn=1e14,d,x; n map<long long,long long>las,ll; n vector<long long>v; n struct am{ n long long va,ind; n }; n am a[400005]; n bool cmp(am zz,am y){ n return zz.ind<y.ind; n } n set<long long>st; n int main(){ n ios::sync_with_stdio(NULL); n cin.tie(0); n cout.tie(0); n cin>>q; n  n while (q--){ n cin>>n; n st.clear(); n v.clear(); n las.clear(); n ll.clear(); n mn=1e14; n for (long long i=1;i<=n;i++){ n     cin>>x; n     las[x]++; n } n z=1; n for (auto it=las.begin();it!=las.end();it++){ n a[z].va=it->first; n a[z].ind=it->second; n z++; n } n  n sort (a+1,a+z,cmp); n for (long long i=z-1;i>=1;i--){ n         x=st.size(); n     mn=min(mn,n-(a[i].ind + a[i].ind*x)); n st.insert(a[i].va); n } n  n  n cout<<mn<<  n ; n } n  n  n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200009; n int n, a[N], cnt[N], mode, fsm[N], tmp[N], ans; n void larg(int x) { n   int sm = 0, res = 0; n   memset(fsm, 0, sizeof(fsm)); n   for (int i = 1; i <= n; i++) { n     if (a[i] == mode) n       sm++; n     else if (a[i] == x) n       sm--; n     if (sm && !fsm[sm]) fsm[sm] = i; n     res = max(res, i - fsm[sm]); n   } n   ans = max(ans, res); n } n void smal(int x) { n   int l = 1, p = 0, res = 0; n   memset(tmp, 0, sizeof(tmp)); n   for (int i = 1; i <= n; i++) { n     tmp[a[i]]++; n     if (tmp[a[i]] == x) p++; n     while (l <= i && tmp[a[i]] > x) { n       if (tmp[a[l]] == x) --p; n       tmp[a[l++]]--; n     } n     if (p >= 2) res = max(res, i - l + 1); n   } n   ans = max(ans, res); n } n int main() { n   scanf( %d , &n); n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (cnt[a[i]] > cnt[mode]) mode = a[i]; n   } n   for (int i = 1; i <= 200000; i++) { n     if (!cnt[i]) continue; n     if (i != mode && cnt[i] == cnt[mode]) { n       printf( %d n , n); n       return 0; n     } n     if (cnt[i] > sqrt(n)) larg(i); n   } n   for (int i = 1; i <= sqrt(n); i++) smal(i); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300; n map<int, int> m; n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, n; n   cin >> n; n   int p = 0; n   int k = 0; n   for (i = 1; i <= n; ++i) { n     cin >> j; n     if (m[j] == 0) n       ++k; n     else n       m[j]--; n     m[i]++; n   } n   cout << k << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N, R; n int result = INT_MAX; n char opt[] =  TB ; n int turns; n int sLast; n char rChar[1000001]; n int main() { n   int x, y; n   cin >> N >> R; n   if (N == 1 && R == 1) { n     cout << 0 << endl <<  T  << endl; n     return 0; n   } n   for (int i = 1; i < R; i++) { n     x = i; n     y = R; n     int *smaller, *larger; n     int mistakes = 0; n     int times = 0; n     int turn_over = 0; n     while (x > 0 && y > 0) { n       if (x > y) { n         larger = &x; n         smaller = &y; n       } else { n         larger = &y; n         smaller = &x; n       } n       mistakes += *larger / *smaller - 1; n       times += *larger / *smaller; n       turn_over++; n       *larger %= *smaller; n     } n     if (x + y == 1 && times == N) { n       mistakes--; n       turn_over++; n       if (mistakes < result) { n         result = mistakes; n         sLast = i; n         turns = turn_over; n       } n     } n   } n   if (result == INT_MAX) { n     cout <<  IMPOSSIBLE  << endl; n   } else { n     cout << result << endl; n     if (turns & 1) { n       x = R; n       y = sLast; n     } else { n       x = sLast; n       y = R; n     } n     for (int i = N - 1; i > 0; i--) { n       if (x > y) { n         rChar[i] =  T ; n         x -= y; n       } else { n         rChar[i] =  B ; n         y -= x; n       } n     } n     rChar[0] =  T ; n     cout << rChar << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long PRIME = 999727999; n long long PRIME2 = 999999937; n long long INF = LLONG_MAX / 4; n template <class F1, class F2> n ostream& operator<<(ostream& os, const pair<F1, F2>& p) { n   return os << p.first <<     << p.second; n } n template <class F1, class F2> n istream& operator>>(istream& is, pair<F1, F2>& p) { n   return is >> p.first >> p.second; n } n template <class F> n ostream& operator<<(ostream& os, const std::vector<F>& v) { n   for (int i = 0; i < (int)v.size(); i++) os << v[i] <<    ; n   return os; n } n template <class F> n istream& operator>>(istream& is, std::vector<F>& v) { n   for (int i = 0; i < (int)v.size(); i++) is >> v[i]; n   return is; n } n vector<string> split(const std::string& s, char delim) { n   vector<string> e; n   stringstream ss(s); n   string item; n   while (getline(ss, item, delim)) e.push_back(item); n   return e; n } n long long Pow(long long a, long long b, long long Mo) { n   long long ans = 1; n   for (; b; b >>= 1, a = a * a % Mo) n     if (b & 1) ans = ans * a % Mo; n   return ans; n } n long long _div(long long a, long long b) { n   return (a * Pow(b, MOD - 2, MOD)) % MOD; n } n long long nCr(long long n, long long r) { n   static long long MAXF = 1e6; n   static std::vector<long long> fact(MAXF, 1); n   for (int i = 1; i < MAXF; ++i) fact[i] = (fact[i - 1] * i) % MOD; n   MAXF = 0; n   return (fact[n] * Pow((fact[r] * fact[n - r]) % MOD, MOD - 2, MOD)) % MOD; n } n vector<int> Zfunc(string& s) { n   int n = s.length(); n   vector<int> z(n, 0); n   for (int i = 1, l = 0, r = 0; i < n; i++) { n     if (i <= r) z[i] = min(z[i - l], r - i + 1); n     while (i + z[i] < n && s[i + z[i]] == s[z[i]]) z[i]++; n     if (r < i + z[i] - 1) l = i, r = i + z[i] - 1; n   } n   return z; n } n string s; n int l; n std::vector<std::vector<long long> > DP; n long long getWaysAny(int c, int left) { n   if (c > left) return 0; n   return nCr(left, c); n } n long long getWays(int c, int takencare) { n   if (takencare == l) { n     if (c == 0) return 1; n     return 0; n   } n   long long& w = DP[c][takencare]; n   if (w != -1) return w; n   w = 0; n   if (s[takencare] ==  1 ) { n     if (c > 0) w = (w + getWays(c - 1, takencare + 1)) % MOD; n     w = (w + getWaysAny(c, l - (takencare + 1))) % MOD; n   } else { n     w = (w + getWays(c, takencare + 1)) % MOD; n   } n   return w; n } n long long solve() { n   int k; n   cin >> s >> k; n   l = s.length(); n   if (k == 0) { n     return 1; n   } n   DP.resize(1002, std::vector<long long>(1002, -1)); n   if (k == 1) { n     return (getWays(1, 0) + MOD - 1) % MOD; n   } n   long long w = 0; n   for (int i = 2; i <= 1000; ++i) { n     long long needk = 0; n     long long i2 = i; n     while (i2 > 1) { n       i2 = __builtin_popcount(i2); n       needk++; n     } n     if (needk == k - 1) { n       w = (w + getWays(i, 0)) % MOD; n     } n   } n   return w; n } n int main(int argc, char const* argv[]) { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout << solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100002; n int n, m, ok[N], a, b, c, l[N], nr[N]; n vector<pair<int, int>> G[N]; n int p[20][N]; n void parentDFS(int x, int par) { n   p[0][x] = par; n   l[x] = l[par] + 1; n   for (auto it : G[x]) n     if (it.first != par) parentDFS(it.first, x); n } n int lca(int x, int y) { n   if (l[x] < l[y]) swap(x, y); n   for (int i = 0; i < 18; i++) n     if (((l[x] - l[y]) & (1 << i))) x = p[i][x]; n   if (x == y) return x; n   for (int i = 17; i >= 0; i--) n     if (p[i][x] != p[i][y]) { n       x = p[i][x]; n       y = p[i][y]; n     } n   return p[0][x]; n } n pair<int, int> dist[N], ans[N]; n pair<int, int> maxx(pair<int, int> a, pair<int, int> b, int nod) { n   if (a.first < 0) return b; n   if (b.first < 0) return a; n   if (a.first == b.first) n     return {a.first, nod}; n   else n     return max(a, b); n } n void dfs1(int x, int p) { n   if (ok[x]) { n     dist[x] = {0, x}; n   } else n     dist[x] = {-1e9, -1}; n   for (auto it : G[x]) { n     int u = it.first, c = it.second; n     if (u == p) continue; n     dfs1(u, x); n     dist[x] = maxx(dist[x], {dist[u].first + c, dist[u].second}, x); n   } n } n void dfs2(int x, int par, pair<int, int> sus) { n   ans[x] = maxx(sus, dist[x], x); n   if (ok[x]) { n     int t = ans[x].second; n     int L = lca(x, t); n     nr[x]++; n     nr[t]++; n     nr[L]--; n     if (p[0][L] != 0) nr[p[0][L]]--; n   } n   pair<int, int> aux; n   if (ok[x]) n     aux = {0, x}; n   else n     aux = {-1e9, -1}; n   vector<pair<int, int>> child; n   vector<pair<int, int>> pr; n   int j = 0; n   for (auto it : G[x]) n     if (it.first != par) child.push_back(it); n   for (auto it : child) { n     int u = it.first, c = it.second; n     pr.push_back(aux); n     aux = maxx(aux, {dist[u].first + c, dist[u].second}, x); n   } n   for (int i = child.size() - 1; i >= 0; i--) { n     int u = child[i].first; n     int c = child[i].second; n     pair<int, int> aux1 = maxx(sus, pr[i], x); n     aux1.first += c; n     dfs2(u, x, aux1); n     sus = maxx(sus, {dist[u].first + c, dist[u].second}, x); n   } n } n void mars(int x, int p) { n   for (auto it : G[x]) { n     int u = it.first; n     if (u == p) continue; n     mars(u, x); n   } n   nr[p] += nr[x]; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a; n     ok[a] = 1; n   } n   for (int i = 1; i < n; i++) { n     cin >> a >> b >> c; n     G[a].push_back({b, c}); n     G[b].push_back({a, c}); n   } n   parentDFS(1, 0); n   for (int i = 1; i < 18; i++) n     for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]]; n   dfs1(1, 0); n   dfs2(1, 0, {-1e9, -1}); n   mars(1, 0); n   int r = 0, sol = 0; n   for (int i = 1; i <= n; i++) { n     if (!ok[i]) { n       if (sol < nr[i]) { n         sol = nr[i]; n         r = 1; n       } else if (sol == nr[i]) n         r++; n     } n   } n   cout << sol <<     << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string A[4][102]; n string M[202]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int p = 0; p < 4; p++) { n     for (int i = 0; i < n; i++) { n       cin >> A[p][i]; n     } n   } n   vector<int> T; n   for (int i = 0; i < 4; i++) T.push_back(i); n   int ans = int(1e9); n   do { n     for (int i = 0; i < n; i++) M[i] = A[T[0]][i] + A[T[1]][i]; n     for (int i = n; i < 2 * n; i++) M[i] = A[T[2]][i - n] + A[T[3]][i - n]; n     int op1 = 0, op2 = 0; n     for (int i = 0; i < 2 * n; i++) { n       int sq = (i % 2) ? 1 : 0; n       for (int j = 0; j < 2 * n; j++) { n         if (M[i][j] -  0  != sq) op1++; n         sq ^= 1; n       } n     } n     for (int i = 0; i < 2 * n; i++) { n       int sq = (i % 2) ? 0 : 1; n       for (int j = 0; j < 2 * n; j++) { n         if (M[i][j] -  0  != sq) op2++; n         sq ^= 1; n       } n     } n     ans = min(ans, min(op1, op2)); n   } while (next_permutation(T.begin(), T.end())); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int gi() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  ? ) return -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n bool eq(int a, int b) { n   if (b == -1) return 1; n   return a == b; n } n int n, D[310], L[310], Cnt[310], CNT[310], exist[310], maxdep[310]; n int p[310]; n int fir[610], dep[610], head[610], dis[10000010], nxt[10000010], w[10000010], n     id, cnt; n int S, T, numl[310], numr[310], chain[310][310], chlen[310]; n void link(int a, int b, int c) { n   nxt[++id] = fir[a], fir[a] = id, dis[id] = b, w[id] = c; n   nxt[++id] = fir[b], fir[b] = id, dis[id] = a, w[id] = 0; n } n int edger[310], edgelr[310][310], edgelc[310][310][310]; n bool BFS() { n   static int que[610], hd, tl; n   hd = tl = 0; n   que[tl++] = S; n   memset(dep, 0, sizeof dep); n   dep[S] = 1; n   while (hd ^ tl) { n     int x = que[hd++]; n     for (int i = fir[x]; i; i = nxt[i]) n       if (w[i] && !dep[dis[i]]) dep[dis[i]] = dep[x] + 1, que[tl++] = dis[i]; n   } n   return dep[T]; n } n int Dinic(int x, int maxflow) { n   if (x == T) return maxflow; n   int ret = 0; n   for (int& i = head[x]; i; i = nxt[i]) n     if (dep[dis[i]] == dep[x] + 1 && w[i]) { n       int d = Dinic(dis[i], std::min(w[i], maxflow - ret)); n       w[i] -= d, w[i ^ 1] += d; n       ret += d; n       if (ret == maxflow) break; n     } n   return ret; n } n std::vector<int> V[310][310]; n int Get(int i, int j) { n   int r = V[i][j].back(); n   V[i][j].pop_back(); n   return r; n } n int main() { n   n = gi(); n   int maxl = 0; n   for (int i = 1; i <= n; ++i) { n     D[i] = gi(), L[i] = gi(); n     if (~L[i]) { n       if (~D[i]) maxdep[L[i]] = std::max(maxdep[L[i]], D[i]); n       if (D[i] == 0) n         ++Cnt[L[i]]; n       else n         exist[L[i]] = 1; n     } else n       maxl = std::max(maxl, D[i]); n   } n   for (int i = 1; i <= n; ++i) { n     CNT[i] = (Cnt[i] + i - 1) / i; n     if (exist[i]) CNT[i] = std::max(CNT[i], 1); n   } n   bool FLG = 0; n   for (int o = 1; o <= n; ++o) { n     cnt = 2; n     S = 1, T = 2; n     memset(fir, 0, sizeof fir); n     id = 1; n     int tot = 0; n     bool flg = !CNT[o]; n     if (flg) n       if (!FLG) n         FLG = 1, CNT[o] = 1; n       else n         continue; n     for (int i = 1; i <= n; ++i) { n       int c = CNT[i] * i - Cnt[i]; n       if (c) n         edger[i] = id + 1, link(numr[i] = ++cnt, T, c), tot += c; n       else n         edger[i] = -1; n     } n     int sum = tot; n     for (int i = 1; i <= n; ++i) { n       chlen[i] = std::max(o == i ? maxl : 0, maxdep[i]); n       sum += chlen[i]; n     } n     if (sum > n) { n       if (flg) CNT[o] = 0; n       continue; n     } n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= chlen[i]; ++j) n         link(chain[i][j] = ++cnt, T, 1), ++tot; n     } n     for (int i = 1; i <= n; ++i) { n       memset(edgelr[i], -1, sizeof edgelr[i]); n       memset(edgelc[i], -1, sizeof edgelc[i]); n       if (D[i] == 0 && ~L[i]) continue; n       link(S, numl[i] = ++cnt, 1); n       if (D[i] <= 0) n         for (int j = 1; j <= n; ++j) n           if (numr[j] && eq(j, L[i])) n             edgelr[i][j] = id + 1, link(numl[i], numr[j], 1); n       if (D[i] != 0) { n         for (int j = 1; j <= n; ++j) n           if (eq(j, L[i])) n             for (int k = 1; k <= chlen[j]; ++k) n               if (k == D[i] || D[i] == -1) n                 edgelc[i][j][k] = id + 1, link(numl[i], chain[j][k], 1); n       } n     } n     while (BFS()) memcpy(head, fir, sizeof head), tot -= Dinic(S, 1e9); n     if (tot) { n       if (flg) CNT[o] = 0; n       continue; n     } n     for (int i = 1; i <= n; ++i) p[i] = -1; n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) n         if (~edgelr[i][j] && !w[edgelr[i][j]]) L[i] = j, D[i] = 0; n       for (int j = 1; j <= n; ++j) n         for (int k = 1; k <= chlen[j]; ++k) n           if (~edgelc[i][j][k] && !w[edgelc[i][j][k]]) L[i] = j, D[i] = k; n       if (~L[i] && ~D[i]) V[D[i]][L[i]].push_back(i); n     } n     for (int i = 1; i <= n; ++i) { n       if (!CNT[i]) continue; n       static int seq[82]; n       while (CNT[i]--) { n         for (int j = 1; j <= i; ++j) seq[j] = Get(0, i); n         seq[0] = seq[i]; n         for (int j = 1; j <= i; ++j) p[seq[j - 1]] = seq[j]; n       } n       for (int j = 1; j <= chlen[i]; ++j) seq[j] = Get(j, i); n       for (int j = 1; j <= chlen[i]; ++j) p[seq[j]] = seq[j - 1]; n       for (int j = 1; j <= n; ++j) n         if (p[j] == -1 && D[j] && eq(i, L[j]) && chlen[i] >= D[j] - 1) n           p[j] = seq[std::max(0, D[j] - 1)]; n     } n     for (int i = 1; i <= n; ++i) printf( %d  , ~p[i] ? p[i] : i); n     exit(0); n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s, t; n     cin >> s >> t; n     std::map<char, bool> ms, mt; n     for (int i = 0; i < s.size(); i++) { n       ms[s[i]] = true; n       mt[t[i]] = true; n     } n     for (char x =  a ; x <=  z ; x++) n       if (ms[x] && mt[x]) { n         cout <<  YES ; n         goto flag; n       } n     cout <<  NO ; n   flag: n     cout << endl; n     ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n #define endl   n  n #define pb push_back n const ll INF=1e18+10; n  n void solve() n { n     ll n,m; n     cin>>n>>m; n     string s; n     cin>>s; n     string p=s; n     while(m--) n     { n         s=p; n         for(ll i=0;i<n;i++) n         { n             if(p[i]== 1 ) continue; n             else{ n             if(i==0) n             { n                // cout<< YES <<endl; n                 if(s[i+1]== 1 ) p[i]= 1 ; n                 else p[i]= 0 ; n                 //cout<<p[i]<<endl; n             } n            else if(i==n-1) n             { n                 if(s[i-1]== 1 ) p[i]= 1 ; n                 else p[i]= 0 ; n             } n             else n             { n                 if((s[i-1]== 1 &&s[i+1]== 0 )||(s[i+1]== 1 &&s[i-1]== 0 )) p[i]= 1 ; n                 else p[i]= 0 ; n             } n         } n         } n         if(p==s) break; n     } n  n     cout<<p<<endl; n    // cout<<p[0]<<endl; n  n } n int main() n { n     ios::sync_with_stdio(0); n     cin.tie(0); n     ll t; n     t=1; n    cin>>t; n     while(t--) n     { n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 200005; n const int MD = 998244353; n void add(int& a, int b) { n   a += b; n   if (a >= MD) a -= MD; n } n void sub(int& a, int b) { n   a -= b; n   if (a < 0) a += MD; n } n int mod_sum(int a, int b) { n   a += b; n   if (a >= MD) a -= MD; n   return a; n } n int mod_mult(long long a, long long b) { return (a * b) % MD; } n int exp(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = mod_mult(res, a); n     a = mod_mult(a, a); n     b /= 2; n   } n   return res; n } n int inv(int a) { return exp(a, MD - 2); } n int p[MX]; n struct DATA { n   int s, m; n }; n DATA recalc(DATA L, DATA R) { n   DATA res; n   res.m = mod_mult(L.m, R.m); n   res.s = mod_sum(mod_mult(L.s, R.m), R.s); n   return res; n } n struct { n   int L, R; n   int pl, pr; n   DATA d; n } T[2 * MX]; n int tsz = 0; n int buildTree(int L, int R) { n   int cur = tsz++; n   T[cur].L = L; n   T[cur].R = R; n   if (L < R) { n     T[cur].pl = buildTree(L, (L + R) / 2); n     T[cur].pr = buildTree((L + R) / 2 + 1, R); n     T[cur].d = recalc(T[T[cur].pl].d, T[T[cur].pr].d); n   } else { n     T[cur].d.m = inv(p[L]); n     T[cur].d.s = inv(p[L]); n   } n   return cur; n } n DATA get(int L, int R, int cur) { n   if (L <= T[cur].L && R >= T[cur].R) return T[cur].d; n   DATA d; n   bool any = false; n   if (L <= T[T[cur].pl].R) { n     d = get(L, R, T[cur].pl); n     any = true; n   } n   if (R >= T[T[cur].pr].L) { n     DATA tmp = get(L, R, T[cur].pr); n     if (any) n       d = recalc(d, tmp); n     else n       d = tmp; n   } n   return d; n } n int main() { n   int n, q; n   ignore = scanf( %d %d , &n, &q); n   int inv_100 = inv(100); n   for (int i = 1; i <= n; i++) { n     ignore = scanf( %d , p + i); n     p[i] = mod_mult(p[i], inv_100); n   } n   int rt = buildTree(1, n); n   int res = T[rt].d.s; n   set<int> toggled; n   toggled.insert(1); n   toggled.insert(n + 1); n   while (q--) { n     int i; n     ignore = scanf( %d , &i); n     if (!toggled.count(i)) { n       int L = *prev(toggled.lower_bound(i)); n       int R = *toggled.lower_bound(i) - 1; n       DATA LL = get(L, i - 1, rt); n       DATA RR = get(i, R, rt); n       sub(res, recalc(LL, RR).s); n       add(res, LL.s); n       add(res, RR.s); n       toggled.insert(i); n     } else { n       toggled.erase(i); n       int L = *prev(toggled.lower_bound(i)); n       int R = *toggled.lower_bound(i) - 1; n       DATA LL = get(L, i - 1, rt); n       DATA RR = get(i, R, rt); n       add(res, recalc(LL, RR).s); n       sub(res, LL.s); n       sub(res, RR.s); n     } n     printf( %d n , res); n   } n } n