#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n const int N = 2005; n int n, m, d; n string p, q; n int a[N], b[N]; n int dp[N][N][2][2]; n int calc(int id, int rem, int aflag, int bflag) { n   if (id == n) { n     return (rem == 0); n   } n   int& ans = dp[id][rem][aflag][bflag]; n   if (ans != -1) { n     return ans; n   } n   ans = 0; n   if (aflag && bflag) { n     for (int x = 0; x < 10; x++) { n       if ((id % 2 && x != d) || (id % 2 == 0 && x == d)) { n         continue; n       } n       ans = (ans + calc(id + 1, (10 * rem + x) % m, 1, 1)) % M; n     } n   } else if (aflag && bflag == 0) { n     for (int x = 0; x < b[id] + 1; x++) { n       if ((id % 2 && x != d) || (id % 2 == 0 && x == d)) { n         continue; n       } n       ans = (ans + calc(id + 1, (10 * rem + x) % m, 1, x != b[id])) % M; n     } n   } else if (aflag == 0 && bflag) { n     for (int x = a[id]; x < 10; x++) { n       if ((id % 2 && x != d) || (id % 2 == 0 && x == d)) { n         continue; n       } n       ans = (ans + calc(id + 1, (10 * rem + x) % m, x != a[id], 1)) % M; n     } n   } else { n     for (int x = a[id]; x < b[id] + 1; x++) { n       if ((id % 2 && x != d) || (id % 2 == 0 && x == d)) { n         continue; n       } n       ans = n           (ans + calc(id + 1, (10 * rem + x) % m, x != a[id], x != b[id])) % M; n     } n   } n   return ans; n } n void solve() { n   memset(dp, -1, sizeof(dp)); n   cin >> m >> d >> p >> q; n   n = (int)((p).size()); n   for (int i = 0; i < n; i++) { n     a[i] = p[i] -  0 ; n     b[i] = q[i] -  0 ; n   } n   cout << calc(0, 0, 0, 0); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n string a[N]; n int r[N], c[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     for (int i = 0; i <= n; i++) r[i] = 0; n     for (int j = 0; j <= m; j++) c[j] = 0; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         if (a[i][j] ==  . ) r[i]++, c[j]++; n     int ans = 1e9 + 7; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         int cur = r[i] + c[j]; n         if (a[i][j] ==  . ) cur--; n         ans = min(ans, cur); n       } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, ans, mas[2000000], j; n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> mas[i]; n     while (m < mas[i] - mas[j]) j++; n     if (i > j + 1) ans += (i - j - 1) * (i - j) / 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct _2DLL { n   _2DLL *R, *D; n   int val; n   _2DLL() { n     R = D = NULL; n     val = 0; n   } n }; n int n, m, q; n _2DLL *grid[1005][1005]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) grid[i][j] = new _2DLL(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int x; n       scanf( %d , &x); n       grid[i][j]->val = x; n     } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       if (j + 1 <= m) grid[i][j]->R = grid[i][j + 1]; n       if (i + 1 <= n) { n         grid[i][j]->D = grid[i + 1][j]; n       } n     } n   } n   int x1, y1, x2, y2, h, w; n   while (q--) { n     scanf( %d%d%d%d%d%d , &x1, &y1, &x2, &y2, &h, &w); n     { n       _2DLL *tp1 = grid[0][y1 - 1], *tp2 = grid[0][y2 - 1]; n       for (int i = 0; i < x1; tp1 = tp1->D, i++) n         ; n       for (int i = 0; i < x2; tp2 = tp2->D, i++) n         ; n       for (int i = 0; i < h; i++, tp1 = tp1->D, tp2 = tp2->D) n         swap(tp1->R, tp2->R); n     } n     { n       _2DLL *tp1 = grid[0][y1 + w - 1], *tp2 = grid[0][y2 + w - 1]; n       for (int i = 0; i < x1; tp1 = tp1->D, i++) n         ; n       for (int i = 0; i < x2; tp2 = tp2->D, i++) n         ; n       for (int i = 0; i < h; i++, tp1 = tp1->D, tp2 = tp2->D) n         swap(tp1->R, tp2->R); n     } n     { n       _2DLL *lft1 = grid[x1 - 1][0], *lft2 = grid[x2 - 1][0]; n       for (int i = 0; i < y1; lft1 = lft1->R, i++) n         ; n       for (int i = 0; i < y2; lft2 = lft2->R, i++) n         ; n       for (int i = 0; i < w; i++, lft1 = lft1->R, lft2 = lft2->R) n         swap(lft1->D, lft2->D); n     } n     { n       _2DLL *lft1 = grid[x1 + h - 1][0], *lft2 = grid[x2 + h - 1][0]; n       for (int i = 0; i < y1; lft1 = lft1->R, i++) n         ; n       for (int i = 0; i < y2; lft2 = lft2->R, i++) n         ; n       for (int i = 0; i < w; i++, lft1 = lft1->R, lft2 = lft2->R) n         swap(lft1->D, lft2->D); n     } n   } n   for (int i = 1; i <= n; i++) { n     auto x = grid[i][0]; n     for (int j = 0; j < m; j++) { n       x = x->R; n       printf( %d  , x->val); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n long int pos[100001] = {0}, neg[100001] = {0}; n int main() { n   long int n, count = 0, i; n   scanf( %ld , &n); n   long int arr[n]; n   for (i = 0; i < n; i++) { n     scanf( %ld , arr + i); n     if (arr[i] > 0) n       pos[arr[i]]++; n     else if (arr[i] < 0) n       neg[-arr[i]]++; n   } n   for (i = 0; i < 100001; i++) { n     if (pos[i] > 0) count++; n     if (neg[i] > 0) count++; n   } n   printf( %ld , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<pair<int, int>> dec; n   vector<pair<int, int>> inc; n   dec.push_back({1e7, 0}); n   inc.push_back({-1e7, 0}); n   for (int i = 0; i < n; i++) { n     if (a[i] >= dec[dec.size() - 1].first and n         a[i] <= inc[inc.size() - 1].first) { n       cout <<  NO ; n       return 0; n     } else if (a[i] < dec[dec.size() - 1].first and n                a[i] <= inc[inc.size() - 1].first) { n       dec.push_back({a[i], i}); n     } else if (a[i] >= dec[dec.size() - 1].first and n                a[i] > inc[inc.size() - 1].first) { n       inc.push_back({a[i], i}); n     } else { n       if (i < n - 1) { n         if (a[i] > a[i + 1]) { n           dec.push_back({a[i], i}); n         } else { n           inc.push_back({a[i], i}); n         } n       } else { n         inc.push_back({a[i], i}); n       } n     } n   } n   cout <<  YES n ; n   vector<int> ans(n, 1); n   for (int i = 1; i < inc.size(); i++) { n     ans[inc[i].second] = 0; n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, i, j, a; n int c[11]; n int main() { n   cin >> n >> m >> k; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) { n       char x; n       cin >> x; n       c[j] += (x ==  Y ); n     } n   } n   for (i = 1; i <= m; ++i) a += (c[i] >= k); n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int t2[2], t[2]; n vector<int> type; n int main() { n   cin >> s; n   int n = s.length(); n   int c = 1; n   while (c < n && s[c] == s[0]) c++; n   if (c == n) { n     cout <<  0 n ; n     return 0; n   } n   int prev = 0; n   for (int i = 1; i < n; i++) { n     int ind = (c + i) % n; n     if (s[(prev + c) % n] != s[ind]) { n       if (i - prev <= 1) n         type.push_back(0); n       else n         type.push_back(1); n       prev = i; n     } n   } n   if (n - prev <= 1) n     type.push_back(0); n   else n     type.push_back(1); n   c = 1; n   while (c < type.size() && type[c] == type[0]) c++; n   c %= n; n   int ans = 0; n   prev = 0; n   for (int i = 1; i < type.size(); i++) { n     int ind = (c + i) % type.size(); n     if (type[ind] != type[(prev + c) % type.size()]) { n       if (type[(prev + c) % type.size()] == 0) n         ans += (i - prev) / 2; n       else n         ans += i - prev; n       prev = i; n     } n   } n   if (type[(prev + c) % type.size()] == 0) n     ans += (type.size() - prev) / 2; n   else n     ans += type.size() - prev; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void even() { n   puts( YES ); n   printf( %d * %d = %d n , 1, 2, 2); n   printf( %d * %d = %d n , 2, 3, 6); n   printf( %d * %d = %d n , 6, 4, 24); n   for (int i = 5; i + 1 <= n; i += 2) { n     printf( %d - %d = 1 n , i + 1, i); n     printf( 1 * 24 = 24 n ); n   } n } n void odd() { n   puts( YES ); n   printf( 5 - 1 = 4 n ); n   printf( 4 - 2 = 2 n ); n   printf( 4 * 2 = 8 n ); n   printf( 8 * 3 = 24 n ); n   for (int i = 6; i + 1 <= n; i += 2) { n     printf( %d - %d = 1 n , i + 1, i); n     printf( 1 * 24 = 24 n ); n   } n } n int main() { n   cin >> n; n   if (n < 4) { n     puts( NO ); n     return 0; n   } n   if (n % 2 == 0) n     even(); n   else n     odd(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mx[4005], need[4005]; n int n, l[4005], r[4005], a[4005]; n bool inter(int i, int j) { n   if (l[i] >= l[j] && l[i] <= r[j]) return 1; n   if (r[i] >= l[j] && r[i] <= r[j]) return 1; n   if (l[j] >= l[i] && l[j] <= r[i]) return 1; n   if (r[j] >= r[i] && r[j] <= r[i]) return 1; n   return 0; n } n bool check(int d) { n   int i, j, p, s; n   r[0] = (1 << 30); n   for (i = 1; i <= n; i++) need[i] = 0, mx[i] = n; n   need[n] = n; n   for (i = 1; i <= n; i++) { n     for (j = i, s = 0; j <= n; j++) n       if ((s += need[j]) > j - i + 1) return false; n     for (j = i, s = 0; j <= n; j++) n       if ((s += need[j]) == j - i + 1) break; n     s = j; n     for (j = 1, p = 0; j <= n; j++) n       if (mx[j] > 0 && mx[j] <= s && r[j] < r[p]) p = j; n     need[mx[p]]--, mx[p] = -1; n     for (j = 1; j <= n; j++) n       if (inter(p, j) && i + d < mx[j]) { n         need[i + d]++, need[mx[j]]--; n         mx[j] = i + d; n       } n     a[i] = p; n   } n   return 1; n } n int main() { n   int i, ll, rr, mid; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d %d , &l[i], &r[i]); n   ll = 0, rr = n - 1; n   while (rr - ll > 1) { n     mid = (ll + rr) >> 1; n     if (check(mid)) n       rr = mid; n     else n       ll = mid; n   } n   check(rr); n   for (i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3010; n int n; n struct edge { n   int ne, to; n   edge(int N = 0, int T = 0) : ne(N), to(T) {} n } e[MAXN << 1]; n int fir[MAXN], num = 0; n inline void join(int a, int b) { n   e[++num] = edge(fir[a], b); n   fir[a] = num; n } n int pa[MAXN][MAXN], siz[MAXN][MAXN]; n void pre(int u, int fa, const int rt) { n   siz[rt][u] = 1; n   pa[rt][u] = fa; n   for (int i = fir[u]; i; i = e[i].ne) { n     int v = e[i].to; n     if (v == fa) continue; n     pre(v, u, rt); n     siz[rt][u] += siz[rt][v]; n   } n } n long long f[MAXN][MAXN]; n long long dfs(int u, int v) { n   if (u == v) return 0ll; n   if (f[u][v]) return f[u][v]; n   return f[u][v] = max(dfs(u, pa[u][v]), dfs(pa[v][u], v)) + n                    1ll * siz[v][u] * siz[u][v]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     join(u, v); n     join(v, u); n   } n   for (int i = 1; i <= n; i++) pre(i, 0, i); n   long long ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) ans = max(ans, dfs(i, j)); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793238463; n vector<vector<int> > g; n int parent[1009]; n void dfs(int u, int prev) { n   parent[u] = prev; n   for (int v : g[u]) n     if (v != prev) dfs(v, u); n } n bool fme[1009], fhim[1009]; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     g = vector<vector<int> >(n + 9); n     for (int i = 0; i < (n - 1); i++) { n       int u, v; n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     int k1; n     cin >> k1; n     vector<int> me(k1); n     memset((fme), (0), sizeof(fme)); n     for (int i = 0; i < (k1); i++) cin >> me[i], fme[me[i]] = 1; n     int k2; n     cin >> k2; n     vector<int> him(k2); n     memset((fhim), (0), sizeof(fhim)); n     for (int i = 0; i < (k2); i++) cin >> him[i], fhim[him[i]] = 1; n     int root = me[0]; n     memset((parent), (0), sizeof(parent)); n     dfs(root, -1); n     cout <<  B   << him[0] << endl; n     int u; n     cin >> u; n     while (u != -1 && !fme[u]) u = parent[u]; n     if (u == -1) n       cout <<  C -1  << endl; n     else { n       cout <<  A   << u << endl; n       int v; n       cin >> v; n       if (fhim[v]) n         cout <<  C   << u << endl; n       else n         cout <<  C -1  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int INF = 0x3f3f3f3f; n int m, n, c[N], cnt[N]; n vector<int> g[N]; n int bit[N]; n void add(int x, int t) { n   for (; x <= n; x += x & -x) bit[x] += t; n } n int ask(int x) { n   int ret = 0; n   for (; x > 0; x -= x & -x) ret += bit[x]; n   return ret; n } n struct Que { n   int p, c, ret; n } q[N]; n vector<int> geet[N]; n int st[N], ed[N], clk, sz[N], son[N], mat[N]; n void predfs(int u, int f) { n   st[u] = ++clk, mat[clk] = u, son[u] = -1, sz[u] = 1; n   for (int i = 0; i < (int)g[u].size(); ++i) { n     int v = g[u][i]; n     if (v == f) continue; n     predfs(v, u); n     sz[u] += sz[v]; n     if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v; n   } n   ed[u] = clk; n } n void dfs(int u, int f, bool keep) { n   for (int i = 0; i < (int)g[u].size(); ++i) { n     int v = g[u][i]; n     if (v == f || v == son[u]) continue; n     dfs(v, u, 0); n   } n   if (son[u] != -1) dfs(son[u], u, 1); n   ++cnt[c[u]]; n   if (cnt[c[u]] - 1 > 0) add(cnt[c[u]] - 1, -1); n   add(cnt[c[u]], 1); n   for (int i = 0; i < (int)g[u].size(); ++i) { n     int v = g[u][i]; n     if (v == f || v == son[u]) continue; n     for (int j = st[v]; j <= ed[v]; ++j) { n       ++cnt[c[mat[j]]]; n       if (cnt[c[mat[j]]] - 1 > 0) add(cnt[c[mat[j]]] - 1, -1); n       add(cnt[c[mat[j]]], 1); n     } n   } n   for (int i = 0; i < (int)geet[u].size(); ++i) { n     int id = geet[u][i]; n     q[id].ret = ask(n) - ask(min(n, q[id].p - 1)); n   } n   if (!keep) { n     for (int i = st[u]; i <= ed[u]; ++i) { n       add(cnt[c[mat[i]]], -1); n       --cnt[c[mat[i]]]; n       if (cnt[c[mat[i]]]) add(cnt[c[mat[i]]], 1); n     } n   } n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &c[i]); n     } n     for (int i = 0; i < N; ++i) g[i].clear(), geet[i].clear(); n     memset(bit, 0, sizeof(bit)); n     memset(cnt, 0, sizeof(cnt)); n     clk = 0; n     for (int i = 1; i < n; ++i) { n       int u, v; n       scanf( %d%d , &u, &v); n       g[u].push_back(v); n       g[v].push_back(u); n     } n     predfs(1, 0); n     for (int i = 1; i <= m; ++i) { n       int u; n       scanf( %d%d , &u, &q[i].p); n       geet[u].push_back(i); n     } n     dfs(1, 0, 1); n     for (int i = 1; i <= m; ++i) { n       printf( %d n , q[i].ret); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n const long double pi = acosl(-1.); n int T; n const long long mod = 258280327; n long long gcd(long long x, long long y) { n   if (!y) return x; n   return gcd(y, x % y); n } n long long quickfind(long long x, long long y) { n   long long result = 1; n   while (y) { n     if (y & 1) result = result * x % mod; n     y >>= 1; n     x = x * x % mod; n   } n   return result; n } n using namespace std; n int dfn[33333]; n int cnt[33333]; n struct master { n   int x, y, z; n   master(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} n }; n int n; n int size[33333]; n vector<master> cd[33333]; n vector<int> ans; n int q[11111111] = {0}; n int h = 5000000, t = 5000000 - 1; n void solve() { n   for (int i = 1; i <= n; i++) q[++t] = i, size[i] = 4; n   while (h <= t) { n     int x = q[h++]; n     if ((int)cd[x].size() == 0) continue; n     int y = cd[x][(int)cd[x].size() - 1].x; n     int z = cd[x][(int)cd[x].size() - 1].y; n     size[x]--, size[y]++, size[z]++; n     if (size[y] > 9 || size[z] > 9) { n       size[x]++; n       size[y]--; n       size[z]--; n       q[++t] = x; n       continue; n     } n     ans.push_back(cd[x][(int)cd[x].size() - 1].z); n     cd[x].pop_back(); n     q[++t] = x; n   } n } n int main() { n   srand(time(NULL)); n   scanf( %d , &n); n   for (int i = 1; i <= n << 2; i++) { n     int a, b, c; n     scanf( %d , &a); n     scanf( %d , &b); n     scanf( %d , &c); n     cd[a].push_back(master(b, c, i)); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= 16; j++) { n       int x = rand() % 4; n       int y = rand() % 4; n       swap(cd[i][x], cd[i][y]); n     } n   } n   solve(); n   if ((int)ans.size() == n * 4) { n     printf( YES n ); n     printf( %d , ans[0]); n     for (int i = 1; i <= (int)ans.size() - 1; i++) printf(  %d , ans[i]); n     puts(  ); n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n int d[N], head[N], tot; n int to[N * N], nex[N * N], vis[N * N]; n vector<pair<int, int> > ans; n void init() { n   tot = 0; n   memset(d, 0, sizeof(d)); n   memset(head, -1, sizeof(head)); n   ans.clear(); n } n void add(int u, int v, int w) { n   d[v]++; n   to[tot] = v; n   nex[tot] = head[u]; n   vis[tot] = w; n   head[u] = tot++; n } n void dfs(int x) { n   for (int& i = head[x]; ~i;) { n     if (vis[i] == 1) { n       i = nex[i]; n       continue; n     } n     if (vis[i] == 0) ans.push_back(make_pair(x, to[i])); n     vis[i] = vis[i ^ 1] = 1; n     dfs(to[i]); n   } n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     init(); n     int n, m, u, v; n     scanf( %d%d , &n, &m); n     for (int i = 0; i < m; i++) { n       scanf( %d%d , &u, &v); n       add(u, v, 0); n       add(v, u, 0); n     } n     vector<int> tmp; n     int ret = n; n     for (int i = 1; i <= n; i++) n       if (d[i] & 1) tmp.push_back(i), ret--; n     for (int i = 0; i < tmp.size(); i += 2) { n       add(tmp[i], tmp[i + 1], -1); n       add(tmp[i + 1], tmp[i], -1); n     } n     for (int i = 1; i <= n; i++) dfs(i); n     printf( %d n , ret); n     for (int i = 0; i < ans.size(); i++) n       printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[(int)2e5][2]; n int main() { n   cin >> n; n   if (n == 3) { n     cout <<  1 2 3  << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i][0] >> a[i][1]; n     a[i][0]--; n     a[i][1]--; n   } n   int next = 0; n   for (int i = 0; i < n; i++) { n     cout << (next + 1) <<    ; n     int b = a[next][0]; n     int c = a[next][1]; n     if (a[b][0] == c || a[b][1] == c) { n       next = b; n     } else { n       next = c; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 998244353; n struct mat { n   long long a[2][2]; n }; n mat mul(mat a, mat b) { n   mat c; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 2; j++) { n       c.a[i][j] = 0; n       for (int k = 0; k < 2; k++) c.a[i][j] += a.a[i][k] * b.a[k][j]; n       c.a[i][j] %= N; n     } n   } n   return c; n } n mat make_mat(long long x, long long y) { n   mat ret; n   ret.a[0][0] = x; n   ret.a[1][0] = y; n   ret.a[0][1] = 1; n   ret.a[1][1] = 0; n   return ret; n } n mat val[2000005]; n void pushup(int x) { val[x] = mul(val[(x << 1)], val[((x << 1) | 1)]); } n void modify(int x, int l, int r, int pos, mat v) { n   if (l == r) { n     val[x] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) modify((x << 1), l, mid, pos, v); n   if (pos > mid) modify(((x << 1) | 1), mid + 1, r, pos, v); n   pushup(x); n } n int n, m, x, y, d[500005]; n string s; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   cin >> s; n   reverse(s.begin(), s.end()); n   s =     + s; n   for (int i = 1; i <= n; i++) { n     d[i] = s[i] -  0 ; n     modify(1, 1, n, i, n            make_mat(d[i] + 1, (i == 1 ? 0 : (d[i] == 1) * (9 - d[i - 1])))); n   } n   while (m--) { n     cin >> x >> y; n     x = n - x + 1; n     d[x] = y; n     modify(1, 1, n, x, n            make_mat(d[x] + 1, (x == 1 ? 0 : (d[x] == 1) * (9 - d[x - 1])))); n     if (x < n) n       modify(1, 1, n, x + 1, n              make_mat(d[x + 1] + 1, (d[x + 1] == 1) * (9 - d[x]))); n     cout << val[1].a[0][0] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 4e18; n const int MAX_LOG = 21; n const int MAX_N = (1 << 17); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k, a, b, c, d; n   cin >> n >> k >> a >> b >> c >> d; n   if (n < 5 || k < n + 1) { n     cout << -1 << endl; n   } else { n     int arr[n]; n     arr[0] = a, arr[n - 1] = b, arr[1] = c, arr[n - 2] = d; n     vector<bool> taken(n + 1); n     taken[a] = taken[b] = taken[c] = taken[d] = true; n     int id = 1; n     for (int i = 2; i < n - 2; i++) { n       while (taken[id]) { n         id++; n       } n       arr[i] = id; n       taken[id] = true; n     } n     for (int i = 0; i < n; i++) { n       cout << arr[i] <<    ; n     } n     cout << endl; n     vector<int> v; n     v.push_back(c); n     v.push_back(a); n     v.push_back(arr[2]); n     for (int i = 3; i <= n - 3; i++) { n       v.push_back(arr[i]); n     } n     v.push_back(b); n     v.push_back(d); n     for (int x : v) { n       cout << x <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int x, int y) { return x > y; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int tt = 1; n   long long m, n, k; n   while (tt--) { n     cin >> n >> k; n     vector<int> vec(n); n     for (long long i = 0; i < n; i++) { n       cin >> vec[i]; n     }; n     sort(vec.begin(), vec.end()); n     set<int> s; n     for (long long i = 0; i < n; i++) { n       s.insert(vec[i]); n     } n     if (s.size() != n) { n       cout << 0 << endl; n     } else { n       for (long long i = 0; i < n; i++) { n         int no = vec[i] & k; n         if (no == vec[i]) n           ; n         if (no == vec[i] and n             binary_search(vec.begin() + i + 1, vec.end(), no)) { n           cout << 1 << endl; n           return 0; n         } else if (no != vec[i] and binary_search(vec.begin(), vec.end(), no)) { n           cout << 1 << endl; n           return 0; n         } n       } n       s.clear(); n       for (long long i = 0; i < n; i++) { n         s.insert(vec[i] & k); n       } n       if (s.size() != n) n         cout << 2 << endl; n       else n         cout << -1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], res = 1e9, A, B, x[100005], kq[100005], kq1[100005], dem = 0, n                   e[100005]; n void no(int val) { n   if (a[n] >= 0 || a[n - 1] >= 0 || res <= val) return; n   res = val; n   for (int i = 2; i <= n - 1; i++) kq[i] = x[i]; n   return; n } n void xuli(int i, int val) { n   if (val >= res) return; n   if (i == 2 || i == n - 1 || 1 == 1) { n     for (int j = 0; j <= 16; j++) { n       bool ok = true; n       x[i] = j; n       a[i - 1] -= B * j; n       a[i] -= A * j; n       a[i + 1] -= B * j; n       if (a[i - 1] >= 0) ok = false; n       if (ok == true) { n         if (i == n - 1) n           no(val + j); n         else n           xuli(i + 1, val + j); n       } n       a[i - 1] += B * j; n       a[i] += A * j; n       a[i + 1] += B * j; n     } n   } else { n     int tg = max(e[i] / A + 1, max(e[i - 1] / B + 1, e[i + 1] / B + 1)); n     for (int j = 0; j <= tg; j++) { n       bool ok = true; n       x[i] = j; n       a[i - 1] -= B * j; n       a[i] -= A * j; n       a[i + 1] -= B * j; n       if (a[i - 1] >= 0) ok = false; n       if (ok == true) { n         if (i == n - 1) n           no(val + j); n         else n           xuli(i + 1, val + j); n       } n       a[i - 1] += B * j; n       a[i] += A * j; n       a[i + 1] += B * j; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> A >> B; n   for (int i = 1; i <= n; i++) cin >> a[i], e[i] = a[i]; n   xuli(2, 0); n   cout << res <<   n ; n   for (int i = 2; i <= n - 1; i++) n     for (int j = 1; j <= kq[i]; j++) kq1[++dem] = i; n   for (int i = 1; i <= dem; i++) cout << kq1[i] <<    ; 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(NULL); n   long long int l, r; n   cin >> l >> r; n   vector<long long int> v, ans; n   long long int x = 1; n   while (x <= r) { n     v.push_back(x); n     x *= 2; n   } n   x = 1; n   while (x <= r) { n     for (auto i : v) { n       long long int y = x * i; n       if (y > r) break; n       ans.push_back(y); n     } n     x *= 3; n   } n   sort(ans.begin(), ans.end()); n   long long int n = 0; n   for (auto i : ans) { n     if (i >= l && i <= r) n++; n   } n   cout << n; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005]; n int main(void) { n   int n; n   stack<int> s; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = n; i >= 1; i--) { n     if (s.empty()) { n       s.push(a[i]); n       b[i] = 0; n       continue; n     } n     if (a[i] < s.top()) { n       s.push(a[i]); n       b[a[i]] = 0; n     } else if (a[i] > s.top()) { n       int temp = max(1, b[s.top()]); n       s.pop(); n       while (!s.empty() && a[i] > s.top()) { n         temp = max(temp + 1, b[s.top()]); n         s.pop(); n       } n       b[a[i]] = temp; n       s.push(a[i]); n     } n   } n   int k = 0; n   for (int i = 1; i <= n; i++) k = max(k, b[i]); n   printf( %d n , k); n } n


