#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n } q[1010 * 1010 * 4]; n struct node { n   int next, now; n } d[1010 * 1010 * 2]; n int c_c[1010], c_r[1010]; n int dis[1010 * 1010]; n bool vis[1010 * 1010], use_r[1010], use_c[1010]; n int n, m; n void add_edge_col(int u, int v, int i) { n   d[i].next = c_c[u]; n   c_c[u] = i; n   d[i].now = v; n } n void add_edge_row(int u, int v, int i) { n   d[i].next = c_r[u]; n   c_r[u] = i; n   d[i].now = v; n } n void init() { n   cin >> n >> m; n   int l = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       char ch; n       cin >> ch; n       if (ch ==  # ) { n         add_edge_col(j, i, ++l); n         add_edge_row(i, j, ++l); n       } n     } n   add_edge_row(n, m + 1, ++l); n } n void solve() { n   int head = 1, tail = 1; n   memset(dis, 64, sizeof(dis)); n   q[1].x = 1; n   q[1].y = 0; n   dis[0] = 0; n   vis[0] = true; n   bool flag = 0; n   while (head <= tail) { n     int u = (q[head].x - 1) * m + q[head].y; n     if (dis[u] >= dis[n * m + 1]) { n       vis[u] = false; n       head++; n       continue; n     } n     if (!use_r[q[head].x]) n       for (int i = c_r[q[head].x]; i; i = d[i].next) { n         int v = (q[head].x - 1) * m + d[i].now; n         if (dis[v] > dis[u] + 1) { n           if (v == n * m + 1) flag = 1; n           if (!vis[v] && v != n * m + 1) { n             tail++; n             q[tail].x = q[head].x; n             q[tail].y = d[i].now; n             vis[v] = true; n           } n           dis[v] = dis[u] + 1; n         } n       } n     use_r[q[head].x] = true; n     if (!use_c[q[head].y]) n       for (int i = c_c[q[head].y]; i; i = d[i].next) { n         int v = (d[i].now - 1) * m + q[head].y; n         if (dis[v] > dis[u] + 1) { n           if (v == n * m + 1) flag = 1; n           if (!vis[v] && v != n * m + 1) { n             tail++; n             q[tail].x = d[i].now; n             q[tail].y = q[head].y; n             vis[v] = true; n           } n           dis[v] = dis[u] + 1; n         } n       } n     use_c[q[head].y] = true; n     vis[u] = false; n     head++; n   } n   if (flag) n     cout << dis[n * m + 1] - 1 << endl; n   else n     cout <<  -1  << endl; n } n int main() { n   ios::sync_with_stdio(false); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K, A, B, Q, x, y, t, ans; n int data[200010]; n class segleaf { n  public: n   int s[2]; n } seg[2000010]; n void change(int x, int l, int r, int t, int delta) { n   if (l == r) { n     data[t] += delta; n     seg[x].s[0] = min(data[t], B); n     seg[x].s[1] = min(data[t], A); n     return; n   } n   int mid = (l + r) >> 1; n   if (t <= mid) n     change(x << 1, l, mid, t, delta); n   else n     change((x << 1) + 1, mid + 1, r, t, delta); n   seg[x].s[0] = seg[x << 1].s[0] + seg[(x << 1) + 1].s[0]; n   seg[x].s[1] = seg[x << 1].s[1] + seg[(x << 1) + 1].s[1]; n } n int ask(int x, int l, int r, int ll, int rr, int tt) { n   if (l == ll && r == rr) return seg[x].s[tt]; n   int mid = (l + r) >> 1; n   if (rr <= mid) n     return ask(x << 1, l, mid, ll, rr, tt); n   else if (ll > mid) n     return ask((x << 1) + 1, mid + 1, r, ll, rr, tt); n   else n     return ask(x << 1, l, mid, ll, mid, tt) + n            ask((x << 1) + 1, mid + 1, r, mid + 1, rr, tt); n } n int main() { n   scanf( %d%d%d%d%d , &N, &K, &A, &B, &Q); n   for (int i = 1; i <= Q; i++) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d%d , &x, &y); n       change(1, 1, N, x, y); n     } else { n       scanf( %d , &x); n       ans = 0; n       if (x > 1) ans += ask(1, 1, N, 1, x - 1, 0); n       if (x + K <= N) ans += ask(1, 1, N, x + K, N, 1); n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, w, b; n   int a[100]; n   cin >> n >> w >> b; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int mini, maxi; n   if (w <= b) { n     mini = w; n     maxi = b; n   } else { n     mini = b; n     maxi = w; n   } n   int st = 0, endi = n - 1, cost = 0; n   while (st <= endi) { n     if (a[st] == a[endi] && a[st] == 2) { n       if (st != endi) n         cost += 2 * mini; n       else n         cost += mini; n     } n     if (a[st] != a[endi]) { n       if (a[st] != 2 && a[endi] != 2) { n         cout <<  -1  << endl; n         return 0; n       } n       if (a[st] == 0 || a[endi] == 0) n         cost += w; n       else if (a[st] == 1 || a[endi] == 1) n         cost += b; n     } n     st++; n     endi--; n   } n   cout << cost << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n long long fact(long long n) { n   long long ret = 1; n   for (int i = 1; i <= n; i++) ret *= i; n   return ret; n } n bool is_vowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y ) n     return 1; n   return 0; n } n long double getDistance(long double x1, long double y1, long double x2, n                         long double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n int n, b, q, upto[N], qu[N]; n vector<pair<int, int> > v; n int main() { n   scanf( %d%d%d , &n, &b, &q); n   for (int i = 0; i < q; i++) { n     scanf( %d%d , &upto[i], &qu[i]); n     v.push_back(pair<int, int>(upto[i], qu[i])); n   } n   sort(v.begin(), v.end()); n   v.push_back(pair<int, int>(b, n)); n   for (int i = 0; i < q; i++) { n     if (v[i].second > v[i + 1].second) { n       cout <<  unfair ; n       return 0; n     } n   } n   for (int i = 1; i < (1 << 5); i++) { n     int sum = 0; n     for (int j = 0; j < 5; j++) { n       if (i & (1 << j)) sum++; n     } n     sum *= (n / 5); n     int cur = 1, prev = 0; n     for (int j = 0; j < v.size(); j++) { n       if (v[j].first < cur) continue; n       int matching = 0; n       while (cur <= v[j].first) { n         int rem = cur % 5; n         if (i & (1 << rem)) { n           matching++; n         } n         cur++; n       } n       sum -= min(matching, v[j].second - prev); n       prev = v[j].second; n     } n     if (sum > 0) { n       printf( unfair ); n       return 0; n     } n   } n   printf( fair ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n const int mod = 1e9 + 7; n int dp[N][21][2]; n int n, f[21][2], two[21], three[2]; n long long a, b, c; n signed main() { n   scanf( %d , &n); n   for (int i = 0; i <= 20; ++i) { n     if (n < (int)(pow(2, i))) break; n     for (int j = 0; j <= 1; ++j) { n       if (n < (int)(pow(2, i) * pow(3, j))) break; n       f[i][j] = n / (int)(pow(2, i) * pow(3, j)); n     } n   } n   three[0] = two[0] = 1; n   three[1] = 3; n   for (int i = 1; i <= 20; ++i) { n     two[i] = two[i - 1] * 2; n   } n   memset(dp, 0, sizeof(dp)); n   a = log2(n); n   dp[1][a][0] = 1; n   if (n >= pow(2, a - 1) * 3) dp[1][a - 1][1] = 1; n   for (int i = 2; i <= n; ++i) { n     for (int j = 0; j <= 20; ++j) { n       if (n < two[j]) break; n       for (int k = 0; k <= 1; ++k) { n         if (n < two[j] * three[k]) break; n         b = dp[i - 1][j][k]; n         c = f[j][k] - (i - 1); n         a = (b * c) % mod; n         a %= mod; n         if (n >= two[j + 1] * three[k]) { n           b = dp[i - 1][j + 1][k]; n           c = f[j][k] - f[j + 1][k]; n           a += (b * c) % mod; n           a %= mod; n         } n         if (!k && n >= two[j] * three[k + 1]) { n           b = dp[i - 1][j][k + 1]; n           c = f[j][k] - f[j][k + 1]; n           a += (b * c) % mod; n           a %= mod; n         } n         dp[i][j][k] = a % mod; n       } n     } n   } n   printf( %d , dp[n][0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n const int mod = 95542721, magic = 48; n int inf = 1e9; n int tr[4 * N][magic], lz[4 * N]; n int a[N]; n void build(int cn, int b, int e) { n   if (b == e) { n     int x = a[b]; n     for (int i = 0; i < magic; i++) { n       tr[cn][i] = x; n       int y = 1ll * x * x % mod; n       y = 1ll * y * x % mod; n       x = y; n     } n     return; n   } n   int lc = cn << 1, rc = lc | 1, mid = b + e >> 1; n   build(lc, b, mid); n   build(rc, mid + 1, e); n   for (int i = 0; i < magic; i++) tr[cn][i] = (tr[lc][i] + tr[rc][i]) % mod; n } n void propagate(int u, int st, int en) { n   if (!lz[u]) return; n   if (st != en) { n     lz[2 * u] += lz[u]; n     lz[2 * u] %= magic; n     lz[2 * u + 1] += lz[u]; n     lz[2 * u + 1] %= magic; n   } n   int tmp[magic]; n   for (int i = 0; i < magic; i++) tmp[i] = tr[u][(i + lz[u]) % magic]; n   for (int i = 0; i < magic; i++) tr[u][i] = tmp[i]; n   lz[u] = 0; n } n void update(int u, int st, int en, int l, int r) { n   propagate(u, st, en); n   if (r < st || en < l) n     return; n   else if (l <= st && en <= r) { n     lz[u]++; n     lz[u] %= magic; n     propagate(u, st, en); n   } else { n     int mid = (st + en) / 2; n     update(2 * u, st, mid, l, r); n     update(2 * u + 1, mid + 1, en, l, r); n     for (int i = 0; i < magic; i++) n       tr[u][i] = (tr[u << 1][i] + tr[(u << 1) | 1][i]) % mod; n   } n } n int query(int u, int st, int en, int l, int r) { n   propagate(u, st, en); n   if (r < st || en < l) n     return 0; n   else if (l <= st && en <= r) n     return tr[u][0]; n   else { n     int mid = (st + en) / 2; n     return (query(2 * u, st, mid, l, r) + query(2 * u + 1, mid + 1, en, l, r)) % n            mod; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   build(1, 1, n); n   cin >> q; n   while (q--) { n     int t, l, r; n     cin >> t >> l >> r; n     if (t == 2) n       update(1, 1, n, l, r); n     else n       cout << query(1, 1, n, l, r) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll N = 1e6 + 10; n ll n, m, q, v; n ll kl, ke; n ll l[N]; n ll e[N]; n void umin(ll &a, ll b) { a = min(a, b); } n signed main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> m; n   cin >> kl >> ke; n   cin >> v; n   for (ll i = 0; i < kl; ++i) { n     cin >> l[i]; n   } n   for (ll i = 0; i < ke; ++i) { n     cin >> e[i]; n   } n   sort(l, l + kl); n   sort(e, e + ke); n   cin >> q; n   while (q--) { n     ll x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (y1 > y2) { n       swap(x1, x2); n       swap(y1, y2); n     } n     ll ans = (ll)1e18; n     auto it = lower_bound(l, l + kl, y1); n     if (it != l + kl) { n       umin(ans, abs(y1 - *it) + abs(y2 - *it) + abs(x1 - x2)); n     } n     it = upper_bound(l, l + kl, y2); n     if (it != l) { n       --it; n       umin(ans, abs(y1 - *it) + abs(y2 - *it) + abs(x1 - x2)); n     } n     it = lower_bound(e, e + ke, y1); n     if (it != e + ke) { n       umin(ans, abs(y1 - *it) + abs(y2 - *it) + (abs(x1 - x2) + v - 1) / v); n     } n     it = upper_bound(e, e + ke, y2); n     if (it != e) { n       --it; n       umin(ans, abs(y1 - *it) + abs(y2 - *it) + (abs(x1 - x2) + v - 1) / v); n     } n     if (x1 == x2) { n       ans = abs(y1 - y2); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int cg(long long int n) { return n ^ (n >> 1); } n long long int SUM(long long int a) { return (a * (a + 1) / 2); } n bool CAN(int x, int y, int n, int m) { n   if (x >= 0 && y >= 0 && x < n && y < m) { n     return true; n   } n   return false; n } n double her(double x1, double arayikhalatyan, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + n               (arayikhalatyan - y2) * (arayikhalatyan - y2)); n } n string strsum(string a, string b) { n   int p = 0; n   string c; n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   if (b.length() < a.length()) { n     for (int i = b.length(); i < a.length(); i++) { n       b +=  0 ; n     } n   } else { n     for (int i = a.length(); i < b.length(); i++) { n       a +=  0 ; n     } n   } n   a +=  0 , b +=  0 ; n   for (int i = 0; i < a.length(); i++) { n     c += (a[i] -  0  + b[i] -  0  + p) % 10 +  0 ; n     p = (a[i] + b[i] -  0  -  0  + p) / 10; n   } n   if (c[c.length() - 1] ==  0 ) c.erase(c.length() - 1, 1); n   reverse(c.begin(), c.end()); n   return c; n } n string strmin(string a, string b) { n   if (a.length() > b.length()) return b; n   if (b.length() > a.length()) return a; n   for (int i = 0; i < a.length(); i++) { n     if (a[i] > b[i]) return b; n     if (b[i] > a[i]) return a; n   } n   return a; n } n char vow[] = { a ,  e ,  i ,  o ,  u }; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n const int N = 1e6 + 30; n const long long int mod = 1e9 + 7; n int q; n int n, k; n vector<pair<long long int, int> > g[N]; n long long int dp[N][2]; n void dfs(int v, int par) { n   vector<int> sm; n   for (auto p : g[v]) { n     if (p.second == par) continue; n     dfs(p.second, v); n     dp[v][0] += dp[p.second][1]; n     dp[v][1] += dp[p.second][1]; n     sm.push_back(p.first + dp[p.second][0] - dp[p.second][1]); n   } n   sort(sm.begin(), sm.end(), greater<int>()); n   for (int i = 0; i < sm.size(); i++) { n     if (i == k) break; n     if (i == k - 1) { n       dp[v][1] += max(0, sm[i]); n     } else { n       dp[v][0] += max(0, sm[i]); n       dp[v][1] += max(0, sm[i]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> q; n   while (q--) { n     cin >> n >> k; n     for (int i = 0; i < n - 1; i++) { n       int a, b, c; n       cin >> a >> b >> c; n       g[a].push_back(make_pair(c, b)); n       g[b].push_back(make_pair(c, a)); n     } n     dfs(1, -1); n     cout << dp[1][1] << endl; n     for (int i = 1; i <= n; i++) { n       g[i].clear(); n       dp[i][0] = 0; n       dp[i][1] = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, fut, duim; n int main() { n   cin >> a; n   a += 1; n   fut = a / 36; n   duim = round((a - fut * 36) / 3); n   cout << fut <<     << duim << endl; n   fclose(stdin); n   fclose(stdout); n } n


