#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 2; n long long fact[N]; n void factorial() { n   fact[0] = fact[1] = 1; n   for (long long i = 2; i < N; i++) fact[i] = (fact[i - 1] * i) % 1000000007; n } n long long power(long long a, long long b, long long mod) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2) { n       x = (x * y) % mod; n     } n     y = (y * y) % mod; n     b /= 2; n   } n   return x % mod; n } n long long modular_inverse(long long n, long long mod) { n   return power(n, mod - 2, mod); n } n long long nCr(long long n, long long k, long long mod) { n   if (k > n) n     return 0; n   else if (n == k) n     return 1; n   return (fact[n] * ((modular_inverse(fact[k], 1000000007) * n                       modular_inverse(fact[n - k], 1000000007)) % n                      mod)) % n          mod; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   long long int n = s.size(), temp = 0, p = 1, ans = 0; n   for (long long int i = 0; i < n; i++) { n     temp += s[i] -  0 ; n   } n   for (long long int i = n - 1; i >= 0; i--) { n     temp -= s[i] -  0 ; n     ans = (ans + (((p * (n - i)) % 1000000007) * (temp)) % 1000000007) % n           1000000007; n     ans = (ans + (((((i * (i + 1) / 2) % 1000000007) * p) % 1000000007) * n                   (s[i] -  0 )) % n                      1000000007) % n           1000000007; n     p *= 10; n     p %= 1000000007; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long max_N = 100003; n const long long max_M = 200003; n vector<pair<long long, long long> > briaunos[max_N]; n bool nepavyko_uzdavinys = false; n struct reiskinys { n   long long skaicius, a; n   long double a_reiksme; n   bool isreikstas; n   bool isvestas; n   long double a_reiksme_kai_reisinys_nulis; n   reiskinys() { n     isreikstas = false; n     isvestas = false; n     skaicius = 0; n     a = 0; n   } n   void tapk_pradiniu() { n     skaicius = 0; n     a = 1; n     isreikstas = true; n   } n   long double reiksme() { n     return (long double)a_reiksme * (long double)a + (long double)skaicius; n   } n   void kada_tampa_nuliu() { n     if ((a == 0) && (skaicius == 0)) n       a_reiksme_kai_reisinys_nulis = 0; n     else if (a == 0) n       a_reiksme_kai_reisinys_nulis = 1000000000; n     else n       a_reiksme_kai_reisinys_nulis = n           ((long double)0 - (long double)skaicius) / (long double)a; n   } n   bool operator<(const reiskinys &kitas) const { n     return a_reiksme_kai_reisinys_nulis < kitas.a_reiksme_kai_reisinys_nulis; n   } n }; n reiskinys virsuniu_reiksmes[max_N]; n void isreisk(reiskinys &turimas, reiskinys &norimas, long long suma) { n   norimas.a = 0 - turimas.a; n   norimas.skaicius = suma - turimas.skaicius; n   norimas.isreikstas = true; n } n pair<bool, long double> rask_a_reiksme(reiskinys &pirmas, reiskinys &antras, n                                        long double suma) { n   long long skaicius = 0 - (suma - pirmas.skaicius - antras.skaicius); n   long long a = 0 - pirmas.a - antras.a; n   if ((a == 0) && (skaicius != 0)) { n     nepavyko_uzdavinys = true; n     return {false, 0}; n   } else if (a == 0) { n     return {false, 0}; n   } else { n     long double a_reiksme = (long double)skaicius / (long double)a; n     return {true, a_reiksme}; n   } n } n bool tenkina(reiskinys &pirmas, reiskinys &antras, long double suma) { n   if (pirmas.reiksme() + antras.reiksme() == suma) return true; n   return false; n } n void ieskok_minimalaus_a(vector<long long> komponente) { n   for (long long i = 0; i < komponente.size(); i++) { n     virsuniu_reiksmes[komponente[i]].kada_tampa_nuliu(); n   } n   sort(komponente.begin(), komponente.end(), [&](long long &i, long long &j) { n     return virsuniu_reiksmes[i] < virsuniu_reiksmes[j]; n   }); n   reiskinys neigiami; n   reiskinys teigiami; n   long double a_reiksme = n       virsuniu_reiksmes[komponente[0]].a_reiksme_kai_reisinys_nulis - 1; n   bool ar_teigiamas[komponente.size()]; n   for (long long i = 0; i < komponente.size(); i++) { n     virsuniu_reiksmes[komponente[i]].a_reiksme = a_reiksme; n     if (virsuniu_reiksmes[komponente[i]].reiksme() > (long double)0) { n       ar_teigiamas[i] = true; n       teigiami.a += virsuniu_reiksmes[komponente[i]].a; n       teigiami.skaicius += virsuniu_reiksmes[komponente[i]].skaicius; n     } else { n       ar_teigiamas[i] = false; n       neigiami.a -= virsuniu_reiksmes[komponente[i]].a; n       neigiami.skaicius -= virsuniu_reiksmes[komponente[i]].skaicius; n     } n   } n   neigiami.a_reiksme = a_reiksme; n   teigiami.a_reiksme = a_reiksme; n   long double minimalus_ats = teigiami.reiksme() + neigiami.reiksme(); n   long double minimali_a_reiksme = a_reiksme; n   for (long long i = 0; i < komponente.size(); i++) { n     long double a_reiksme = n         virsuniu_reiksmes[komponente[i]].a_reiksme_kai_reisinys_nulis; n     neigiami.a_reiksme = a_reiksme; n     teigiami.a_reiksme = a_reiksme; n     if (ar_teigiamas[i]) { n       teigiami.a -= virsuniu_reiksmes[komponente[i]].a; n       teigiami.skaicius -= virsuniu_reiksmes[komponente[i]].skaicius; n       neigiami.a -= virsuniu_reiksmes[komponente[i]].a; n       neigiami.skaicius -= virsuniu_reiksmes[komponente[i]].skaicius; n     } else { n       neigiami.a += virsuniu_reiksmes[komponente[i]].a; n       neigiami.skaicius += virsuniu_reiksmes[komponente[i]].skaicius; n       teigiami.a += virsuniu_reiksmes[komponente[i]].a; n       teigiami.skaicius += virsuniu_reiksmes[komponente[i]].skaicius; n     } n     if (teigiami.reiksme() + neigiami.reiksme() < minimalus_ats) { n       minimalus_ats = teigiami.reiksme() + neigiami.reiksme(); n       minimali_a_reiksme = a_reiksme; n     } n   } n   for (long long i = 0; i < komponente.size(); i++) n     virsuniu_reiksmes[komponente[i]].a_reiksme = minimali_a_reiksme; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long N, M; n   cin >> N >> M; n   for (long long i = 0; i < M; i++) { n     long long a, b, tipas; n     cin >> a >> b >> tipas; n     briaunos[a].push_back({b, tipas}); n     briaunos[b].push_back({a, tipas}); n   } n   bool surastas[N + 1]; n   for (long long i = 1; i <= N; i++) surastas[i] = false; n   for (long long i = 1; i <= N; i++) { n     if (surastas[i]) continue; n     queue<long long> eile; n     vector<long long> komponente; n     eile.push(i); n     komponente.push_back(i); n     surastas[i] = true; n     virsuniu_reiksmes[i].tapk_pradiniu(); n     while (!eile.empty()) { n       long long g = eile.front(); n       eile.pop(); n       for (long long j = 0; j < briaunos[g].size(); j++) { n         long long kitas = briaunos[g][j].first; n         if (surastas[kitas]) continue; n         if (g == kitas) continue; n         isreisk(virsuniu_reiksmes[g], virsuniu_reiksmes[kitas], n                 briaunos[g][j].second); n         surastas[kitas] = true; n         eile.push(kitas); n         komponente.push_back(kitas); n       } n     } n     bool radau_a_reiksme = false; n     long double a_reiksme; n     for (long long j = 0; j < komponente.size(); j++) { n       long long dab = komponente[j]; n       for (long long x = 0; x < briaunos[dab].size(); x++) { n         long long kitas = briaunos[dab][x].first; n         pair<bool, long double> ats = n             rask_a_reiksme(virsuniu_reiksmes[dab], virsuniu_reiksmes[kitas], n                            briaunos[dab][x].second); n         radau_a_reiksme = ats.first; n         a_reiksme = ats.second; n         if (radau_a_reiksme) { n           break; n         } n       } n       if (radau_a_reiksme) break; n     } n     if (radau_a_reiksme) { n       for (long long j = 0; j < komponente.size(); j++) { n         virsuniu_reiksmes[komponente[j]].a_reiksme = a_reiksme; n       } n       for (long long j = 0; j < komponente.size(); j++) { n         long long dab = komponente[j]; n         for (long long x = 0; x < briaunos[dab].size(); x++) { n           long long kitas = briaunos[dab][x].first; n           if (!tenkina(virsuniu_reiksmes[dab], virsuniu_reiksmes[kitas], n                        briaunos[dab][x].second)) { n             nepavyko_uzdavinys = true; n           } n         } n       } n     } else { n       ieskok_minimalaus_a(komponente); n     } n   } n   if (nepavyko_uzdavinys) n     cout <<  NO ; n   else { n     cout <<  YES n ; n     for (long long i = 1; i <= N; i++) n       cout << fixed << setprecision(9) << virsuniu_reiksmes[i].reiksme() <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int i, l = s.length(); n   char max = s[0]; n   for (i = 1; i < l; i++) { n     if (s[i] > max) max = s[i]; n   } n   for (i = 0; i < l; i++) { n     if (max == s[i]) cout << max; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int used[200001]; n int socks[200001]; n bool used2[200001]; n map<pair<int, int>, int> colors; n map<pair<int, int>, int>::iterator it; n int Stage = 0; n vector<vector<int> > V; n pair<int, int> P; n int MaXS = 0; n int countOfSocks = 0; n void Dfs(int v) { n   countOfSocks++; n   P.first = Stage; n   P.second = socks[v]; n   colors[P]++; n   MaXS = max(MaXS, colors[make_pair(Stage, socks[v])]); n   for (int i = 0; i < V[v].size(); i++) { n     if (used[V[v][i]] != true) { n       used[V[v][i]] = true; n       Dfs(V[v][i]); n     } n   } n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> socks[i]; n     vector<int> a; n     V.push_back(a); n   } n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     V[l].push_back(r); n     V[r].push_back(l); n     used2[l] = true; n     used2[r] = true; n   } n   long long ans = 0; n   if (m == 0) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (used[i] == false && used2[i] == true) { n       MaXS = 0; n       countOfSocks = 0; n       used[i] = true; n       Dfs(i); n       ans += countOfSocks - MaXS; n       Stage++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   int i; n   int j; n } AXIS; n AXIS D[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n bool DFS(char maze[][505], AXIS S, int &k, int &n, int &m) { n   maze[S.i][S.j] =  * ; n   for (int i = 0; i < 4; i++) { n     AXIS next; n     next.i = S.i + D[i].i; n     next.j = S.j + D[i].j; n     if (maze[next.i][next.j] ==  .  && 0 <= next.i && next.i < n && n         0 <= next.j && next.j < m && DFS(maze, next, k, n, m)) { n       maze[next.i][next.j] =  X ; n       k--; n     } n     if (!k) return 0; n   } n   return 1; n } n int main() { n   int n, m, k; n   char maze[505][505]; n   AXIS start = {-1, -1}; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> maze[i][j]; n       if (start.i < 0 && maze[i][j] ==  . ) { n         start.i = i; n         start.j = j; n       } n     } n   } n   DFS(maze, start, k, n, m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (maze[i][j] ==  * ) { n         cout <<  . ; n       } else { n         cout << maze[i][j]; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n vector<int> ans[N]; n int a[N], b[N], c[N]; n int n, k, p; n int main() { n   scanf( %d%d%d , &n, &k, &p); n   int s = 0; n   int _x = 0, _y = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     s += a[i]; n     if (a[i] & 1) n       b[_x++] = a[i]; n     else n       c[_y++] = a[i]; n   } n   if ((k - p) & 1) { n     if (s % 2 == 0) { n       printf( NO n ); n       return 0; n     } n   } n   if ((k - p) % 2 == 0) { n     if (s & 1) { n       printf( NO n ); n       return 0; n     } n   } n   int j = 0; n   if (_y >= p) { n     for (int i = 0; i < p; ++i) { n       ans[j++].push_back(c[i]); n     } n     if (_x < (k - p)) { n       printf( NO n ); n       return 0; n     } else { n       for (int i = 0; i < k - p; ++i) { n         ans[j++].push_back(b[i]); n       } n       for (int i = p; i < _y; ++i) ans[j - 1].push_back(c[i]); n       for (int i = k - p; i < _x; ++i) ans[j - 1].push_back(b[i]); n     } n   } else { n     for (int i = 0; i < _y; ++i) { n       ans[j++].push_back(c[i]); n     } n     if (_x / 2 < (p - _y)) { n       printf( NO n ); n       return 0; n     } n     for (int i = 0; i < p - _y; ++i) { n       ans[j].push_back(b[i * 2]); n       ans[j++].push_back(b[i << 1 | 1]); n     } n     if ((_x - (p - _y) * 2) < (k - p)) { n       printf( NO n ); n       return 0; n     } else { n       for (int i = 2 * (p - _y); i < (2 * (p - _y) + k - p); ++i) { n         ans[j++].push_back(b[i]); n       } n       for (int i = (2 * (p - _y) + k - p); i < _x; ++i) n         ans[j - 1].push_back(b[i]); n     } n   } n   printf( YES n ); n   for (int i = 0; i < j; ++i) { n     int n = ans[i].size(); n     printf( %d  , n); n     for (int k = 0; k < n; ++k) { n       printf( %d  , ans[i][k]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2, class T3 = hash<T1>> n using umap = unordered_map<T1, T2, T3>; n template <class T> n using uset = unordered_set<T>; n template <class T> n using vec = vector<T>; n const long long infll = numeric_limits<long long>::max() >> 1; n const int inf = numeric_limits<int>::max() >> 1; n const int maxN = 2e6 + 1; n const int mod = 1e9 + 7; n const int N = 50001; n const int L = 30; n int n, m, cur; n int arr[N]; n struct Trie { n   int cnt[maxN]; n   int nxt[maxN][2]; n   int bit[maxN][L]; n } trie; n void insert(int key) { n   int node = 0; n   for (int i = L - 1; i >= 0; --i) { n     int j = key >> i & 1; n     if (!trie.nxt[node][j]) { n       trie.nxt[node][j] = ++cur; n     } n     node = trie.nxt[node][j]; n   } n   trie.cnt[node]++; n   for (int i = 0; i < L; ++i) { n     if (key >> i & 1) { n       trie.bit[node][i]++; n     } n   } n } n void dfs(int u = 0) { n   for (int i = 0; i < 2; ++i) { n     int v = trie.nxt[u][i]; n     if (!v) { n       continue; n     } n     dfs(v); n     trie.cnt[u] += trie.cnt[v]; n     for (int j = 0; j < L; ++j) { n       trie.bit[u][j] += trie.bit[v][j]; n     } n   } n } n int cnt_query(int key, int lim) { n   int res = 0; n   int node = 0; n   for (int i = L - 1; i >= 0; --i) { n     int j = key >> i & 1; n     int k = lim >> i & 1; n     if (!k) { n       res += trie.nxt[node][j] ? trie.cnt[trie.nxt[node][j]] : 0; n       node = trie.nxt[node][!j]; n     } else { n       node = trie.nxt[node][j]; n     } n     if (!node) { n       return res; n     } n   } n   res += trie.cnt[node]; n   return res; n } n int sum_query(int key, int lim) { n   int res = 0; n   int node = 0; n   for (int i = L - 1; i >= 0; --i) { n     int j = key >> i & 1; n     int k = lim >> i & 1; n     if (!k) { n       int sum = 0; n       for (int b = 0; b < L; ++b) { n         int s = trie.cnt[trie.nxt[node][j]]; n         int x = trie.bit[trie.nxt[node][j]][b]; n         int c[2] = {s - x, x}; n         sum += ((1ll * c[key >> b & 1]) << b) % mod; n         sum %= mod; n       } n       res += trie.nxt[node][j] ? sum : 0; n       res %= mod; n       node = trie.nxt[node][!j]; n     } else { n       node = trie.nxt[node][j]; n     } n     if (!node) { n       return res; n     } n   } n   int sum = 0; n   for (int b = 0; b < L; ++b) { n     int s = trie.cnt[node]; n     int x = trie.bit[node][b]; n     int c[2] = {s - x, x}; n     sum += ((1ll * c[key >> b & 1]) << b) % mod; n     sum %= mod; n   } n   res += sum; n   res %= mod; n   return res; n } n void input() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> arr[i]; n     insert(arr[i]); n   } n   dfs(); n } n int count(int q) { n   long long res = 0; n   for (int i = 1; i <= n; ++i) { n     res += cnt_query(~arr[i], q); n   } n   return res / 2; n } n int compute(int q) { n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     res += sum_query(~arr[i], q); n     res %= mod; n   } n   return 1ll * res * (mod + 1) / 2 % mod; n } n int bin_search(int l, int r) { n   while (l < r) { n     int q = l + r + 1 >> 1; n     if (count(q) < m) { n       r = q - 1; n     } else { n       l = q; n     } n   } n   return l; n } n void solve() { n   int q = bin_search(0, inf); n   int c = count(q); n   int res = compute(q) - 1ll * (c - m) * q % mod; n   res += mod; n   res %= mod; n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   input(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, sum, mbit; n int Ask(vector<int> vec) { n   int ret = 0; n   printf( ? %d  , vec.size()); n   for (int i = 0; i < vec.size(); i++) printf( %d  , vec[i]); n   puts(  ); n   fflush(stdout); n   scanf( %d , &ret); n   return ret; n } n int Solve(vector<int> a) { n   vector<int> tmp; n   int l = 0, r = a.size() - 1, ans = 0; n   sort(a.begin(), a.end()); n   while (l <= r) { n     int mid = (l + r) >> 1; n     for (int i = 0; i <= mid; i++) tmp.push_back(a[i]); n     int ret = Ask(tmp); n     tmp.clear(); n     if (ret == y || ret == (y ^ x)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   return a[ans]; n } n int main() { n   vector<int> vec; n   scanf( %d%d%d , &n, &x, &y); n   for (int i = 0; i <= 9; i++) { n     for (int j = 1; j <= n; j++) n       if (j >> i & 1) vec.push_back(j); n     if (!vec.size()) break; n     int ret = Ask(vec); n     vec.clear(); n     if (ret == y || ret == (y ^ x)) sum |= (1 << i), mbit = i; n   } n   vector<int> ta, tb; n   for (int i = 1; i <= n; i++) n     if (i >> mbit & 1) n       ta.push_back(i); n     else n       tb.push_back(i); n   if (ta.size() > tb.size()) swap(ta, tb); n   int pos1 = Solve(ta), pos2 = (sum ^ pos1); n   if (pos1 > pos2) swap(pos1, pos2); n   printf( ! %d %d n , pos1, pos2); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, s1; n int a[1000]; n set<string> l; n void fun() { n   memset(a, 0, sizeof(a)); n   int len = s.length(); n   int j = 0; n   for (int i = len - 1; i >= 0; i--) { n     a[j++] = s[i] -  0 ; n   } n   a[0]++; n   for (int i = 0; i < len; i++) { n     if (a[i] >= 10) { n       a[i + 1] += a[i] / 10; n       a[i] %= 10; n     } n   } n   if (a[len] != 0) len++; n   int index; n   for (int i = 0; i < len; i++) { n     if (a[i] == 0) continue; n     index = i; n     break; n   } n   for (int i = len - 1; i >= index; i--) { n     s1 += a[i] +  0 ; n   } n } n int main() { n   while (cin >> s) { n     l.clear(); n     l.insert(s); n     s1 =   ; n     fun(); n     int sum = 1; n     while (l.count(s1) != 1) { n       l.insert(s1); n       s = s1; n       s1 =   ; n       fun(); n       sum++; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = long unsigned long; n using ld = double long; n template <class T, int N, int B> n struct asym_ra { n   T a[N + 1]; n   T b[N / B + 2]; n   T get(int r) { n     if (r <= 0) return T(); n     if (r > N) r = N; n     return a[r] + b[r / B]; n   } n   T operator()(int l, int r) { return get(r) - get(l); } n   void add(int p, const T& v) { n     for (int i = p + 1; i % B && i <= N; i++) a[i] += v; n     for (int i = p / B + 1; i < N / B + 2; i++) b[i] += v; n   } n }; n int n; n int a[200005]; n const int H = 300000; n asym_ra<ll, H + 5, 512> cnt; n asym_ra<ll, H + 5, 512> sum; n int main() { n   ios::sync_with_stdio(!cin.tie(0)); n   cin >> n; n   ll sol = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     for (int j = 0; j <= H; j += x) { n       sol += sum(j, j + x) - cnt(j, j + x) * 1ll * j; n     } n     for (int q = 1;;) { n       int r = x / q; n       int l = x / (q + 1) + 1; n       r++; n       sol += x * cnt(l, r) - q * sum(l, r); n       if (l == 1) break; n       q = x / (l - 1); n     } n     sol += x * 1ll * cnt(x + 1, H + 1); n     cout << sol <<    ; n     cnt.add(x, 1); n     sum.add(x, x); n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, cnt = 0; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) n     if (s[i] ==  2 ) cnt++; n   if (cnt == 0) { n     cout <<  YES  <<   n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (i == j) n           cout <<  X ; n         else n           cout <<  = ; n       } n       cout <<   n ; n     } n   } else if (cnt == 1 || cnt == 2) n     cout <<  NO  <<   n ; n   else { n     char a[n][n]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) a[i][j] =  0 ; n     } n     cout <<  YES  <<   n ; n     for (int i = 0; i < n; i++) { n       bool win = 1; n       for (int j = 0; j < n; j++) { n         if (i == j) n           a[i][j] =  X ; n         else { n           if (s[j] ==  1 ) n             a[i][j] =  = , a[j][i] =  = ; n           else { n             if (a[i][j] ==  0  and a[j][i] ==  0 ) { n               if (win) n                 a[i][j] =  + , a[j][i] =  - , win = 0; n               else n                 a[i][j] =  - , a[j][i] =  + ; n             } n           } n         } n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) cout << a[i][j]; n       cout <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   while (n--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   T = 1; n   while (T--) { n     int n; n     cin >> n; n     vector<int> v; n     int c = 0; n     for (int i = 0; i < n; i++) { n       int in; n       cin >> in; n       if (in == 5) { n         c++; n       } n       v.push_back(in); n     } n     int nn = c / 9; n     if (c == n) { n       cout <<  -1  << endl; n     } else { n       string s =   ; n       if (nn == 0) { n         cout <<  0  << endl; n       } else { n         for (int i = 0; i < nn * 9; i++) { n           s = s +  5 ; n         } n         for (int i = 0; i < n - c; i++) { n           s = s +  0 ; n         } n         cout << s << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j, c = 0; n   char a; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> a; n       if (a ==  C  || a ==  M  || a ==  Y ) c++; n     } n   } n   if (c == 0) n     cout <<  #Black&White  << endl; n   else n     cout <<  #Color  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n int even[1 << 17]; n bool used[1 << 17]; n vector<int> path; n void addV(int v) { n   path.push_back(v + 1); n   even[v] ^= 1; n } n void dfs(int v, int p) { n   used[v] = true; n   addV(v); n   for (int i(0); i <= ((long long)(((int)g[v].size())) - 1); ++i) { n     int to(g[v][i]); n     if (to == p) continue; n     if (!used[to]) { n       dfs(to, v); n       addV(v); n     } n   } n   if (even[v]) { n     if (p == v) { n       path.pop_back(); n     } else { n       addV(p); n       addV(v); n     } n   } n } n void solve() { n   int n, m; n   scanf( %d%d , &n, &m); n   g.resize(n); n   for (int i(0); i <= ((long long)(m)-1); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i(0); i <= ((long long)(n)-1); ++i) scanf( %d , &even[i]); n   bool ok(false); n   for (int i(0); i <= ((long long)(n)-1); ++i) { n     if (!used[i]) { n       if (even[i] && ok) { n         puts( -1 ); n         return; n       } n       if (even[i]) { n         dfs(i, i); n         ok = true; n       } n     } n   } n   printf( %d n , ((int)path.size())); n   for (int i(0); i <= ((long long)(((int)path.size())) - 1); ++i) n     printf( %d%c , path[i], i + 1 == ((int)path.size()) ?   n  :    ); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n const int mod7 = 1e9 + 7; n const int mod = 1e9 + 7; n void read(int &a) { n   a = 0; n   int d = 1; n   char ch; n   while (ch = getchar(), ch >  9  || ch <  0 ) n     if (ch ==  - ) d = -1; n   a = ch ^ 48; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) n     a = (a << 3) + (a << 1) + (ch ^ 48); n   a *= d; n } n int b[N][35], p[N][35]; n void ins(int x, int v) { n   for (register int i = 0; i <= 30; i++) { n     p[x][i] = p[x - 1][i]; n     b[x][i] = b[x - 1][i]; n   } n   int now = x; n   for (register int i = 30; i >= 0; i--) { n     if (v & (1 << i)) { n       if (!b[x][i]) { n         b[x][i] = v; n         p[x][i] = now; n         return; n       } n       if (p[x][i] < now) { n         swap(b[x][i], v); n         swap(p[x][i], now); n       } n       v ^= b[x][i]; n     } n   } n } n int query(int l, int r) { n   int ans = 0; n   for (register int i = 30; i >= 0; i--) n     if (p[r][i] >= l && (b[r][i] ^ ans) > ans) ans ^= b[r][i]; n   return ans; n } n int main() { n   int n; n   read(n); n   for (register int i = 1, x; i <= n; i++) { n     read(x); n     ins(i, x); n   } n   int T; n   read(T); n   while (T--) { n     int l, r; n     read(l), read(r); n     printf( %d n , query(l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, x, y; n int ABS(int x) { return (x < 0) ? (-x) : (x); } n int main() { n   scanf( %d%d%d%d , &m, &x, &n, &y); n   if (m > n) { n     swap(m, n); n     swap(x, y); n   } n   int d = ABS(x - y); n   long long ans = 1 + m + n; n   for (int i = 1; i <= m; i++) { n     int b = i + d - 1, a = ABS(d - i) + 1; n     a = max(a, 1); n     b = min(b, n); n     if (a <= b) ans += (b - a + 1) * 2 - 1; n     if (i > d) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, w; n   cin >> n >> m >> w; n   vector<long long> A(n); n   for (int x = 0; x < n; x++) cin >> A[x]; n   long long low = 1, high = (long long)1e9 + m; n   long long ans = low, mid; n   while (high >= low) { n     mid = (low + high) / 2; n     long long left = 0; n     vector<long long> tmp(n + 1); n     long long add = 0; n     vector<long long> B = A; n     for (int x = 0; x < n; x++) { n       add += tmp[x]; n       B[x] += add; n       if (B[x] < mid) { n         long long needed = mid - B[x]; n         tmp[min(n, w + x)] -= needed; n         add += needed; n         left += needed; n       } n     } n     if (left <= m) n       ans = max(mid, ans), low = mid + 1; n     else n       high = mid - 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int nextInt() { n   int x; n   scanf( %d , &x); n   return x; n } n const int MAX_N = 1e5 + 100; n int arr[MAX_N]; n int main() { n   int n = nextInt(); n   for (int i = 0; i < n; ++i) arr[i] = nextInt(); n   sort(arr, arr + n); n   for (int i = 1; i < n; ++i) { n     if (arr[i] % arr[0] != 0) { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << arr[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, bool> ma; n   char ch[100][100]; n   int n, m, answer = 0; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; cin >> ch[i][j++]) n       ; n   for (int i = 0; i < m; ++i) { n     char Max = NULL; n     for (int j = 0; j < n; Max = max(Max, ch[j++][i])) n       ; n     for (int j = 0; j < n; ++j) n       if (ch[j][i] == Max && !ma[j]) { n         ++answer; n         ma[j] = true; n       } n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 200010; n int a[maxN]; n long long presum[maxN]; n int n; n set<long long> st; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     presum[i] = presum[i - 1] + a[i]; n   } n   for (int i = 1; i <= n; i++) { n     long long M = 1; n     while (M <= a[i]) M += M; n     for (int j = i + 2; j <= n && presum[j - 1] - presum[i] <= M; j++) n       if ((a[i] ^ a[j]) == (presum[j - 1] - presum[i])) n         st.insert(1LL * i * n + j); n     for (int j = i - 2; j >= 1 && presum[i - 1] - presum[j] <= M; j--) n       if ((a[j] ^ a[i]) == (presum[i - 1] - presum[j])) n         st.insert(1LL * j * n + i); n   } n   printf( %d n , (int)st.size()); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1, c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f ^= 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return f ? x : -x; n } n int tar; n int val[26]; n int tp[26]; n int ans; n char cmd[26][105]; n void dfs(int i) { n   if (val[i - 1] == tar) { n     printf( %d n , i - 1); n     for (int j = 0; j < (int)(i - 1); ++j) puts(cmd[j + 1]); n     exit(0); n   } n   if (i - 1 >= ans) return; n   for (int j = 0; j < (int)(i); ++j) n     for (int coef = 0; coef < (int)(4); ++coef) { n       int mul = 1 << coef; n       tp[i] = j * 10 + mul; n       val[i] = mul * val[j]; n       sprintf(cmd[i],  lea e%cx, [%d*e%cx] , i +  a , mul, j +  a ); n       if (val[i] <= tar) dfs(i + 1); n     } n   for (int j = 0; j < (int)(i); ++j) n     for (int k = 0; k < (int)(i); ++k) n       for (int coef = 0; coef < (int)(4); ++coef) { n         int mul = 1 << coef; n         tp[i] = (j * 26 + k) * 10 + mul; n         val[i] = val[j] + mul * val[k]; n         sprintf(cmd[i],  lea e%cx, [e%cx + %d*e%cx] , i +  a , j +  a , mul, n                 k +  a ); n         if (val[i] <= tar) dfs(i + 1); n       } n } n int main() { n   tar = read(); n   val[0] = 1; n   for (ans = 0; ans <= 26; ++ans) dfs(1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, l, k, a[100005], x = 1; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     if (a[i] == 25) { n       l++; n     } else if (a[i] == 50) { n       if (l < 1) { n         x = 0; n         break; n       } else { n         l--; n         k++; n       } n     } else { n       if (l > 0 && k > 0) { n         l--; n         k--; n       } else if (l >= 3) { n         l -= 3; n       } else { n         x = 0; n         break; n       } n     } n   } n   if (x == 1) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n bool f; n int col[N]; n long long p1 = 0, p2 = 0; n const long long mod = 998244353; n vector<int> E[N]; n void dfs(int u) { n   if (col[u] == 1) n     p1++; n   else n     p2++; n   for (int i = 0; i < E[u].size(); i++) { n     int v = E[u][i]; n     if (!col[v]) { n       if (col[u] == 1) n         col[v] = 2; n       else if (col[u] == 2) n         col[v] = 1; n       dfs(v); n     } else if (col[u] == col[v]) n       f = 0; n   } n } n long long fast_mod(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return res; n } n int main() { n   int t, n, m; n   cin >> t; n   while (t--) { n     f = 1; n     long long ans = 1; n     cin >> n >> m; n     for (int i = 1; i <= m; i++) { n       int u, v; n       cin >> u >> v; n       E[u].push_back(v); n       E[v].push_back(u); n     } n     for (int i = 1; i <= n; i++) { n       if (!col[i]) { n         col[i] = 1; n         p1 = 0; n         p2 = 0; n         dfs(i); n         ans = ans * ((fast_mod(2, p1) + fast_mod(2, p2)) % mod) % mod; n       } n     } n     if (!f) n       cout << 0 << endl; n     else n       cout << ans << endl; n     for (int i = 1; i <= n; i++) E[i].clear(), col[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5001; n bool dp1[N][N], dp2[N][N]; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   } n   string s; n   memset(dp1, false, sizeof dp1); n   memset(dp2, false, sizeof dp2); n   cin >> s; n   long long n = s.length(); n   for (long long i = 0; i < n; i++) { n     long long curr = 0; n     for (long long j = i; j < n; j++) { n       if (s[j] ==  ) ) n         curr--; n       else n         curr++; n       if (curr >= 0) n         dp1[i][j] = true; n       else n         break; n     } n   } n   for (long long i = n - 1; i >= 0; i--) { n     long long curr = 0; n     for (long long j = i; j >= 0; j--) { n       if (s[j] ==  ( ) n         curr--; n       else n         curr++; n       if (curr >= 0) n         dp2[j][i] = true; n       else n         break; n     } n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     for (long long j = i; j < n; j++) { n       if (dp1[i][j] && dp2[i][j] && (j - i + 1) % 2 == 0) ans++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int gcd(int a, int b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n bool sortbysec(const pair<char, int> &a, const pair<char, int> &b) { n   return (a.second < b.second); n } n bool checkParity(long long int a, long long int b) { n   if (a % 2 == 0 && b % 2 == 0 || a % 2 == 1 && b % 2 == 1) return true; n   return false; n } n bool isPrime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cerr.tie(NULL); n   ; n   int n; n   cin >> n; n   string s; n   cin >> s; n   string p =   ; n   for (long long int i = 0; i < s.size(); i++) p += s[i]; n   int count = 0; n   for (long long int i = 0; i < s.size() - 1; i++) { n     if (s[i] == s[i + 1]) { n       if (s[i] ==  R ) { n         if (i + 2 <= s.size() - 1) { n           if (s[i + 2] ==  B ) { n             count++; n             s[i + 1] =  G ; n           } else if (s[i + 2] ==  G ) { n             count++; n             s[i + 1] =  B ; n           } else n             s[i + 1] =  G ; n         } else n           s[i + 1] =  G ; n       } else if (s[i] ==  G ) { n         if (i + 2 <= s.size() - 1) { n           if (s[i + 2] ==  B ) { n             count++; n             s[i + 1] =  R ; n           } else if (s[i + 2] ==  R ) { n             count++; n             s[i + 1] =  B ; n           } else n             s[i + 1] =  R ; n         } else n           s[i + 1] =  R ; n       } else { n         if (i + 2 <= s.size() - 1) { n           if (s[i + 2] ==  G ) { n             count++; n             s[i + 1] =  R ; n           } else if (s[i + 2] ==  R ) { n             count++; n             s[i + 1] =  G ; n           } else n             s[i + 1] =  R ; n         } else n           s[i + 1] =  R ; n       } n     } n   } n   int l = 0; n   for (long long int i = 0; i < n; i++) n     if (p[i] != s[i]) l++; n   cout << l << endl; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int Max() { return -1000000007; } n int Min() { return 1000000007; } n template <typename... Args> n int Max(int a, Args... args) { n   return max(a, Max(args...)); n } n template <typename... Args> n int Min(int a, Args... args) { n   return min(a, Min(args...)); n } n long long a[7][7]; n int main() { n   for (int i = (1); i <= (5); ++i) { n     for (int j = (1); j <= (5); ++j) { n       scanf( %lld , &a[i][j]); n     } n   } n   vector<long long> vc; n   for (int i = (1); i <= (5); ++i) vc.push_back((long long)i); n   long long ans = 0LL; n   do n     ans = max(ans, (a[vc[0]][vc[1]] + a[vc[1]][vc[0]]) + n                        (a[vc[2]][vc[3]] + a[vc[3]][vc[2]]) + n                        (a[vc[1]][vc[2]] + a[vc[2]][vc[1]]) + n                        (a[vc[3]][vc[4]] + a[vc[4]][vc[3]]) + n                        (a[vc[2]][vc[3]] + a[vc[3]][vc[2]]) + n                        (a[vc[3]][vc[4]] + a[vc[4]][vc[3]])); n   while (next_permutation(vc.begin(), vc.end())); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> ans[1000]; n int main() { n   cin >> n; n   int k = 2; n   while ((k * (k + 1)) / 2 <= n) k++; n   int cnt = 1; n   for (int i = 1; i <= k; i++) n     for (int j = 1; j < i; j++) { n       ans[i].push_back(cnt); n       ans[j].push_back(cnt); n       cnt++; n     } n   cout << k << endl; n   for (int i = 1; i <= k; i++) { n     for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void openfile() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n const int nmax = 1 << 16; n int n, k; n int a[nmax]; n vector<string> name; n map<string, int> num; n vector<vector<int> > g; n int main() { n   cin >> n >> k; n   g.resize(n); n   name.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> name[i]; n     num[name[i]] = i; n   } n   for (int i = 0; i < k; i++) { n     string s1, s2; n     cin >> s1 >> s2; n     int v1 = num[s1], v2 = num[s2]; n     g[v1].push_back(v2); n     g[v2].push_back(v1); n   } n   int ans, cans = 0; n   for (int i = 0; i < nmax; i++) { n     int cur = 0; n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) cur++; n     } n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) { n         for (int q = 0; q < ((int)((g[j]).size())); q++) { n           if (i & (1 << g[j][q])) { n             cur = 0; n             break; n           } n         } n       } n     } n     if (cur > cans) { n       ans = i; n       cans = cur; n     } n   } n   cout << cans << endl; n   set<string> b; n   for (int i = 0; i < n; i++) { n     if (ans & (1 << i)) { n       b.insert(name[i]); n     } n   } n   for (set<string>::iterator it = b.begin(); it != b.end(); it++) { n     cout << *it << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[400005]; n   int n, k; n   char c =  a ; n   int T[26]; n   memset(T, 0, sizeof(T)); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf(  %c , &s[i]); n     T[s[i] -  a ]++; n   } n   int maxx; n   for (int i = 0; i < 26; i++) { n     if (k >= T[i]) n       k -= T[i]; n     else { n       maxx = i, T[i] = k; n       break; n     } n   } n   for (int i = 0; i < n; i++) { n     if (s[i] -  a  <= maxx && T[s[i] -  a ] > 0) n       T[s[i] -  a ]--, s[i] =  * ; n     else n       printf( %c , s[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int sum[1010][1010]; n char mp[1010][1010]; n int getsum(int x1, int y1, int x2, int y2) { n   return sum[x1][y1] - sum[x2 + 1][y1] - sum[x1][y2 + 1] + sum[x2 + 1][y2 + 1]; n } n bool check(int a, int b, int x, int y) { n   while (1) { n     if (x + a - 1 > n || y + b - 1 > m) return 0; n     if (getsum(x, y, x + a - 1, y + b - 1) != a * b) return 0; n     if (getsum(x + a, y, n, y) > 0 && getsum(x, y + b, x, m) > 0) return 0; n     if (sum[x + a][y] == 0 && sum[x][y + b] == 0) break; n     if (getsum(x + a, y, n, y) > 0) { n       x++; n       if (getsum(x + a - 1, y, x + a - 1, y + b - 1) > 0 && n           getsum(x + a - 1, y, x + a - 1, y + b - 1) != b) n         return 0; n     } else { n       y++; n       if (getsum(x, y + b - 1, x + a - 1, y + b - 1) > 0 && n           getsum(x, y + b - 1, x + a - 1, y + b - 1) != a) n         return 0; n     } n   } n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , mp[i] + 1); n   memset(sum, 0, sizeof(sum)); n   int x0, y0; n   x0 = y0 = 10000; n   for (int i = n; i >= 1; i--) n     for (int j = m; j >= 1; j--) { n       sum[i][j] = n           sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1] + (mp[i][j] ==  X ); n       if (mp[i][j] ==  X ) { n         x0 = min(x0, i); n         y0 = min(y0, j); n       } n     } n   int ans = n * m + 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (i * j >= ans) continue; n       if (check(i, j, x0, y0)) ans = i * j; n     } n   if (ans > n * m) n     printf( -1 n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int c = 1, flag = 0, flag1 = 0, flag2 = 0, flag3 = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  U ) { n       if (flag1 == 1) { n         c++; n         flag2 = 0, flag3 = 0, flag1 = 0; n       } n       flag = 1; n     } n     if (s[i] ==  D ) { n       if (flag == 1) { n         c++; n         flag2 = 0, flag3 = 0, flag = 0; n       } n       flag1 = 1; n     } n     if (s[i] ==  L ) { n       if (flag2 == 1) { n         flag = 0, flag1 = 0, flag2 = 0; n         c++; n       } n       flag3 = 1; n     } n     if (s[i] ==  R ) { n       if (flag3 == 1) { n         c++; n         flag1 = 0, flag = 0, flag3 = 0; n       } n       flag2 = 1; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n int n, k, s; n int main() { n   scanf( %d%d , &k, &n); n   while (n > k && n % k == 0) { n     s++; n     n /= k; n   } n   if (n != k) n     printf( NO n ); n   else n     printf( YES n%d n , s); n } n