#include <bits/stdc++.h> n int dp[200100]; n int X[200100]; n int Y[200100]; n int T[200100]; n int MX[200100]; n int main(void) { n   int n, r; n   scanf( %d %d , &r, &n); n   for (int i = 1; i <= n; i++) dp[i] = MX[i] = -200100; n   MX[0] = X[0] = Y[0] = dp[0] = T[0] = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d , &T[i], &X[i], &Y[i]); n     X[i]--; n     Y[i]--; n     for (int j = i - 1; j >= 0; j--) { n       if (T[i] - T[j] > r * 2) { n         dp[i] = std::max(dp[i], MX[j] + 1); n         break; n       }; n       int xdiff = std::abs(X[i] - X[j]); n       int ydiff = std::abs(Y[i] - Y[j]); n       if (xdiff + ydiff <= T[i] - T[j]) { n         dp[i] = std::max(dp[i], dp[j] + 1); n       }; n     }; n     MX[i] = std::max(MX[i - 1], dp[i]); n   }; n   printf( %d n , MX[n]); n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int u, v; n }; n node p[200010]; n int du[200010], n, m; n int _first[200010], _next[200010], _to[200010], cnt; n int add(int a, int b) { n   _next[++cnt] = _first[a]; n   _first[a] = cnt; n   _to[cnt] = b; n   du[b]++; n } n bool check(int x) { n   cnt = 0; n   bool ok = 1; n   int now; n   for (int i = 1; i <= n; i++) du[i] = 0, _first[i] = 0; n   for (int i = 1; i <= x; i++) add(p[i].u, p[i].v); n   for (int i = 1; i <= n; i++) { n     if (!ok && !du[i]) return false; n     if (!du[i]) now = i, ok = 0; n   } n   if (ok) return false; n   while (_first[now]) { n     ok = 1; n     for (int i = _first[now]; i; i = _next[i]) { n       du[_to[i]]--; n       if (!ok && !du[_to[i]]) return false; n       if (!du[_to[i]]) now = _to[i], ok = 0; n     } n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &p[i].u, &p[i].v); n     if (p[i].u == 0 || p[i].v == 0) return 0; n   } n   int l = 1, r = m, ans = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m; n   scanf( %lld , &n); n   scanf( %lld , &m); n   long long int i, j; n   string s; n   cin >> s; n   long long int arr[400], brr[400]; n   for (i = 1; i <= 300; i++) { n     arr[i] = i; n     brr[i] = i; n   } n   while (m--) { n     char a, b; n     cin >> a >> b; n     long long int ind1 = brr[a]; n     long long int ind2 = brr[b]; n     arr[ind1] = b; n     arr[ind2] = a; n     brr[b] = ind1; n     brr[a] = ind2; n   } n   for (i = 0; i < (long long int)s.size(); i++) { n     s[i] = (char)arr[s[i]]; n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n vector<long long> p; n int ho, mi, se; n void fix_time() { n   if (se > 59) mi += se / 60, se = se % 60; n   if (se < 0) se += 60, mi--; n   if (mi > 59) ho += mi / 60, mi = mi % 60; n   if (mi < 0) mi += 60, ho--; n   if (ho < 0) ho += 24; n   if (ho > 24) ho -= 24; n } n long long my_pow(int x, int y) { n   long long num = 1; n   for (long long i = x; y > 0; i = (i * i), y >>= 1) n     if (y & 1) num = (num * i); n   return num; n } n bool prime[1000001], prime2[1000001]; n void sieve() { n   for (int i = 2; i <= 1000000; i += 2) prime[i] = false, prime[i - 1] = true; n   prime[2] = true; n   for (int i = 3; i * i <= 1000000; i += 2) n     if (prime[i]) n       for (int j = i * i; j <= 1000000; j += 2 * i) prime[j] = false; n } n vector<int> vv; n int cnt = 0; n void pfact(long long x) { n   if (x % 2 == 0) { n     p.push_back(2); n     while (x % 2 == 0) x /= 2, cnt++; n     vv.push_back(cnt); n   } n   for (long long i = 3; i * i <= x; i += 2) n     if (x % i == 0) { n       cnt = 0; n       p.push_back(i); n       while (x % i == 0) x /= i, cnt++; n       vv.push_back(cnt); n     } n   if (x > 1) p.push_back(x), cnt++, vv.push_back(1); n } n int gcd(int a, int b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n int m, x, n, y, z, k, mx, one, gd, x2, y2, two, mn = 1e9, mid, l, r, pos, n                                                 a[500003], b[500003], c[500000]; n string s2; n vector<int> v, v2[100003]; n long long sum, ans, num; n char ch; n map<string, int> mp1; n set<int> myset; n string s, s1; n int tmp; n bool yes, lft, rt; n multiset<string> lfts[100001], rts[100001]; n int t, sum2, y3, z3; n bool used; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> n; n     bool res[n]; n     memset(res, 0, n); n     vector<pair<pair<int, int>, int> > myvec; n     for (int i = 0; i < n; i++) { n       cin >> x >> y; n       myvec.push_back({{y, x}, i}); n     } n     sort(myvec.begin(), myvec.end()); n     int ind = myvec[0].second; n     int beg = myvec[0].first.second; n     int en = myvec[0].first.first; n     res[ind] = true; n     vector<int> shits; n     for (int i = 1; i < n; i++) { n       m = i; n       int f = myvec[i].first.first; n       int se = myvec[i].first.second; n       ind = myvec[i].second; n       if (en >= se) { n         if (beg > se) beg = se; n         en = f; n         shits.push_back(ind); n         for (int j = 0; j < shits.size(); j++) res[shits[j]] = true; n         shits.clear(); n       } else n         shits.push_back(ind); n     } n     yes = false; n     for (int i = 0; i < n; i++) n       if (!res[i]) yes = true; n     if (yes) n       for (int i = 0; i < n; i++) n         if (res[i]) n           cout <<  1  ; n         else n           cout <<  2  ; n     else n       cout << -1; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u, v, d, id; n   long long cnt; n   bool operator<(const struct Edge& rhs) const { return d < rhs.d; } n } edg[100001]; n int vert[100001]; n long long sz[100001]; n int find(int i) { n   if (vert[i] == i) return i; n   return vert[i] = find(vert[i]); n } n void Union(int x, int y) { n   int xset = find(x); n   int yset = find(y); n   if (xset != yset) { n     vert[xset] = yset; n     long long total = sz[xset] + sz[yset]; n     sz[xset] = sz[yset] = total; n   } n } n int n; n int tmp_id[100001]; n vector<pair<int, int> > gr[100001]; n long long total_size[100001]; n bool vis[100001]; n long long ans[100001]; n long long dfs_cnt(int cur, int id) { n   vis[cur] = true; n   tmp_id[cur] = id; n   long long res = sz[cur]; n   for (int i = 0; i < gr[cur].size(); i++) { n     int nxt = gr[cur][i].first; n     if (vis[nxt]) continue; n     res += dfs_cnt(nxt, id); n   } n   return res; n } n long long dfs(int cur) { n   vis[cur] = true; n   long long res = sz[cur]; n   for (int i = 0; i < gr[cur].size(); i++) { n     int nxt = gr[cur][i].first, edg_id = gr[cur][i].second; n     if (vis[nxt]) continue; n     long long sub_size = dfs(nxt); n     ans[edg_id] += sub_size * (total_size[tmp_id[cur]] - sub_size) * 2; n     res += sub_size; n   } n   return res; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v, d; n     cin >> u >> v >> d; n     struct Edge tmp = {u, v, d, i + 1, 0}; n     edg[i] = tmp; n   } n   for (int i = 1; i <= n; i++) { n     vert[i] = i; n     sz[i] = 1; n   } n   sort(edg, edg + n - 1); n   int st = 0; n   for (int i = 0; i < n - 1; i++) { n     if (i == n - 1 || edg[i + 1].d != edg[i].d) { n       vector<int> cln; n       cln.clear(); n       for (int j = st; j <= i; j++) { n         int u = edg[j].u, v = edg[j].v, id = edg[j].id; n         int cmp1 = find(u), cmp2 = find(v); n         vis[cmp1] = false; n         vis[cmp2] = false; n         gr[cmp1].push_back(make_pair(cmp2, id)); n         gr[cmp2].push_back(make_pair(cmp1, id)); n         cln.push_back(cmp1); n         cln.push_back(cmp2); n       } n       int id = 0; n       for (int j = 0; j < cln.size(); j++) { n         if (vis[cln[j]]) continue; n         total_size[id] = dfs_cnt(cln[j], id); n         id++; n       } n       for (int j = 0; j < cln.size(); j++) { n         vis[cln[j]] = false; n       } n       for (int j = 0; j < cln.size(); j++) { n         if (vis[cln[j]]) continue; n         dfs(cln[j]); n       } n       for (int j = 0; j < cln.size(); j++) { n         vis[cln[j]] = false; n         gr[cln[j]].clear(); n       } n       for (int j = st; j <= i; j++) { n         int u = edg[j].u, v = edg[j].v; n         int cmp1 = find(u), cmp2 = find(v); n         Union(cmp1, cmp2); n       } n       st = i + 1; n     } n   } n   long long mx = -1; n   for (int i = 1; i <= n - 1; i++) { n     mx = max(mx, ans[i]); n   } n   vector<int> buff; n   for (int i = 1; i <= n - 1; i++) { n     if (ans[i] == mx) buff.push_back(i); n   } n   cout << mx <<     << buff.size() << endl; n   for (int i = 0; i < buff.size(); i++) { n     cout << buff[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<16 * 2002> dp[1 << 16]; n int n, k; n int a[16]; n int weight[16]; n int calc(int &x) { n   int c = 0; n   while (x % k == 0) x /= k, c++; n   return c; n } n int _calc(int x) { n   int c = 0; n   while (x % k == 0) x /= k, c++; n   return c; n } n void dfs(int s, int x) { n   if (__builtin_popcount(s) == 1) return; n   int y = x, z = x; n   while (y < 16 * 2002) { n     if (dp[s][y]) z = y; n     y = y * k; n   } n   if (z != x) { n     for (int i = 0; i < n; i++) n       if ((1 << i) & s) weight[i] += _calc(z / x); n     dfs(s, z); n   } else { n     for (int i = 0; i < n; i++) n       if ((1 << i) & s and x >= a[i] and dp[s ^ (1 << i)][x - a[i]]) { n         dfs(s ^ (1 << i), x - a[i]); n         break; n       } n   } n } n signed main() { n   dp[0][0] = 1; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) n       if (i & (1 << j)) dp[i] |= (dp[i - (1 << j)] << a[j]); n     for (int j = 16 * 2000 / k; j >= 0; j--) n       if (dp[i][j * k]) dp[i][j] = 1; n   } n   if (!dp[(1 << n) - 1][1]) return puts( NO ) * 0; n   puts( YES ); n   dfs((1 << n) - 1, 1); n   int cnt = n - 1; n   while (cnt--) { n     int f1 = -1, f2 = -1; n     for (int i = 0; i < n; i++) { n       if (f1 == -1 or weight[i] > weight[f1]) n         f2 = f1, f1 = i; n       else if (f2 == -1 or weight[i] > weight[f2]) n         f2 = i; n     } n     printf( %d %d n , a[f1], a[f2]); n     a[f1] += a[f2]; n     weight[f2] = -1e9; n     weight[f1] -= calc(a[f1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m, i, a[5]; n   cin >> n; n   for (i = 0; i < 5; i++) { n     a[i] = n % 10; n     n /= 10; n   } n   n = a[3] + 10 * a[1] + 100 * a[0] + 1000 * a[2] + 10000 * a[4]; n   m = n; n   m = n * n; n   m %= 100000; n   m = m * n; n   m %= 100000; n   m = m * n; n   m %= 100000; n   m = m * n; n   m %= 100000; n   if (m < 10000) { n     cout <<  0 ; n   } n   if (m < 1000) { n     cout <<  0 ; n   } n   if (m < 100) { n     cout <<  0 ; n   } n   if (m < 10) { n     cout <<  0 ; n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 5]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     int ans = 1; n     for (int i = 1; i <= 26; i++) { n       int x = 1; n       while (true) { n         int count = 0; n         int j, k; n         for (j = 0; j < n; j++) { n           if (a[j] == i) count++; n           if (count == x) break; n         } n         int count1 = 0; n         for (k = n - 1; k >= 0; k--) { n           if (a[k] == i) count1++; n           if (count1 == x) break; n         } n         if (j == n || k == -1) break; n         if (j >= k) break; n         vector<int> freq; n         for (int l = 0; l <= 27; l++) freq.push_back(0); n         for (int l = j + 1; l < k; l++) freq[a[l]]++; n         int y = 0; n         for (int l = 0; l <= 26; l++) y = max(y, freq[l]); n         ans = max(ans, 2 * x + y); n         x++; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1000000 + 10; n long long d[maxn]; n long long ans = 0; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long v, u; n     cin >> v >> u; n     d[u]++; n     d[v]++; n   } n   for (long long i = 1; i <= n; i++) ans += d[i] * (n - 1 - d[i]); n   cout << (n * (n - 1) * (n - 2)) / 6 - (ans / 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n int n; n char S[300000]; n struct edge { n   int to, nxt; n } E[500000]; n int H[500000], tot; n inline void add_edge(int a, int b) { E[++tot] = (edge){b, H[a]}, H[a] = tot; } n struct SegmentTree { n   struct node { n     node *L, *R; n     node() { L = R = NULL; } n   }; n   node *root; n   void insert(node *&now, int l, int r, int k) { n     if (!now) now = new node; n     if (l == r) return; n     int mdl = (l + r) >> 1; n     if (k <= mdl) n       insert(now->L, l, mdl, k); n     else n       insert(now->R, mdl + 1, r, k); n   } n   bool query(node *now, int l, int r, int L, int R) { n     if (l > R || r < L || !now) return false; n     if (L <= l && r <= R) return true; n     int mdl = (l + r) >> 1; n     return query(now->L, l, mdl, L, R) | query(now->R, mdl + 1, r, L, R); n   } n   SegmentTree() { root = NULL; } n }; n SegmentTree::node *merge(SegmentTree::node *&A, SegmentTree::node *&B) { n   if (!(A && B)) return A ? A : B; n   SegmentTree::node *nw = new SegmentTree::node; n   nw->L = merge(A->L, B->L), nw->R = merge(A->R, B->R); n   return nw; n } n struct SAM { n   struct node { n     int son[26], len, link, R; n     SegmentTree S; n     node() { n       memset(son, 0xff, sizeof(int) * 26); n       len = link = R = 0; n     } n   } T[500000]; n   int tot, lst; n   void insert(char C) { n     C -=  a ; n     int q = ++tot, p = lst; n     T[q].len = T[p].len + 1, n     T[q].S.insert(T[q].S.root, 1, n, T[q].R = T[q].len); n     lst = q; n     while (~p && !~T[p].son[C]) T[p].son[C] = q, p = T[p].link; n     if (!~p) return; n     int np = T[p].son[C]; n     if (T[p].len + 1 == T[np].len) n       T[q].link = np; n     else { n       int nq = ++tot; n       memcpy(T[nq].son, T[np].son, sizeof(int) * 26); n       T[nq].link = T[np].link; n       T[nq].len = T[p].len + 1; n       T[np].link = T[q].link = nq; n       while (~p && T[p].son[C] == np) T[p].son[C] = nq, p = T[p].link; n     } n   } n   SAM() { tot = lst = 0, T[0].link = -1; } n } G; n void pre(int now) { n   for (int i = H[now]; i; i = E[i].nxt) { n     pre(E[i].to); n     G.T[now].R = std::max(G.T[now].R, G.T[E[i].to].R); n     G.T[now].S.root = merge(G.T[now].S.root, G.T[E[i].to].S.root); n   } n } n int Ans; n void solve(int now, int ans, int top) { n   Ans = std::max(Ans, ans); n   for (int i = H[now]; i; i = E[i].nxt) { n     if (G.T[top].S.query(G.T[top].S.root, 1, n, n                          G.T[E[i].to].R - G.T[E[i].to].len + G.T[top].len, n                          G.T[E[i].to].R - 1)) n       solve(E[i].to, ans + 1, E[i].to); n     else n       solve(E[i].to, ans, top); n   } n } n int main() { n   scanf( %d%s , &n, S + 1); n   if (n == 1) return puts( 1 ), 0; n   for (int i = 1; i <= n; i++) G.insert(S[i]); n   for (int i = 1; i <= G.tot; i++) add_edge(G.T[i].link, i); n   pre(0), solve(0, 0, 0); n   printf( %d n , Ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 45; n int a[maxn], b[maxn]; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n int N; n struct Martix { n   long long arr[maxn][maxn]; n   void init() { memset(arr, 0x3f, sizeof(arr)); } n   void to() { n     this->init(); n     for (int i = 0; i < N; i++) { n       arr[i][i] = 0; n     } n   } n   void out() { n     for (int i = 0; i < N; i++) { n       for (int j = 0; j < N; j++) { n         long long v = arr[i][j] == INFF ? -1 : arr[i][j]; n         cout << v <<    ; n       } n       cout << endl; n     } n     puts( -- ); n   } n }; n Martix operator*(const Martix& a, const Martix& b) { n   Martix ret; n   ret.init(); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       for (int k = 0; k < N; k++) { n         ret.arr[i][j] = min(ret.arr[i][j], a.arr[i][k] + b.arr[k][j]); n       } n     } n   } n   return ret; n } n long long dp[maxn][maxn][maxn]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) scanf( %d , &b[i]); n   memset(dp, 0x3f, sizeof(dp)); n   N = n + 1; n   for (int i = 0; i < N; i++) { n     dp[i][i][0] = 0; n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 0; i < N; i++) { n       for (int j = 0; j < N; j++) { n         if (j - 1 >= 0) n           dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + a[k - 1]); n         if (j + 1 < N) n           dp[i][j][k] = min(dp[i][j][k], dp[i][j + 1][k - 1] + b[k - 1]); n       } n     } n   } n   Martix x; n   x.init(); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       x.arr[i][j] = dp[i][j][n]; n     } n   } n   Martix ret; n   ret.to(); n   while (m) { n     if (m & 1) ret = ret * x; n     x = x * x; n     m >>= 1; n   } n   printf( %I64d n , ret.arr[0][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7LL; n const int MAXN = 212345; n int vis[MAXN]; n vector<int> g[MAXN]; n long long dfs(int u) { n   vis[u] = 1; n   long long r = 1; n   for (int v : g[u]) n     if (!vis[v]) r += dfs(v); n   return r; n } n long long fexp(long long b, long long e) { n   if (b == 1 || e == 0) return 1; n   if (b == 0) return 0; n   long long resp = fexp(b, e / 2); n   resp = ((resp % MOD) * (resp % MOD)) % MOD; n   if (e & 1) resp = ((resp % MOD) * (b % MOD)) % MOD; n   return resp % MOD; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 0, u, v, w; i < n - 1; ++i) { n     cin >> u >> v >> w; n     if (!w) { n       g[u].push_back(v); n       g[v].push_back(u); n     } n   } n   long long r = fexp(n, k); n   for (int i = 1; i <= n; ++i) { n     if (!vis[i]) { n       r = (r % MOD - fexp(dfs(i), k) % MOD) % MOD; n       if (r < 0) r += MOD; n       r %= MOD; n     } n   } n   cout << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long base, long long exponent, long long mod) { n   long long ans = 1; n   while (exponent) { n     if (exponent & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     exponent /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n const int upperlimit = 71; n const int mod = 998244353; n long long fact[upperlimit]; n long long invfact[upperlimit]; n vector<long long> pdv; n void func(long long n) { n   for (int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) pdv.push_back(i); n     while (n % i == 0) n /= i; n   } n   if (n > 1) pdv.push_back(n); n } n int pw(long long n, long long i) { n   int ans = 0; n   while (n % i == 0) { n     ans++; n     n /= i; n   } n   return ans; n } n int main() { n   fact[0] = 1; n   invfact[0] = 1; n   for (int i = 1; i < upperlimit; i++) { n     fact[i] = fact[i - 1]; n     fact[i] *= i; n     fact[i] %= mod; n     invfact[i] = powmod(fact[i], mod - 2, mod); n   } n   long long d, u, v; n   int q; n   long long temp = 1; n   int lol = 0; n   scanf( %lld , &d); n   func(d); n   scanf( %d , &q); n   while (q--) { n     scanf( %lld , &u); n     scanf( %lld , &v); n     long long gc = gcd(u, v); n     temp = 1; n     lol = 0; n     for (int i = 0; i < pdv.size(); i++) { n       int gg = abs(pw(gc, pdv[i]) - pw(u, pdv[i])); n       temp *= invfact[gg]; n       temp %= mod; n       lol += gg; n     } n     temp *= fact[lol]; n     temp %= mod; n     lol = 0; n     for (int i = 0; i < pdv.size(); i++) { n       int gg = abs(pw(gc, pdv[i]) - pw(v, pdv[i])); n       temp *= invfact[gg]; n       temp %= mod; n       lol += gg; n     } n     temp *= fact[lol]; n     temp %= mod; n     printf( %lld n , temp); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[2513][2513]; n bitset<2513> big[2513]; n vector<int> comp; n vector<pair<int, int> > need[2513 * 2513]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       scanf( %d , &a[i][j]); n       if (i == j && a[i][j] != 0) return printf( NOT MAGIC n ), 0; n       if (i > j && a[i][j] != a[j][i]) return printf( NOT MAGIC n ), 0; n       comp.push_back(a[i][j]); n     } n   sort(comp.begin(), comp.end()); n   comp.erase(unique(comp.begin(), comp.end()), comp.end()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       need[lower_bound(comp.begin(), comp.end(), a[i][j]) - comp.begin()] n           .push_back(make_pair(i, j)); n     } n   for (int i = 0; i < n * n; i++) { n     auto& v = need[i]; n     for (auto& p : v) { n       if ((big[p.first] & big[p.second]).any()) return printf( NOT MAGIC n ), 0; n     } n     for (auto& p : v) big[p.first][p.second] = 1; n   } n   printf( MAGIC n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(int n) { n   string x =   ; n   if (n >= 5) { n     x +=  -O| ; n     n -= 5; n   } else { n     x +=  O-| ; n   } n   while (n--) { n     x +=  O ; n   } n   x +=  - ; n   while (x.size() < 8) x +=  O ; n   cout << x << endl; n } n int main() { n   int n; n   cin >> n; n   if (n == 0) cout <<  O-|-OOOO ; n   while (n) { n     print(n % 10); n     n /= 10; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<vector<long long>> in(n); n   vector<long long> deg(n); n   long long a, b; n   for (long long i = 0; i < m; ++i) { n     cin >> a >> b; n     --a; n     --b; n     if (a > b) { n       swap(a, b); n     } n     ++deg[a]; n     ++deg[b]; n     in[a].push_back(b); n   } n   long long answ = 0; n   for (long long i = 0; i < n; ++i) { n     answ += ((deg[i] - in[i].size()) * in[i].size()); n   } n   cout << answ <<   n ; n   long long q; n   cin >> q; n   long long x; n   for (long long i = 0; i < q; ++i) { n     cin >> x; n     --x; n     answ -= ((deg[x] - in[x].size()) * in[x].size()); n     for (long long c : in[x]) { n       answ -= ((deg[c] - in[c].size()) * in[c].size()); n       in[c].push_back(x); n       answ += ((deg[c] - in[c].size()) * in[c].size()); n     } n     in[x].clear(); n     cout << answ <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cou = 0, ans = 0, mx = INT_MIN, mn = INT_MAX, sum = 0, modu = 1e9 + 7; n vector<vector<int>> from, to; n vector<bool> visited(1e3, false); n void dfs(int t) { n   visited[t] = true; n   for (int c = 0; c < from[t].size(); c++) { n     if (!visited[from[t][c]]) dfs(from[t][c]); n   } n } n void d(int t) { n   dfs(t); n   for (int z = 0; z < to[t].size(); z++) { n     if (!visited[to[t][z]]) d(to[t][z]); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   cin >> t; n   vector<pair<int, int>> v(t); n   for (long long i = 0; i < t; i++) cin >> v[i].first >> v[i].second; n   sort(v.begin(), v.end()); n   if (t < 3) { n     cout <<  YES ; n     return 0; n   } n   int n1 = v[0].second, n2 = v[1].second; n   for (int x = 2; x < t; x++) { n     if (n1 >= v[x].first && n2 >= v[x].first) { n       cout <<  NO ; n       return 0; n     } else { n       if (n1 < n2) n         n1 = v[x].second; n       else n         n2 = v[x].second; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[2048][2048]; n int flagr[2048], flagc[2048]; n int n, ans = 0; n void print() { n   cout << endl; n   cout <<  ==========  << endl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) cout << a[i][j]; n     cout << endl; n   } n   cout << endl; n } n void solve() { n   for (int i = 0; i < n; i++) { n     for (int j = n - 1; j > i; j--) { n       if ((a[i][j] -  0  + flagr[i] + flagc[j]) & 1) { n         a[i][j] =  0 ; n         flagc[j]++, flagr[i]++, ans++; n       } n       a[i][j] =  0 ; n     } n   } n   for (int i = 0; i < n; i++) { n     a[i][i] = (a[i][i] -  0  + flagr[i] + flagc[i]) % 2 + 48; n   } n   memset(flagr, 0, sizeof(flagr)); n   memset(flagc, 0, sizeof(flagc)); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < i; j++) { n       if ((a[i][j] -  0  + flagr[i] + flagc[j]) & 1) { n         flagc[j]++, flagr[i]++, ans++; n       } n       a[i][j] =  0 ; n     } n   } n   for (int i = 0; i < n; i++) n     if ((a[i][i] -  0  + flagr[i] + flagc[i]) % 2) ans++; n } n int main() { n   int i, j, k, l; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , a[i]); n   } n   solve(); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y; n string h; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> h >> x >> y; n     if (x >= 2400 and y > x) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d; n vector<int> e[100000 + 5], lst[100000 + 5]; n int dfn[100000 + 5], low[100000 + 5], cnt, tot, sta[100000 + 5], ptr, n     scc[100000 + 5], cycle[100000 + 5]; n char open[100000 + 5][55]; n void tarjan(int now) { n   dfn[now] = low[now] = ++cnt; n   sta[++ptr] = now; n   for (auto v : e[now]) { n     if (!dfn[v]) { n       tarjan(v); n       low[now] = min(low[now], low[v]); n     } else if (!scc[v]) n       low[now] = min(low[now], dfn[v]); n   } n   if (low[now] == dfn[now]) { n     tot++; n     while (sta[ptr] != now) scc[sta[ptr--]] = tot; n     scc[sta[ptr--]] = tot; n   } n   return; n } n int gcd(int x, int y) { return x ? gcd(y % x, x) : y; } n int _gcd[55]; n bool can[100000 + 5][55]; n int off[100000 + 5]; n pair<int, int> q[100000 * 50 + 5]; n void bfs(int s, int id) { n   int rear = 0; n   q[rear].first = s; n   q[rear].second = 0; n   can[s][0] = 1; n   for (int front = 0; front <= rear; front++) { n     int now = q[front].first; n     int day = q[front].second; n     int nd = (day + 1) % d; n     for (auto v : e[now]) n       if (scc[v] == id) { n         if (can[v][nd] == 0) { n           can[v][nd] = 1; n           rear++; n           q[rear].first = v; n           q[rear].second = nd; n         } n       } n   } n   cycle[id] = d; n   for (int i = 1; i <= d - 1; i++) n     if (can[s][i]) { n       cycle[id] = i; n       break; n     } n   assert(d % cycle[id] == 0); n } n int dp[100000 + 5][55], dpscc[100000 + 5][55]; n void upd(int &x, int y) { n   if (y > x) x = y; n } n int main() { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n   } n   for (int i = 1; i <= n; i++) scanf( %s , open[i]); n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i); n   for (int i = 1; i <= n; i++) lst[scc[i]].push_back(i); n   for (int i = 1; i <= tot; i++) bfs(lst[i][0], i); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= d - 1; j++) n       if (can[i][j]) { n         off[i] = j; n         break; n       } n   } n   memset(dp, -1, sizeof dp); n   memset(dpscc, -1, sizeof dpscc); n   int ans = 0; n   dpscc[scc[1]][0] = 0; n   for (int id = tot; id >= 1; id--) { n     int c = cycle[id]; n     int cnt[55] = {}; n     for (auto now : lst[id]) { n       bool tmp[55] = {}; n       for (int i = 0; i <= d - 1; i++) n         if (open[now][i] ==  1 ) tmp[(i - off[now] + c) % c] = 1; n       for (int i = 0; i <= d - 1; i++) cnt[i] += tmp[i]; n     } n     for (auto now : lst[id]) { n       for (int i = 0; i <= d - 1; i++) n         if (dpscc[id][(i - off[now] + c) % c] >= 0) n           dp[now][i] = n               dpscc[id][(i - off[now] + c) % c] + cnt[(i - off[now] + c) % c]; n       for (int i = 0; i <= d - 1; i++) ans = max(ans, dp[now][i]); n     } n     for (auto now : lst[id]) n       for (auto v : e[now]) n         if (scc[v] != id) { n           int nid = scc[v]; n           for (int i = 0; i <= d - 1; i++) n             upd(dpscc[nid][(i + 1 - off[v] + cycle[nid]) % cycle[nid]], n                 dp[now][i]); n         } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n string s; n int n; n int main() { n   cin >> s; n   n = s.length(); n   for (int i = 1; i < n - 1; i++) { n     if (s[i - 1] ==  .  || s[i] ==  .  || s[i + 1] ==  . ) continue; n     int c1 = s[i - 1] -  A  + 1; n     int c2 = s[i] -  A  + 1; n     int c3 = s[i + 1] -  A  + 1; n     int t = 0; n     if (c1 != c2 && c1 != c3) t++; n     if (c2 != c1 && c2 != c3) t++; n     if (c3 != c1 && c3 != c2) t++; n     if (t == 3) { n       cout <<  Yes ; n       return 0; n     } n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926535897932384626433832795; n const double EPS = 1e-9; n const int MAX_NUMBER = 25 * 1000 * 1000; n const int ROOT = 10000; n bool isPrime[MAX_NUMBER]; n int primesAmount; n bool canMult(int a, int b) { n   if (b == 0) { n     return true; n   } n   return a <= MAX_NUMBER / b; n } n void precalc() { n   int op = 0; n   memset(isPrime, true, sizeof(isPrime)); n   isPrime[0] = isPrime[1] = false; n   primesAmount = 0; n   double t = clock(); n   for (int i = 2; i < MAX_NUMBER; ++i) { n     ++op; n     if (isPrime[i]) { n       ++primesAmount; n       if (i < ROOT) { n         for (int j = i * i; j < MAX_NUMBER; j += i) { n           ++op; n           isPrime[j] = false; n         } n       } n     } n   } n   cerr <<  op   << op << endl; n   cerr <<  amount primes   << primesAmount << endl; n   t = clock() - t; n   t /= CLOCKS_PER_SEC; n   cerr <<  precalc time is   << t << endl; n } n int solve(int x, const vector<int>& a) { n   if (x == 2) { n     return 0; n   } n   vector<bool> haveNumber(MAX_NUMBER, false); n   for (int i = 0; i < a.size(); ++i) { n     if (a[i] < MAX_NUMBER) { n       haveNumber[a[i]] = true; n     } n   } n   if (haveNumber[1]) { n     return 1; n   } n   int primesToX = 0; n   for (int i = 2; i <= x - 1; ++i) { n     if (isPrime[i]) { n       ++primesToX; n       if (!haveNumber[i]) { n         return -1; n       } n     } n   } n   return primesToX; n } n int main() { n   precalc(); n   int n, x; n   while (cin >> n >> x) { n     vector<int> a(n); n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     int res = solve(x, a); n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5, mod = 1e9 + 7; n struct pt { n   long long x, y; n   inline void in() { scanf( %lld%lld , &x, &y); } n   inline long long operator*(const pt& rhs) const { n     return x * rhs.y - rhs.x * y; n   } n   inline pt operator+(const pt& rhs) const { n     return (pt){x + rhs.x, y + rhs.y}; n   } n   inline pt operator-(const pt& rhs) const { n     return (pt){x - rhs.x, y - rhs.y}; n   } n   inline void mo() { n     x %= mod; n     y %= mod; n   } n } a[N], s2[N]; n int n, i, j, ans, s1[N]; n long long s0[N], z, tot; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) a[i].in(), s2[i] = s2[i - 1] + a[i], s2[i].mo(); n   for (i = 2; i <= n; i++) n     z = a[i - 1] * a[i], s0[i] = s0[i - 1] + z, n     s1[i] = (s1[i - 1] + s0[i]) % mod; n   tot = abs(s0[n + 1] = s0[n] + a[n] * a[1]); n   for (i = 3, j = 1; i <= n; i++) { n     for (; j < i - 1 && abs(s0[i] - s0[j] + a[i] * a[j]) > tot / 2.0; j++) n       ; n     ans = (ans + s0[i] % mod * (i - 1 - j) - (s1[i - 2] - s1[j - 1]) + n            a[i] * (s2[i - 2] - s2[j - 1])) % n           mod; n     ans = (ans + s1[j - 1] + s2[j - 1] * a[i] + n            (s0[n + 1] - s0[i]) % mod * (j - 1)) % n           mod; n   } n   ans = ((tot % mod * (mod + 1 >> 2) % mod * n % mod * (n - 3) % mod + ans) % n              mod + n          mod) * n         2 % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300 + 10; n const int MAXM = 1e5 + 10; n const int MOD = 1e9 + 7; n int T, n, q, t, a[MAXN], dp[MAXN][MAXM], deg[MAXN], cnt[MAXN]; n vector<int> g[MAXN]; n bool visited[MAXN]; n void dfs(int v, int x) { n   visited[v] = true; n   cnt[v] = 0; n   for (auto u : g[v]) { n     if (!visited[u]) { n       dfs(u, x + a[v]); n       cnt[v] = cnt[u] + 1; n     } n   } n   t -= a[v] * cnt[v]; n   a[v] += x; n   if (t < 0) { n     cout << 0 <<   n ; n     exit(0); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   T = 1; n   while (T--) { n     cin >> n >> q >> t; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     for (int i = 1; i <= q; i++) { n       int u, v; n       cin >> u >> v; n       g[u].push_back(v); n       deg[v]++; n     } n     for (int i = 1; i <= n; i++) { n       if (!deg[i]) { n         dfs(i, 0); n       } n     } n     for (int i = 1; i <= n; i++) { n       if (!visited[i]) { n         cout << 0 <<   n ; n         return 0; n       } n     } n     for (int i = 1; i <= n; i++) { n       dp[i][0] = 1; n       for (int j = 1; j <= t; j++) { n         dp[i][j] = dp[i - 1][j]; n         if (a[i] <= j) { n           dp[i][j] = (dp[i][j] + dp[i][j - a[i]]) % MOD; n         } n       } n     } n     cout << dp[n][t] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n inline char smax(A &a, const B &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename A, typename B> n inline char smin(A &a, const B &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename I> n inline void read(I &x) { n   int f = 0, c; n   while (!isdigit(c = getchar())) c ==  -  ? f = 1 : 0; n   x = c & 15; n   while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); n   f ? x = -x : 0; n } n const int N = 1e5 + 7; n const int LOG = 36; n const double eps = 1e-13; n int n, nl; n long long t; n double M; n int a[N], b[N], q[N]; n double p[N]; n inline int dcmp(const double &x) { n   return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1); n } n struct Line { n   double k, b; n   inline bool operator<(const Line &a) const { n     return dcmp(k - a.k) < 0 || (dcmp(k - a.k) == 0 && b < a.b); n   } n } l[N]; n inline double crs(const Line &l1, const Line &l2) { n   return (l2.b - l1.b) / (l1.k - l2.k); n } n struct Matrix { n   double a[3][3]; n   inline Matrix() { memset(a, 0, sizeof(a)); } n   inline Matrix(const double &x) { n     memset(a, 0, sizeof(a)); n     a[0][0] = a[1][1] = a[2][2] = x; n   } n   inline void print() { n     for (int i = 0; i < 3; ++i) { n       for (int j = 0; j < 3; ++j) fprintf(stderr,  %.13lf  , a[i][j]); n       puts(  ); n     } n   } n   inline Matrix operator*(const Matrix &b) { n     Matrix c; n     c.a[0][0] = a[0][0] * b.a[0][0] + a[0][1] * b.a[1][0] + a[0][2] * b.a[2][0]; n     c.a[0][1] = a[0][0] * b.a[0][1] + a[0][1] * b.a[1][1] + a[0][2] * b.a[2][1]; n     c.a[0][2] = a[0][0] * b.a[0][2] + a[0][1] * b.a[1][2] + a[0][2] * b.a[2][2]; n     c.a[1][0] = a[1][0] * b.a[0][0] + a[1][1] * b.a[1][0] + a[1][2] * b.a[2][0]; n     c.a[1][1] = a[1][0] * b.a[0][1] + a[1][1] * b.a[1][1] + a[1][2] * b.a[2][1]; n     c.a[1][2] = a[1][0] * b.a[0][2] + a[1][1] * b.a[1][2] + a[1][2] * b.a[2][2]; n     c.a[2][0] = a[2][0] * b.a[0][0] + a[2][1] * b.a[1][0] + a[2][2] * b.a[2][0]; n     c.a[2][1] = a[2][0] * b.a[0][1] + a[2][1] * b.a[1][1] + a[2][2] * b.a[2][1]; n     c.a[2][2] = a[2][0] * b.a[0][2] + a[2][1] * b.a[1][2] + a[2][2] * b.a[2][2]; n     return c; n   } n } f[LOG]; n inline void ycl() { n   for (int i = 1; i <= n; ++i) n     smax(M, p[i] * b[i]), l[i] = (Line){p[i], p[i] * a[i]}; n   sort(l + 1, l + n + 1); n   for (int i = 1; i <= n; ++i) n     if (dcmp(l[i].k - l[nl].k)) n       l[++nl] = l[i]; n     else n       l[nl] = l[i]; n   n = nl, nl = 1; n   q[1] = 1; n   for (int i = 2; i <= n; ++i) { n     while (nl > 1 && n            dcmp(crs(l[q[nl - 1]], l[i]) - crs(l[q[nl - 1]], l[q[nl]])) <= 0) n       --nl; n     q[++nl] = i; n   } n   for (int i = 1; i <= nl; ++i) l[i] = l[q[i]]; n   n = nl; n } n inline void work() { n   ycl(); n   double dp = 0; n   long long tt = 0; n   for (int i = 1; i <= n && tt < t; ++i) { n     double stp; n     if (i < n) n       stp = crs(l[i], l[i + 1]); n     else n       stp = 1e20; n     if (tt < t && dcmp(tt * M - dp - stp) >= 0) continue; n     Matrix A, B; n     A.a[0][0] = dp, A.a[1][0] = tt, A.a[2][0] = 1; n     B.a[0][0] = 1 - l[i].k, B.a[0][1] = l[i].k * M, B.a[0][2] = l[i].b; n     B.a[1][1] = 1, B.a[1][2] = 1; n     B.a[2][2] = 1; n     f[0] = B; n     for (int i = 1; i < LOG; ++i) f[i] = f[i - 1] * f[i - 1]; n     for (int i = LOG - 1; ~i; --i) n       if (tt + (1ll << i) < t) { n         Matrix C = f[i] * A; n         if (dcmp(C.a[1][0] * M - C.a[0][0] - stp) < 0) A = C, tt += 1ll << i; n       } n     A = f[0] * A, ++tt; n     dp = A.a[0][0]; n   } n   printf( %.13lf n , dp); n } n inline void init() { n   read(n), read(t); n   for (int i = 1; i <= n; ++i) read(a[i]), read(b[i]), scanf( %lf , &p[i]); n } n int main() { n   init(); n   work(); n   fclose(stdin), fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 123; n using LL = long long; n using PLL = pair<LL, LL>; n PLL lr[maxn], a[maxn]; n LL ans[maxn]; n deque<pair<LL, PLL>> itv; n priority_queue<PLL, vector<PLL>, greater<PLL>> Q; n LL n, m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> lr[i].first >> lr[i].second; n   sort(lr, lr + n); n   for (int i = 0; i < n - 1; ++i) n     itv.emplace_back(lr[i + 1].first - lr[i].second, n                      make_pair(lr[i + 1].second - lr[i].first, i)); n   sort(itv.begin(), itv.end()); n   for (int i = 0; i < m; ++i) { n     LL t; n     cin >> t; n     a[i] = {t, i + 1}; n   } n   sort(a, a + m); n   bool flag(true); n   for (int i = 0; i < m; ++i) { n     while (!itv.empty() && itv[0].first <= a[i].first) { n       Q.push(itv[0].second); n       itv.pop_front(); n     } n     LL r, id; n     if (Q.empty()) continue; n     tie(r, id) = Q.top(); n     Q.pop(); n     if (r < a[i].first) { n       flag = false; n       break; n     } n     ans[id] = a[i].second; n   } n   if (itv.empty() && flag) { n     cout <<  Yes  << endl; n     cout << ans[0]; n     for (int i = 1; i < n - 1; ++i) cout <<     << ans[i]; n     cout << endl; n   } else n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   char arr[4][4]; n   int i, j; n   std::map<int, int> map; n   for (i = 0; i < 4; i++) { n     for (j = 0; j < 4; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  . ) { n         continue; n       } else { n         map[arr[i][j]]++; n       } n     } n   } n   for (i = 0; i < 4; i++) { n     for (j = 0; j < 4; j++) { n       if (map[arr[i][j]] > 2 * k) { n         cout <<  NO ; n         exit(0); n       } n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n //#include <bits/extc++.h> n #define ll long long n #define endl   n  n #define pb push_back n #define ms(v,x) memset(v,x,sizeof(v)) n #define ff first n #define ss second n #define td(v) v.begin(),v.end() n #define rep(i,a,n) for (int i=(a);i<(n);i++) n #define per(i,a,n) for (int i=(n-1);i>=a;i--) n #define trav(a, x) for(auto& a : x) n #define re(v) {for(auto &_re : v) cin >> _re;} n #define pr(v) {for(auto _pr : v) cout << _pr <<    ; cout << endl;} n #define sz(x) (int)(x).size() n #define all(x) x.begin(), x.end() n #define pii pair<int,int> n #define pll pair<ll,ll> n #define vi vector<int> n #define vl vector<ll> n #define eb emplace_back n using namespace std; n using vvi = vector<vi>; n using vvl = vector<vl>; n const ll M = 1e9 + 7; n //const ll M = 998244353; n //const ll M = 1e6; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n ll binpow(ll a, ll b){ n  ll res = (a!=0); n  while(b > 0){ n   if(b&1) res = res * a % M; n   a = a * a % M; n   b>>=1; n  } n  return res; n } n //#define double long double n //#define int long long n //#define tiii tuple<int,int,int> n //#define uint unsigned int n void solve(){ n  int n; n  cin >> n; n  vi a(n), c(n + 1);  n  re(a); n  for(int i=0;i<n;i++){ n   int j = i; n   while(j+1 < n && a[j + 1] == a[i]){ n    j++; n   } n   c[a[i]]++; n   i = j; n  } n  int ans = n + 1; n  for(int i=0;i<n;i++){ n   ans = min(ans, c[a[i]] + 1 - (a[i] == a[0]) - (a[i] == a.back())); n  } n  cout << ans << endl; n } n int32_t main(){ n   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n   //freopen( .in ,  r , stdin); n  //freopen( .out ,  w , stdout); n  int t = 1; n  cin >> t; n  while(t--){ n   solve(); n  } n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, s, a[101], b[101], x, y, i; n   double c, d, e, f, g = 10000000, h, k = -1; n   cin >> n >> m >> s; n   for (i = 1; i <= n; i++) cin >> a[i]; n   cin >> x >> y; n   for (i = 2; i <= n; i++) { n     c = double(a[i]) / double(m); n     d = (x - a[i]) * (x - a[i]); n     e = y * y; n     f = double(sqrt(d + e)); n     f /= s; n     c += f; n     if (c < g || (c == g && d < k)) { n       k = d; n       g = c; n       h = i; n     } n   } n   cout << h; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int val; n   int num; n } c[100010]; n bool cmp(node a, node b) { return a.val < b.val; } n int main() { n   int n, d, a, b, u, v, res = 0; n   scanf( %d%d%d%d , &n, &d, &a, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &u, &v); n     c[i].val = u * a + v * b; n     c[i].num = i; n   } n   sort(c + 1, c + 1 + n, cmp); n   int i; n   for (i = 1; i <= n; i++) { n     if (d - c[i].val < 0) { n       res = i - 1; n       break; n     } n     d = d - c[i].val; n   } n   if (i == n + 1) res = n; n   printf( %d n , res); n   for (int i = 1; i <= res; i++) printf( %d  , c[i].num); n   return 0; n } n