#include <bits/stdc++.h> n struct itv { n   int s1; n   int e1; n   int s2; n   int e2; n   int gr; n   void up() { n     gr = (e2 - s2) + 1; n     if (e1) { n       gr += (e1 - s1) + 1; n     }; n   }; n }; n std::vector<std::pair<int, int> > aus; n void merge(int s1, int s2, int sz) { n   for (int i = 0; i < sz; i++) { n     aus.push_back({s1 + i, s2 + i}); n   }; n }; n itv tt[1000]; n int main(void) { n   int n; n   scanf( %d , &n); n   int akt = 1; n   for (int i = 1; i <= 20; i++) { n     int sz = (1 << i); n     if (sz > n) break; n     for (int j = 1; j <= n; j += sz) { n       int s1 = j; n       int s2 = j + (sz / 2); n       if (s1 + sz > n + 1) break; n       merge(s1, s2, sz / 2); n     }; n   }; n   akt = n; n   std::vector<int> pw; n   for (int i = 0; i <= 20; i++) { n     if (n & (1 << i)) { n       pw.push_back(i); n       tt[i].s2 = (akt - (1 << i)) + 1; n       tt[i].e2 = akt; n       tt[i].s1 = 0; n       tt[i].e1 = 0; n       tt[i].up(); n       akt -= (1 << i); n     }; n   }; n   if (pw.size() > 2) { n     int ak = 1; n     for (int i = 0; i < pw.size() - 2; i++) { n       int ap = pw[i]; n       int np = pw[i + 1]; n       int ss = tt[ap].e2 - tt[ap].s2 + 1; n       if (!i) { n         merge(tt[ap].s2, ak, ss); n         tt[ap].s1 = ak; n         ak += (ss); n         tt[ap].e1 = ak - 1; n       }; n       tt[ap].up(); n       while (tt[ap].gr < tt[np].gr) { n         merge(tt[ap].s1, ak, tt[ap].e1 - tt[ap].s1 + 1); n         ak += (tt[ap].e1 - tt[ap].s1 + 1); n         merge(tt[ap].s2, ak, ss); n         ak += (ss); n         tt[ap].e1 = ak - 1; n         tt[ap].up(); n       }; n       merge(tt[ap].s2, tt[np].s2, ss); n       merge(tt[ap].s1, tt[np].s2 + ss, tt[ap].e1 - tt[ap].s1 + 1); n       tt[np].e2 = tt[ap].e2; n       tt[np].s1 = tt[ap].s1; n       tt[np].e1 = tt[ap].e1; n       tt[np].up(); n     }; n   }; n   printf( %d n , aus.size()); n   for (auto i : aus) { n     printf( %d %d n , i.first, i.second); n   }; n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5; n int n, k; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k; n   int odd = 0, even = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     if (num % 2) n       odd++; n     else n       even++; n   } n   for (int i = 0; i < k; i++) { n     int num; n     cin >> num; n     if (num % 2 && even) { n       ans++; n       even--; n     } n     if (num % 2 == 0 && odd) { n       ans++; n       odd--; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long v, n, arr[50005], ans, o = 9999, minans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     v = max(arr[i], v); n     o = min(arr[i], o); n     ans += arr[i]; n   } n   minans = ans; n   for (int x = 1; x <= n; x++) { n     for (int i = 1; i <= v; i++) { n       if (arr[x] % i == 0) { n         long long nowc = arr[x] / i - arr[x] + o * (i - 1); n         minans = min(minans, ans + nowc); n       } n     } n   } n   cout << minans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, N, val[53][53], rk[53][53], fr[103]; n vector<int> peo[53]; n int pos[53]; n void go(bool flg) { n   queue<int> q; n   for (int i = 1; i <= N; ++i) { n     peo[i].clear(); n     for (int j = 1; j <= N; ++j) peo[i].push_back(j); n     sort(peo[i].begin(), peo[i].end(), n          [&](int p, int q) { return (val[i][p] < val[i][q]) ^ flg; }); n     q.push(i); n     pos[i] = fr[i] = fr[i + N] = 0; n   } n   for (int i = 1; i <= N; ++i) { n     vector<int> tmp; n     for (int j = 1; j <= N; ++j) tmp.push_back(j); n     sort(tmp.begin(), tmp.end(), n          [&](int p, int q) { return (val[p][i] > val[q][i]) ^ flg; }); n     for (int j = 0; j < tmp.size(); ++j) rk[i][tmp[j]] = j + 1; n   } n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     while (!fr[t]) { n       int tar = peo[t][pos[t]++]; n       if (!fr[tar + N] || rk[tar][fr[tar + N]] > rk[tar][t]) { n         fr[fr[tar + N]] = 0; n         if (fr[tar + N]) q.push(fr[tar + N]); n         fr[tar + N] = t; n         fr[t] = tar + N; n       } n     } n   } n } n int main() { n   for (cin >> T; T; --T) { n     cin >> N; n     for (int i = 1; i <= N; ++i) n       for (int j = 1; j <= N; ++j) cin >> val[i][j]; n     cout <<  B  << endl; n     char c; n     cin >> c; n     if (c ==  D ) n       for (int i = 1; i <= N; ++i) n         for (int j = 1; j <= N; ++j) val[i][j] = -val[i][j]; n     int p; n     cin >> p; n     go(p > N); n     while (p != -1) { n       cout << fr[p] << endl; n       cin >> p; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n; n   scanf( %ld n , &n); n   std::vector<long> array(n, 0); n   for (int p = 0; p < n; p++) { n     scanf( %ld  , &array[p]); n   } n   bool down = 0, possible = 1; n   long moves(0); n   for (int p = 1; p < n; p++) { n     if (array[p - 1] <= array[p]) { n       continue; n     } n     if (down) { n       possible = 0; n       break; n     } n     down = 1; n     moves = n - p; n     if (array[p] > array[0]) { n       possible = 0; n       break; n     } n   } n   if (down && array[n - 1] > array[0]) { n     possible = 0; n   } n   if (!possible) { n     puts( -1 ); n   } else { n     printf( %ld n , moves); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, l[3001][3001], dp[3001][3001], mod = 1000000007; n char cc; n long long find(long long aa, long long bb, long long qq, long long ww) { n   if (l[aa][bb] == 2) return 0; n   memset(dp, 0, sizeof(dp)); n   dp[aa][bb] = 1; n   for (int i = aa; i <= qq; i++) { n     for (int j = bb; j <= ww; j++) { n       if (i == aa && j == bb) continue; n       if (l[i][j] == 2) continue; n       dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; n       dp[i][j] %= 1000000007; n     } n   } n   return dp[qq][ww] % 1000000007; n } n int main() { n   scanf( %lld%lld , &a, &b); n   getchar(); n   for (int i = 1; i <= a; i++) { n     for (int j = 1; j <= b; j++) { n       l[i][j] = (getchar() ==  . ) ? 1 : 2; n     } n     getchar(); n   } n   printf( %lld , ((find(1, 2, a - 1, b) * find(2, 1, a, b - 1) % 1000000007 - n                    find(1, 2, a, b - 1) * find(2, 1, a - 1, b)) % n                       1000000007 + n                   1000000007) % n                      1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100000], p[100000], b[100000]; n vector<int> v[100000]; n set<int> s; n int main() { n   int n, cnt = -1; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     p[i] = arr[i]; n   } n   sort(p, p + n); n   int i, j = 0; n   while (s.size() != n) { n     i = j; n     if (b[i] != -1) cnt++; n     while (b[i] != 1) { n       b[i] = 1; n       v[cnt].push_back(i); n       s.insert(i); n       i = lower_bound(p, p + n, arr[i]) - p; n     } n     j++; n   } n   int ans = 0; n   for (int i = 0; i <= cnt; i++) { n     if (v[i].size() > 0) ans++; n   } n   cout << ans << endl; n   for (int i = 0; i <= cnt; i++) { n     if (v[i].size() > 0) { n       cout << v[i].size() <<    ; n       for (int j = 0; j < v[i].size(); j++) cout << v[i][j] + 1 <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int r, s, p; n double dp[N][N][N], ans[4]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> r >> s >> p; n   dp[r][s][p] = 1.0; n   for (int i = r; i >= 0; i--) { n     for (int j = s; j >= 0; j--) { n       for (int k = p; k >= 0; k--) { n         double cur = i * j + j * k + k * i; n         if (i && j) dp[i][j - 1][k] += (1.0 * i * j / cur) * dp[i][j][k]; n         if (j && k) dp[i][j][k - 1] += (1.0 * j * k / cur) * dp[i][j][k]; n         if (k && i) dp[i - 1][j][k] += (1.0 * k * i / cur) * dp[i][j][k]; n       } n     } n   } n   for (int i = 1; i <= r; i++) ans[1] += dp[i][0][0]; n   for (int j = 1; j <= s; j++) ans[2] += dp[0][j][0]; n   for (int k = 1; k <= p; k++) ans[3] += dp[0][0][k]; n   cout << fixed << setprecision(12) << ans[1] <<     << ans[2] <<     << ans[3] n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long getLCM(int a, int b) { n   int copyA = a, copyB = b; n   int r = a % b; n   while (r) { n     a = b; n     b = r; n     r = a % b; n   } n   int gcd = b; n   return 1LL * copyA * copyB / gcd; n } n int main() { n   int n, a, b, p, q; n   cin >> n >> a >> b >> p >> q; n   int divA = n / a; n   int divB = n / b; n   int divAB = n / getLCM(a, b); n   divA -= divAB; n   divB -= divAB; n   cout << 1LL * divA * p + 1LL * divB * q + 1LL * divAB * max(p, q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1005; n bool vis[mx][mx][2]; n double dp[mx][mx][2]; n double func(int white, int black, int turn) { n   if (white <= 0) { n     return 0.0; n   } n   if (black <= 0) { n     if (!turn) return 1.0; n     return 0.0; n   } n   if (vis[white][black][turn]) return dp[white][black][turn]; n   double up, down, p, q, r, ans, x, y; n   if (!turn) { n     up = white; n     down = (double)white + black; n     ans = up / down; n     p = (1.0 - ans); n     ans += p * func(white, black - 1, 1); n   } else { n     up = black; n     down = white + black; n     p = up / down; n     black--; n     q = 0.0; n     x = 0.0; n     if (black) { n       up = black; n       down = black + white; n       q = up / down; n       x = q * func(white, black - 1, 0); n     } n     r = (1.0 - q); n     y = r * func(white - 1, black, 0); n     ans = p * (x + y); n   } n   vis[white][black][turn] = 1; n   return dp[white][black][turn] = ans; n } n int main() { n   int b, w; n   scanf( %d %d , &w, &b); n   memset(vis, 0, sizeof(vis)); n   double ans = func(w, b, 0); n   printf( %.15lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = ~(((long long)0x1) << 63) / 2; n const int INF = 0X3F3F3F3F; n const double eps = 1e-7; n int num[10]; n int ok[10]; n int main() { n   int n, k, i, j, v; n   string s, ans, now, ss; n   cin >> n >> v; n   cin >> s; n   for (i = 0; i < n; i++) num[s[i] -  0 ]++; n   int minvalue = INF; n   for (i = 0; i < 10; i++) { n     memset(ok, 0, sizeof(ok)); n     int now = v, last = -1; n     now -= num[i]; n     ok[i] = 1; n     last = i; n     for (j = 1; j < 10 && now > 0; j++) { n       if (i - j >= 0) { n         now -= num[i - j]; n         ok[i - j] = 1; n       } n       if (now <= 0) { n         last = i - j; n         now += num[last]; n         break; n       } n       if (i + j < 10) { n         now -= num[i + j]; n         ok[i + j] = 1; n       } n       if (now <= 0) { n         last = i + j; n         now += num[last]; n         break; n       } n     } n     if (last == i) now += num[i]; n     int value = 0; n     ans =   ; n     for (j = 0; j < n; j++) n       if (ok[s[j] -  0 ] && s[j] -  0  != last) n         ans += i +  0 , value += abs(s[j] -  0  - i); n       else n         ans += s[j]; n     if (last >= i) { n       for (j = 0; j < n && now > 0; j++) n         if (ans[j] == last +  0 ) n           ans[j] = i +  0 , value += abs(ans[j] -  0  - last), now--; n     } else { n       for (j = n; j >= 0 && now > 0; j--) n         if (ans[j] == last +  0 ) n           ans[j] = i +  0 , value += abs(ans[j] -  0  - last), now--; n     } n     if (value < minvalue) { n       minvalue = value; n       ss = ans; n     } else if (value == minvalue && ans < ss) n       ss = ans; n     memset(ok, 0, sizeof(ok)); n     now = v, last = -1; n     now -= num[i]; n     ok[i] = 1; n     last = i; n     for (j = 1; j < 10 && now > 0; j++) { n       if (i + j < 10) { n         now -= num[i + j]; n         ok[i + j] = 1; n       } n       if (now <= 0) { n         last = i + j; n         now += num[last]; n         break; n       } n       if (i - j >= 0) { n         now -= num[i - j]; n         ok[i - j] = 1; n       } n       if (now <= 0) { n         last = i - j; n         now += num[last]; n         break; n       } n     } n     if (last == i) now += num[i]; n     value = 0; n     ans =   ; n     for (j = 0; j < n; j++) n       if (ok[s[j] -  0 ] && s[j] -  0  != last) n         ans += i +  0 , value += abs(s[j] -  0  - i); n       else n         ans += s[j]; n     if (last >= i) { n       for (j = 0; j < n && now > 0; j++) n         if (ans[j] == last +  0 ) n           ans[j] = i +  0 , value += abs(ans[j] -  0  - last), now--; n     } else { n       for (j = n; j >= 0 && now > 0; j--) n         if (ans[j] == last +  0 ) n           ans[j] = i +  0 , value += abs(ans[j] -  0  - last), now--; n     } n     if (value < minvalue) { n       minvalue = value; n       ss = ans; n     } else if (value == minvalue && ans < ss) n       ss = ans; n   } n   cout << minvalue << endl; n   cout << ss << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[100010], l[100010], r[100010], LS[100010], T; n int G[100010][10]; n struct MAT { n   int data[10][10]; n   MAT() { memset(data, 0, sizeof(data)); } n   MAT operator*(const MAT& ma) const { n     MAT ret; n     for (int i = 1; i <= 3; ++i) n       for (int j = 1; j <= 3; ++j) n         for (int k = 1; k <= 3; ++k) n           ret.data[i][j] = n               (ret.data[i][j] + 1ll * data[i][k] * ma.data[k][j]) % 1000000007; n     return ret; n   } n }; n MAT A; n int main() { n   scanf( %lld%lld , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%lld%lld , &a[i], &l[i], &r[i]); n     LS[++T] = l[i], LS[++T] = r[i] + 1; n   } n   LS[++T] = 2, LS[++T] = m + 1; n   sort(LS + 1, LS + T + 1); n   T = unique(LS + 1, LS + T + 1) - LS - 1; n   for (int i = 1; i <= n; ++i) { n     l[i] = lower_bound(LS + 1, LS + T + 1, l[i]) - LS; n     r[i] = lower_bound(LS + 1, LS + T + 1, r[i] + 1) - LS; n     G[l[i]][a[i]]++, G[r[i]][a[i]]--; n   } n   for (int i = 1; i <= 3; ++i) A.data[i][i] = 1; n   for (int i = 1; i < T; ++i) { n     for (int j = 1; j <= 3; ++j) G[0][j] += G[i][j]; n     MAT I; n     for (int j = 1; j <= 3; ++j) n       for (int k = max(1, j - 1); k <= min(3, j + 1); ++k) n         if (!G[0][k]) I.data[j][k] = 1; n     long long len = LS[i + 1] - LS[i]; n     for (; len; len >>= 1, I = I * I) n       if (len & 1) A = A * I; n   } n   printf( %d , A.data[2][2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mx = 10000007; n using pii = pair<long long, long long>; n const long long mod = 1e9 + 7; n const double pi = acos(-1); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<pii> v; n     vector<long long> ev, od; n     set<long long> s; n     for (long long i = 0; i < n; i++) { n       s.insert(i + 1); n       if ((i + 1) % 2 == 0) n         ev.push_back(i + 1); n       else n         od.push_back(i + 1); n     } n     if (n == 2) { n       cout << 2 << endl; n       cout << 1 <<     << 2 << endl; n       continue; n     } n     sort(ev.begin(), ev.end()); n     sort(od.begin(), od.end()); n     reverse(ev.begin(), ev.end()); n     reverse(od.begin(), od.end()); n     long long d; n     if (n % 2 == 0) { n       d = ev[0]; n       s.erase(d); n       long long i = 1, j = 0; n       while (1) { n         if (d % 2 == 0) { n           long long a = d; n           long long b = ev[i]; n           s.erase(b); n           v.push_back({a, b}); n           d = (a + b) / 2; n           if (s.size() == 0) break; n           i++; n           if (i > ev.size()) break; n         } else { n           long long a = d; n           long long b = od[j]; n           s.erase(b); n           v.push_back({a, b}); n           j++; n           d = (a + b) / 2; n           if (s.size() == 0) break; n           if (j > od.size()) break; n         } n         if (s.size() == 0) break; n       } n     } else if (n % 2 == 1) { n       d = od[0]; n       s.erase(d); n       long long i = 1, j = 0; n       while (1) { n         if (d % 2 == 1) { n           long long a = d; n           long long b = od[i]; n           s.erase(b); n           v.push_back({a, b}); n           d = (a + b) / 2; n           if (s.size() == 0) break; n           i++; n           if (i > od.size()) break; n         } else { n           long long a = d; n           long long b = ev[j]; n           s.erase(b); n           v.push_back({a, b}); n           j++; n           d = (a + b) / 2; n           if (s.size() == 0) break; n           if (j > ev.size()) break; n         } n         if (s.size() == 0) break; n       } n     } n     cout << d << endl; n     for (long long i = 0; i < v.size(); i++) { n       cout << v[i].first <<     << v[i].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   vector<double> vv; n   vv.push_back(0); n   for (double i = 1; i <= 400; i++) { n     vv.push_back(180 / i); n   } n   cin >> t; n   while (t--) { n     double n; n     cin >> n; n     bool is = 0; n     for (int i = 3; i < vv.size(); i++) { n       double x = 0; n       bool don = 0; n       for (int j = 0; j < i - 2; j++) { n         x += vv[i]; n         if (x == n) { n           cout << i << endl; n           don = 1; n           break; n         } n       } n       if (don) { n         is = 1; n         break; n       } n     } n     if (!is) cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20, K = (1 << 16) + 5, M = 2005, mod = 998244353; n int k, sum, a[N], b[N]; n bitset<M> f[K]; n inline void dfs(register int s, register int now) { n   if (!s) return; n   register int i; n   for (; now * k <= sum && f[s][now * k]; now *= k) n     for (i = s; i; i ^= i & -i) ++b[__builtin_ctz(i & -i)]; n   for (i = s; i; i ^= i & -i) n     if (now >= a[__builtin_ctz(i & -i)] && n         f[s ^ i & -i][now - a[__builtin_ctz(i & -i)]]) { n       dfs(s ^ i & -i, now - a[__builtin_ctz(i & -i)]); n       return; n     } n } n priority_queue<pair<int, int> > q; n int main() { n   register int n, i, s, t; n   scanf( %d%d , &n, &k); n   for (i = 0; i ^ n; ++i) scanf( %d , &a[i]), sum += a[i]; n   t = (1 << n) - 1; n   f[0][0] = 1; n   for (s = 1; s <= t; ++s) { n     for (i = s; i; i ^= i & -i) n       f[s] |= f[s ^ i & -i] << a[__builtin_ctz(i & -i)]; n     for (i = sum / k; i >= 1; --i) n       if (f[s][i * k]) f[s][i] = 1; n   } n   if (!f[t][1]) n     puts( NO ); n   else { n     puts( YES ); n     dfs(t, 1); n     register pair<int, int> x1, x2; n     for (i = 0; i ^ n; ++i) q.push(make_pair(b[i], a[i])); n     while (q.size() > 1) { n       x1 = q.top(); n       q.pop(); n       x2 = q.top(); n       q.pop(); n       printf( %d %d n , x1.second, x2.second); n       x1.second += x2.second; n       while (x1.second % k == 0) x1.second /= k, --x1.first; n       q.push(x1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int B[1 << 17]; n int R[1 << 17]; n vector<int> Add(const vector<int>& a, const vector<int>& b) { n   vector<int> res = a; n   int i; n   for (i = (0); i < (int((b).size())); ++i) res.push_back(b[i]); n   return res; n } n int main() { n   int n; n   scanf( %d , &n); n   int i, j; n   for (i = (0); i < (n); ++i) { n     int a; n     scanf( %d , &a); n     --a; n     R[a] = i; n   } n   memset(B, 0, sizeof(B)); n   vector<vector<int> > ret; n   vector<vector<int> > ret2; n   vector<vector<int> > A[8]; n   for (i = (0); i < (n); ++i) n     if (B[i] == 0) { n       int pos = i; n       vector<int> v; n       while (B[pos] == 0) { n         B[pos] = 1; n         v.push_back(pos); n         pos = R[pos]; n       } n       pos = 0; n       int len = int((v).size()); n       while (len - pos > 4) { n         vector<int> r; n         for (j = (0); j < (5); ++j) r.push_back(v[pos + j]); n         ret.push_back(r); n         rotate(r.begin(), r.end() - 1, r.end()); n         ret2.push_back(r); n         pos += 4; n       } n       if (len - pos > 1) { n         vector<int> w; n         for (j = (pos); j < (len); ++j) w.push_back(v[j]); n         A[len - pos].push_back(w); n       } n     } n   int L[8]; n   int C[8]; n   memset(L, 0, sizeof(L)); n   memset(C, 0, sizeof(C)); n   for (i = (0); i < (8); ++i) L[i] = int((A[i]).size()); n   for (i = (0); i < (L[4]); ++i) { n     vector<int> r = A[4][i]; n     ret.push_back(r); n     rotate(r.begin(), r.end() - 1, r.end()); n     ret2.push_back(r); n   } n   while (C[2] + C[3] != L[2] + L[3]) { n     if (C[2] != L[2]) { n       vector<int> r = A[2][C[2]]; n       ++C[2]; n       vector<int> r2; n       if (C[3] != L[3]) { n         r2 = A[3][C[3]]; n         ++C[3]; n       } else if (C[2] != L[2]) { n         r2 = A[2][C[2]]; n         ++C[2]; n       } n       ret.push_back(Add(r, r2)); n       rotate(r.begin(), r.end() - 1, r.end()); n       if (!r2.empty()) rotate(r2.begin(), r2.end() - 1, r2.end()); n       ret2.push_back(Add(r, r2)); n       continue; n     } n     vector<int> r = A[3][C[3]]; n     ++C[3]; n     vector<int> r2; n     if (C[3] != L[3]) { n       r2 = A[3][C[3]]; n       r2.erase(r2.end() - 1); n       ++C[3]; n     } n     ret.push_back(Add(r, r2)); n     rotate(r.begin(), r.end() - 1, r.end()); n     if (!r2.empty()) rotate(r2.begin(), r2.end() - 1, r2.end()); n     ret2.push_back(Add(r, r2)); n     if (!r2.empty()) { n       vector<int> t; n       t.push_back(A[3][C[3] - 1][1]); n       t.push_back(A[3][C[3] - 1][2]); n       A[2].push_back(t); n       ++L[2]; n     } n   } n   printf( %d n , int((ret).size())); n   for (i = (0); i < (int((ret).size())); ++i) { n     printf( %d n , int((ret[i]).size())); n     for (j = (0); j < (int((ret[i]).size())); ++j) n       printf( %d%c , ret[i][j] + 1, j == int((ret[i]).size()) - 1 ?   n  :    ); n     for (j = (0); j < (int((ret2[i]).size())); ++j) n       printf( %d%c , ret2[i][j] + 1, n              j == int((ret2[i]).size()) - 1 ?   n  :    ); n   } n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 300005; n const long long INF = 0x3f3f3f3f3f3f3f3f; n long long n; n long long Read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x * 10) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n void Put1(long long x) { n   if (x > 9) Put1(x / 10); n   putchar(x % 10 ^ 48); n } n void Put(long long x, char c = -1) { n   if (x < 0) putchar( - ), x = -x; n   Put1(x); n   if (c >= 0) putchar(c); n } n template <typename T> n T Max(T x, T y) { n   return x > y ? x : y; n } n template <typename T> n T Min(T x, T y) { n   return x < y ? x : y; n } n template <typename T> n T Abs(T x) { n   return x < 0 ? -x : x; n } n long long a, d[MAXN], c[MAXN], f[MAXN]; n struct node { n   long long d, maxf, maxans; n   node() {} n   node(long long d1, long long maxf1, long long maxans1) { n     d = d1; n     maxf = maxf1; n     maxans = maxans1; n   } n } sta[MAXN]; n long long pow2(long long x) { return x * x; } n void solve() { n   long long ans = Max(0ll, a - (c[n] - c[n - 1])), max_pref, ed = 0; n   sta[0].maxans = -INF; n   sta[++ed] = node(0, f[n], f[n]); n   for (long long l = n - 1; l >= 1; --l) { n     max_pref = -INF; n     while (ed > 0 && sta[ed].d <= d[l + 1] - d[l]) { n       max_pref = Max(max_pref, sta[ed].maxf); n       sta[ed--] = node(0, 0, 0); n     } n     if (max_pref != -INF) n       sta[++ed] = n           node(d[l + 1] - d[l], max_pref, n                Max(max_pref - pow2(d[l + 1] - d[l]), sta[ed - 1].maxans)); n     sta[++ed] = node(0, f[l], Max(f[l], sta[ed - 1].maxans)); n     ans = Max(ans, sta[ed].maxans - f[l - 1]); n   } n   Put(ans,   n ); n } n signed main() { n   n = Read(), a = Read(); n   for (long long i = 1; i <= n; ++i) n     d[i] = Read(), c[i] = c[i - 1] + Read(), f[i] = a * i - c[i]; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_LN = 20; n const int D = 10; n const long long INF = 1LL << 60; n const double EPS = 1e-9; n const double PI = 3.14159265358979; n long long n; n long long dp[MAX_LN][1 << D][D][D]; n long long pat[2][D]; n void init(int S, int v0) { n   long long sdp[20]; n   fill(sdp, sdp + 20, INF); n   sdp[10 + v0] = 0; n   sdp[10] = min(sdp[10], 1LL); n   for (int i = (0); i < (D); i++) n     if (S & (1 << i)) { n       for (int j = (10 - i); j < (20 - i); j++) { n         sdp[j] = min(sdp[j], sdp[j + i] + 1); n       } n     } n   for (int i = (0); i < (D); i++) dp[0][S][v0][i] = sdp[i]; n } n void calc(int d, int S) { n   long long sdp[2][D]; n   long long *curr = sdp[0], *next = sdp[1]; n   for (int s = (0); s < (D); s++) { n     for (int i = (0); i < (2); i++) n       for (int j = (0); j < (D); j++) sdp[i][j] = INF; n     curr[s] = 0; n     if (d > 0) { n       { n         for (int j = (0); j < (D); j++) next[j] = INF; n         for (int j = (0); j < (D); j++) n           for (int k = (0); k < (D); k++) { n             next[k] = min(next[k], curr[j] + dp[d][S | (1 << 9)][j][k]); n           } n         swap(curr, next); n       } n       for (int i = 9; i >= 1; i--) { n         { n           for (int j = (0); j < (D); j++) next[j] = INF; n           for (int j = (0); j < (D); j++) n             for (int k = (0); k < (D); k++) { n               next[k] = min(next[k], curr[j] + dp[0][S | (1 << i)][j][k]); n             } n           swap(curr, next); n         } n         { n           for (int j = (0); j < (D); j++) next[j] = INF; n           for (int j = (0); j < (D); j++) n             for (int k = (0); k < (D); k++) { n               next[k] = min(next[k], curr[j] + dp[d][S | (1 << (i - 1))][j][k]); n             } n           swap(curr, next); n         } n       } n     } else { n       for (int i = 9; i >= 1; i--) { n         for (int j = (0); j < (D); j++) next[j] = INF; n         for (int j = (0); j < (D); j++) n           for (int k = (0); k < (D); k++) { n             next[k] = min(next[k], curr[j] + dp[0][S | (1 << i)][j][k]); n           } n         swap(curr, next); n       } n     } n     for (int i = (0); i < (D); i++) dp[d + 1][S][s][i] = curr[i]; n   } n } n int main() { n   cin >> n; n   if (n == 0) { n     cout << 0 << endl; n     return 0; n   } n   int m = 0; n   long long t = n; n   while (t > 0) { n     t /= 10; n     m++; n   } n   for (int i = (0); i < (m - 1); i++) n     for (int j = (0); j < (D); j++) n       for (int k = (0); k < (D); k++) n         for (int l = (0); l < (D + 1); l++) dp[i][j][k][l] = INF; n   for (int i = (0); i < (1 << D); i++) n     for (int j = (0); j < (D); j++) { n       init(i, j); n     } n   for (int i = (0); i < (m - 2); i++) { n     for (int S = (0); S < (1 << D); S++) { n       calc(i, S); n     } n   } n   long long *curr = pat[0], *next = pat[1]; n   for (int i = (0); i < (D); i++) { n     curr[i] = (n % 10 == i) ? 0 : INF; n   } n   for (int i = (0); i < (m - 1); i++) { n     n /= 10; n     int S = 0; n     int mod = n % 10; n     long long t = n / 10; n     while (t > 0) { n       S |= (1 << (t % 10)); n       t /= 10; n     } n     if (i > 0) { n       for (int j = mod; j >= 1; j--) { n         { n           for (int k = (0); k < (D); k++) next[k] = INF; n           for (int k = (0); k < (D); k++) n             for (int l = (0); l < (D); l++) { n               next[l] = min(next[l], curr[k] + dp[0][S | (1 << j)][k][l]); n             } n           swap(curr, next); n         } n         { n           for (int k = (0); k < (D); k++) next[k] = INF; n           for (int k = (0); k < (D); k++) n             for (int l = (0); l < (D); l++) { n               next[l] = min(next[l], curr[k] + dp[i][S | (1 << (j - 1))][k][l]); n             } n           swap(curr, next); n         } n       } n     } else { n       for (int j = mod; j >= 1; j--) { n         for (int k = (0); k < (D); k++) next[k] = INF; n         for (int k = (0); k < (D); k++) n           for (int l = (0); l < (D); l++) { n             next[l] = min(next[l], curr[k] + dp[i][S | (1 << j)][k][l]); n           } n         swap(curr, next); n       } n     } n   } n   cout << *min_element(curr, curr + D) + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 2; n bool v[N], use[N]; n int main() { n   int n, i, j; n   long long T; n   vector<int> p; n   scanf( %d , &n); n   for (i = 2; i <= n; ++i) { n     if (!v[i]) { n       p.push_back(i); n       T = i; n       T *= i; n       while (T <= n) v[T] = 1, T += i; n     } n   } n   vector<pair<int, int> > ret; n   vector<int> x; n   for (i = p.size() - 1; i >= 1; --i) { n     j = p[i]; n     x.clear(); n     while (j <= n) { n       if (!use[j]) { n         x.push_back(j); n       } n       j = j + 2 * p[i]; n     } n     for (j = 0; j < x.size(); j += 2) { n       if (j == x.size() - 1) break; n       ret.push_back(make_pair(x[j], x[j + 1])); n       use[x[j]] = 1, use[x[j + 1]] = 1; n     } n     if (x.size() & 1) { n       if (p[i] * 2 <= n) { n         ret.push_back(make_pair(x.back(), p[i] * 2)); n         use[x.back()] = 1, use[p[i] * 2] = 1; n       } n     } n   } n   x.clear(); n   for (i = 2; i <= n; i += 2) n     if (!use[i]) x.push_back(i); n   int sz = x.size() / 2; n   sz *= 2; n   for (i = 0; i < sz; i += 2) ret.push_back(make_pair(x[i], x[i + 1])); n   printf( %d n , (int)ret.size()); n   for (i = 0; i < ret.size(); ++i) n     printf( %d %d n , ret[i].first, ret[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n using namespace rel_ops; n const int inf = ~0u >> 2; n const long long INF = ~0ull >> 2; n const double err = 1e-11; n const double pi = acos(-1); n inline long long read() { n   long long x; n   if (scanf( %lld , &x) == -1) exit(0); n   return x; n } n inline double readf() { n   double x; n   if (scanf( %lf , &x) == -1) exit(0); n   return x; n } n template <typename _> n inline _ sqr(_ x) { n   return x * x; n } n ostream &operator<<(ostream &o, const pair<int, int> &x) { n   return o <<  (  << x.first <<  ,  << x.second <<  ) ; n } n mt19937 rnd(time(0)); n const int N = 20010; n const int mod = (0 ? 1000000007 : 998244353); n long long T; n string s; n deque<char> a; n set<char> Set; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> T; n   while (T--) { n     cin >> s; n     Set.clear(); n     a.clear(); n     long long i = 0, f = 1; n     for (auto c : s) { n       if (Set.empty()) { n         Set.insert(c); n         a.push_back(c); n         continue; n       } n       if (Set.count(c)) { n         if (i != 0 && a[i - 1] == c) { n           i--; n           continue; n         } n         if (i != (long long)a.size() - 1 && a[i + 1] == c) { n           i++; n           continue; n         } n         f = false; n       } else { n         if (i == 0) { n           a.push_front(c); n           Set.insert(c); n           continue; n         } n         if (i == (long long)a.size() - 1) { n           a.push_back(c); n           Set.insert(c); n           i++; n           continue; n         } n         f = false; n       } n     } n     if (f) { n       cout <<  YES  << endl; n       for (long long i = ( a ), _ = ( z  + 1); i < _; i++) n         if (!Set.count(i)) { n           a.push_back(i); n         } n       for (auto i : a) cout << char(i); n       cout << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; n int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; n int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; n int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1}; n const int M = 100005; n vector<int> Slope[M * 4], C[M * 4], idx[M * 4]; n long long get(int n, long long id, long long x) { n   return (Slope[n][id] * x + C[n][id]); n } n double cross(int n, int x, int y) { n   return (double)(C[n][x] - C[n][y]) / (Slope[n][y] - Slope[n][x]); n } n void AddLine(int n, int id, long long p, long long q) { n   if (Slope[n].size() > 0 && Slope[n].back() == p) { n     if (C[n].back() < q) { n       Slope[n].pop_back(); n       C[n].pop_back(); n       idx[n].pop_back(); n     } else n       return; n   } n   Slope[n].push_back(p); n   C[n].push_back(q); n   idx[n].push_back(id); n   int hsz = Slope[n].size(); n   while (hsz >= 3 && cross(n, hsz - 3, hsz - 2) > cross(n, hsz - 3, hsz - 1)) { n     --hsz; n     Slope[n][hsz - 1] = Slope[n][hsz]; n     C[n][hsz - 1] = C[n][hsz]; n     idx[n][hsz - 1] = idx[n][hsz]; n     Slope[n].pop_back(); n     C[n].pop_back(); n     idx[n].pop_back(); n   } n } n pair<long long, long long> query(int n, long long x) { n   long long hsz = (int)Slope[n].size() - 1; n   long long lw = 0, hi = hsz, mid, midd; n   pair<long long, long long> ans = {-9e18, 0}; n   while (hi - lw > 3) { n     mid = (lw + hi) / 2; n     midd = min(hsz, mid + 1); n     if (get(n, mid, x) >= get(n, midd, x)) { n       hi = mid; n     } else { n       lw = midd; n     } n   } n   for (long long i = lw; i <= hi; i++) n     ans = max(ans, {get(n, i, x), idx[n][i]}); n   return ans; n } n void update(int l, int r, int k, int L, pair<long long, long long> p) { n   if (l > L || r < L) return; n   AddLine(k, L, p.first, p.second); n   if (l == r) return; n   int mid = (l + r) / 2; n   update(l, mid, k * 2, L, p); n   update(mid + 1, r, k * 2 + 1, L, p); n } n pair<long long, long long> query(int l, int r, int k, int L, int R, n                                  long long val) { n   if (l > R || r < L) return {-9e18, 0}; n   if (l >= L && r <= R) { n     return query(k, val); n   } n   int mid = (l + r) / 2; n   pair<long long, long long> ret = {-9e18, 0}; n   ret = max(ret, query(l, mid, k * 2, L, R, val)); n   ret = max(ret, query(mid + 1, r, k * 2 + 1, L, R, val)); n   return ret; n } n vector<pair<pair<long long, long long>, long long> > vec; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     vec.push_back({{y, x}, i}); n   } n   sort(vec.begin(), vec.end()); n   for (auto z : vec) { n     int id = z.second; n     update(1, n, 1, id, z.first); n   } n   for (int i = 1; i <= m; i++) { n     int l, r, x; n     cin >> l >> r >> x; n     cout << query(1, n, 1, l, r, x).second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y; n long long a[300010]; n vector<long long> fac, facn; n vector<int> facx, facy; n vector<pair<int, int> > facv; n vector<long long> check; n int n; n long long sum[300010]; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n void factorize(long long val) { n   for (long long i = 2; i <= val && i <= 1000000ll; i++) n     if (val % i == 0) { n       fac.push_back(i); n       while (val % i == 0) val /= i; n     } n   if (val > 1) { n     long long q = (long long)(sqrt(val)); n     if (q * q == val) { n       fac.push_back(q); n       return; n     } n     for (int i = 1; i <= n; i++) check.push_back(a[i]); n     check.push_back(x); n     check.push_back(y); n     long long g = val; n     for (int i = 0; i < check.size(); i++) { n       g = gcd(val, check[i]); n       if (g != 1 && g != val) { n         if (g < val / g) { n           fac.push_back(g); n           fac.push_back(val / g); n         } else { n           fac.push_back(val / g); n           fac.push_back(g); n         } n         return; n       } n     } n     fac.push_back(val); n   } n } n int main() { n   scanf( %d%I64d%I64d , &n, &x, &y); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   factorize(y); n   if (y % x) { n     printf( 0 ); n     return 0; n   } n   for (int i = 0; i < fac.size(); i++) facv.push_back(make_pair(0, 0)); n   long long x1 = x; n   long long chunhua = y; n   for (int i = 0; i < fac.size(); i++) { n     int cnt = 0; n     while (x1 % fac[i] == 0) { n       x1 /= fac[i]; n       cnt++; n     } n     facv[i].first = cnt; n   } n   for (int i = 0; i < fac.size(); i++) { n     int cnt = 0; n     while (chunhua % fac[i] == 0) { n       chunhua /= fac[i]; n       cnt++; n     } n     facv[i].second = cnt; n   } n   for (int i = 0; i < fac.size(); i++) { n     if (facv[i].first != facv[i].second) { n       facn.push_back(fac[i]); n       facx.push_back(facv[i].first); n       facy.push_back(facv[i].second); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] % x) continue; n     long long val = a[i]; n     int mask = 0; n     for (int j = 0; j < facn.size(); j++) { n       int cnt = 0; n       while (val % facn[j] == 0) { n         val /= facn[j]; n         cnt++; n       } n       mask += ((cnt > facx[j]) << j); n     } n     sum[mask]++; n   } n   for (int i = 0; i < facn.size(); i++) n     for (int mask = 0; mask < (1 << facn.size()); mask++) n       if (mask & (1 << i)) sum[mask] += sum[mask - (1 << i)]; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (y % a[i]) continue; n     long long val = a[i]; n     int mask = 0; n     for (int j = 0; j < facn.size(); j++) { n       int cnt = 0; n       while (val % facn[j] == 0) { n         val /= facn[j]; n         cnt++; n       } n       mask += ((cnt < facy[j]) << j); n     } n     ans += sum[(1 << facn.size()) - mask - 1]; n   } n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     long long a, b, c, r; n     cin >> a >> b >> c >> r; n     if (a > b) swap(a, b); n     long long left = c - r, right = c + r; n     long long dist = 0; n     if (b < left) { n       cout << b - a <<   n ; n     } else if (a > right) { n       cout << b - a <<   n ; n     } else { n       dist += max(0ll, left - a); n       dist += max(0ll, b - right); n       cout << dist <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[55]; n int a[55]; n int main() { n   int n, i; n   long long k; n   f[1] = 1, f[2] = 2; n   for (i = 3; i <= 50; i++) f[i] = f[i - 1] + f[i - 2]; n   while (scanf( %d%lld , &n, &k) == 2) { n     for (i = 1; i <= n;) { n       if (k <= f[n - i] || i == n) { n         a[i] = i; n         i++; n       } else { n         k = k - f[n - i]; n         a[i] = i + 1; n         i++; n         a[i] = i - 1; n         i++; n       } n     } n     for (i = 1; i <= n; i++) printf( %d%c , a[i], (i == n) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M = 1e9; n int ask(int x, int y) { n   cout <<  ?   << x <<     << y << endl; n   cin >> x; n   return x; n } n int main() { n   int s = ask(1, 1), k; n   int u, v, l = 1, r = M, mid; n   while (r > l + 1) { n     mid = l + r >> 1; n     k = ask(1, mid); n     if (k + mid - 1 != s) n       r = mid; n     else n       l = mid; n   } n   u = ask(1, l) + 1; n   v = l; n   printf( ! %d %d %d %d , u, v, v - 1 + M - ask(M, 1), u - 1 + M - ask(1, M)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, k, x, y; n int main() { n   scanf( %lld%lld%lld , &l, &r, &k), x = 1; n   if (k == 1 || l == r || (r - l == 1 && l % 2 && (l ^ r) >= l)) n     return printf( %lld n1 n%lld n , l, l), 0; n   for (int i = 0;; i++) { n     if (x > l) { n       y = i; n       break; n     } n     x *= 2; n   } n   if (x + x / 2 <= r && k >= 3) n     return printf( 0 n3 n%lld %lld %lld n , x - 1, x + x / 2, x + x / 2 - 1), 0; n   if (r - l == 1) n     return printf( %lld n2 n%lld %lld n , l ^ (l + 1), l, l + 1), 0; n   if (l % 2) l++; n   if (l + 3 <= r && k >= 4) n     return printf( 0 n4 n%lld %lld %lld %lld n , l, l + 1, l + 2, l + 3), 0; n   else n     return printf( 1 n2 n%lld %lld n , l, l + 1), 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 10004, NA = -1, MaxC = 0x3F3F3F3F; n int m, n, x; n int f(int n, int m, int x) { n   n -= x + x; n   m -= x + x; n   if (n <= 0 || m <= 0) return 0; n   return (n * m + 1) >> 1; n } n int main(void) { n   while (scanf(  %d %d %d , &n, &m, &x) != EOF) { n     printf( %d n , f(n, m, x - 1) - f(n, m, x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n int arr[500005], st[500005][25], fa[500005][25], bit[500005]; n inline int calc(int x, int y) { n   if (arr[x] <= arr[y]) return x; n   return y; n } n inline int query(int l, int r) { n   if (l < 0 || l > r) return 0; n   int len = bit[r - l + 1]; n   return calc(st[l][len], st[r - (1 << len) + 1][len]); n } n int main() { n   for (int i = 2; i < 500005; i++) bit[i] = bit[i >> 1] + 1; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , arr + i); n     arr[i + n] = arr[i]; n   } n   for (int i = 0; i < n << 1; i++) { n     arr[i] = i - arr[i]; n     st[i][0] = i; n   } n   for (int i = 1; i < 22; i++) { n     for (int j = 0; j + (1 << i) <= n << 1; j++) n       st[j][i] = calc(st[j][i - 1], st[j + (1 << i - 1)][i - 1]); n   } n   for (int i = 0; i < n << 1; i++) fa[i][0] = query(arr[i], i); n   for (int i = 1; i < 20; i++) { n     for (int j = 0; j < n << 1; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; n   } n   long long ans = 0; n   for (int i = n; i < n << 1; i++) { n     int x = i; n     ans += (arr[i] > i - n + 1) + 1; n     for (int j = 19; j >= 0; j--) { n       if (arr[fa[x][j]] > i - n + 1) { n         x = fa[x][j]; n         ans += 1 << j; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int num = 0; n int cnt[1000] = {0}; n int candidate[400005] = {0}; n string s; n bool check(int added) { n   if (added % 2) return false; n   if (n % (added + num)) return false; n   if ((n / (added + num)) % 2 == 0) return false; n   return true; n } n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; ++i) cnt[s[i]]++; n   for (int i = 0; i < 200; ++i) { n     if (cnt[i] % 2) { n       cnt[i] -= 1; n       candidate[num++] = i; n     } n   } n   if (num == 0) { n     string before =   ; n     string after =   ; n     for (int j = 40; j < 130; ++j) { n       while (cnt[j] >= 2) { n         cnt[j] -= 2; n         before += j; n         after += j; n       } n     } n     cout << 1 << endl; n     reverse(before.begin(), before.end()); n     cout << before + after << endl; n     return 0; n   } n   int added = 0; n   while (!check(added)) ++added; n   for (int i = 40; i < 130; ++i) { n     if (!added) break; n     if (cnt[i] > added) { n       for (int j = 0; j < added; ++j) candidate[num++] = i; n       cnt[i] -= added; n       added = 0; n     } else { n       for (int j = 0; j < cnt[i]; ++j) candidate[num++] = i; n       added -= cnt[i]; n       cnt[i] = 0; n     } n   } n   cout << num << endl; n   for (int i = 0; i < num; ++i) { n     string before =   ; n     string after =   ; n     after += char(candidate[i]); n     int remain = (n / num) - 1; n     for (int j = 40; j < 130; ++j) { n       if (!remain) break; n       if (cnt[j] >= remain) { n         for (int k = 0; k < remain / 2; ++k) { n           before += char(j); n           after += char(j); n         } n         cnt[j] -= remain; n         remain = 0; n       } else { n         for (int k = 0; k < cnt[j] / 2; ++k) { n           before += char(j); n           after += char(j); n         } n         remain -= cnt[j]; n         cnt[j] = 0; n       } n     } n     reverse(before.begin(), before.end()); n     cout << before + after <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r1, c1, r2, c2, a[505][505], N, M, dx[4] = {-1, 0, 1, 0}, n                                        dy[4] = {0, 1, 0, -1}; n bool viz[505][505]; n char c; n queue<pair<int, int> > Q; n bool bfs(int x, int y, int xx, int yy) { n   Q.push(make_pair(x, y)); n   while (Q.size()) { n     pair<int, int> nod = Q.front(); n     Q.pop(); n     for (int i = 0; i < 4; ++i) { n       int nx = nod.first + dx[i], ny = nod.second + dy[i]; n       if (a[nx][ny] == 1 && nx == xx && ny == yy) { n         return 1; n       } else if (a[nx][ny] == 2) { n         --a[nx][ny]; n         Q.push(make_pair(nx, ny)); n       } n     } n   } n   return 0; n } n int main() { n   cin.sync_with_stdio(false); n   cin >> N >> M; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) { n       cin >> c; n       if (c ==  . ) n         a[i][j] = 2; n       else n         a[i][j] = 1; n     } n   cin >> r1 >> c1 >> r2 >> c2; n   if (bfs(r1, c1, r2, c2)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Line { n   long long a, b; n   int id; n   Line(long long a, long long b, int id) : a(a), b(b), id(id) {} n   bool before(const Line &A, const Line B) { n     long long p1 = A.b - b, p2 = B.b - b; n     long long q1 = a - A.a, q2 = a - B.a; n     if (q1 < 0) { n       p1 *= -1; n       q1 *= -1; n     } n     if (q2 < 0) { n       p2 *= -1; n       q2 *= -1; n     } n     return p1 * q2 <= p2 * q1; n   } n   long long value(int x) { return a * x + b; } n }; n struct Hull { n   vector<int> a; n   vector<bool> taken; n   vector<Line> h; n   vector<int> order; n   int ptr; n   int size = 0; n   long long sum = 0; n   Hull() {} n   Hull(const vector<int> &a) : a(a) { n     taken.resize(a.size(), false); n     for (int i = 0; i < (int)a.size(); ++i) { n       order.push_back(i); n     } n     sort((order).begin(), (order).end(), n          [&](int i, int j) { return a[i] < a[j]; }); n     Build(); n   } n   void Build() { n     const int n = (int)a.size(); n     ptr = 0; n     vector<long long> val(n); n     long long suf = sum; n     int cnt = 0; n     for (int i = 0; i < n; ++i) { n       if (taken[i]) { n         suf -= a[i]; n         ++cnt; n       } else { n         val[i] = suf + a[i] * 1ll * (cnt + 1); n       } n     } n     vector<Line> sorted; n     for (int i : order) { n       if (!taken[i]) { n         sorted.push_back(Line(a[i], val[i], i)); n       } n     } n     h.clear(); n     for (Line &C : sorted) { n       if (!h.empty() && h.back().a == C.a) { n         if (C.b <= h.back().b) n           continue; n         else n           h.pop_back(); n       } n       while ((int)h.size() >= 2) { n         Line &A = h[(int)h.size() - 2]; n         Line &B = h[(int)h.size() - 1]; n         if (A.before(B, C)) break; n         h.pop_back(); n       } n       h.push_back(C); n     } n   } n   pair<long long, int> best(int before) { n     if (h.empty()) return {-1ll, -1}; n     while (ptr <= (int)h.size() - 2 && n            h[ptr].value(before) < h[ptr + 1].value(before)) { n       ++ptr; n     } n     return make_pair(h[ptr].value(before), h[ptr].id); n   } n   void Remove(const int i) { n     taken[i] = true; n     sum += a[i]; n     ++size; n     Build(); n   } n } h[1010]; n const int N = 1e5 + 5; n int a[N]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int _n(n), i(0); i < _n; i++) cin >> a[i]; n   int block = max(1, int(sqrt(n))); n   for (int i = 0; i < n; i += block) { n     vector<int> b; n     for (int j = i; j < min(n, i + block); ++j) { n       b.push_back(a[j]); n     } n     h[i / block] = Hull(b); n   } n   long long best = 0, score = 0; n   while (true) { n     pair<long long, int> m = make_pair(-1ll, -1); n     long long suf = 0; n     int cnt = 0; n     for (int i = 0; i < n; i += block) { n       suf += h[i / block].sum; n     } n     for (int i = 0; i < n; i += block) { n       suf -= h[i / block].sum; n       pair<long long, int> p = h[i / block].best(cnt); n       p.first += suf; n       if (p.second != -1) n         if (m.second == -1 || p.first > m.first) n           m = make_pair(p.first, i + p.second); n       cnt += h[i / block].size; n     } n     if (m.second == -1) break; n     score += m.first; n     best = max(best, score); n     int i = m.second; n     h[i / block].Remove(i - i / block * block); n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010; n long long n, m, k, x, y, z, a, b, c, t1, t2, cnt, p; n long long X[MAXN]; n long long Y[MAXN]; n bool X2[MAXN]; n bool Y2[MAXN]; n long long powmod(long long a, long long b) { n   if (!b) return 1; n   if (b & 1) return a * powmod(a, b - 1) % p; n   return powmod(a * a % p, b >> 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   if ((m ^ n) & 1) return cout << 0 <<   n , 0; n   ; n   for (int i = 0; i < k; i++) { n     cin >> a >> b >> c; n     X[a]++; n     Y[b]++; n     if (c == -1) { n       X2[a] ^= 1; n       Y2[b] ^= 1; n     } n   } n   for (int i = 1; i <= n; i++) n     if (X[i] == m) { n       if (!X2[i]) return cout << 0 <<   n , 0; n       ; n       t1++; n     } n   for (int i = 1; i <= m; i++) n     if (Y[i] == n) { n       if (!Y2[i]) return cout << 0 <<   n , 0; n       ; n       t2++; n     } n   cnt = (n - t1 - 1) * (m - t2 - 1) - (k - t1 * m - t2 * n + t1 * t2); n   cin >> p; n   cout << powmod(2, cnt) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int deg[10000 + 10]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     deg[a - 1]++, deg[b - 1]++; n   } n   int ans = 0; n   for (int i = 0; i < n; i++) ans += deg[i] * (deg[i] - 1) / 2; n   cout << ans; n } n