#include <bits/stdc++.h> n using namespace std; n template <class __Ty> n bool minimize(__Ty& a, __Ty b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class __Ty> n bool maximize(__Ty& a, __Ty b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const int N = 2003; n const int mod = 1e9 + 7; n int f[N][N] = {0}; n int n, k; n vector<int> g[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= i; ++j) n       if (i % j == 0) g[i].push_back(j); n   } n   for (int j = 1; j <= n; ++j) f[1][j] = 1; n   for (int i = 2; i <= k; ++i) n     for (int j = 1; j <= n; ++j) { n       for (int v : g[j]) { n         f[i][j] = (f[i][j] + f[i - 1][v]) % mod; n       } n     } n   int ans = 0; n   for (int i = 1; i <= n; ++i) ans = (ans + f[k][i]) % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using int64 = long long; n template <class T> n struct Matrix { n   vector<vector<T> > A; n   Matrix() {} n   Matrix(size_t n, size_t m) : A(n, vector<T>(m, 0)) {} n   Matrix(size_t n) : A(n, vector<T>(n, 0)){}; n   size_t height() const { return (A.size()); } n   size_t width() const { return (A[0].size()); } n   inline const vector<T> &operator[](int k) const { return (A.at(k)); } n   inline vector<T> &operator[](int k) { return (A.at(k)); } n   static Matrix I(size_t n) { n     Matrix mat(n); n     for (int i = 0; i < n; i++) mat[i][i] = 1; n     return (mat); n   } n   Matrix &operator+=(const Matrix &B) { n     size_t n = height(), m = width(); n     assert(n == B.height() && m == B.width()); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j]; n     return (*this); n   } n   Matrix &operator-=(const Matrix &B) { n     size_t n = height(), m = width(); n     assert(n == B.height() && m == B.width()); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j]; n     return (*this); n   } n   Matrix &operator*=(const Matrix &B) { n     size_t n = height(), m = B.width(), p = width(); n     assert(p == B.height()); n     vector<vector<T> > C(n, vector<T>(m, 0)); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         for (int k = 0; k < p; k++) n           C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]); n     A.swap(C); n     return (*this); n   } n   Matrix &operator^=(long long k) { n     Matrix B = Matrix::I(height()); n     while (k > 0) { n       if (k & 1) B *= *this; n       *this *= *this; n       k >>= 1LL; n     } n     A.swap(B.A); n     return (*this); n   } n   Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); } n   Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); } n   Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); } n   Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } n   friend ostream &operator<<(ostream &os, Matrix &p) { n     size_t n = p.height(), m = p.width(); n     for (int i = 0; i < n; i++) { n       os <<  [ ; n       for (int j = 0; j < m; j++) { n         os << p[i][j] << (j + 1 == m ?  ] n  :  , ); n       } n     } n     return (os); n   } n   T determinant() { n     Matrix B(*this); n     assert(width() == height()); n     T ret = 1; n     for (int i = 0; i < width(); i++) { n       int idx = -1; n       for (int j = i; j < width(); j++) { n         if (B[j][i] != 0) idx = j; n       } n       if (idx == -1) return (0); n       if (i != idx) { n         ret *= -1; n         swap(B[i], B[idx]); n       } n       ret *= B[i][i]; n       T vv = B[i][i]; n       for (int j = 0; j < width(); j++) { n         B[i][j] /= vv; n       } n       for (int j = i + 1; j < width(); j++) { n         T a = B[j][i]; n         for (int k = 0; k < width(); k++) { n           B[j][k] -= B[i][k] * a; n         } n       } n     } n     return (ret); n   } n }; n template <int mod> n struct ModInt { n   int x; n   ModInt() : x(0) {} n   ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} n   ModInt &operator+=(const ModInt &p) { n     if ((x += p.x) >= mod) x -= mod; n     return *this; n   } n   ModInt &operator-=(const ModInt &p) { n     if ((x += mod - p.x) >= mod) x -= mod; n     return *this; n   } n   ModInt &operator*=(const ModInt &p) { n     x = (int)(1LL * x * p.x % mod); n     return *this; n   } n   ModInt &operator/=(const ModInt &p) { n     *this *= p.inverse(); n     return *this; n   } n   ModInt operator-() const { return ModInt(-x); } n   ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } n   ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } n   ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } n   ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } n   bool operator==(const ModInt &p) const { return x == p.x; } n   bool operator!=(const ModInt &p) const { return x != p.x; } n   ModInt inverse() const { n     int a = x, b = mod, u = 1, v = 0, t; n     while (b > 0) { n       t = a / b; n       a -= t * b; n       swap(a, b); n       u -= t * v; n       swap(u, v); n     } n     return ModInt(u); n   } n   friend ostream &operator<<(ostream &os, const ModInt<mod> &p) { n     return os << p.x; n   } n   friend istream &operator>>(istream &is, ModInt<mod> &a) { n     long long x; n     is >> x; n     a = ModInt<mod>(x); n     return (is); n   } n }; n const int mod = 1e9 + 7; n using modint = ModInt<mod>; n int main() { n   int64 N, M; n   cin >> N >> M; n   map<int64, vector<pair<int, int> > > add; n   for (int i = 0; i < N; i++) { n     int64 A, L, R; n     cin >> A >> L >> R; n     --A; n     add[L - 1].emplace_back(A, 1); n     add[R].emplace_back(A, -1); n   } n   add[1].emplace_back(-1, -1); n   add[M].emplace_back(-1, -1); n   vector<modint> sum(3, 0); n   Matrix<modint> mat(3, 3); n   vector<int> inc(3, 0); n   int64 pv = -1; n   for (auto &p : add) { n     for (auto &v : p.second) { n       if (~v.first) inc[v.first] += v.second; n     } n     Matrix<modint> mat2(3, 3); n     for (int i = 0; i < 3; i++) { n       for (int j = -1; j <= 1; j++) { n         int k = i + j; n         if (k < 0 || k >= 3) continue; n         if (inc[k] == 0) mat2[i][k] = 1; n       } n     } n     if (~pv) { n       mat ^= p.first - pv; n       vector<modint> sum2(3); n       for (int i = 0; i < 3; i++) { n         for (int j = 0; j < 3; j++) { n           sum2[j] += sum[i] * mat[i][j]; n         } n       } n       sum.swap(sum2); n     } else { n       sum[1] = 1; n     } n     mat = mat2; n     pv = p.first; n   } n   cout << sum[1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n struct Matrix { n   int n; n   vector<vector<long long>> a; n   Matrix(int _n) { n     n = _n; n     a = vector<vector<long long>>(n, vector<long long>(n, 0)); n   } n   Matrix operator+(Matrix o) { n     Matrix ans(n); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) ans[i][j] = a[i][j] + o[i][j]; n     ans.modulate(); n     return ans; n   } n   Matrix operator*(Matrix o) { n     Matrix ans(n); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         for (int k = 0; k < n; k++) ans[i][j] += a[i][k] * o[k][j]; n     ans.modulate(); n     return ans; n   } n   vector<long long>& operator[](int index) { return a[index]; } n   void modulate() { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) a[i][j] %= MOD; n   } n   void eye() { n     a = vector<vector<long long>>(n, vector<long long>(n, 0)); n     for (int i = 0; i < n; i++) a[i][i] = 1; n   } n }; n Matrix mod_pow(Matrix a, long long e) { n   Matrix ans(2); n   ans.eye(); n   while (e) { n     if (e & 1) ans = ans * a; n     a = a * a; n     e >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long A, B, n, x; n   cin >> A >> B >> n >> x; n   Matrix ans(2); n   ans[0][0] = A; n   ans[0][1] = B; n   ans[1][1] = 1; n   ans = mod_pow(ans, n); n   cout << (ans[0][0] * x + ans[0][1]) % MOD <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000010]; n int main() { n   char str; n   int i, n, x, maxn = 0, num = 0; n   cin >> n; n   while (n--) { n     cin >> str >> x; n     if (str ==  + ) { n       num++; n       a[x] = 1; n       maxn = max(maxn, num); n     } else { n       if (a[x]) { n         num--; n         a[x] = 0; n       } else { n         maxn++; n       } n     } n   } n   cout << maxn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void chkmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void chkmax(T &a, T b) { n   if (a < b) a = b; n } n int n; n double q0, a0, b0; n double y_best[6000], x[6000], chain[6000]; n double quad_min(double a, double b, double c) { return -b / (2.0 * a); } n double bound(double h, double v) { return max(1.0 + h, min(q0, v)); } n void pull_chain(int j0, double h, double a, double b, double c, n                 double &res_yopt, int &res_j) { n   for (int j = j0; j >= 0; j--) { n     double yi_nat = bound(h, quad_min(a, b, c)); n     if (y_best[j] < yi_nat - h - b0) { n       double ny = x[j] + h + b0; n       chain[j] = b0; n       h += b0; n       a += 1.0; n       b += -2.0 * ny; n       c += ny * ny; n     } else if (y_best[j] > yi_nat - h - a0) { n       double ny = x[j] + h + a0; n       chain[j] = a0; n       h += a0; n       a += 1.0; n       b += -2.0 * ny; n       c += ny * ny; n     } else { n       res_yopt = yi_nat; n       res_j = j; n       return; n     } n   } n   res_yopt = bound(h, quad_min(a, b, c)); n   res_j = -1; n } n void fill_in_y(int i) { n   while (i >= 0) { n     double anything; n     int j; n     pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], anything, j); n     for (int k = i - 1; k > j; k--) { n       y_best[k] = y_best[k + 1] - chain[k]; n     } n     i = j; n   } n } n int main() { n   scanf( %d%lf%lf%lf , &n, &q0, &a0, &b0); n   for (int i = 0; i < n; i++) scanf( %lf , &x[i]); n   for (int i = 0; i < n; i++) y_best[i] = x[i]; n   for (int i = 0; i < n; i++) chain[i] = 0.0; n   for (int i = 1; i < n; i++) { n     double yopt; n     int j; n     pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], yopt, j); n     y_best[i] = yopt; n   } n   fill_in_y(n - 1); n   double total = 0.0; n   for (int i = 0; i < n; i++) { n     total = total + (y_best[i] - x[i]) * (y_best[i] - x[i]); n   } n   for (int i = 0; i < n; i++) { n     printf( %.8f  , y_best[i]); n   } n   printf(  n%.8lf n , total); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nxt[500010 << 1], to[500010 << 1], head[500010], cnt; n int n, m; n struct LCT { n   int ch[500010][2], fa[500010], cnt; n   long long val[500010], sum[500010], s[500010]; n   void init(int n) { n     for (int i = 1; i <= n; i++) s[i] = 1; n   } n   bool is_root(int u) { n     return !fa[u] || (ch[fa[u]][0] != u && ch[fa[u]][1] != u); n   } n   void update(int u) { n     s[u] = val[u] + 1; n     if (ch[u][0]) s[u] += s[ch[u][0]]; n     if (ch[u][1]) s[u] += s[ch[u][1]]; n   } n   void rotate(int u) { n     int f = fa[u], ff = fa[f]; n     int k = ch[f][1] == u, v = ch[u][!k]; n     if (!is_root(f)) ch[ff][ch[ff][1] == f] = u; n     ch[f][k] = v; n     ch[u][!k] = f; n     if (v) fa[v] = f; n     fa[f] = u; n     fa[u] = ff; n     update(f), update(u); n   } n   void splay(int x) { n     while (!is_root(x)) { n       int f = fa[x], ff = fa[f]; n       if (!is_root(f)) rotate((ch[ff][1] == f) ^ (ch[f][1] == x) ? x : f); n       rotate(x); n     } n   } n   void access(int x) { n     for (int y = 0; x; y = x, x = fa[x]) { n       splay(x); n       int &v = ch[x][1]; n       if (v) val[x] += s[v], sum[x] += 1ll * s[v] * s[v]; n       v = y; n       if (v) val[x] -= s[v], sum[x] -= 1ll * s[v] * s[v]; n     } n   } n   int find_root(int x) { n     access(x); n     splay(x); n     while (ch[x][0]) x = ch[x][0]; n     splay(x); n     return x; n   } n   void link(int x, int y) { n     access(y); n     splay(y); n     splay(x); n     fa[x] = y; n     val[y] += s[x]; n     sum[y] += 1ll * s[x] * s[x]; n     update(y); n   } n   void cut(int x, int y) { n     access(y); n     splay(y); n     splay(x); n     fa[x] = 0; n     val[y] -= s[x]; n     sum[y] -= 1ll * s[x] * s[x]; n     update(y); n   } n } t; n int fa[500010]; n void dfs(int u, int pre) { n   fa[u] = pre; n   for (int i = head[u]; i; i = nxt[i]) n     if (to[i] != pre) dfs(to[i], u); n } n void add(int u, int v) { n   nxt[++cnt] = head[u]; n   to[cnt] = v; n   head[u] = cnt; n } n vector<pair<int, int> > q[500010]; n bool df[500010]; n long long answer(int u) { n   int f = t.find_root(u); n   if (df[f]) n     return 1l * t.s[t.ch[f][1]] * t.s[t.ch[f][1]]; n   else n     return 1ll * t.s[f] * t.s[f]; n } n long long change(int u) { n   df[u] ^= 1; n   if (df[u]) { n     if (u == 1) { n       t.access(1); n       return t.sum[1] - 1ll * t.s[1] * t.s[1]; n     } n     long long r = -answer(u); n     t.cut(u, fa[u]); n     t.access(u); n     r += t.sum[u]; n     if (!df[fa[u]]) r += answer(fa[u]); n     return r; n   } else { n     if (u == 1) { n       t.access(1); n       return 1ll * t.s[1] * t.s[1] - t.sum[1]; n     } n     t.access(u); n     long long r = -t.sum[u]; n     if (!df[fa[u]]) r -= answer(fa[u]); n     t.link(u, fa[u]); n     r += answer(u); n     return r; n   } n } n long long ans[500010]; n void solve(int col) { n   for (int i = 0; i < q[col].size(); i++) n     ans[q[col][i].first] += change(q[col][i].second); n   for (int i = (int)q[col].size() - 1; i >= 0; i--) change(q[col][i].second); n } n int col[500010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &col[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v), add(v, u); n   } n   dfs(1, 0); n   t.init(n); n   for (int i = 1; i <= n; i++) t.link(i, fa[i]); n   for (int i = 1; i <= n; i++) q[col[i]].push_back(make_pair(0, i)); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     q[col[u]].push_back(make_pair(i, u)); n     col[u] = v; n     q[col[u]].push_back(make_pair(i, u)); n   } n   for (int i = 1; i <= n; i++) solve(i); n   for (int i = 1; i <= m; i++) ans[i] += ans[i - 1]; n   for (int i = 0; i <= m; i++) printf( %lld n , -ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f(long long n) { n   if (n < 10) return n; n   long long last_digit = n % 10; n   long long ret = n / 10 + 9; n   long long tmp = n; n   while (tmp >= 10) tmp = tmp / 10; n   if (tmp > last_digit) ret--; n   return ret; n } n int main() { n   long long i, j, k, n, m, d, a, b; n   while (cin >> a >> b) { n     cout << f(b) - f(a - 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n char s[100005]; n long long dp[100005], bit[100005], ans[100005], bit2[100005], gbit2[100005], n     B[100005], GB[100005], gbit[100005], T, len, i, j, A, o; n struct node { n   int x, y, z; n } a[100005]; n int cmp(node i, node j) { return i.y < j.y; } n int quick(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1ll * ans * a % MOD; n     a = 1ll * a * a % MOD; n     b >>= 1; n   } n   return ans; n } n long long C(int x, int y) { return B[x] * GB[x - y] % MOD * GB[y] % MOD; } n int main() { n   bit[0] = 1; n   B[0] = 1; n   for (i = 1; i <= 100001; i++) n     bit[i] = bit[i - 1] * 25 % MOD, B[i] = B[i - 1] * i % MOD; n   for (i = 0; i <= 100001; i++) n     gbit[i] = quick(bit[i], MOD - 2), GB[i] = quick(B[i], MOD - 2); n   bit2[0] = 1; n   for (i = 1; i <= 100001; i++) bit2[i] = bit2[i - 1] * 26 % MOD; n   for (i = 0; i <= 100001; i++) gbit2[i] = quick(bit2[i], MOD - 2); n   scanf( %d , &T); n   scanf( %s , s); n   len = strlen(s); n   for (i = 1; i <= T; i++) { n     scanf( %d , &A); n     if (A == 1) { n       scanf( %s , s); n       len = strlen(s); n     } else { n       scanf( %d , &A); n       o++; n       a[o].x = A; n       a[o].y = len; n       a[o].z = o; n     } n   } n   sort(a + 1, a + o + 1, cmp); n   dp[0] = 0; n   for (i = 1; i <= o; i++) { n     if (a[i].y != a[i - 1].y) { n       for (j = 1; j < a[i].y; j++) dp[j] = 0; n       for (j = a[i].y; j <= 100000; j++) n         dp[j] = C(j - 1, a[i].y - 1) * bit[j - a[i].y] % MOD; n       for (j = 1; j <= 100000; j++) n         dp[j] = (dp[j - 1] + dp[j] * bit2[100000 - j]) % MOD; n     } n     ans[a[i].z] = dp[a[i].x] * gbit2[100000 - a[i].x] % MOD; n   } n   for (i = 1; i <= o; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 400010, inf = 1e18; n long long q, lastans, cnt = 1, w[maxn]; n long long fa[maxn][21], sum[maxn][21]; n void add(long long x, long long v) { n   w[++cnt] = v; n   if (w[cnt] <= w[x]) n     fa[cnt][0] = x; n   else { n     int y = x; n     for (int i = 20; i >= 0; i--) { n       if (w[fa[y][i]] < w[cnt]) y = fa[y][i]; n     } n     fa[cnt][0] = fa[y][0]; n   } n   if (fa[cnt][0] == 0) n     sum[cnt][0] = inf; n   else n     sum[cnt][0] = w[fa[cnt][0]]; n   for (int i = 1; i <= 20; i++) { n     fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1]; n     if (fa[cnt][i] == 0) n       sum[cnt][i] = inf; n     else n       sum[cnt][i] = sum[cnt][i - 1] + sum[fa[cnt][i - 1]][i - 1]; n   } n } n long long query(long long x, long long v) { n   if (w[x] > v) return 0; n   v -= w[x]; n   long long res = 1; n   for (int i = 20; i >= 0; i--) { n     if (v >= sum[x][i]) { n       v -= sum[x][i]; n       res += (1 << i); n       x = fa[x][i]; n     } n   } n   return res; n } n int main() { n   w[0] = inf; n   for (int i = 0; i <= 20; i++) sum[1][i] = inf; n   scanf( %lld , &q); n   while (q--) { n     int id; n     long long a, b; n     scanf( %d , &id); n     scanf( %lld%lld , &a, &b); n     a ^= lastans; n     b ^= lastans; n     if (id == 1) n       add(a, b); n     else n       printf( %lld n , lastans = query(a, b)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<bool> sub(n, false); n   int c = 0; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (t < 0) { n       sub[i] = true; n       c++; n     } n   } n   if (c > k) { n     cout <<  -1 n ; n     return 0; n   } n   bool f = c; n   k -= c; n   vector<int> r(n + 1, 0); n   c = 0; n   int p = -1; n   bool s = true; n   int last = -1; n   for (int i = 0; i < n; i++) { n     if (sub[i]) { n       if (s) { n         c++; n         s = false; n         if (p != -1) { n           r[i - p]++; n         } n       } n     } else { n       if (!s) { n         p = i; n         s = true; n         c++; n       } n     } n   } n   if (f && !sub[n - 1]) last = n - p; n   for (int i = 1; i < n + 1; i++) { n     while (k - i >= 0 && r[i]) { n       c -= 2; n       k -= i; n       r[i] -= 1; n     } n     if (k - i < 0) { n       if (last != -1 && k - last >= 0) { n         c--; n         last = -1; n       } n       break; n     } n   } n   if (last != -1 && k - last >= 0) c--; n   cout << c <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int p[8505]; n int wh[8505]; n int ans[8505]; n int level[8505]; n int nxt[9][8505]; n int prv[9][8505]; n pair<int, int> bros[9][8505]; n int svd, lsz, l; n inline void go(int &f) { n   int id = f, t = 0; n   f = nxt[l][f]; n   if (wh[f] < wh[id]) id = f, t = 1; n   f = nxt[l][f]; n   if (wh[f] < wh[id]) id = f, t = 2; n   f = nxt[l][f]; n   if (t == 2) bros[l - 1][id] = {prv[l][prv[l][id]], prv[l][id]}; n   if (t == 1) bros[l - 1][id] = {prv[l][id], nxt[l][id]}; n   if (t == 0) bros[l - 1][id] = {nxt[l][id], nxt[l][nxt[l][id]]}; n   level[id]--, lsz++, nxt[l - 1][svd] = id, nxt[l - 1][id] = n + 1, n                               prv[l - 1][id] = svd; n   svd = id; n } n bool comp(int a, int b) { return wh[a] < wh[b]; } n struct genQ { n   int lvl, pl, pr; n }; n genQ s10[25000]; n int s10size; n int pos[4]; n int possize; n inline void solve2(int &l) { n   if (possize < 4) { n     for (int i = 0; i < possize; i++) { n       if (ans[pos[i]] == 0) { n         for (int j = l; j <= l + 2; j++) { n           int t = 1; n           for (int k = 0; k < possize; k++) n             if (ans[pos[k]] == j) t = 0; n           if (t) { n             ans[pos[i]] = j; n             break; n           } n         } n       } n     } n     return; n   } n   int lst = 0; n   for (int i = 1; i < 4; i++) n     if (wh[pos[i]] > wh[pos[lst]]) lst = i; n   if (ans[pos[lst]] == 0) { n     if (ans[pos[3 - lst]] == 0) { n       if (lst == 0) n         ans[pos[0]] = l + 2, ans[pos[1]] = l, ans[pos[2]] = l + 1, n         ans[pos[3]] = l + 2; n       if (lst == 1) n         ans[pos[0]] = l, ans[pos[1]] = l + 2, ans[pos[2]] = l + 1, n         ans[pos[3]] = l + 2; n       if (lst == 2) n         ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l, n         ans[pos[3]] = l + 2; n       if (lst == 3) n         ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l + 2, n         ans[pos[3]] = l; n     } else { n       if (lst > 1) { n         lst = 3 - lst; n         reverse(pos, pos + 4); n       } n       if (lst == 0) { n         ans[pos[1]] = 3 * l + 3 - ans[pos[2]] - ans[pos[3]]; n         ans[pos[0]] = ans[pos[1]] + 1; n         if (ans[pos[0]] == l + 3) ans[pos[0]] -= 3; n       } n       if (lst == 1) { n         ans[pos[1]] = ans[pos[3]]; n         ans[pos[0]] = 3 * l + 3 - ans[pos[1]] - ans[pos[2]]; n       } n     } n     return; n   } n   if (lst > 1) { n     lst = 3 - lst; n     reverse(pos, pos + 4); n   } n   if (lst == 1) { n     if (wh[pos[2]] < wh[pos[3]] || wh[pos[2]] < wh[pos[0]]) { n       ans[pos[3]] = ans[pos[0]]; n       ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]]; n     } else { n       ans[pos[3]] = ans[pos[1]]; n       ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]]; n     } n   } else { n     ans[pos[2]] = ans[pos[1]] + 1; n     if (ans[pos[2]] == l + 3) ans[pos[2]] -= 3; n     ans[pos[3]] = ans[pos[2]] + 1; n     if (ans[pos[3]] == l + 3) ans[pos[3]] -= 3; n   } n } n inline void solve1() { n   int pl = s10[s10size].pl; n   int pr = s10[s10size].pr; n   int lvl = s10[s10size].lvl; n   s10size--; n   possize = 0; n   if (pl != 0) { n     if (bros[lvl][pl].first > pl) pos[possize++] = (bros[lvl][pl].first); n     if (bros[lvl][pl].second > pl) pos[possize++] = (bros[lvl][pl].second); n   } n   if (pr != n + 1) { n     if (bros[lvl][pr].first < pr) pos[possize++] = (bros[lvl][pr].first); n     if (bros[lvl][pr].second < pr) pos[possize++] = (bros[lvl][pr].second); n   } n   int l = 3 * lvl + 1; n   int t = 0; n   for (int i = 0; i < possize; i++) { n     if (ans[pos[i]] == 0) t = 1; n   } n   if (!t) return; n   solve2(l); n } n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) n     wh[p[i]] = i, ans[i] = 0, nxt[8][i] = i + 1, level[i] = 8, n     prv[8][i] = i - 1; n   for (int i = 1; i <= 8; i++) nxt[i][0] = 1, nxt[i][n + 1] = n + 1; n   wh[n + 1] = n + 1; n   if (n > 4) n     for (l = 8; l >= 1; l--) { n       int f = nxt[l][0]; n       lsz = 0, svd = 0; n       while (f != n + 1) go(f); n       if (lsz <= 4) break; n     } n   for (int i = n; i >= 1; i--) { n     for (int gl = level[p[i]]; gl < 8; gl++) { n       int LL = prv[gl][p[i]], RR = nxt[gl][p[i]]; n       s10[++s10size] = {gl, LL, RR}; n       s10[++s10size] = {gl, LL, p[i]}; n       s10[++s10size] = {gl, p[i], RR}; n       prv[gl][RR] = LL; n       nxt[gl][LL] = RR; n     } n   } n   while (s10size > -1) solve1(); n   for (int lvl = 1; lvl <= 8; lvl++) { n     possize = 0; n     for (int i = 1; i <= n; i++) n       if (lvl == level[i] && ans[i] == 0) pos[possize++] = i; n     int c = lvl * 3 - 2; n     solve2(c); n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   scanf( %d , &t); n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   char s[n + 5]; n   scanf( %s , s); n   vector<int>* vp[30]; n   for (int i = 0; i <= 25; i++) vp[i] = new vector<int>(); n   for (int i = 0; s[i]; i++) { n     int now = s[i] -  a ; n     vp[now]->push_back(i); n   } n   while (m--) { n     getchar(); n     char x, y; n     scanf( %c %c , &x, &y); n     int id1 = x -  a ; n     int id2 = y -  a ; n     swap(vp[id1], vp[id2]); n   } n   for (int i = 0; i <= 25; i++) { n     for (vector<int>::iterator it = vp[i]->begin(); it != vp[i]->end(); it++) { n       s[*it] = i +  a ; n     } n   } n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long INF = (sizeof(long long) == 4 ? 1e9 : 2e18) + 1e5; n constexpr long long MOD = 1e9 + 7; n constexpr long long MAXN = 2e5 + 3; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long w = 0; n   queue<long long> arr; n   for (long long i = 0; i < n; ++i) { n     if (i < n && s[i] == s[i + 1]) { n       arr.emplace(i + 1); n     } n   } n   long long ans = 0; n   long long l = 0; n   while (l < n) { n     ++ans; n     while (arr.size() && arr.front() < l) arr.pop(); n     if (arr.size() && arr.front() >= l) { n       arr.pop(); n     } else { n       while (l + 1 < n && s[l] == s[l + 1]) ++l; n       ++l; n     } n     while (l + 1 < n && s[l] == s[l + 1]) ++l; n     ++l; n   } n   cout << ans <<   n ; n } n signed main() { n   const string FILE_NAME =   ; n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct info { n   int d, f, t, cst; n } inp[100006]; n int forw[1000005]; n int forw2[1000005]; n long long cforw[1000005]; n long long cforw2[1000005]; n int ok[1000006]; n int ok2[1000006]; n bool cmp(info a, info b) { return a.d < b.d; } n void process1(int prev, int day) { n   while (prev < day) { n     cforw[prev + 1] = cforw[prev]; n     forw[prev + 1] = forw[prev]; n     prev++; n   } n } n void process2(int prev, int day) { n   while (prev > day) { n     cforw2[prev - 1] = cforw2[prev]; n     forw2[prev - 1] = forw2[prev]; n     prev--; n   } n } n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d %d , &inp[i].d, &inp[i].f, &inp[i].t, &inp[i].cst); n   } n   sort(inp + 1, inp + m + 1, cmp); n   int prev = 0; n   for (int i = 1; i <= m; i++) { n     int day = inp[i].d; n     process1(prev, day); n     prev = day; n     if (inp[i].f != 0) { n       int pos = inp[i].f; n       if (ok[pos] == 0) { n         ok[pos] = inp[i].cst; n         cforw[day] += inp[i].cst; n         forw[day]++; n       } else if (ok[pos] > inp[i].cst) { n         cforw[day] += (-ok[pos] + inp[i].cst); n         ok[pos] = inp[i].cst; n       } n     } n   } n   process1(prev, 1000000); n   prev = n + 1; n   for (int i = m; i >= 1; i--) { n     if (inp[i].f != 0) continue; n     int pos = inp[i].t; n     int day = inp[i].d; n     process2(prev, day); n     prev = day; n     if (ok2[pos] == 0) { n       ok2[pos] = inp[i].cst; n       cforw2[day] += inp[i].cst; n       forw2[day]++; n     } else if (ok2[pos] > inp[i].cst) { n       cforw2[day] += (-ok2[pos] + inp[i].cst); n       ok2[pos] = inp[i].cst; n     } n   } n   process2(prev, 1); n   long long ans = -1; n   for (int i = 1; i <= 1000000 - k - 1; i++) { n     int t = i + k + 1; n     if (forw[i] != n || (forw2[t] != n)) continue; n     long long tot = cforw[i] + cforw2[t]; n     if (ans == -1) n       ans = tot; n     else n       ans = min(ans, tot); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a = -1; n int main() { n   while (cin >> n >> m) a += n + m > 0; n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n struct tr { n   int l, r, c, p; n } a[maxn], b[maxn]; n int n, k, m, maxr = 0; n long long ans = 0, num[maxn << 2], sum[maxn << 2]; n bool cmpl(tr a, tr b) { return a.l < b.l; } n bool cmpr(tr a, tr b) { return a.r < b.r; } n void update(int o, int l, int r, int c, int p) { n   if (l == r) { n     num[o] += c; n     sum[o] += 1LL * c * l; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     update(o << 1, l, mid, c, p); n   else n     update(o << 1 | 1, mid + 1, r, c, p); n   num[o] = num[o << 1] + num[o << 1 | 1]; n   sum[o] = sum[o << 1] + sum[o << 1 | 1]; n } n long long query(int o, int l, int r, int p) { n   if (l == r) { n     if (p <= num[o]) return 1LL * l * p; n     return sum[o]; n   } n   int mid = (l + r) >> 1; n   if (num[o << 1] >= p) return query(o << 1, l, mid, p); n   return query(o << 1 | 1, mid + 1, r, p - num[o << 1]) + sum[o << 1]; n } n int main() { n   scanf( %d%d%d , &n, &k, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &a[i].l, &a[i].r, &a[i].c, &a[i].p); n     b[i] = a[i]; n   } n   for (int i = 1; i <= m; i++) maxr = max(maxr, a[i].p); n   sort(a + 1, a + m + 1, cmpl); n   sort(b + 1, b + m + 1, cmpr); n   for (int i = 1, j = 1, o = 1; i <= n; i++) { n     while (j <= m && a[j].l == i) update(1, 1, maxr, a[j].c, a[j].p), j++; n     ans += query(1, 1, maxr, k); n     while (o <= m && b[o].r == i) update(1, 1, maxr, -b[o].c, b[o].p), o++; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 10; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   int x[4], y[4]; n   for (int i = 1; i <= 3; i++) { n     cin >> x[i] >> y[i]; n   } n   pair<int, pair<int, int> > e = make_pair(INF, make_pair(INF, INF)); n   for (int i = 0; i <= 1000; i++) { n     for (int j = 0; j <= 1000; j++) { n       int w = 0; n       for (int u = 1; u <= 3; u++) { n         w += abs(x[u] - i) + abs(y[u] - j) + 1; n       } n       e = min(e, make_pair(w - 2, make_pair(i, j))); n     } n   } n   cout << e.first << endl; n   for (int i = 1; i <= 3; i++) { n     if (x[i] == e.second.first && y[i] == e.second.second) { n       continue; n     } n     cout << x[i] <<     << y[i] << endl; n     while (x[i] > e.second.first) { n       x[i]--; n       if (x[i] == e.second.first && y[i] == e.second.second) { n         break; n       } n       cout << x[i] <<     << y[i] << endl; n     } n     while (x[i] < e.second.first) { n       x[i]++; n       if (x[i] == e.second.first && y[i] == e.second.second) { n         break; n       } n       cout << x[i] <<     << y[i] << endl; n     } n     while (y[i] > e.second.second) { n       y[i]--; n       if (x[i] == e.second.first && y[i] == e.second.second) { n         break; n       } n       cout << x[i] <<     << y[i] << endl; n     } n     while (y[i] < e.second.second) { n       y[i]++; n       if (x[i] == e.second.first && y[i] == e.second.second) { n         break; n       } n       cout << x[i] <<     << y[i] << endl; n     } n   } n   cout << e.second.first <<     << e.second.second; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100010]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   unsigned long long ans = 0; n   for (int i = 0; i < n; i++) { n     unsigned long long j, k = 0; n     for (j = i; j < n; j++) { n       if (a[i] != a[j]) break; n       k++; n     } n     ans += (k * (k + 1)) / 2; n     i = j - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[5][5005]; n double p[5005], subp[5005]; n int t[5005]; n double qpow(double a, int k) { n   double res = 1; n   while (k) { n     if (k & 1) res *= a; n     a *= a; n     k >>= 1; n   } n   return res; n } n int main(int argc, const char* argv[]) { n   int n, T; n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%d , &p[i], &t[i]); n     p[i] /= 100; n     subp[i] = 1 - p[i]; n   } n   int cur = 1; n   dp[cur ^ 1][0] = 1; n   double ans = 0; n   for (int i = 1; i <= n; ++i, cur ^= 1) { n     double pp = qpow(subp[i], t[i] - 1); n     dp[cur][0] = 0; n     for (int j = 1; j <= T; ++j) { n       double tmp = dp[cur][j - 1]; n       if (j - 1 >= t[i]) tmp -= dp[cur ^ 1][j - 1 - t[i]] * pp; n       tmp *= subp[i]; n       if (j >= t[i]) tmp += dp[cur ^ 1][j - t[i]] * subp[i] * pp; n       tmp += dp[cur ^ 1][j - 1] * p[i]; n       dp[cur][j] = tmp; n       ans += tmp; n     } n   } n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n vector<int> adj[N]; n long long p[N], s[N]; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   for (int i = 1; i <= n; i++) { n     if (~s[i]) { n       int j = p[p[i]]; n       int k = p[i]; n       if (s[i] < s[j]) return puts( -1 ), 0; n       adj[k].push_back(s[i] - s[j]); n     } n   } n   long long dap = s[1]; n   for (int i = 1; i <= n; i++) { n     if (!adj[i].empty()) { n       sort(adj[i].begin(), adj[i].end()); n       dap += adj[i][0]; n       for (int x : adj[i]) dap += (x - adj[i][0]); n     } n   } n   cout << dap << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 200 * 1000 + 24, INF = (1 << 30); n int n; n int a[MAX_N], cnt[MAX_N], mark[MAX_N]; n vector<int> G[MAX_N], vec, avb; n int main() { n   cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i], a[i]--, cnt[a[i]]++; n   for (int i = 0; i < n; i++) n     if (cnt[i] == 0) vec.push_back(i); n   for (int i = 0; i < n; i++) { n     if (cnt[a[i]] > 1) { n       G[a[i]].push_back(i), avb.push_back(i); n       mark[a[i]] = true; n     } n   } n   memset(mark, 0, sizeof mark); n   int j = 0; n   for (int i = 0; i < avb.size(); i++) { n     int p = avb[i]; n     if (mark[a[p]] == false and n         (a[p] < vec[j] or p == G[a[p]][G[a[p]].size() - 1])) { n       mark[a[p]] = true; n     } else { n       a[p] = vec[j++]; n     } n   } n   cout << vec.size() << endl; n   for (int i = 0; i < n; i++) { n     cout << a[i] + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void sublime() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   sublime(); n   long long n, i, j; n   cin >> n; n   long long arr[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   if (n <= 2) { n     cout << 0 <<   n ; n     for (i = 0; i < n; i++) cout << arr[i] <<    ; n     cout <<   n ; n     return 0; n   } else { n     sort(arr, arr + n); n     vector<long long> v; n     i = 0, j = (n - 1) / 2; n     if (n & 1) { n       while (i < (n - 1) / 2 || j < n) { n         v.push_back(arr[j]); n         j++; n         if (i < (n - 1) / 2) { n           v.push_back(arr[i]); n           i++; n         } n       } n     } else { n       j = n / 2; n       while (i < (n - 1) / 2 || j < n) { n         v.push_back(arr[j]); n         j++; n         if (i < (n - 1) / 2) { n           v.push_back(arr[i]); n           i++; n         } n       } n       v.push_back(arr[(n - 1) / 2]); n     } n     long long ans = 0; n     for (i = 1; i < v.size() - 1; i += 2) { n       if (v[i] < v[i + 1] && v[i] < v[i - 1]) ans++; n     } n     cout << ans <<   n ; n     for (auto x : v) cout << x <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T modinv(T a, T n) { n   T i = n, v = 0, d = 1; n   while (a > 0) { n     T t = i / a, x = a; n     a = i % x; n     i = x; n     x = d; n     d = v - t * x; n     v = x; n   } n   return (v + n) % n; n } n long long modpow(long long n, long long k, long long mod) { n   long long ans = 1; n   while (k > 0) { n     if (k & 1) ans = (ans * n) % mod; n     k >>= 1; n     n = (n * n) % mod; n   } n   return ans % mod; n } n template <class T> n string str(T Number) { n   string Result; n   ostringstream convert; n   convert << Number; n   Result = convert.str(); n   return Result; n } n int StringToNumber(const string &Text) { n   istringstream ss(Text); n   int result; n   return ss >> result ? result : 0; n } n template <class T> n inline vector<pair<T, int> > FACTORISE(T n) { n   vector<pair<T, int> > R; n   for (T i = 2; n > 1;) { n     if (n % i == 0) { n       int C = 0; n       for (; n % i == 0; C++, n /= i) n         ; n       R.push_back(make_pair(i, C)); n     } n     i++; n     if (i > n / i) i = n; n   } n   if (n > 1) R.push_back(make_pair(n, 1)); n   return R; n } n template <class T> n inline T TOTIENT(T n) { n   vector<pair<T, int> > R = FACTORISE(n); n   T r = n; n   for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1); n   return r; n } n template <class T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n double rnd(float d) { return floor(d + 0.49); } n int edge[3300][3300], Pairs[3300][3300]; n int match[3300]; n bool seen[3300]; n vector<pair<int, int> > groups; n bool bfs(int v) { n   seen[v] = 1; n   for (int i = 0; i < ((int)(groups).size()); i++) { n     if (edge[v][i] && !seen[i]) { n       seen[i] = 1; n       if (match[i] == -1 || bfs(match[i])) { n         match[v] = i; n         match[i] = v; n         return true; n       } n     } n   } n   seen[v] = 0; n   return false; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> A(n); n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n     int x = A[i]; n     for (int j = 2; j * j <= A[i]; j++) { n       if (j > x) break; n       while (x % j == 0) { n         x = x / j; n         groups.push_back(make_pair(j, i)); n       } n     } n     if (x > 1) groups.push_back(make_pair(x, i)); n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     --a, --b; n     Pairs[a][b] = 1, Pairs[b][a] = 1; n   } n   memset(edge, 0, sizeof(edge)); n   for (int i = 0; i < ((int)(groups).size()); i++) n     for (int j = i + 1; j < ((int)(groups).size()); j++) { n       if (Pairs[groups[i].second][groups[j].second] && n           groups[i].first == groups[j].first) n         edge[i][j] = 1, edge[j][i] = 1; n     } n   memset(match, -1, sizeof(match)); n   int matchings = 0; n   for (int i = 0; i < ((int)(groups).size()); i++) { n     if (match[i] != -1) continue; n     memset(seen, false, sizeof(seen)); n     if (bfs(i)) matchings++; n   } n   cout << matchings << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[205][205][205]; n long long int recur(string &a, string &b, long long int index, long long int k, n                     long long int curr) { n   if (index == a.size()) { n     return 0; n   } n   if (dp[index][k][curr] != -1) { n     return dp[index][k][curr]; n   } n   if (k > 0) { n     if (a[index] == b[0]) { n       return dp[index][k][curr] = n                  max(recur(a, b, index + 1, k, curr + 1), n                      curr + recur(a, b, index + 1, k - 1, curr)); n     } else if (a[index] == b[1]) { n       return dp[index][k][curr] = max(curr + recur(a, b, index + 1, k, curr), n                                       recur(a, b, index + 1, k - 1, curr + 1)); n     } else { n       return dp[index][k][curr] = n                  max(recur(a, b, index + 1, k, curr), n                      max(curr + recur(a, b, index + 1, k - 1, curr), n                          recur(a, b, index + 1, k - 1, curr + 1))); n     } n   } else { n     if (a[index] == b[1]) { n       return dp[index][k][curr] = curr + recur(a, b, index + 1, k, curr); n     } else if (a[index] == b[0]) { n       return dp[index][k][curr] = recur(a, b, index + 1, k, curr + 1); n     } else { n       return dp[index][k][curr] = recur(a, b, index + 1, k, curr); n     } n   } n } n void solve() { n   long long int n, k; n   cin >> n >> k; n   string a, b; n   cin >> a >> b; n   memset(dp, -1, sizeof(dp)); n   if (b[0] == b[1]) { n     long long int curr = 0; n     for (int i = 0; i < a.size(); i++) { n       if (a[i] == b[0]) { n         curr++; n       } n     } n     curr = min(curr + k, n); n     long long int res = (curr) * (curr - 1); n     res = res >> 1; n     cout << res <<   n ; n   } else { n     cout << recur(a, b, 0, k, 0) <<   n ; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 70 + 10; n int n, dist[MAXN * MAXN * MAXN], u[3]; n char arr[MAXN][MAXN]; n int a[MAXN * MAXN * MAXN], b[MAXN * MAXN * MAXN]; n struct node { n   int v[3] = {-1, -1, -1}; n   int A = -1, B = -1; n   node(int v1 = -1, int v2 = -1, int v3 = -1, int A_ = -1, int B_ = -1) { n     v[0] = v1; n     v[1] = v2; n     v[2] = v3; n     A = A_; n     B = B_; n     sort(this->v, this->v + 3); n   } n   int number() { return v[0] + n * v[1] + n * n * v[2]; } n   vector<node> adj() { n     vector<node> adj_; n     for (int j = 0; j < n; j++) { n       if (j == v[0] || j == v[1] || j == v[2]) continue; n       if (arr[v[1]][v[2]] == arr[v[0]][j]) n         adj_.push_back(node(v[1], v[2], j, v[0], j)); n       if (arr[v[0]][v[2]] == arr[v[1]][j]) n         adj_.push_back(node(v[0], v[2], j, v[1], j)); n       if (arr[v[1]][v[0]] == arr[v[2]][j]) n         adj_.push_back(node(v[1], v[0], j, v[2], j)); n     } n     return adj_; n   } n } Q[MAXN * MAXN * MAXN], par[MAXN * MAXN * MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   cin >> u[0] >> u[1] >> u[2]; n   u[0]--; n   u[1]--; n   u[2]--; n   memset(dist, 63, sizeof(dist)); n   dist[node(u[0], u[1], u[2]).number()] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> arr[i][j]; n     } n   } n   int L = 0, R = 0; n   Q[R++] = node(u[0], u[1], u[2]); n   while (L < R) { n     node tp = Q[L++]; n     for (auto i : tp.adj()) n       if (dist[i.number()] == dist[MAXN * MAXN * MAXN - 1]) { n         dist[i.number()] = dist[tp.number()] + 1; n         Q[R++] = i; n         par[i.number()] = tp; n         a[i.number()] = i.A; n         b[i.number()] = i.B; n       } n   } n   if (dist[node(0, 1, 2).number()] != dist[MAXN * MAXN * MAXN - 1]) { n     cout << dist[node(0, 1, 2).number()] <<   n ; n     vector<pair<int, int>> answer; n     node me = node(0, 1, 2); n     for (int i = 0; i < dist[node(0, 1, 2).number()]; i++) { n       answer.push_back(pair<int, int>(a[me.number()], b[me.number()])); n       me = par[me.number()]; n     } n     reverse(answer.begin(), answer.end()); n     for (auto i : answer) cout << i.first + 1 <<     << i.second + 1 <<   n ; n   } else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prime; n int y; n int checker(int n) { n   for (int i = 0; i < y && i < n; i++) { n     if (prime[i] + prime[i + 1] + 1 == n) return 1; n     if (prime[i] + prime[i + 1] + 1 > n) return 0; n   } n } n int isprime(int n) { n   int x = sqrt(n); n   for (int i = 2; i <= x; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int main() { n   int n, k, counter = 0; n   cin >> n >> k; n   prime.push_back(2); n   prime.push_back(3); n   prime.push_back(5); n   y = 3; n   for (int i = 7; i <= n; i++) { n     if (isprime(i)) { n       prime.push_back(i); n       y++; n       if (checker(i)) counter++; n       if (counter >= k) break; n     } n   } n   if (counter >= k) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, m, r, l, h, i, ii, j, x[1000001], y[1000001], z, g, a, b, c, d, n     k, mod = 1e9 + 7, q; n vector<long long> v; n vector<pair<long long, long long> > vv; n queue<pair<long long, long long> > qq; n pair<long long, long long> p[1000001]; n map<long long, long long> mp, mpp, mppp; n set<pair<long long, long long> > se[1000001]; n multiset<long long> sw; n multiset<long long>::iterator it; n string s; n char cc; n int main() { n   ios::sync_with_stdio(0); n   cin >> t; n   while (t--) { n     mp.clear(); n     cin >> n; n     b = 0; n     for (i = 0; i < n; i++) cin >> x[i]; n     sort(x, x + n); n     for (i = 0; i < n; i++) { n       mp[x[i] % 2]++; n       if (x[i] == x[i - 1] + 1 && i > 0) b = 1; n     } n     a = mp[0] % 2; n     c = mp[1] % 2; n     if ((a == c && a == 0) || (a == c && a == 1 && b == 1)) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp1(int& a, int& b); n int64_t gcd(int64_t a, int64_t b); n bool comp2(int& a, int& b); n bool comp3(pair<int, int>& a, pair<int, int>& b); n bool comp4(pair<int, int>& a, pair<int, int>& b); n bool isPrime(int& x); n void input(vector<int>& v, int& n) { n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n   } n } n int sum(vector<int>& v) { n   int s = 0; n   for (auto& el : v) { n     s += el; n   } n   return s; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int64_t ans = 1; n   set<char> simbols; n   int cntQ = 0; n   ; n   for (size_t i = 0; i < s.size(); ++i) { n     if (s[i] ==  ? ) { n       cntQ++; n     } else if (s[i] >=  A  && s[i] <=  J ) { n       simbols.insert(s[i]); n     } n   } n   int k = 10; n   int size = simbols.size(); n   if (s[0] ==  ? ) { n     ans = 9; n     cntQ--; n   } else if (s[0] >=  A  && s[0] <=  J ) { n     size--; n     k--; n     ans = 9; n   } n   for (int i = 0; i < size; ++i) { n     ans *= k; n     k--; n   } n   string g; n   while (ans > 0) { n     g += (ans % 10 +  0 ); n     ans /= 10; n   } n   reverse(g.begin(), g.end()); n   for (int i = 0; i < cntQ; ++i) { n     g +=  0 ; n   } n   cout << g; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ara[500005], k, ara2[500005]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long l, h, i, j, m, z, t, o, a, b; n   cin >> n >> k; n   a = 0; n   for (i = 0; i < n; i++) cin >> ara[i]; n   l = 1; n   h = 10000000000; n   z = 0; n   while (l < h) { n     m = (l + h) / 2; n     t = 0; n     for (i = 0; i < n; i++) t += max(m - ara[i], z); n     if (t <= k) n       l = m + 1; n     else n       h = m - 1; n   } n   o = 1; n   for (j = (max(o, l - 50)); j < l + 50; j++) { n     t = 0; n     for (i = 0; i < n; i++) t += max(j - ara[i], z); n     if (t > k) { n       if (j == 1) n         l = j; n       else n         l = j - 1; n       break; n     } n   } n   t = 0; n   for (i = 0; i < n; i++) { n     t += max(l - ara[i], z); n     ara[i] = max(ara[i], l); n   } n   for (i = 0; i < n; i++) { n     if (ara[i] == l && k != t) { n       ara[i]++; n       t++; n     } n   } n   l = 1; n   h = 10000000000; n   z = 0; n   while (l < h) { n     m = (l + h) / 2; n     t = 0; n     for (i = 0; i < n; i++) t += max(ara[i] - m, z); n     if (t <= k) n       h = m; n     else n       l = m + 1; n   } n   for (j = h + 50; j >= max(o, h - 50); j--) { n     t = 0; n     for (i = 0; i < n; i++) t += max(ara[i] - j, z); n     if (t > k) { n       l = j + 1; n       break; n     } n   } n   t = 0; n   for (i = 0; i < n; i++) { n     t += max(ara[i] - l, z); n     ara[i] = min(ara[i], l); n   } n   for (i = 0; i < n; i++) { n     if (ara[i] == l && k != t) { n       ara[i]--; n       t++; n     } n   } n   sort(ara, ara + n); n   cout << ara[n - 1] - ara[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const long long mxN = 1e5 + 1; n vector<long long> dat; n long long arr[4000], n; n bool dp[2001]; n void solve() { n   cin >> n; n   n *= 2; n   long long p = 0, temp = 0; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   dat.clear(); n   long long i = 0; n   while (i < n) { n     long long j = i + 1; n     while (j < n && arr[i] > arr[j]) { n       j++; n     } n     dat.push_back(j - i); n     i = j; n   } n   memset(dp, 0, sizeof(dp)); n   dp[0] = true; n   for (long long i = 0; i < dat.size(); i++) { n     for (long long j = n / 2; j >= 0; j--) { n       if (j + dat[i] <= n / 2) dp[j + dat[i]] = dp[j] | dp[j + dat[i]]; n     } n   } n   dp[n / 2] ? cout <<  YES n  : cout <<  NO n ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long T = 1, t = 0; n   cin >> T; n   while (t++ < T) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[10000], b[10000], f[10000000]; n int main() { n   int n, ans = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     f[a[i]] = 1; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     f[b[i]] = 1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (f[a[i] ^ b[j]]) ans++; n     } n   } n   if (ans % 2 == 0) n     printf( Karen n ); n   else n     printf( Koyomi n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double CHANGE = 1e-8; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n int main() { n   puts( YES ); n   int n; n   scanf( %d , &n); n   while (n--) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     int ans = 0; n     if (abs(min(x1, x2)) % 2) ans ^= 1; n     if (abs(min(y1, y2)) % 2) ans ^= 2; n     printf( %d n , ans + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO  <<   n ; n     return 0; n   } n   int arr[200005] = {0}; n   deque<int> d; n   for (int i = 0; i < 2 * n; i++) d.push_back(i + 1); n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       arr[i] = d.front(); n       d.pop_front(); n       arr[i + n] = d.front(); n       d.pop_front(); n     } else { n       arr[i] = d.back(); n       d.pop_back(); n       arr[i + n] = d.back(); n       d.pop_back(); n     } n   } n   cout <<  YES  <<   n ; n   for (int i = 0; i < 2 * n; i++) cout << arr[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n; n int fa[maxn]; n bool vis[maxn]; n int cnt_loop = 0; n vector<int> loop[maxn]; n void get(int x, int id) { n   vis[x] = 1; n   loop[id].push_back(x); n   if (vis[fa[x]]) return; n   get(fa[x], id); n } n bool num[maxn]; n int rid[maxn]; n int main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) { n     scanf( %d , &fa[i]); n   } n   for (register int i = 1; i <= n; i++) { n     if (!vis[i]) get(i, ++cnt_loop); n   } n   bool fg2 = 0; n   for (register int i = 1; i <= cnt_loop; i++) { n     num[loop[i].size()] = 1; n     rid[loop[i].size()] = i; n     if (((int)loop[i].size()) & 1) { n       fg2 = 1; n     } n   } n   if (num[1]) { n     printf( YES n ); n     for (register int i = 1; i <= cnt_loop; i++) { n       if (rid[1] == i) continue; n       for (register int j = 0; j < (int)loop[i].size(); j++) { n         printf( %d %d n , loop[rid[1]][0], loop[i][j]); n       } n     } n   } else if (num[2] && !fg2) { n     printf( YES n ); n     for (register int i = 1; i <= cnt_loop; i++) { n       if (rid[2] == i) { n         printf( %d %d n , loop[i][0], loop[i][1]); n         continue; n       } n       for (register int j = 0; j < (int)loop[i].size(); j++) { n         printf( %d %d n , loop[rid[2]][j & 1], loop[i][j]); n       } n     } n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n template <typename T> n inline T Bigmod(T base, T power, T MOD) { n   T ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     map<int, int> ind; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       ind[arr[i]] = i + 1; n     } n     int l, r; n     l = ind[1]; n     string s; n     s.push_back( 1 ); n     if (n > 1) { n       int in = ind[2]; n       if (in > l) n         r = in; n       else { n         r = l; n         l = in; n       } n       if ((r - l) <= 1) n         s.push_back( 1 ); n       else n         s.push_back( 0 ); n     } n     for (int i = 3; i <= n; i++) { n       int in = ind[i]; n       if (in < l) { n         if ((r - in) < i) { n           s.push_back( 1 ); n         } else { n           s.push_back( 0 ); n         } n         l = in; n       } else if (in > r) { n         if ((in - l) < i) { n           s.push_back( 1 ); n         } else { n           s.push_back( 0 ); n         } n         r = in; n       } else { n         if ((r - l) < i) n           s.push_back( 1 ); n         else n           s.push_back( 0 ); n       } n     } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m[20], r[20], c; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &m[i]); n   for (int i = 0; i < n; i++) scanf( %d , &r[i]); n   for (int i = 0; i < 720720; i++) { n     bool you = false; n     for (int j = 0; j < n; j++) n       if (i % m[j] == r[j]) you = true; n     if (you) c++; n   } n   printf( %.6lf n , c / 720720.); n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n using pt = std::pair<T, T>; n using namespace std; n struct poledia { n   int lx, ly, rx, ry; n   int cnt = 0; n   poledia() : lx(- -1e9), ly(- -1e9), ry(-1e9), rx(-1e9){}; n   poledia(bool is_dw) : lx(- -1e9), ly(-1e9), ry(- -1e9), rx(-1e9){}; n }; n struct poleos { n   int x1, y1, x2, y2; n   int cnt = 0; n   poleos() : x1(0), x2(0), y1(-1e9), y2(- -1e9){}; n   poleos(bool is_vert) : x1(- -1e9), x2(-1e9), y1(0), y2(0){}; n }; n struct ferz { n   int x, y; n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<ferz> a(m); n   vector<poledia> diag1(2 * 1e5 + 1); n   vector<poledia> diag2(2 * 1e5 + 1); n   vector<poleos> ossx(1e5 + 1); n   vector<poleos> ossy(1e5 + 1); n   for (int i = 0; i < m; i++) { n     cin >> a[i].x >> a[i].y; n     int k = a[i].x + a[i].y - 1; n     if (diag1[k].cnt == 0) { n       diag1[k] = poledia(true); n     } n     diag1[k].lx = min(a[i].x, diag1[k].lx); n     diag1[k].ly = max(a[i].y, diag1[k].ly); n     diag1[k].rx = max(a[i].x, diag1[k].rx); n     diag1[k].ry = min(a[i].y, diag1[k].ry); n     diag1[k].cnt++; n     k = n - a[i].x + a[i].y; n     diag2[k].lx = min(diag2[k].lx, a[i].x); n     diag2[k].ly = min(diag2[k].ly, a[i].y); n     diag2[k].rx = max(diag2[k].rx, a[i].x); n     diag2[k].ry = max(diag2[k].ry, a[i].y); n     diag2[k].cnt++; n     k = a[i].y; n     if (ossx[k].cnt == 0) { n       ossx[k] = poleos(true); n     } n     ossx[k].x1 = min(ossx[k].x1, a[i].x); n     ossx[k].x2 = max(ossx[k].x2, a[i].x); n     ossx[k].cnt++; n     k = a[i].x; n     ossy[k].y1 = max(ossy[k].y1, a[i].y); n     ossy[k].y2 = min(ossy[k].y2, a[i].y); n     ossy[k].cnt++; n   } n   vector<int> ans(9); n   for (int i = 0; i < m; i++) { n     int sum = 0; n     int k1 = a[i].x + a[i].y - 1, k2 = n - a[i].x + a[i].y; n     if (diag1[k1].cnt != 1) { n       if (a[i].x == diag1[k1].lx && a[i].y == diag1[k1].ly || n           a[i].x == diag1[k1].rx && a[i].y == diag1[k1].ry) n         sum++; n       else n         sum += 2; n     } n     if (diag2[k2].cnt != 1) { n       if (a[i].x == diag2[k2].lx && a[i].y == diag2[k2].ly || n           a[i].x == diag2[k2].rx && a[i].y == diag2[k2].ry) n         sum++; n       else n         sum += 2; n     } n     if (ossx[a[i].y].cnt != 1) { n       if (a[i].x == ossx[a[i].y].x1 || a[i].x == ossx[a[i].y].x2) n         sum++; n       else n         sum += 2; n     } n     if (ossy[a[i].x].cnt != 1) { n       if (a[i].y == ossy[a[i].x].y1 || a[i].y == ossy[a[i].x].y2) n         sum++; n       else n         sum += 2; n     } n     ans[sum]++; n   } n   for (int i = 0; i < 9; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n