#include<bits/stdc++.h> n #define R register n #define I inline n #define ll long long n #define ull unsigned long long n #define LL __int128 n #define db double n using namespace std; n #define pii pair<int,int> n #define mp(x,y) make_pair(x,y) n #define piii pair<pair<int,int>,int> n #define mp3(x,y,z) make_pair(make_pair(x,y),z) n #define fi first.first n #define se first.second n #define th second n #define putint(x) printf( %d n ,x) n #define putll(x) printf( %lld n ,x) n #define putull(x) printf( %llu n ,x) n #define lowbit(x) ((x)&(-(x))) n #define inf (0x7fffffff) n #define INF (1e18) n #define eps (1e-8) n I int read() n { n  char ch=getchar(); n  int res=0,flag=1; n  while(ch< 0 ||ch> 9 ) n  { n   if(ch== - ) n    flag=-1; n   ch=getchar(); n  } n  while(ch>= 0 &&ch<= 9 ) n  { n   res=res*10+ch- 0 ; n   ch=getchar(); n  } n  return res*flag; n } n int a,b,k; n int main() n { n  a=read(); n  b=read(); n  k=read(); n  if(b==1) n  { n   if(k==0) n   { n    puts( YES ); n    cout<< 1 ; n    for(R int i=1;i<=a;++i) n     cout<< 0 ; n    puts(  ); n    cout<< 1 ; n    for(R int i=1;i<=a;++i) n     cout<< 0 ; n    puts(  ); n    return 0; n   } n   else n   { n    puts( NO ); n    return 0; n   } n  } n  if(a==0) n  { n   if(k==0) n   { n    puts( YES ); n    for(R int i=1;i<=b;++i) n     cout<< 1 ; n    puts(  ); n    for(R int i=1;i<=b;++i) n     cout<< 1 ; n    puts(  ); n   } n   else n    puts( NO ); n   return 0; n  } n  if(k<a+b-1) n  { n   puts( YES ); n   int pos1=a+b,pos2=a+b-k,cnt=0; n   for(R int i=1;i<=a+b;++i) n   { n    if(i==pos2) n     cout<<1,++cnt; n    else if(cnt<b-(i<pos2)) n     cout<<1,++cnt; n    else n     cout<<0; n   } n   puts(  ); n   cnt=0; n   for(R int i=1;i<=a+b;++i) n   { n    if(i==pos2&&pos1!=pos2) n    { n     cout<<0; n     continue; n    } n    if(i==pos1) n     cout<<1,++cnt; n    else if(cnt<b-1) n     cout<<1,++cnt; n    else n     cout<<0; n   } n   puts(  ); n  } n  else n   puts( NO ); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1ll << 62; n struct Query { n   long long n; n   int id; n   Query(long long _n = 0, int _id = 0) : n(_n), id(_id) {} n   inline bool operator<(const Query& rhs) const { return n > rhs.n; } n } qry[100010]; n long long ans[100010]; n vector<pair<long long, int> > dic[65]; n inline long long qpow(long long base, int pw) { n   long long res = 1; n   while (pw) { n     if (pw & 1) { n       if (res < inf / base) n         res = res * base; n       else n         return inf; n     } n     if (pw > 1) { n       if (base < inf / base) n         base = base * base; n       else n         return inf; n     } n     pw >>= 1; n   } n   return res; n } n inline long long get_sqrt(long long x) { n   long long l = 1, r = 1000000001ll; n   while (r - l > 1) { n     long long mid = (l + r) >> 1; n     if (mid * mid <= x) n       l = mid; n     else n       r = mid; n   } n   return l; n } n bool notPrime[65]; n int mu[65]; n vector<int> prime; n void linear_sieve() { n   notPrime[0] = notPrime[1] = true; n   for (int i = 2; i <= 60; i++) { n     if (!notPrime[i]) { n       prime.push_back(i); n       mu[i] = -1; n     } n     for (int j = 0; j < (int)prime.size() && i * prime[j] <= 60; j++) { n       int d = i * prime[j]; n       notPrime[d] = true; n       mu[d] = -mu[i]; n       if (i % prime[j] == 0) { n         mu[d] = 0; n         break; n       } n     } n   } n } n int main() { n   int T; n   long long n; n   scanf( %d , &T); n   for (int i = 1; i <= T; i++) { n     scanf( %lld , &n); n     qry[i] = Query(n, i); n   } n   sort(qry + 1, qry + 1 + T); n   for (int i = 3; i <= 60; i++) { n     long long v; n     for (int j = 2; (v = qpow(j, i)) <= 1000000000000000000ll; j++) n       dic[i].push_back(pair<long long, int>(v, j)); n   } n   linear_sieve(); n   for (int i = 1; i <= T; i++) { n     n = qry[i].n; n     long long& res = ans[qry[i].id]; n     res = n - 1 + (get_sqrt(n) - 1) * mu[2]; n     for (int i = 3; i <= 60; i++) { n       while (!dic[i].empty() && dic[i].back().first > n) dic[i].pop_back(); n       if (dic[i].empty()) break; n       res += (dic[i].back().second - 1) * mu[i]; n     } n   } n   for (int i = 1; i <= T; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, bool> tz; n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int cnk(long long n, long long k, long long mod) { n   long long result = 1; n   int lal = 0; n   for (int lol = n; lal < k; lal++, lol--) { n     result *= lol; n     result %= mod; n   } n   return result; n } n const long long mod = 1e9 + 7; n vector<long long> graph[100000]; n vector<long long> vec; n vector<long long> vec1[1001]; n vector<long long> vec2[1001]; n long long mas[100][100]; n vector<long long> nap[4]; n bool masi[100000]; n long long mas1[100]; n long long mas2[100]; n string s; n bool comp(long long a, long long b) { return a > b; } n int main() { n   long long a, b, c, d; n   long long k = 0; n   double p; n   cin >> a; n   long long mini = 1e9; n   for (long long lol = 0; lol < a; lol++) { n     scanf( %i , &k); n     mini = min(mini, k); n   } n   cin >> a; n   for (long long lol = 0; lol < a; lol++) { n     scanf( %i , &k); n     vec.push_back(k); n   } n   long long res = 0; n   std::sort(vec.begin(), vec.end(), comp); n   for (long long bom = 0; bom < vec.size(); bom++) { n     if (bom % (mini + 2) < mini) { n       res += vec[bom]; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5005]; n int vals[5005]; n int n; n long long d[2][5005]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     vals[i] = a[i]; n   } n   sort(vals, vals + n); n   memset(d, 0, sizeof(d)); n   int cur = 0, nw = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       d[nw][j] = d[cur][j] + abs(a[i] - vals[j]); n       if (j > 0) { n         d[nw][j] = min(d[nw][j], d[nw][j - 1]); n       } n     } n     swap(cur, nw); n   } n   long long res = d[cur][0]; n   for (int i = 0; i < n; i++) { n     res = min(res, d[cur][i]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> A; n int F(int q, int w, int pr) { n   if (w - q <= 2) return w - q; n   int s = q; n   int g = 0; n   int ans = 2; n   while (s < w) { n     int f = s; n     int st = 1; n     while (st <= (A[s] - pr)) st <<= 1; n     st = pr + (st >> 1); n     while (f < w && (A[f] & st) == st) f++; n     ans = max(ans, F(s, f, st) + g + ((f == w) ? 0 : 1)); n     s = f; n     g++; n   } n   return ans; n } n int main() { n   int n; n   scanf( %d , &n); n   A.resize(n); n   for (int i = 0; i < n; i++) scanf( %d , &A[i]); n   sort(A.begin(), A.end()); n   reverse(A.begin(), A.end()); n   printf( %d n , n - F(0, n, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acosl(-1); n void solve() { n   long long n, s, t; n   cin >> n >> s >> t; n   if (n == s + t) { n     cout << max(s, t) + 1 <<   n ; n   } else { n     if (2 * n == s + t) { n       cout << 1 <<   n ; n     } else { n       if (s > t) { n         swap(s, t); n       } n       long long same = t - (n - s); n       cout << max(n - s, s - same) + 1 <<   n ; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tt = 1; n   cin >> tt; n   while (tt--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 10; n const int MOD = 1e9 + 7; n long long bit[maxn], n, k; n long long sum(int i) { n   long long s = 0; n   while (i > 0) { n     s += bit[i]; n     s %= MOD; n     i -= i & -i; n   } n   return s; n } n void add(int i, long long x) { n   while (i <= n) { n     bit[i] += x; n     bit[i] %= MOD; n     i += i & -i; n   } n } n long long dp[maxn]; n long long a[maxn]; n long long quick_mod(long long a, long long b) { n   long long ans = 1; n   a %= MOD; n   while (b) { n     if (b & 1) { n       ans = ans * a % MOD; n       b--; n     } n     b >>= 1; n     a = a * a % MOD; n   } n   return ans; n } n long long C(long long n, long long m) { n   if (m > n) return 0; n   long long ans = 1; n   for (int i = 1; i <= m; i++) { n     long long a = (n + i - m) % MOD; n     long long b = i % MOD; n     ans = ans * (a * quick_mod(b, MOD - 2) % MOD) % MOD; n   } n   return ans; n } n long long Lucas(long long n, long long m) { n   if (m == 0) return 1; n   return C(n % MOD, m % MOD) * Lucas(n / MOD, m / MOD) % MOD; n } n long long combine[maxn]; n void init_combine() { n   for (int i = 1; i <= n; i++) combine[i] = Lucas(k + i - 1, i); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   dp[1] = a[1]; n   init_combine(); n   for (int i = 2; i <= n; i++) { n     dp[i] = a[i]; n     for (int j = 1; j <= i - 1; j++) { n       dp[i] += (combine[j] * a[i - j]) % MOD; n       dp[i] %= MOD; n     } n   } n   for (int i = 1; i <= n; i++) cout << dp[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x, y; n   cin >> n >> x >> y; n   vector<char> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long j = 0, p = 0; n   while (j < n) { n     while (a[j] == 48 && j < n) { n       j++; n     } n     p++; n     while (a[j] == 49 && j < n) { n       j++; n     } n   } n   if (a[0] == 49) { n     p--; n   } n   if (p == 0) { n     cout <<  0  << endl; n     return 0; n   } n   cout << (p - 1) * min(x, y) + y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, x, y, z; n int f[100005]; n int main() { n   scanf( %d%d , &n, &m); n   for (; m; --m) { n     scanf( %d%d%d , &x, &y, &z); n     k = 7; n     k -= f[x]; n     k -= f[y]; n     k -= f[z]; n     if (!f[x]) f[x] = k & -k, k -= k & -k; n     if (!f[y]) f[y] = k & -k, k -= k & -k; n     if (!f[z]) f[z] = k & -k, k -= k & -k; n   } n   for (i = 1; i <= n; ++i) { n     if (f[i] == 1) n       printf( 1 ); n     else if (f[i] == 2) n       printf( 2 ); n     else n       printf( 3 ); n     if (i == n) n       printf(  n ); n     else n       printf(   ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n const int mod = 1e9 + 9; n int Case = 1, n, m; n char ss[maxn][maxn]; n int dr[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; n int vis[maxn][maxn][5][5]; n struct node { n   int x, y, d, cnt; n   node(int x = 0, int y = 0, int d = 0, int cnt = 0) { n     this->x = x; n     this->y = y; n     this->d = d; n     this->cnt = cnt; n   } n } s; n void bfs() { n   queue<node> A; n   for (int i = 0; i < 4; i++) { n     int nx = s.x + dr[i][0]; n     int ny = s.y + dr[i][1]; n     if (nx >= 0 && nx < n && ny >= 0 && ny < m && ss[nx][ny] !=  * ) { n       vis[nx][ny][i][2] = 1; n       A.push(node(nx, ny, i, 2)); n     } n   } n   while (!A.empty()) { n     node temp = A.front(); n     A.pop(); n     if (ss[temp.x][temp.y] ==  T ) { n       printf( YES n ); n       return; n     } n     for (int i = 0; i < 4; i++) { n       int nx = temp.x + dr[i][0]; n       int ny = temp.y + dr[i][1]; n       if (nx >= 0 && nx < n && ny >= 0 && ny < m && ss[nx][ny] !=  * ) { n         if (temp.d != i) { n           if (temp.cnt <= 0) continue; n           if (!vis[nx][ny][i][temp.cnt - 1]) { n             vis[nx][ny][i][temp.cnt - 1] = 1; n             A.push(node(nx, ny, i, temp.cnt - 1)); n           } n         } else { n           if (!vis[nx][ny][i][temp.cnt]) { n             vis[nx][ny][i][temp.cnt] = 1; n             A.push(node(nx, ny, i, temp.cnt)); n           } n         } n       } n     } n   } n   printf( NO n ); n } n void solve() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , ss[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (ss[i][j] ==  S ) s.x = i, s.y = j; n     } n   } n   bfs(); n } n int main() { n   ios::sync_with_stdio(false); n   while (Case--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * flag; n } n int n; n int s[109]; n int main() { n   n = read(); n   for (int i = (1), i_end_ = (n); i <= i_end_; ++i) { n     char c = getchar(); n     if (c ==  U ) s[i] = 1; n     if (c ==  R ) s[i] = 2; n   } n   int ans = 0, i = 1; n   while (i <= n) { n     if (s[i] == 1) n       if (s[i + 1] == 2) n         ++ans, i = i + 2; n       else n         i++; n     else if (s[i] == 2) n       if (s[i + 1] == 1) n         ++ans, i = i + 2; n       else n         i++; n   } n   printf( %d n , n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int base = 31337; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const int logo = 20; n const int off = 1 << logo; n const int treesiz = off << 1; n int n; n int niz[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , niz + i); n     niz[i] *= 2; n   } n   int sum = 0; n   for (int i = 0; i < n; i++) sum += niz[i]; n   if (sum >= 9 * n) n     printf( 0 ); n   else { n     sort(niz, niz + n); n     for (int i = 0; i < n; i++) { n       sum += 10 - niz[i]; n       if (sum >= 9 * n) { n         printf( %d n , i + 1); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, m; n vector<long long> g[N]; n vector<long long> rg[N]; n vector<long long> vs; n bool used[N]; n int cmp[N], cmpsize[N]; n void add_edge(int from, int to) { n   g[from].push_back(to); n   rg[to].push_back(from); n } n void dfs(int v) { n   used[v] = 1; n   for (int i = 0; i < (((int)(g[v]).size())); i++) { n     if (!used[g[v][i]]) dfs(g[v][i]); n   } n   vs.push_back(v); n } n void rdfs(int v, int k) { n   used[v] = 1; n   cmp[v] = k; n   for (int i = 0; i < (((int)(rg[v]).size())); i++) { n     if (!used[rg[v][i]]) rdfs(rg[v][i], k); n   } n } n int scc() { n   memset(used, 0, sizeof(used)); n   vs.clear(); n   for (int v = 0; v < (n); v++) { n     if (!used[v]) dfs(v); n   } n   memset(used, 0, sizeof(used)); n   int k = 0; n   for (int i = ((int)(vs).size()) - 1; i >= 0; i--) { n     if (!used[vs[i]]) rdfs(vs[i], k++); n   } n   return k; n } n int dfs2(int v) { n   used[v] = 1; n   int res = cmpsize[cmp[v]] == 1; n   for (int i = 0; i < (((int)(g[v]).size())); i++) n     if (!used[g[v][i]]) { n       res &= dfs2(g[v][i]); n     } n   for (int i = 0; i < (((int)(rg[v]).size())); i++) n     if (!used[rg[v][i]]) { n       res &= dfs2(rg[v][i]); n     } n   return res; n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < (m); i++) { n     int a, b; n     cin >> a >> b; n     add_edge(a - 1, b - 1); n   } n   scc(); n   memset(cmpsize, 0, sizeof(cmpsize)); n   for (int i = 0; i < (n); i++) { n     cmpsize[cmp[i]]++; n   } n   memset(used, 0, sizeof(used)); n   int ans = n; n   for (int i = 0; i < (n); i++) n     if (!used[i]) { n       ans -= dfs2(i); n     } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   int a = 0, b = 0, c = 0; n   for (int i = 0; i < n; ++i) { n     a += (s[i] ==  a ); n     b += (s[i] ==  b ); n     c += (s[i] ==  c ); n   } n   if (a == 0 || b == 0 || c == 0) { n     cout <<  NO n ; n     return 0; n   } n   if (c != a && c != b) { n     cout <<  NO n ; n     return 0; n   } n   int lasta = -1, lastb = -1, lastc = -1; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  a ) lasta = max(lasta, i); n     if (s[i] ==  b ) lastb = max(lastb, i); n     if (s[i] ==  c ) lastc = max(lastc, i); n   } n   if (!(lasta < lastb && lastb < lastc)) { n     cout <<  NO n ; n     return 0; n   } n   for (int i = 0; i <= lasta; ++i) { n     if (s[i] !=  a ) { n       cout <<  NO n ; n       return 0; n     } n   } n   for (int i = lasta + 1; i <= lastb; ++i) { n     if (s[i] !=  b ) { n       cout <<  NO n ; n       return 0; n     } n   } n   for (int i = lastb + 1; i <= lastc; ++i) { n     if (s[i] !=  c ) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int t, n, lenn; n long long int last(long long int x) { n   long long int tr = x; n   lenn = 0; n   while (x > 0) { n     tr = x % 10; n     x /= 10; n     ++lenn; n   } n   return tr; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   long long int x, lllll; n   while (t--) { n     cin >> n; n     lllll = last(n); n     if (lenn == 1) { n       x = 1; n     } else if (lenn == 2) { n       x = 3; n     } else if (lenn == 3) { n       x = 6; n     } else { n       x = 10; n     } n     cout << x + 10 * (lllll - 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n template <typename T> n using V = vector<T>; n template <typename T, typename S> n using P = pair<T, S>; n template <class T> n using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>; n using LD = long double; n template <typename T, typename S> n void check(T& a, S& b) { n   if (a >= b) { n     a %= b; n   } n } n template <typename T> n T gcd(T u, T v) { n   if (u == v) return u; n   if (u == 0) return v; n   if (v == 0) return u; n   if (~u & 1) { n     if (v & 1) n       return gcd(u >> 1, v); n     else n       return gcd(u >> 1, v >> 1) << 1; n   } n   if (~v & 1) return gcd(u, v >> 1); n   if (u > v) return gcd((u - v) >> 1, v); n   return gcd((v - u) >> 1, u); n } n LL mulmod(LL a, LL b, LL m) { n   check(a, m); n   check(b, m); n   LL q = (LL)(((LD)a * (LD)b) / (LD)m); n   LL r = a * b - q * m; n   if (r > m) r %= m; n   if (r < 0) r += m; n   return r; n } n template <typename T, typename S> n T expo(T e, S n) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = x * p; n     p = p * p; n     n >>= 1; n   } n   return x; n } n template <typename T> n T powerL(T e, T n, T m) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = mulmod(x, p, m); n     p = mulmod(p, p, m); n     n >>= 1; n   } n   return x; n } n const int N = 200010; n const LL mod = (LL)1e9 + 7; n LL f[N], inv[N]; n void pre() { n   f[0] = inv[0] = f[1] = inv[1] = 1; n   for (LL i = 2; i < N; i++) { n     f[i] = mulmod(f[i - 1], i, mod); n     inv[i] = powerL(f[i], mod - 2, mod); n   } n   return; n } n LL ncr(LL n, LL r) { n   LL ans = mulmod(f[n], inv[n - r], mod); n   return mulmod(ans, inv[r], mod); n } n LL solve(V<LL>& a) { n   LL n = (LL)a.size(); n   LL x = (n >> 1) - 1; n   V<LL> co; n   for (LL i = 0; i < (LL)(x / 2) + 1; i++) { n     LL t = ncr(x, i); n     co.push_back(t); n     co.push_back(t); n   } n   if ((LL)co.size() * 2 > n) co.pop_back(); n   auto temp = co; n   reverse(temp.begin(), temp.end()); n   for (LL& i : temp) co.push_back(i); n   if (n % 4 == 0) { n     for (LL i = 1; i < n; i += 2) co[i] = -co[i]; n   } n   LL ans = 0; n   for (LL i = 0; i < (LL)n; i++) { n     LL t = mulmod(a[i], co[i], mod); n     ans = (ans + t); n     ans %= mod; n     ans = (ans + mod) % mod; n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cerr.tie(NULL); n   ; n   pre(); n   LL n; n   cin >> n; n   V<LL> a(n); n   for (LL i = 0; i < (LL)n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << a[0] <<   n ; n     return 0; n   } n   if (n & 1) { n     V<LL> t; n     for (LL i = 0; i < (LL)n - 1; i++) { n       t.push_back(a[i] + (2 * (!(i & 1)) - 1) * a[i + 1]); n     } n     a = t; n   } n   cout << solve(a) <<   n ; n   cerr <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int a[100 + 10]; n int main() { n   int n, m, min_, max_; n   scanf( %d %d %d %d , &n, &m, &min_, &max_); n   int i, j, t = 0; n   for (i = 0; i < m; i++) { n     scanf( %d , &a[i]); n   } n   for (i = 0; i < m; i++) { n     for (j = i + 1; j < m; j++) { n       if (a[j] < a[i]) { n         t = a[i]; n         a[i] = a[j]; n         a[j] = t; n       } n     } n   } n   if (a[0] < min_ || a[m - 1] > max_) n     printf( Incorrect n ); n   else if (a[0] == min_ && a[m - 1] == max_) n     printf( Correct n ); n   else if (a[0] != min_ && a[m - 1] != max_) { n     if (n - m >= 2) n       printf( Correct n ); n     else n       printf( Incorrect n ); n   } else { n     if (n - m >= 1) n       printf( Correct n ); n     else n       printf( Incorrect n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[101]; n long long arr[100001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n   } n   long long sum1 = sum; n   sort(arr, arr + n); n   for (int i = 0; i < n; i++) { n     for (long long j = 1; j <= arr[i]; j++) { n       if (arr[i] % j == 0) { n         long long sum2 = sum + arr[0] * (j - 1) + arr[i] / j - arr[i]; n         sum1 = min(sum2, sum1); n       } n     } n   } n   cout << sum1; n   return 0; n } n