#include <bits/stdc++.h> n void build(const std::vector<std::pair<int, int>>& values, n            std::vector<int64_t>& seg, int node, int start, int end) { n   if (start == end) { n     seg[node] = values[start].second; n   } else if (start < end) { n     int mid = (start + end) / 2; n     build(values, seg, 2 * node, start, mid); n     build(values, seg, 2 * node + 1, mid + 1, end); n     seg[node] = seg[2 * node] + seg[2 * node + 1]; n   } n } n int64_t query(const std::vector<int64_t>& seg, int node, int start, int end, n               int l, int r) { n   if (r < start || end < l) { n     return 0; n   } n   if (l <= start && end <= r) { n     return seg[node]; n   } n   int mid = (start + end) / 2; n   int64_t p1 = query(seg, 2 * node, start, mid, l, r); n   int64_t p2 = query(seg, 2 * node + 1, mid + 1, end, l, r); n   return (p1 + p2); n } n int getUpperBound(const std::vector<std::pair<int, int>>& values, int v, n                   int start, int end) { n   int l = start; n   int r = end; n   while (l < r) { n     int m = (l + r + 1) / 2; n     if (v >= values[m].first) { n       l = m; n     } else { n       r = m - 1; n     } n   } n   if (values[r].first <= v) return r; n   return -1; n } n int main() { n   std::ios::sync_with_stdio(false); n   int s, b; n   std::cin >> s >> b; n   std::vector<int> a(s, 0); n   for (int i = 0; i < s; ++i) std::cin >> a[i]; n   std::vector<std::pair<int, int>> dg(b, {0, 0}); n   for (int i = 0; i < b; ++i) std::cin >> dg[i].first >> dg[i].second; n   std::sort(dg.begin(), dg.end()); n   std::vector<int64_t> seg(4 * b, 0); n   build(dg, seg, 1, 0, b - 1); n   for (int i = 0; i < s; ++i) { n     auto offset = getUpperBound(dg, a[i], 0, b - 1); n     if (offset != -1) { n       std::cout << query(seg, 1, 0, b - 1, 0, offset) <<    ; n     } else { n       std::cout << 0 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long int mod = 1000000007; n const long long int AM = 2e5 + 5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     string s[n + 1]; n     for (__typeof(0) i = 0; i < n; ++i) cin >> s[i]; n     long long int cnt[200] = {0}; n     for (__typeof(0) i = 0; i < n; ++i) { n       for (__typeof(0) j = 0; j < s[i].size(); ++j) { n         cnt[s[i][j] -  a ]++; n       } n     } n     long long int flag = 1; n     for (__typeof(0) i = 0; i < 26; ++i) { n       if (cnt[i] % n != 0) flag = 0; n     } n     if (flag) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n long long n, a[103][2601] = {}, ans = 0, mod = 1000000007, tmp = 0; n string second; n char c; n vector<pair<int, int> > vp; n bool first, f1; n bool cmpvp(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first < p2.first) return 1; n   if (p1.first == p2.first) n     if (p1.second < p2.second) return 1; n   return 0; n } n int main() { n   for (int i = 0; i < 26; ++i) a[1][i] = 1; n   for (int i = 2; i <= 100; ++i) { n     for (int j = 0; j <= 2600; ++j) { n       for (int k = 0; k < 26; ++k) n         a[i][j + k] = (a[i][j + k] + a[i - 1][j]) % mod; n     } n   } n   cin >> n; n   for (int i = 0; i < n; ++i) { n     tmp = 0; n     cin >> second; n     for (int j = 0; second[j]; ++j) tmp += second[j] -  a ; n     cout << a[second.size()][tmp] - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n  n  n int main() n { n     int t; n     cin>>t; n     while(t--) n     { n         long long int n; n         cin>>n; n         long long int sum=0; n         for(int i=0;i<n;i++) n         { n             long long int a; n             cin>>a; n             sum+=a; n         } n         sum%=n; n         long long int k=n-sum; n         cout<<sum*k<<endl; n     } n     return 0; n } n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n int n, a[200005]; n map<int, int> mp; n int temp, largest_num; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]] = max(mp[a[i]], mp[a[i] - 1] + 1); n     if (temp < mp[a[i]]) { n       largest_num = a[i]; n       temp = mp[a[i]]; n     } n   } n   cout << temp << endl; n   int start = largest_num - temp + 1; n   for (int i = 0; i < n; i++) { n     if (a[i] == start) { n       cout << i + 1 <<    ; n       start++; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string second; n   cin >> second; n   map<string, string> mp1; n   for (int i = 0; i < 10; i++) { n     string s1; n     cin >> s1; n     mp1[s1] = to_string(i); n   } n   string p =   ; n   string ans =   ; n   for (int i = 0; i < second.length(); i++) { n     if (i % 10 == 0) { n       p =   ; n     } n     p += second[i]; n     if (i % 10 == 9) n       if (p.length() != 0) ans += mp1[p]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int x = 0, y = 0, z = 0; n   for (int i = 0; i < a.length(); i++) { n     if (a[i] != b[i]) { n       if (a[i] ==  4 ) n         x++; n       else n         y++; n     } n   } n   cout << min(x, y) + abs(x - y); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 1; n int a[MAXN], pre[MAXN], num[MAXN]; n int main() { n   int n, m; n   cin >> n >> m; n   int i; n   int Biggest = -1, Smallest = 0x7ffffff; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     Biggest = max(Biggest, a[i]); n     Smallest = min(Smallest, a[i]); n     num[a[i] - 1]++; n   } n   if (Biggest == Smallest) { n     cout << 0 << endl; n     return 0; n   } n   for (i = Biggest - 1; i >= Smallest; i--) { n     pre[i] = pre[i + 1] + num[i]; n   } n   int ans = 1; n   int now = 0; n   for (i = Biggest; i >= Smallest; i--) { n     now += pre[i]; n     if (now > m) { n       now = pre[i]; n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n struct Node { n   int value; n   struct Node *next; n } *head = NULL, *tail; n struct Query { n   int index, ans1, ans2; n   long long round; n } queries[maxn]; n bool cmp_by_round(const Query &lhs, const Query &rhs) { n   return lhs.round < rhs.round; n } n bool cmp_by_index(const Query &lhs, const Query &rhs) { n   return lhs.index < rhs.index; n } n int a[maxn], b[maxn]; n int main() { n   int n, q, maxv = 0; n   scanf( %d%d , &n, &q); n   struct Node *last = NULL; n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     struct Node *node = (Node *)malloc(sizeof(Node)); n     node->value = a[i]; n     node->next = NULL; n     if (head == NULL) head = node; n     if (last) last->next = node; n     last = tail = node; n     if (a[i] > a[maxv]) maxv = i; n   } n   for (int i = 0; i < q; i++) { n     scanf( %lld , &queries[i].round); n     queries[i].index = i; n   } n   sort(queries, queries + q, cmp_by_round); n   int index_query = 0; n   for (int i = 0; i < maxv; i++) { n     struct Node *node1 = (Node *)malloc(sizeof(Node)); n     struct Node *node2 = (Node *)malloc(sizeof(Node)); n     int v1 = head->value, v2 = head->next->value; n     while (1LL * i == queries[index_query].round - 1) { n       queries[index_query].ans1 = head->value; n       queries[index_query].ans2 = head->next->value; n       index_query++; n     } n     if (v1 < v2) swap(v1, v2); n     node1->value = v1; n     if (head->next->next) n       node1->next = head->next->next; n     else n       node1->next = node2; n     node2->value = v2; n     node2->next = NULL; n     tail->next = node2; n     tail = node2; n     free(head->next); n     free(head); n     head = node1; n   } n   struct Node *node = head; n   for (int i = 0; i < n; i++) { n     b[i] = node->value; n     node = node->next; n   } n   while (index_query < q) { n     long long temp_round = queries[index_query].round - maxv; n     queries[index_query].ans1 = b[0]; n     queries[index_query].ans2 = b[int((temp_round - 1) % (n - 1) + 1)]; n     index_query++; n   } n   sort(queries, queries + q, cmp_by_index); n   for (int i = 0; i < q; i++) n     printf( %d %d n , queries[i].ans1, queries[i].ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   map<char, char> mp = {{ A ,  A }, { T ,  T }, { b ,  d }, { d ,  b }, n                         { H ,  H }, { I ,  I }, { M ,  M }, { O ,  O }, n                         { o ,  o }, { p ,  q }, { q ,  p }, { V ,  V }, n                         { v ,  v }, { W ,  W }, { w ,  w }, { X ,  X }, n                         { x ,  x }, { Y ,  Y }, { U ,  U }}; n   string s; n   cin >> s; n   int n = s.size(); n   for (int i = 0; i <= n / 2 - (n % 2 == 0); ++i) { n     if (!mp.count(s[i]) or mp[s[i]] != s[n - i - 1]) { n       cout <<  NIE  <<   n ; n       return 0; n     } n   } n   cout <<  TAK  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, 1, -1, 1}; n int dy[] = {-1, 1, 0, 0, 1, 1, -1, -1}; n int dz[] = {0, 0, 1, -1, -1, 1, -1, 1}; n int kx[] = {2, 2, -2, -2, 1, -1, 1, -1}; n int ky[] = {1, -1, 1, -1, 2, 2, -2, -2}; n double _distance(double x1, double y1, double x2, double y2) { n   double x1x2 = x1 - x2; n   double y1y2 = y1 - y2; n   return sqrt((x1x2 * x1x2) + (y1y2 * y1y2)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long c[n]; n   long long sum = 0; n   for (int i = 0; i < n; i++) cin >> c[i]; n   sum += c[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     if (c[i] < c[i + 1]) n       sum += c[i]; n     else { n       c[i] = max(0LL, c[i + 1] - 1); n       sum += c[i]; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     int a = 100000, b = 100000, c = -100000, d = -100000; n     vector<bool> ok(4, false); n     for (int i = 0; i < n; i++) { n       int x, y; n       cin >> x >> y; n       int f[4]; n       for (int j = 0; j < 4; j++) cin >> f[j]; n       if (f[0] == 0) d = max(d, x), ok[0] = true; n       if (f[1] == 0) a = min(a, y), ok[1] = true; n       if (f[2] == 0) b = min(b, x), ok[2] = true; n       if (f[3] == 0) c = max(c, y), ok[3] = true; n     } n     if (a < c || d > b) n       cout << 0; n     else n       cout << 1 <<     << (d + b) / 2 <<     << (a + c) / 2; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, r, R, k, l, A[100005]; n string cad, aux; n char x, y; n map<int, int> M; n struct Res { n   int X, Y, Z; n   Res() {} n   Res(int a, int b, int c) { n     int F[5]; n     F[0] = a; n     F[1] = b; n     F[2] = c; n     sort(F, F + 3); n     X = F[2]; n     Y = F[1]; n     Z = F[0]; n   } n }; n vector<Res> Sol; n struct node { n   int elem, prio; n   node() {} n   node(int e, int p) { n     elem = e; n     prio = p; n   } n   bool operator<(const node &b) const { n     if (prio != b.prio) return prio < b.prio; n     return elem < b.elem; n   } n }; n priority_queue<node> q; n void calc() { n   node n1, n2, n3; n   int r; n   Sol.clear(); n   while (1) { n     if (q.empty()) return; n     n1 = q.top(); n     q.pop(); n     if (q.empty()) return; n     n2 = q.top(); n     q.pop(); n     if (q.empty()) return; n     n3 = q.top(); n     q.pop(); n     r = 1; n     for (int i = 0; i < r; i++) { n       Sol.push_back(Res(n1.elem, n2.elem, n3.elem)); n     } n     if (r != n1.prio) q.push(node(n1.elem, n1.prio - r)); n     if (r != n2.prio) q.push(node(n2.elem, n2.prio - r)); n     if (r != n3.prio) q.push(node(n3.elem, n3.prio - r)); n   } n } n int main() { n   scanf( %d , &n); n   M.clear(); n   for (int i = 0; i < n; i++) { n     scanf( %d , &A[i]); n   } n   sort(A, A + n); n   int c = 1; n   for (int i = 1; i < n; i++) n     if (A[i] != A[i - 1]) { n       q.push(node(A[i - 1], c)); n       c = 1; n     } else n       c++; n   q.push(node(A[n - 1], c)); n   calc(); n   cout << Sol.size() << endl; n   for (int i = 0; i < Sol.size(); i++) n     cout << Sol[i].X <<     << Sol[i].Y <<     << Sol[i].Z << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long N = 1e6 + 10; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n const double eps = 1e-8; n const double pi = acos(-1); n using namespace std; n namespace geo { n long long sgn(double x) { n   if (fabs(x) < eps) return 0; n   if (x < 0) n     return -1; n   else n     return 1; n } n struct point { n   double x, y; n   point() {} n   point(double xx, double yy) { n     x = xx; n     y = yy; n   } n   point operator+(point b) { return point(x + b.x, y + b.y); } n   point operator-(point b) { return point(x - b.x, y - b.y); } n   double operator^(point b) { return x * b.y - y * b.x; } n   double operator*(point b) { return x * b.x + y * b.y; } n   point operator*(double b) { return point(x * b, y * b); } n   double gettan() { return atan2(y, x); } n   void read() { scanf( %lf%lf , &x, &y); } n }; n struct line { n   point s, e, l; n   line() {} n   line(point ss, point ee) { n     s = ss; n     e = ee; n     l = e - s; n   } n   void read() { n     s.read(), e.read(); n     l = e - s; n   } n   double gettan() { return atan2(e.y - s.y, e.x - s.x); } n }; n struct circle { n   point o; n   double r; n   circle() {} n   circle(point oo, double rr) { n     o = oo; n     r = rr; n   } n }; n double dis2(point a, point b) { return (b - a) * (b - a); } n double dis(point a, point b) { return sqrt(dis2(a, b)); } n bool onseg(line l, point p) { n   bool x1 = sgn((l.s - p) ^ (l.e - p)) == 0; n   bool x2 = sgn((l.s - p) * (l.e - p)) <= 0; n   return x1 && x2; n } n point spin(point p, double a) { n   return point(p.x * cos(a) - p.y * sin(a), p.y * cos(a) + p.x * sin(a)); n } n double len(point a) { return sqrt(a * a); } n point projection(line l, point p) { n   return l.s + l.l * ((l.l * (p - l.s)) / ((l.l) * (l.l))); n } n bool judge(line l1, line l2) { n   point a = l1.s, b = l1.e, c = l2.s, d = l2.e; n   bool x1 = sgn((c - a) ^ l1.l) * sgn((d - a) ^ l1.l) <= 0; n   bool x2 = sgn((a - c) ^ l2.l) * sgn((b - c) ^ l2.l) <= 0; n   return x1 && x2 && max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) && n          max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) && n          max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) && n          max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y); n } n point getcross(line l1, line l2) { n   double s1 = ((l2.s - l1.s) ^ (l2.e - l1.s)); n   double s2 = ((l2.e - l1.e) ^ (l2.s - l1.e)); n   if (s1 + s2 == 0) return point{inf, inf}; n   return l1.s + l1.l * (s1 / (s1 + s2)); n } n double dis_pl(point p, line l) { n   point v = p - l.s; n   double ans = (v ^ l.l) / sqrt(l.l * l.l); n   return fabs(ans); n } n double askarea(point d[], long long n) { n   d[n + 1] = d[1]; n   double area = 0; n   for (long long i = 1; i <= n; i++) area += (d[i] ^ d[i + 1]); n   area /= 2; n   return fabs(area); n } n long long cmp(line a, line b) { n   double t1 = a.gettan(), t2 = b.gettan(); n   if (sgn(t1 - t2) == 0) return (a.l ^ (b.e - a.s)) > 0; n   return t1 < t2; n } n long long onright(line a, line b, line c) { n   point p = getcross(b, c); n   return (a.l ^ (p - a.s)) < 0; n } n void hpi(line li[], long long n) { n   line que[N]; n   sort(li + 1, li + n + 1, cmp); n   long long l = 1, r = 1, cnt = 0, i; n   for (i = 1; i < n; i++) n     if (sgn(li[i].gettan() - li[i + 1].gettan())) li[++cnt] = li[i]; n   li[++cnt] = li[n]; n   for (i = 1; i <= cnt; i++) { n     while (r - l > 1 && onright(li[i], que[r - 1], que[r - 2])) r--; n     while (r - l > 1 && onright(li[i], que[l], que[l + 1])) l++; n     que[r++] = li[i]; n   } n   while (r - l > 1 && onright(que[l], que[r - 1], que[r - 2])) r--; n   while (r - l > 1 && onright(que[r - 1], que[l], que[l + 1])) l++; n } n }  // namespace geo n long long a[N], b[40]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, i, j, ans = 0; n     cin >> n; n     memset(b, 0, sizeof(b)); n     for (i = 1; i <= n; i++) { n       cin >> a[i]; n       for (j = 1; j < 33; j++) n         if ((a[i] >> j) == 0) { n           ans += b[j]; n           b[j]++; n           break; n         } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(2e6) + 12, mod = int(1e9) + 7; n int n, z[N]; n char second[N]; n int Z(string x) { n   int m = int(x.size()); n   string t = x; n   reverse((t).begin(), (t).end()); n   string second = x +  #  + t; n   int res = 0; n   for (int i = 1, l = 0, r = 0; i < second.size(); i++) { n     z[i] = 0; n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < second.size() && second[z[i]] == second[i + z[i]]) z[i]++; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n     if (i > m && i + z[i] == int(second.size())) { n       res = max(res, z[i]); n     } n   } n   return res; n } n void solve() { n   scanf(  n%s , second + 1); n   n = strlen(second + 1); n   int l = 1, r = n; n   while (l <= r) { n     if (second[l] == second[r]) { n       l++; n       r--; n     } else n       break; n   } n   string a =   ; n   for (int i = l; i <= r; i++) { n     a += second[i]; n   } n   string b = a; n   string ans =   ; n   reverse((b).begin(), (b).end()); n   int z1 = Z(a); n   int z2 = Z(b); n   for (int i = 1; i < l; i++) ans += second[i]; n   if (z1 > z2) { n     for (int i = 0; i < z1; i++) ans += a[i]; n   } else { n     for (int i = 0; i < z2; i++) ans += b[i]; n   } n   for (int i = max(r + 1, l); i <= n; i++) ans += second[i]; n   for (int i = 0; i < ans.size(); i++) { n     putchar(ans[i]); n   } n   puts(  ); n } n int main() { n   int T = 1; n   scanf( %d , &T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 5e5 + 5; n vector<int> elm[N]; n struct SegTree { n   struct Dat { n     int l, r; n     Dat *lc, *rc; n     long long lmx, rmx, mx, sum; n     int len() { return r - l + 1; } n     void fset(long long kval) { lmx = rmx = mx = sum = kval; } n     void pushup() { n       lmx = max(lc->lmx, lc->sum + rc->lmx); n       rmx = max(rc->rmx, rc->sum + lc->rmx); n       mx = max({lc->mx, rc->mx, lc->rmx + rc->lmx}); n       sum = lc->sum + rc->sum; n     } n     void pushdown() {} n   } dat[N << 2]; n   int siz = 0; n   void init(int n) { n     function<Dat *(int, int)> build = [&](int l, int r) { n       Dat &nw = dat[siz++]; n       nw = {l, r}; n       if (l == r) { n       } else { n         int mid = (l + r) >> 1; n         nw.lc = build(l, mid), nw.rc = build(mid + 1, r); n         nw.pushup(); n       } n       return &nw; n     }; n     build(1, n); n   } n   template <typename Fn> n   void travsal(int l, int r, Fn f) { n     function<void(Dat &)> trav = [&](Dat &p) { n       if (r < p.l || l > p.r) return; n       if (p.l >= l && p.r <= r) { n         return f(p); n       } else { n         p.pushdown(); n         int mid = (l + r) >> 1; n         trav(*p.lc); n         trav(*p.rc); n         p.pushup(); n       } n     }; n     trav(dat[0]); n   } n   void update(int x, long long val) { n     travsal(x, x, [&](Dat &e) { e.fset(val); }); n   } n   int query() { return dat[0].mx; } n } seg; n int a[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, c; n   cin >> n >> c; n   seg.init(n); n   int base = 0; n   for (int i = 1; i < n + 1; i++) { n     cin >> a[i]; n     if (a[i] == c) n       seg.update(i, -1), base++; n     else n       elm[a[i]].push_back(i); n   } n   int ans = base; n   for (int i = 1; i < N; i++) { n     if (elm[i].empty()) continue; n     for (int pos : elm[i]) seg.update(pos, 1); n     ans = max(ans, base + seg.query()); n     for (int pos : elm[i]) seg.update(pos, 0); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using namespace std; n long long n, m; n long long a[((long long)(2e6 + 600))]; n int main() { n   cin >> n >> m; n   long long l = 0; n   long long pas = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i && a[i] == a[i - 1]) l = i; n     pas = max(pas, i - l + 1); n   } n   cout << pas; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const long long oo = 1LL << 60; n const long long kNumMoves = 4; n const long long kMoves[kNumMoves][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; n int n, v, e; n vector<int> a, b; n vector<vector<int> > conn, edges; n vector<int> ansX, ansY, ansD; n vector<int> topSort; n vector<int> seen; n void DFS_Topological(int node) { n   if (seen[node]) return; n   seen[node] = 1; n   for (int i = 0; i < int(n); ++i) { n     if (edges[node][i] && !seen[i]) { n       DFS_Topological(i); n       conn[node].push_back(i); n       conn[i].push_back(node); n     } n   } n   topSort.push_back(node); n } n int AddTransfusionStep(int src, int dest, int delta) { n   int mxMoveOut = min(a[src], delta); n   int mxMoveIn = min(v - a[dest], mxMoveOut); n   a[src] -= mxMoveIn; n   a[dest] += mxMoveIn; n   if (mxMoveIn) n     ansX.push_back(src + 1), ansY.push_back(dest + 1), ansD.push_back(mxMoveIn); n   return mxMoveIn; n } n int Transfuse_Out(int node, int dest, int delta) { n   if (!delta || seen[node] || seen[dest]) return 0; n   int step1 = AddTransfusionStep(node, dest, delta); n   if (step1 == delta) return delta; n   for (int i = 0; i < int(conn[dest].size()); ++i) { n     int newDest = conn[dest][i]; n     if (newDest == node) continue; n     if (seen[newDest]) continue; n     AddTransfusionStep(dest, newDest, v); n     Transfuse_Out(dest, newDest, v); n     AddTransfusionStep(dest, newDest, v); n   } n   int step2 = AddTransfusionStep(node, dest, delta - step1); n   return step1 + step2; n } n int Transfuse_In(int node, int dest, int delta) { n   if (!delta || seen[node] || seen[dest]) return 0; n   int step1 = AddTransfusionStep(dest, node, delta); n   if (step1 == delta) return delta; n   for (int i = 0; i < int(conn[dest].size()); ++i) { n     int newDest = conn[dest][i]; n     if (newDest == node) continue; n     if (seen[newDest]) continue; n     AddTransfusionStep(newDest, dest, v); n     Transfuse_In(dest, newDest, v); n     AddTransfusionStep(newDest, dest, v); n   } n   int step2 = AddTransfusionStep(dest, node, delta - step1); n   return step1 + step2; n } n bool Transfuse(int node) { n   int delta = a[node] - b[node]; n   if (delta > 0) { n     for (int i = 0; i < int(conn[node].size()); ++i) { n       int dest = conn[node][i]; n       if (seen[dest]) continue; n       delta -= Transfuse_Out(node, dest, delta); n     } n     return (delta == 0 && a[node] == b[node]); n   } else if (delta < 0) { n     delta = -delta; n     for (int i = 0; i < int(conn[node].size()); ++i) { n       int dest = conn[node][i]; n       if (seen[dest]) continue; n       delta -= Transfuse_In(node, dest, delta); n     } n     return (delta == 0 && a[node] == b[node]); n   } n   return true; n } n bool FindTransfusions() { n   seen.clear(); n   seen.resize(n, 0); n   for (int i = 0; i < int(n); ++i) DFS_Topological(i); n   seen.clear(); n   seen.resize(n, 0); n   for (int i = 0; i < int(n); ++i) { n     if (!Transfuse(topSort[i])) return false; n     seen[topSort[i]] = 1; n   } n   return true; n } n int main() { n   cin >> n >> v >> e; n   a.resize(n), b.resize(n); n   for (int i = 0; i < int(n); ++i) cin >> a[i]; n   for (int i = 0; i < int(n); ++i) cin >> b[i]; n   conn.resize(n); n   edges.resize(n, vector<int>(n, 0)); n   for (int i = 0; i < int(e); ++i) { n     int x, y; n     cin >> x >> y; n     --x, --y; n     edges[x][y] = edges[y][x] = 1; n   } n   if (FindTransfusions()) { n     cout << ansX.size() << endl; n     for (int i = 0; i < int(ansX.size()); ++i) n       cout << ansX[i] <<     << ansY[i] <<     << ansD[i] << endl; n   } else { n     cout <<  NO ; n   } n   int i; n   cin >> i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n int A[maxn], B[maxn], N, MOD, fir, d, len; n int ksm(int x, int y) { n   int res = 1; n   for (; y; y >>= 1, x = (long long)x * x % MOD) n     if (y & 1) res = (long long)res * x % MOD; n   return res; n } n void Solve(int *P, int Len) { n   int kd = P[1] - P[0], cnt = 0; n   map<int, bool> vis; n   for (int i = 0; i < N; ++i) vis[P[i]] = true; n   for (int i = 0; i < N; ++i) n     if (!vis.count((P[i] + kd) % MOD)) ++cnt; n   d = (long long)kd * ksm(cnt, MOD - 2) % MOD; n   fir = -1; n   len = (MOD - d) % MOD; n   for (int i = 0; i < N; ++i) n     if (!vis.count((P[i] + d) % MOD)) n       if (~fir) { n         fir = -1; n         break; n       } else n         fir = P[i]; n } n int main() { n   MOD = read(); n   N = read(); n   for (int i = 0; i < N; ++i) A[i] = read(); n   if (N == 1 || N == MOD) n     printf( %d 1 n , A[0]); n   else { n     sort(A, A + N); n     if (2 * N < MOD) n       Solve(A, N); n     else { n       map<int, bool> vis; n       for (int i = 0; i < N; ++i) vis[A[i]] = true; n       N = 0; n       for (int i = 0; i < MOD; ++i) n         if (!vis.count(i)) B[N++] = i; n       Solve(B, N); n       if (~fir) (fir += (long long)len * N % MOD) %= MOD; n     } n     if (~fir) n       printf( %d %d n , fir, len); n     else n       puts( -1 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t, k; n   long long int ans; n   cin >> t; n   while (t--) { n     cin >> k; n     ans = (k / 18) * 30; n     k = k % 18; n     if (k == 0) { n       ans--; n     } n     while (k) { n       ans++; n       if (ans % 3 != 0 && ans % 10 != 3) { n         k--; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int64_t binexp(int64_t base, int64_t exponent) { n   int64_t res = 1; n   base = (base + 1000000007) % 1000000007; n   while (exponent) { n     if (exponent & 1) { n       res = (res * base) % 1000000007; n     } n     exponent = exponent >> 1; n     base = (base * base) % 1000000007; n   } n   return res; n } n void solve() { n   double n; n   cin >> n; n   n = 2.0 * n; n   double param, result; n   param = (3.14159265 / n); n   result = sin(param); n   double ans1 = (1.0 / (result)); n   double res = cos(param / 2.0); n   double ans2 = (1.0 * res); n   double ans = (ans1 * ans2); n   printf( %.10f n , ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int N = 200100; n int a[N], t[N]; n multiset<int> st1, st2; n multiset<int>::iterator it; n int sum = 0, nowt = 0, res = 0; n int n, m, k; n void ins(int x) { n   sum += a[x]; n   if (st1.size() < m) { n     st1.insert(t[x]); n     nowt += (t[x] + 1) / 2; n   } else { n     it = st1.begin(); n     if ((*it) < t[x]) { n       nowt += (t[x] + 1) / 2; n       nowt -= ((*it) + 1) / 2; n       nowt += (*it); n       st1.insert(t[x]); n       st2.insert((*it)); n       st1.erase(it); n     } else { n       nowt += t[x]; n       st2.insert(t[x]); n     } n   } n } n void del(int x) { n   sum -= a[x]; n   if (st2.size() && st2.find(t[x]) != st2.end()) { n     nowt -= t[x]; n     st2.erase(st2.find(t[x])); n   } else { n     nowt -= (t[x] + 1) / 2; n     assert(st1.find(t[x]) != st1.end()); n     st1.erase(st1.find(t[x])); n     if (st2.size()) { n       it = st2.end(); n       it--; n       nowt -= (*it); n       nowt += ((*it) + 1) / 2; n       st1.insert((*it)); n       st2.erase(it); n     } n   } n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &t[i]); n   } n   for (int i = 1, j = 1; i <= n; i++) { n     while (j <= n) { n       ins(j); n       if (nowt > k) { n         del(j); n         break; n       } n       j++; n     } n     if (nowt <= k) res = max(res, sum); n     if (i != j) n       del(i); n     else n       j++; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q, n, x; n   char a, b; n   vector<char> v; n   cin >> q; n   for (int i = 0; i < q; i++) { n     x = 0; n     cin >> n; n     for (int j = 0; j < n; j++) { n       cin >> a; n       v.push_back(a); n     } n     if (v[0] < v[n - 1]) { n       cout <<  YES  << endl; n       cout <<  2  << endl; n       for (int j = 0; j < n; j++) { n         if (j == 0) n           cout << v[j] <<    ; n         else n           cout << v[j]; n       } n       cout << endl; n     } else { n       for (int j = 1; j < n - 1; j++) { n         if (v[j] !=  0 ) { n           x = 1; n           break; n         } n       } n       if (x == 0) n         cout <<  NO  << endl; n       else { n         cout <<  YES  << endl; n         cout <<  2  << endl; n         for (int j = 0; j < n; j++) { n           if (j == 0) n             cout << v[j] <<    ; n           else n             cout << v[j]; n         } n         cout << endl; n       } n     } n     v.clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 100; n const int mod = (int)1e9 + 7; n const int maxlog = (int)20; n const int P = mod; n int n, m, q, id[maxn], sq, res, cnt[maxn][500]; n long long toadd[505], a[maxn]; n long long ans[505]; n vector<int> g[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m >> q; n   sq = 320; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     int k; n     cin >> k; n     if (k >= sq) { n       id[i] = ++res; n     } n     for (; k--;) { n       int x; n       cin >> x; n       g[i].push_back(x); n     } n   } n   for (int i = 1; i <= m; i++) { n     if (id[i]) { n       static bool used[maxn + 5]; n       memset(used, 0, sizeof used); n       for (int x : g[i]) ans[id[i]] += a[x], used[x]++; n       for (int j = 1; j <= m; j++) { n         for (int x : g[j]) { n           cnt[j][id[i]] += used[x]; n         } n       } n     } n   } n   for (; q--;) { n     char s; n     cin >> s; n     if (s ==  ? ) { n       int k; n       cin >> k; n       if (id[k]) { n         cout << ans[id[k]] << endl; n       } else { n         long long Res = 0; n         for (int x : g[k]) Res += a[x]; n         for (int i = 1; i <= res; i++) Res += toadd[i] * cnt[k][i]; n         cout << Res << endl; n       } n     } else { n       int k, val; n       cin >> k >> val; n       if (!id[k]) n         for (int x : g[k]) a[x] += val; n       else n         toadd[id[k]] += val; n       for (int i = 1; i <= res; i++) { n         ans[i] += val * 1ll * cnt[k][i]; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int rlen = 1 << 18 | 1; n inline char gc() { n   static char buf[rlen], *ib, *ob; n   (ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)); n   return ib == ob ? -1 : *ib++; n } n inline int read() { n   int ans = 0; n   char ch = gc(); n   while (!isdigit(ch)) ch = gc(); n   while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc(); n   return ans; n } n const int N = 1e5 + 5; n vector<pair<int, int> > e[N]; n int n, m, dis[N]; n bool vis[N]; n namespace xxj { n int a[31]; n inline void insert(int x) { n   if (!x) return; n   for (register int i = 30; ~i; --i) n     if ((x >> i) & 1) n       if (!a[i]) { n         a[i] = x; n         break; n       } else n         x ^= a[i]; n } n inline int query(int x) { n   for (register int i = 30; ~i; --i) x = min(x, x ^ a[i]); n   return x; n } n }  // namespace xxj n void dfs(int p, int fa) { n   vis[p] = 1; n   for (register int i = 0, v; i < e[p].size(); ++i) { n     if ((v = e[p][i].first) == fa) continue; n     if (!vis[v]) n       dis[v] = dis[p] ^ e[p][i].second, dfs(v, p); n     else n       xxj::insert(dis[v] ^ dis[p] ^ e[p][i].second); n   } n } n int main() { n   n = read(), m = read(); n   for (register int i = 1, u, v, w; i <= m; ++i) n     u = read(), v = read(), w = read(), e[u].push_back(pair<int, int>(v, w)), n     e[v].push_back(pair<int, int>(u, w)); n   dfs(1, 0), cout << xxj::query(dis[n]); n   return 0; n } n