/** try <3 **/ n #include<bits/stdc++.h> n  n using namespace std; n  n #define task  sol  n #define lb lower_bound n #define ub upper_bound n #define fi first n #define se second n #define pb push_back n #define mp make_pair n #define zs(v) ((int)(v).size()) n #define BIT(x, i) (((x) >> (i)) & 1) n #define CNTBIT __builtin_popcountll n #define ALL(v) (v).begin(),(v).end() n #define endl   n  n  n typedef long double ld; n typedef long long ll; n typedef pair<int, int> pii; n  n const int dx[4]={-1, 0, 1, 0}, dy[4]={0, 1, 0, -1}; n const int dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8]={0, 1, 1, 1, 0, -1, -1, -1}; n  n const ll mod=998244353 ; /// 998244353 n const int base=311; n const int N=2e5+5; n  n int n,k; n int a[N],pos[N]; n ll gt[N]; n  n ll binpow(ll n, ll k) n { n     if(k==0) return 1; n     ll t=binpow(n,k/2); n     t=t*t%mod; n     if(k&1) t=t*n%mod; n     return t; n } n int C(int k, int n) n { n     if(k>n) return 0; n     return gt[n]*binpow(gt[k]*gt[n-k]%mod,mod-2)%mod; n } n void add(int &a, int b) n { n     a+=b; n     if(a>=mod) a-=mod; n } n void gogo() n { n     cin>>n>>k; n     for(int i=1;i<=n;++i) n     { n         cin>>a[i]; n         a[i]++; n         pos[a[i]]=i; n     } n     gt[0]=1; n     for(int i=1;i<=N-5;++i) gt[i]=gt[i-1]*i%mod; n     int cnt=1; n     for(int i=1;i<n;++i) n     { n //        cout<<a[i]<<   <<a[i+1]<<   <<pos[a[i]+1]<<   <<pos[a[i+1]+1]<<  n ; n         if(pos[a[i]+1]>pos[a[i+1]+1]) n         { n             cnt++; n         } n     } n //    cout<<cnt<<endl; n     int ans=0; n     for(int i=cnt;i<=min(n,k);++i) n     { n         /// choose i-cnt such that it will not be equal n         add(ans,(ll)C(i,k)*C(i-cnt,n-cnt)%mod); n     } n     cout<<ans; n } n int main() n { n     ios_base::sync_with_stdio(0); cin.tie(0); n     if(fopen(task .inp ,  r )) n     { n         freopen(task .inp ,  r , stdin); n         freopen(task .out ,  w , stdout); n     } n     gogo(); n } n  n  n  n  n  n  n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n const int INF = 1e8; n const int N = 2e5 + 2; n int n, m, k; n vector<int> G[N]; n int a[N]; n bool sp[N]; n int distN[N], dist1[N]; n int suf[N]; n bool cmp(int& v, int& u) { return dist1[v] < dist1[u]; } n void bfsN() { n   for (int i = 0; i < n; i++) distN[i] = INF; n   distN[n - 1] = 0; n   queue<pair<int, int>> q; n   q.push({0, n - 1}); n   while (!q.empty()) { n     int v = q.front().second; n     int d = q.front().first; n     q.pop(); n     for (auto u : G[v]) { n       if (distN[u] > d + 1) { n         distN[u] = d + 1; n         q.push({distN[u], u}); n       } n     } n   } n } n void bfs1() { n   for (int i = 0; i < n; i++) dist1[i] = INF; n   dist1[0] = 0; n   queue<pair<int, int>> q; n   q.push({0, 0}); n   while (!q.empty()) { n     int v = q.front().second; n     ; n     int d = q.front().first; n     q.pop(); n     for (auto u : G[v]) { n       if (dist1[u] > d + 1) { n         dist1[u] = d + 1; n         q.push({dist1[u], u}); n       } n     } n   } n } n int getAns() { n   int ans = 0; n   for (int i = k - 2; i >= 0; i--) ans = max(ans, dist1[a[i]] + 1 + suf[i + 1]); n   return min(ans, distN[0]); n } n signed main() { n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     cin >> a[i], a[i]--; n     sp[a[i]] = true; n   } n   for (int i = 0; i < m; i++) { n     int v, u; n     cin >> v >> u, v--, u--; n     G[v].push_back(u); n     G[u].push_back(v); n   } n   bfsN(); n   bfs1(); n   sort(a, a + k, cmp); n   suf[k - 1] = distN[a[k - 1]]; n   for (int i = k - 2; i >= 0; i--) suf[i] = max(suf[i + 1], distN[a[i]]); n   cout << getAns() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2222; n const int INF = 0x3f3f3f3f; n int n, m, k, s; n int mat[2][N][9], cp[9][9]; n void tomin(int &a, int b) { n   if (a > b) a = b; n } n void tomax(int &a, int b) { n   if (a < b) a = b; n } n void work() { n   for (int i = 0; i < m; i++) n     for (int j = 0; j < k; j++) { n       for (int r = i; r < m; r++) n         for (int p = 0; p < k; p++) { n           tomax(cp[j][p], abs(r - i) + mat[1][i][j] - mat[0][r][p]); n           tomax(cp[j][p], abs(r - i) + mat[1][r][p] - mat[0][i][j]); n           tomax(cp[p][j], cp[j][p]); n         } n     } n   int d, ans = 0; n   scanf( %d , &d); n   d--; n   for (int i = 1; i < s; i++) { n     int x; n     scanf( %d , &x); n     x--; n     tomax(ans, cp[d][x]); n     d = x; n   } n   printf( %d n , ans); n } n int main() { n   scanf( %d%d%d%d , &n, &m, &k, &s); n   memset(mat[0], INF, sizeof(mat[0])); n   memset(mat[1], -1, sizeof(mat[1])); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int d; n       scanf( %d , &d); n       d--; n       tomin(mat[0][j][d], i); n       tomax(mat[1][j][d], i); n     } n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long arr[4]; n   for (int i = 0; i < 4; i++) cin >> arr[i]; n   sort(arr, arr + 4); n   long long w, x, y, z; n   w = arr[0]; n   x = arr[1]; n   y = arr[2]; n   long long a, b, c, d; n   b = (w - x + y) / 2; n   a = w - b; n   c = x - a; n   cout << a <<     << b <<     << c; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[10000]; n int n, m, mn, mn1; n multiset<int> s; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s.insert(a[i]); n   } n   int q = *s.rbegin() + m; n   while (m--) { n     int t = *s.begin(); n     s.erase(s.begin()); n     s.insert(t + 1); n   } n   cout << *s.rbegin() <<     << q; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int div_floor(const long long int &a, const long long int &b) { n   return a / b - (((a ^ b) < 0) and a % b); n } n long long int div_ceil(const long long int &a, const long long int &b) { n   return a / b + (((a ^ b) >= 0) and a % b); n } n long long int modpow(long long int a, long long int b, n                      long long int m = 998244353) { n   a %= m; n   long long int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % m; n     a = (a * a) % m; n     b >>= 1; n   } n   return res; n } n long long int modinv(long long int a, long long int m = 998244353) { n   return modpow(a, m - 2, m); n } n vector<long long int> f; n vector<long long int> invf; n const int MAXVAL = 2e5 + 7; n void init() { n   f.resize(MAXVAL, 1); n   invf.resize(MAXVAL, 1); n   for (int i = 1; i < MAXVAL; i++) f[i] = (f[i - 1] * i) % 998244353; n   for (int i = 1; i < MAXVAL; i++) invf[i] = modinv(f[i]); n } n long long int ncr(long long int n, long long int r) { n   if (r > n) return 0; n   return invf[n - r] * (f[n] * invf[r] % 998244353) % 998244353; n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (auto &e : a) cin >> e; n   int x = -1, cnt = 0; n   for (auto &e : a) n     if (e != x) x = e, cnt++; n   if (a.back() == a[0]) cnt = max(1, cnt - 1); n   if (k == 1 || cnt == 1) { n     cout << 0 <<   n ; n     return; n   } n   long long int ans = 0; n   for (long long int i = 0; i < cnt; i++) { n     long long int add = (modpow(k - 2, i) * ncr(cnt, i)) % 998244353; n     long long int rem = cnt - i; n     if (rem % 2) n       add = (add * modpow(2, rem - 1)) % 998244353; n     else n       add = (add * (modpow(2, rem) - ncr(rem, rem / 2))) % 998244353, n       add = (add + 998244353) % 998244353, add = (add * modinv(2)) % 998244353; n     ans += add; n   } n   ans %= 998244353; n   ans = (ans * modpow(k, n - cnt)) % 998244353; n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   init(); n   int t = 1; n   while (t--) solve(); n } n