#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 3, M = 3e4 + 4, inf = 2e9 + 1; n int m, n, s, t, cur, tim, mn; n int low[N], dfn[N], vis[N], cst[M]; n vector<pair<int, int> > vt[N]; n vector<int> pat, rut, ans; n bool dfs(int u) { n   vis[u] = 1; n   if (u == t) return 1; n   for (int i = 0; i < vt[u].size(); i++) { n     int v = vt[u][i].first; n     int id = vt[u][i].second; n     if (!vis[v] && dfs(v)) { n       pat.push_back(id); n       return 1; n     } n   } n   return 0; n } n bool tarjan(int u, int p = -1) { n   vis[u] = 1; n   bool cap = u == t; n   low[u] = dfn[u] = ++tim; n   for (int i = 0; i < vt[u].size(); i++) { n     int v = vt[u][i].first; n     int id = vt[u][i].second; n     if (id == cur || id == p) continue; n     if (vis[v]) n       low[u] = min(low[u], dfn[v]); n     else { n       bool flag = tarjan(v, id); n       cap = flag || cap; n       low[u] = min(low[u], low[v]); n       if (flag && low[v] == dfn[v]) rut.push_back(id); n     } n   } n   return cap; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &s, &t); n   for (int i = 1; i < m + 1; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     vt[u].push_back(make_pair(v, i)); n     vt[v].push_back(make_pair(u, i)); n     cst[i] = w; n   } n   if (!dfs(s)) { n     puts( 0 n0 n ); n     return 0; n   } n   mn = inf; n   for (int i = 0; i < pat.size(); i++) { n     cur = pat[i]; n     tim = 0; n     memset(vis, 0, sizeof(vis)); n     rut.clear(); n     if (!tarjan(s)) n       if (mn > cst[cur]) { n         mn = cst[cur]; n         ans.clear(); n         ans.push_back(cur); n       } n     for (int j = 0; j < rut.size(); j++) { n       int sec = rut[j]; n       if (mn > cst[sec] + cst[cur]) { n         mn = cst[sec] + cst[cur]; n         ans.clear(); n         ans.push_back(cur); n         ans.push_back(sec); n       } n     } n   } n   if (mn == inf) { n     puts( -1 ); n     return 0; n   } n   int len = ans.size(); n   printf( %d n%d n%d , mn, len, ans[0]); n   if (len > 1) n     printf(  %d n , ans[1]); n   else n     puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, c, d, p, nl, nd, np, min = 0, totl, totp, res; n   cin >> n >> k >> l >> c >> d >> p >> nl >> np; n   nd = d * c; n   totl = (k * l) / nl; n   totp = p / np; n   if (totl <= nd && totl <= totp) n     min = totl; n   else if (nd <= totl && nd <= totp) n     min = nd; n   else if (totp <= totl && totp <= nd) n     min = totp; n   res = min / n; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr << x; } n void __print(const char *x) { cerr << x; } n void __print(const string &x) { cerr << x; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  ,  ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) { n     cerr << (f++ ?  ,   :   ); n     __print(i); n   } n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) { n     cerr <<  ,  ; n   } n   _print(v...); n } n string go(long long i, long long n, long long dist, string nw, long long k, n           string ori) { n   ; n   if (dist > k) { n     return   ; n   } n   if (nw.substr(0, i) < ori.substr(0, i)) { n     return   ; n   } n   if (i >= n) { n     return nw; n   } n   for (char val =  0 ; val <=  9 ; ++val) { n     nw[i] = val; n     bool found = false; n     for (long long j = 0; j < i; ++j) { n       if (nw[j] == val) { n         found = true; n         break; n       } n     } n     string chk = go(i + 1, n, dist + (found == false), nw, k, ori); n     if ((long long)chk.size()) { n       return chk; n     } n   } n   return   ; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   string s = to_string(n); n   long long dist = 0; n   map<char, long long> g; n   for (auto ch : s) { n     g[ch]++; n   } n   string ans = n       go(0, (long long)s.size(), 0, string((long long)s.size(),  0 ), k, s); n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int64_t func(int64_t c) { n   int64_t cost = 0, pc = 1; n   for (int ai : a) { n     long long diff = ai - pc; n     cost += abs(diff); n     if (pc > LLONG_MAX / c) return LLONG_MAX; n     pc *= c; n   } n   return cost; n } n int main(void) { n   int n; n   cin >> n; n   int64_t ans = 0; n   a.resize(n); n   for (int& ai : a) { n     cin >> ai; n     ans += abs(ai - 1); n   } n   sort(a.begin(), a.end()); n   int64_t l = 2, r = 1e5; n   while (r - l > 3) { n     int64_t m1 = l + (r - l) / 3; n     int64_t m2 = r - (r - l) / 3; n     int64_t c1 = func(m1); n     int64_t c2 = func(m2); n     ans = min(ans, c1); n     ans = min(ans, c2); n     if (c1 > c2) n       l = m1; n     else n       r = m2; n   } n   for (int64_t i = l; i <= r; i++) ans = min(ans, func(i)); n   cout << ans <<   n ; n } n


#include <iostream> n  n using namespace std; n  n const int N = 210;  n  n int a[N];  n int main() n { n  int t,i,j,k,n,s1=0,s0=0;  n  cin >> t;  n  while(t--) n  { n      cin>>n; n      s1=0;s0=0; n      for(i=0;i<2*n;i++) n          cin>>a[i]; n      for(i=0;i<2*n;i++) n          { n              if(a[i]%2==1)s1++; n              else s0++; n          } n          if(s1==s0)puts( Yes n ); n          else puts( No n ); n           n  } n  return 0;  n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 1e5 + 7; n int main() { n   int n, a[mxn], q, l[mxn], r[mxn], x; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i == 1) { n       l[i] = 1; n       r[i] = a[i]; n     } else { n       l[i] = r[i - 1] + 1; n       r[i] = a[i] + r[i - 1]; n     } n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int l1 = 0, r1 = n, x; n     cin >> x; n     while (r1 - l1 > 1) { n       int m = (l1 + r1) >> 1; n       if (r[m] >= x) n         r1 = m; n       else n         l1 = m; n     } n     cout << r1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int xs[2123], ys[2123], bs[2123]; n long long fat[2123], prefix[2123][2123]; n long long fatinv[2123]; n long long sum(int row1, int row2, int col1, int col2) { n   if (row1 > row2 || col1 > col2) return 0ll; n   row2 = min(row2, 2000); n   col2 = min(col2, 2000); n   --row1, --col1; n   if (row1 < 0) row1 = 0; n   if (col1 < 0) col1 = 0; n   if (row2 <= row1 or col2 <= col1) return 0ll; n   return prefix[row2][col2] - prefix[row2][col1] - prefix[row1][col2] + n          prefix[row1][col1]; n } n long long sumR(int i, int r) { n   return sum(xs[i] - r, xs[i] + r, ys[i] - r, ys[i] + r); n } n long long fastexp(long long a, long long b, long long m) { n   long long r = 1; n   for (a %= m; b; b >>= 1, a = (a * a) % m) n     if (b & 1) r = (r * a) % m; n   return r; n } n long long comb(int n, int k) { n   if (n < k) return 0ll; n   return (((fat[n] * fatinv[k]) % 1000000007) * fatinv[n - k]) % 1000000007; n } n int main() { n   fat[0] = 1; n   for (int i = 1; i < 2123; ++i) fat[i] = (fat[i - 1] * i) % 1000000007; n   int n, m, r; n   cin >> n >> m >> r; n   for (int i = 0; i < 2123; ++i) n     fatinv[i] = fastexp(fat[i], 1000000007 - 2, 1000000007); n   for (int i = 0; i < n; ++i) { n     cin >> xs[i] >> ys[i] >> bs[i]; n     prefix[xs[i]][ys[i]] = 1; n   } n   for (int i = 1; i < 2123; ++i) { n     for (int j = 1; j < 2123; ++j) { n       prefix[i][j] += n           prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1]; n     } n   } n   long long resp = 0ll; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       int row1 = xs[i] - r; n       int row2 = xs[i] + r; n       int col1 = ys[i] - r; n       int col2 = ys[i] + r; n       row1 = max(row1, xs[j] - r); n       row2 = min(row2, xs[j] + r); n       col1 = max(col1, ys[j] - r); n       col2 = min(col2, ys[j] + r); n       long long s = sum(row1, row2, col1, col2); n       long long si = sumR(i, r); n       long long sj = sumR(j, r); n       long long diff = si + sj - s; n       long long count = (((comb(n, m) - comb(n - si, m) - comb(n - sj, m) + n                            comb(n - diff, m)) % n                           1000000007) * n                          bs[i]) % n                         1000000007; n       resp = (resp + (count * bs[j]) % 1000000007) % 1000000007; n     } n   } n   if (resp < 0) resp += 1000000007; n   cout << resp <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10], flag[200]; n int main() { n   int sum = 0; n   for (int i = 1; i <= 5; i++) { n     cin >> a[i]; n     sum += a[i]; n     flag[a[i]]++; n   } n   int ans = sum; n   for (int i = 1; i <= 5; i++) { n     if (flag[a[i]] > 1 && flag[a[i]] < 4) n       ans = min(ans, sum - flag[a[i]] * a[i]); n     else if (flag[a[i]] > 3) { n       ans = min(ans, sum - 3 * a[i]); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace p316GGoodSubstrings { n template <size_t MAXLEN, size_t ALPHABET> n struct SuffixAutomaton { n   struct Node { n     Node *child[ALPHABET], *suff, *next; n     int len, cnt; n   }; n   Node pool[2 * MAXLEN], *root, *nextnode, *last, *first[MAXLEN + 1]; n   Node* makenode(int length) { n     memset(nextnode, 0, sizeof(Node)); n     nextnode->len = length; n     return nextnode++; n   } n   void init() { n     nextnode = pool; n     last = root = makenode(0); n     memset(first, 0, sizeof(first)); n   } n   void extend(int ch) { n     Node *p = last, *cur = makenode(p->len + 1); n     cur->cnt = 1; n     for (; p != NULL && p->child[ch] == NULL; p = p->suff) p->child[ch] = cur; n     if (p == NULL) n       cur->suff = root; n     else { n       Node* q = p->child[ch]; n       if (p->len + 1 == q->len) n         cur->suff = q; n       else { n         Node* copy = makenode(p->len + 1); n         memcpy(copy->child, q->child, sizeof(q->child)); n         copy->suff = q->suff; n         q->suff = cur->suff = copy; n         for (; p != NULL && p->child[ch] == q; p = p->suff) p->child[ch] = copy; n       } n     } n     last = cur; n   } n   void go() { n     for (Node* i = pool; i < nextnode; i++) { n       i->next = first[i->len]; n       first[i->len] = i; n     } n     for (int len = MAXLEN; len > 0; len--) n       for (Node* i = first[len]; i != NULL; i = i->next) i->suff->cnt += i->cnt; n   } n }; n struct TNode { n   TNode* ch[26]; n   bool bad; n   TNode() : bad(false) { memset(ch, 0, sizeof(ch)); } n }; n SuffixAutomaton<55555, 26> sam; n TNode* root; n char str[55555]; n int l, r, res = 0; n void go(TNode* at, SuffixAutomaton<55555, 26>::Node* cur) { n   int cnt = cur ? cur->cnt : 0; n   if (cnt < l || cnt > r) at->bad = true; n   for (int c = 0; c < 26; c++) n     if (at->ch[c] != NULL) n       go(at->ch[c], cur && cur->child[c] ? cur->child[c] : NULL); n } n void dfs(TNode* at) { n   if (!at->bad) res++; n   for (int c = 0; c < 26; c++) n     if (at->ch[c]) dfs(at->ch[c]); n } n void solve(int test_num) { n   root = new TNode(); n   scanf( %s , str); n   int len = strlen(str); n   for (int i = 0; i < len; i++) { n     TNode* at = root; n     for (int j = i, c; j < len; j++) { n       c = str[j] -  a ; n       if (at->ch[c] == NULL) at->ch[c] = new TNode(); n       at = at->ch[c]; n     } n   } n   int cnt; n   scanf( %d , &cnt); n   for (int q = 0; q < cnt; q++) { n     scanf( %s %d %d , str, &l, &r); n     sam.init(); n     for (int i = 0; str[i] >    ; i++) sam.extend(str[i] -  a ); n     sam.go(); n     go(root, sam.root); n   } n   dfs(root); n   if (!root->bad) res--; n   cout << res << endl; n } n void solve() { solve(1); } n }  // namespace p316GGoodSubstrings n int main() { n   p316GGoodSubstrings::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n const double eps = 1e-10; n const int N = 3e5 + 5; n int n, m, d[N]; n vector<pair<int, int> > g[N]; n vector<int> vec; n bool vis[N]; n void dfs(int u) { n   vis[u] = true; n   for (auto &e : g[u]) { n     int &v = e.first; n     if (vis[v]) continue; n     dfs(v); n     if (d[v] == 1) { n       if (~d[u]) d[u] ^= 1; n       d[v] = 0, vec.push_back(e.second); n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   int s = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , d + i); n     if (d[i] == -1) s = i; n   } n   for (int i = 1, u, v; i <= m; i++) { n     scanf( %d %d , &u, &v); n     g[u].push_back(pair<int, int>(v, i)); n     g[v].push_back(pair<int, int>(u, i)); n   } n   dfs(s); n   if (~d[s] && d[s]) { n     puts( -1 ); n     return 0; n   } n   printf( %d n , vec.size()); n   for (int id : vec) printf( %d n , id); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   int fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= fh; n } n struct Onfo { n   int nu, ne; n } a[200010 * 2]; n struct Info { n   int x, t, we, an; n } v[200010]; n struct seg { n   int l, r, ma, ta, de; n } t[200010 * 4]; n int b[200010], num, m, n, x, so[200010], si[200010], d[200010], f[200010], n     tp[200010], bi[200010], fb[200010]; n vector<int> no; n bool co(const Info &a, const Info &b) { n   if (a.t + d[a.x] != b.t + d[b.x]) return a.t + d[a.x] < b.t + d[b.x]; n   return a.x < b.x; n } n bool c1(const Info &a, const Info &b) { return a.we < b.we; } n void jb(int x, int y) { n   a[++num].nu = y; n   a[num].ne = b[x]; n   b[x] = num; n } n void dfs(int x, int fa, int dep) { n   si[x] = 1; n   so[x] = 0; n   f[x] = fa; n   d[x] = dep; n   for (int y = b[x]; y; y = a[y].ne) { n     if (a[y].nu != fa) { n       dfs(a[y].nu, x, dep + 1); n       si[x] += si[a[y].nu]; n       if (si[a[y].nu] > si[so[x]]) so[x] = a[y].nu; n     } n   } n } n void dfs1(int x) { n   bi[x] = ++num; n   fb[num] = x; n   if (so[x] != 0) { n     tp[so[x]] = tp[x]; n     dfs1(so[x]); n   } n   for (int y = b[x]; y; y = a[y].ne) { n     if (a[y].nu != f[x] && a[y].nu != so[x]) { n       tp[a[y].nu] = a[y].nu; n       dfs1(a[y].nu); n     } n   } n } n void build(int nu, int l, int r) { n   t[nu].ma = -2e9; n   t[nu].l = l; n   t[nu].r = r; n   t[nu].ta = -2e9; n   if (l != r) { n     build(nu * 2, l, (l + r) / 2); n     build(nu * 2 + 1, (l + r) / 2 + 1, r); n     t[nu].de = t[nu * 2 + 1].de; n   } else { n     t[nu].de = d[fb[l]]; n   } n } n void ya(int nu) { n   if (t[nu].ta != -2e9) { n     if (t[nu].l != t[nu].r) { n       t[nu * 2].ta = t[nu].ta; n       t[nu * 2 + 1].ta = t[nu].ta; n     } n     t[nu].ma = t[nu].ta + t[nu].de * 2; n     t[nu].ta = -2e9; n   } n } n int que(int nu, int l, int r) { n   ya(nu); n   if (t[nu].l == l && t[nu].r == r) return t[nu].ma; n   int mid = (t[nu].l + t[nu].r) / 2; n   if (l > mid) return que(nu * 2 + 1, l, r); n   if (r <= mid) return que(nu * 2, l, r); n   return max(que(nu * 2, l, mid), que(nu * 2 + 1, mid + 1, r)); n } n void get(int nu, int l, int r) { n   ya(nu); n   if (t[nu].l == l && t[nu].r == r) { n     no.push_back(nu); n     return; n   } n   int mid = (t[nu].l + t[nu].r) / 2; n   if (l <= mid) get(nu * 2, l, min(mid, r)); n   if (r > mid) get(nu * 2 + 1, max(mid + 1, l), r); n } n int getans(int nu, int y) { n   if (t[nu].l == t[nu].r) return fb[t[nu].l]; n   ya(nu * 2 + 1); n   if (t[nu * 2 + 1].ma <= y) return getans(nu * 2, y); n   return getans(nu * 2 + 1, y); n } n int get(int x, int y) { n   while (x != 0) { n     if (que(1, bi[tp[x]], bi[x]) > y) { n       no.clear(); n       get(1, bi[tp[x]], bi[x]); n       for (int i = no.size() - 1; i >= 0; i--) { n         if (t[no[i]].ma > y) return getans(no[i], y); n       } n     } n     x = f[tp[x]]; n   } n   return x; n } n void add(int nu, int l, int r, int x) { n   ya(nu); n   if (t[nu].l == l && t[nu].r == r) { n     t[nu].ta = x; n     return; n   } n   int mid = (t[nu].l + t[nu].r) / 2; n   if (l <= mid) add(nu * 2, l, min(mid, r), x); n   if (r > mid) add(nu * 2 + 1, max(mid + 1, l), r, x); n   ya(nu * 2); n   ya(nu * 2 + 1); n   t[nu].ma = max(t[nu * 2].ma, t[nu * 2 + 1].ma); n } n void fu(int x, int y, int z) { n   while (tp[x] != tp[y]) { n     add(1, bi[tp[x]], bi[x], z); n     x = f[tp[x]]; n   } n   if (x != y) add(1, bi[y] + 1, bi[x], z); n } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= n; i++) { n     read(x); n     jb(x + 1, i + 1); n     jb(i + 1, x + 1); n   } n   n++; n   dfs(1, 0, 1); n   num = 0; n   tp[1] = 1; n   dfs1(1); n   build(1, 1, n); n   add(1, 1, 1, 2e9); n   for (int i = 1; i <= m; i++) { n     read(v[i].x); n     read(v[i].t); n     v[i].x++; n     v[i].we = i; n   } n   sort(v + 1, v + m + 1, co); n   for (int i = 1; i <= m; i++) { n     v[i].an = get(v[i].x, v[i].t + d[v[i].x]); n     fu(v[i].x, v[i].an, v[i].t + d[v[i].x] - d[v[i].an] * 2); n   } n   sort(v + 1, v + m + 1, c1); n   for (int i = 1; i <= m; i++) n     printf( %d  , v[i].t + d[v[i].x] * 2 - d[v[i].an] * 2); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n long double pi = 3.1415926535897932384626433832795; n const int N = 5e5 + 10, lg = 20; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   long long a, b, c; n   cin >> a >> b >> c; n   if (c == 0) { n     if (a != b) n       cout <<  NO ; n     else n       cout <<  YES ; n     return 0; n   } n   if (a == b || ((a > b && c < 0) && (a - b) % (-c) == 0) || n       ((a < b && c > 0) && (b - a) % c == 0)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int check(vector<long long> &a, long long q) { n   multiset<long long> s; n   for (int j = 0; j < n; ++j) { n     if (q + a[j] >= 0) { n       q += a[j]; n       s.insert(a[j]); n     } else if (a[j] > (*s.begin())) { n       q += (a[j] - (*s.begin())); n       s.erase(s.begin()); n       s.insert(a[j]); n     } n   } n   return s.size(); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   vector<pair<long long, int> > b(m); n   vector<int> out(m); n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; ++i) { n     cin >> b[i].first; n     b[i].second = i; n   } n   sort(b.begin(), b.end()); n   int pre = -1, li = 0; n   for (int i = 0; i <= n && pre != m - 1; ++i) { n     int l = pre + 1, h = m - 1; n     while (l < h) { n       int md = (h + l) >> 1; n       if (check(a, b[md].first) >= i) { n         h = md; n       } else { n         l = md + 1; n       } n     } n     if (check(a, b[l].first) != i) { n       continue; n     } n     for (int j = pre + 1; j < l; ++j) { n       out[b[j].second] = n - li; n     } n     li = i; n     out[b[l].second] = n - i; n     pre = l; n   } n   while (pre != m) { n     out[b[pre++].second] = n - li; n   } n   for (int i = 0; i < m; ++i) { n     cout << out[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool life[1000005]; n int main() { n   long long a, b; n   int l, r, mid; n   int ans, cnt = 0; n   scanf( %lld%lld , &a, &b); n   l = 0; n   r = 1000000007; n   while (l != r) { n     mid = (r + l + 1) / 2; n     if (((mid + 1LL) * mid) / 2 > a + b) n       r = mid - 1; n     else n       l = mid; n   } n   ans = l; n   for (int i = l; i > 0; i--) { n     if (a >= i) { n       a -= i; n       life[i] = 1; n       cnt++; n     } n   } n   printf( %d n , cnt); n   if (cnt) { n     for (int i = 1; i <= l; i++) { n       if (life[i]) printf( %d  , i); n     } n     printf(  n ); n   } n   printf( %d n , ans - cnt); n   if (ans - cnt) { n     for (int i = 1; i <= l; i++) { n       if (!life[i]) printf( %d  , i); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int addingval[100]; n long long int lastOcc[100]; n long long int wt[100]; n long long int cum[100005]; n map<long long int, long long int> mp[100]; n int main() { n   for (int i = 0; i < 26; ++i) cin >> wt[i]; n   string s; n   cin >> s; n   int sz = s.size(); n   s =  $  + s; n   long long int cnt = 0; n   for (int i = 1; i <= sz; ++i) { n     char c = s[i] -  a ; n     cum[i] = cum[i - 1] + wt[c]; n     cnt += mp[c][cum[i - 1]]; n     mp[c][cum[i]]++; n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int a[26][26]; n int main() { n   int n; n   cin >> n; n   memset(a, 0, sizeof(a)); n   char ch[20]; n   int len; n   for (int i = 1; i <= n; i++) { n     scanf(  %s , ch); n     for (len = 0; ch[len]; len++) n       ; n     for (int j = 0; j < 26; j++) n       if (a[j][ch[0] -  a ]) { n         a[j][ch[len - 1] -  a ] = n             max(a[j][ch[len - 1] -  a ], a[j][ch[0] -  a ] + len); n       } n     a[ch[0] -  a ][ch[len - 1] -  a ] = n         max(a[ch[0] -  a ][ch[len - 1] -  a ], len); n   } n   int ans = 0; n   for (int i = 0; i < 26; i++) ans = max(ans, a[i][i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1 << 20; n int bio[MAX]; n vector<int> V[MAX]; n int a[MAX], b[MAX]; n int deg[MAX]; n void dfs(int x) { n   if (bio[x]++) return; n   for (auto it : V[x]) dfs(it); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int usebe = 0; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a[i], &b[i]); n     a[i]--; n     b[i]--; n     V[a[i]].push_back(b[i]); n     V[b[i]].push_back(a[i]); n     if (a[i] == b[i]) n       usebe++; n     else { n       deg[a[i]]++; n       deg[b[i]]++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (V[i].size()) { n       dfs(i); n       break; n     } n   } n   for (int i = 0; i < n; i++) n     if (!bio[i] && V[i].size()) { n       printf( 0 n ); n       return 0; n     } n   long long rje = 0; n   rje += (long long)usebe * (usebe - 1) / 2; n   rje += (long long)usebe * (m - usebe); n   for (int i = 0; i < n; i++) rje += (long long)deg[i] * (deg[i] - 1) / 2; n   printf( %lld n , rje); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int n, m; n int fir[N + 10][2], nex[N + 10][2], tot[2], en[N + 10][2]; n int tag[N + 10][2], vis[N + 10]; n void addEdge(int x, int y, int p) { n   tot[p]++; n   nex[tot[p]][p] = fir[x][p]; n   fir[x][p] = tot[p]; n   en[tot[p]][p] = y; n } n void dfs1(int x) { n   vis[x] = 1; n   for (int tmp = fir[x][0]; tmp > 0; tmp = nex[tmp][0]) { n     int y = en[tmp][0]; n     if (vis[y] == 0) { n       dfs1(y); n     } else if (vis[y] == 1) { n       puts( -1 ); n       exit(0); n     } n   } n   vis[x] = 2; n } n void dfs2(int x, int p) { n   if (tag[x][p] == 1) { n     return; n   } n   tag[x][p] = 1; n   for (int tmp = fir[x][p]; tmp > 0; tmp = nex[tmp][p]) { n     int y = en[tmp][p]; n     if (tag[y][p] == 0) { n       dfs2(y, p); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     addEdge(x, y, 0); n     addEdge(y, x, 1); n   } n   for (int i = 1; i <= n; i++) n     if (vis[i] == 0) { n       dfs1(i); n     } n   string ans =   ; n   int cnta = 0; n   for (int i = 1; i <= n; i++) { n     if (tag[i][0] == 0 && tag[i][1] == 0) { n       ans +=  A ; n       cnta++; n     } else { n       ans +=  E ; n     } n     dfs2(i, 0); n     dfs2(i, 1); n   } n   printf( %d n , cnta); n   printf( %s n , ans.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[100001]; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   cin >> s; n   bool gotwater = false; n   long long curt = 0, energy = 0, totwalk = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  L ) { n       curt += arr[i]; n       if (energy < arr[i]) { n         long long diff = arr[i] - energy; n         if (gotwater) n           curt += diff * 3; n         else n           curt += diff * 5; n         energy += diff; n       } n       energy -= arr[i]; n     } else { n       energy += arr[i]; n       if (s[i] ==  W ) { n         gotwater = true; n         curt += arr[i] * 3; n       } else { n         curt += arr[i] * 5; n         totwalk += arr[i] * 2; n       } n     } n     totwalk = min(totwalk, energy); n   } n   if (energy > 0) { n     curt -= totwalk * 2; n     curt -= (energy - totwalk); n   } n   cout << curt; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000000 + 7; n int main() { n   long long a, b; n   cin >> a >> b; n   long long ans = 0; n   for (long long r = 1; r < b; ++r) { n     long long tmp = (a * (a + 1) / 2) % mod; n     tmp = (tmp * r) % mod; n     tmp = (tmp * b) % mod; n     tmp = (tmp + (a * r) % mod) % mod; n     ans = (ans + tmp) % mod; n   } n   if (ans < 0) ans += mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[101010]; n long long l, r, pref[101010]; n long long ans, cnt, sum, max1, min1, ali; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   string s; n   cin >> n; n   while (n--) { n     cin >> l >> r; n     if (l >= r) n       cout << r << endl; n     else { n       min1 = r / l; n       max1 = r % l; n       ans = ((min1 + 1) * (min1 + 1)) * max1; n       ans += (min1 * min1) * (l - max1); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n const int mod = 998244353; n const int maxn = 2e5 + 10; n struct edge { n   int st, ed; n   int w; n   bool operator<(const edge &b) const { return w < b.w; } n   edge() {} n   edge(int st, int ed, int w) : st(st), ed(ed), w(w) {} n }; n edge e[maxn]; n int vet[805]; n ll dis[805][805]; n int temp[805], cnt; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < m; ++i) { n     int x, y, w; n     scanf( %d %d %d , &x, &y, &w); n     e[i] = edge(x, y, w); n   } n   sort(e, e + m); n   for (int i = 0; i < min(m, k); ++i) { n     temp[cnt++] = e[i].st; n     temp[cnt++] = e[i].ed; n   } n   sort(temp, temp + cnt); n   cnt = unique(temp, temp + cnt) - temp; n   memset(dis, 0x3f, sizeof(dis)); n   for (int i = 0; i < min(m, k); ++i) { n     int x, y; n     x = lower_bound(temp, temp + cnt, e[i].st) - temp; n     y = lower_bound(temp, temp + cnt, e[i].ed) - temp; n     dis[x][y] = min(dis[x][y], (ll)e[i].w); n     dis[y][x] = dis[x][y]; n   } n   for (int a = 0; a < cnt; ++a) { n     for (int i = 0; i < cnt; ++i) { n       for (int j = i + 1; j < cnt; ++j) { n         dis[i][j] = min(dis[i][j], dis[i][a] + dis[a][j]); n         dis[j][i] = dis[i][j]; n       } n     } n   } n   vector<ll> ans; n   for (int i = 0; i < cnt; ++i) { n     for (int j = i + 1; j < cnt; ++j) ans.push_back(dis[i][j]); n   } n   sort(ans.begin(), ans.end()); n   printf( %lld n , ans[k - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int check(long long int a) { n   if (a >= 1000000000 + 7) a %= 1000000000 + 7; n   return a; n } n long long int check2(long long int a) { n   if (a > 0) return a; n   long long int b = a / 1000000000 + 7; n   a -= b * 1000000000 + 7; n   if (a < 0) a += 1000000000 + 7; n   return a; n } n long long int exp(long long int a, long long int n) { n   if (n == 0) return 1; n   if (n == 1) return check(a); n   long long int b = exp(a, n / 2); n   if (n % 2 == 0) return check(b * b); n   return check(b * check(b * a)); n } n int arr[4][4]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < 4; j++) cin >> arr[i][j]; n   for (int i = 0; i < 4; i++) { n     if (arr[i][3] == 1) n       for (int j = 0; j < 3; j++) n         if (arr[i][j] == 1) { n           cout <<  YES ; n           return 0; n         } n   } n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < 3; j++) { n       int k = (i + 1 + j) % 4; n       if (arr[i][3] == 1 && arr[i][3] == arr[k][j]) { n         cout <<  YES ; n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:33554432 ) n using namespace std; n const double PI = 2 * acos(0.0); n const double EPS = 1e-8; n const int INF = (1 << 30) - 1; n int n; n string s; n vector<int> p; n set<int> ans; n void add01(char c1, char c2, int a, int b) { n   for (int i = 0; i < a; ++i) s[p[i]] = c1; n   for (int i = 0; i < b; ++i) s[p[a + i]] = c2; n } n int getans() { n   int i = n - 1; n   while (s[i] !=  0 ) --i; n   int j = n - 1; n   while (s[j] !=  1 ) --j; n   if (i < j) n     return 1; n   else n     return 2; n } n void printbin(int n) { cout << n / 2 << n % 2 << endl; } n int main() { n   cin >> s; n   if ((int)((s).size()) % 2 == 1) s =  0  + s; n   n = (int)((s).size()); n   int cnt = 0, cnt1 = 0, cnt0 = 0; n   for (int i = 0; i < n; ++i) n     if (s[i] ==  ? ) { n       ++cnt; n       p.push_back(i); n     } else if (s[i] ==  1 ) n       ++cnt1; n     else if (s[i] ==  0 ) n       ++cnt0; n   if (cnt0 >= cnt1 + cnt + 2) { n     cout <<  00 ; n     return 0; n   } n   if (cnt1 >= cnt0 + cnt + 2) { n     cout <<  11 ; n     return 0; n   } n   if (cnt0 + cnt >= cnt1 + 2) ans.insert(0); n   if (cnt1 + cnt >= cnt0 + 2) ans.insert(3); n   add01( 0 ,  1 , n / 2 - cnt0, n / 2 - cnt1); n   ans.insert(getans()); n   add01( 1 ,  0 , n / 2 - cnt1, n / 2 - cnt0); n   ans.insert(getans()); n   for (set<int>::iterator i = ans.begin(); i != ans.end(); ++i) printbin(*i); n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2 * 100000 + 10; n int _min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n int _max(int a, int b) { n   if (a > b) n     return a; n   else n     return b; n } n char s[1005]; n int col[4] = {0}; n int row[4] = {0}; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  0 ) { n       for (int j = 0; j < 4; j++) { n         if (col[j] == 0) { n           col[j] = 1; n           printf( %d %d n , 1, j + 1); n           break; n         } n       } n     } else { n       for (int j = 0; j < 4; j++) { n         if (row[j] == 0) { n           row[j] = 1; n           if (j == 0) printf( 3 1 n ); n           if (j == 1) printf( 3 3 n ); n           if (j == 2) printf( 4 1 n ); n           if (j == 3) printf( 4 3 n ); n           break; n         } n       } n     } n     int flag = 0; n     for (int j = 0; j < 4; j++) { n       if (col[j] == 0) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       for (int j = 0; j < 4; j++) col[j] = 0; n     } n     if (row[0] == 1 && row[1] == 1) { n       row[0] = 0; n       row[1] = 0; n     } n     if (row[2] == 1 && row[3] == 1) { n       row[2] = 0; n       row[3] = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, v, t, p, cnt1, cnt2, x; n vector<pair<int, int>> k, c; n vector<int> ans; n void add(bool sm) { n   v--; n   if (sm) { n     x += k[cnt1].first; n     ans.push_back(k[cnt1++].second); n   } else { n     x += c[cnt2].first; n     ans.push_back(c[cnt2++].second); n     v--; n   } n } n void solve() { n   cin >> n >> v; n   for (int i = 0; i < n; ++i) { n     cin >> t >> p; n     t--; n     if (t) { n       c.push_back({p, i + 1}); n     } else { n       k.push_back({p, i + 1}); n     } n   } n   sort(k.begin(), k.end()); n   sort(c.begin(), c.end()); n   reverse(k.begin(), k.end()); n   reverse(c.begin(), c.end()); n   while ((v >= 1 && cnt1 < k.size()) || (v >= 2 && cnt2 < c.size())) { n     add((cnt2 == c.size() || v == 1) || (k[cnt1].first > c[cnt2].first) || n         (cnt1 < k.size() - 1 && n          k[cnt1].first + k[cnt1 + 1].first > c[cnt2].first)); n   } n   cout << x << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   map<long long int, long long int> mpp; n   long long int n; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     mpp[__builtin_popcount(x)]++; n   } n   long long int ans = 0; n   for (auto p : mpp) { n     ans += (p.second) * (p.second - 1) / 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int N, T, K; n int a[200333]; n int lo[200333], hi[200333]; n int mid[200333]; n int main() { n   scanf( %d%d%d , &N, &T, &K); n   for (int i = 1; i <= T; i++) scanf( %d , &a[i]); n   K = N - K - 1; n   for (int t = 1; t < T; t++) hi[t] = min(a[t], a[t + 1]); n   for (int t = 1; t < T; t++) lo[t] = 1; n   int hiSum = 0; n   for (int t = 1; t < T; t++) hiSum += hi[t]; n   int loSum = 0; n   for (int t = 1; t < T; t++) loSum += lo[t]; n   bool possible = loSum <= K && K <= hiSum; n   if (!possible) { n     puts( -1 ); n     exit(0); n   } n   for (int t = 1; t <= T; t++) { n     mid[t] = lo[t]; n     K -= lo[t]; n   } n   for (int t = 1; K; t++) { n     int val = min(K, hi[t] - mid[t]); n     K -= val; n     mid[t] += val; n   } n   printf( %d n , N); n   for (int v = 2; v <= 1 + a[1]; v++) printf( %d %d n , 1, v); n   int uStart = 2; n   for (int t = 1; t < T; t++) { n     int u = uStart; n     int upCnt = mid[t] - 1; n     for (int v = uStart + a[t]; v < uStart + a[t] + a[t + 1]; v++) { n       printf( %d %d n , u, v); n       if (upCnt) { n         upCnt--; n         u++; n       } n     } n     uStart = uStart + a[t]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1005; n int parent[2 * M]; n vector<pair<pair<int, int>, pair<int, int> > > edges; n void init() { n   for (int i = 0; i < 2 * M; i++) parent[i] = i; n } n int find(int u) { n   if (u == parent[u]) return u; n   return parent[u] = find(parent[u]); n } n void un(int u, int v) { n   u = find(u); n   v = find(v); n   parent[u] = v; n } n int main() { n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     edges.push_back(make_pair(make_pair(c, i), make_pair(a, b))); n   } n   sort(edges.begin(), edges.end(), n        greater<pair<pair<int, int>, pair<int, int> > >()); n   while (q--) { n     int l, r; n     scanf( %d %d , &l, &r); n     int ans = -1; n     init(); n     for (int i = 0; i < edges.size(); i++) { n       int c = edges[i].first.first; n       int id = edges[i].first.second; n       int u = edges[i].second.first; n       int v = edges[i].second.second; n       if (id < l || id > r) continue; n       if (find(u) == find(v)) { n         ans = c; n         break; n       } n       un(u, v + n); n       un(u + n, v); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long qp(long long x, long long y) { n   long long r = 1; n   for (; y; y >>= 1, x = x * x % 1000000007ll) n     if (y & 1) r = r * x % 1000000007ll; n   return r; n } n long long c[1000010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (~x) c[x]++; n   } n   long long r = 0; n   for (int i = 1; i < 10010; i++) n     r = (r + qp(2, c[i]) - 1 + 1000000007ll) % 1000000007ll; n   cout << (qp(2, n - 1) - 1 - r + 1000000007ll) % 1000000007ll; n } n


#include <bits/stdc++.h> n using namespace std; n const double inf = 1e13; n const double eps = 1e-9; n int Sgn(double k) { return fabs(k) < eps ? 0 : (k < 0 ? -1 : 1); } n int Cmp(double k1, double k2) { return Sgn(k1 - k2); } n struct Point { n   double x, y; n }; n Point operator-(Point k1, Point k2) { n   return (Point){k1.x - k2.x, k1.y - k2.y}; n } n Point operator+(Point k1, Point k2) { n   return (Point){k1.x + k2.x, k1.y + k2.y}; n } n double operator*(Point k1, Point k2) { return k1.x * k2.x + k1.y * k2.y; } n double operator^(Point k1, Point k2) { return k1.x * k2.y - k1.y * k2.x; } n Point operator*(Point k1, double k2) { return (Point){k1.x * k2, k1.y * k2}; } n Point operator/(Point k1, double k2) { return (Point){k1.x / k2, k1.y / k2}; } n double GetLen(Point k) { return sqrt(k * k); } n double GetDisPointToPoint(Point k1, Point k2) { n   return sqrt((k2 - k1) * (k2 - k1)); n } n Point Rotate(Point k, double ang) { n   return (Point){k.x * cos(ang) - k.y * sin(ang), n                  k.x * sin(ang) + k.y * cos(ang)}; n } n Point Rotate90(Point k) { return (Point){-k.y, k.x}; } n struct Circle { n   Point o; n   double r; n }; n bool IsInside(Circle k1, Circle k2) { n   double dis = GetDisPointToPoint(k1.o, k2.o); n   if (Cmp(k1.r - k2.r, dis) >= 0 || Cmp(k2.r - k1.r, dis) >= 0) return true; n   return false; n } n bool IsOutside(Circle k1, Circle k2) { n   double dis = GetDisPointToPoint(k1.o, k2.o); n   if (Cmp(dis, k1.r + k2.r) > 0) return true; n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cout << fixed << setprecision(10); n   Point s, t; n   cin >> s.x >> s.y >> t.x >> t.y; n   int n; n   cin >> n; n   vector<Circle> c(n); n   for (auto &it : c) cin >> it.o.x >> it.o.y >> it.r; n   Point mid_point = (s + t) / 2.0; n   Point dir = Rotate90(t - s) / GetLen(t - s); n   double dis_div2 = GetDisPointToPoint(s, mid_point); n   bool ans_flag = true; n   for (auto &it : c) { n     Circle cur = (Circle){mid_point, dis_div2}; n     if (!IsInside(cur, it) && !IsOutside(cur, it)) { n       ans_flag = false; n       break; n     } n   } n   if (ans_flag) { n     cout << dis_div2 << endl; n     return 0; n   } n   vector<pair<double, double>> record; n   for (auto &it : c) { n     bool dir_flag; n     if (Sgn((it.o - mid_point) * dir) > 0) n       dir_flag = true; n     else n       dir_flag = false; n     double inside_record, outside_record; n     Circle binary_search_cur; n     int binary_search_cnt = 0; n     double binary_search_left = -inf, binary_search_right = inf; n     while (Cmp(binary_search_left, binary_search_right) && n            binary_search_cnt++ < 100) { n       double binary_search_mid = n           (binary_search_left + binary_search_right) / 2.0; n       binary_search_cur.o = dir * binary_search_mid + mid_point; n       binary_search_cur.r = n           sqrt(binary_search_mid * binary_search_mid + dis_div2 * dis_div2); n       if (IsInside(binary_search_cur, it)) { n         inside_record = binary_search_mid; n         if (dir_flag) n           binary_search_right = binary_search_mid; n         else n           binary_search_left = binary_search_mid; n       } else { n         if (dir_flag) n           binary_search_left = binary_search_mid; n         else n           binary_search_right = binary_search_mid; n       } n     } n     binary_search_cnt = 0; n     binary_search_left = -inf, binary_search_right = inf; n     while (Cmp(binary_search_left, binary_search_right) && n            binary_search_cnt++ < 100) { n       double binary_search_mid = n           (binary_search_left + binary_search_right) / 2.0; n       binary_search_cur.o = dir * binary_search_mid + mid_point; n       binary_search_cur.r = n           sqrt(binary_search_mid * binary_search_mid + dis_div2 * dis_div2); n       if (IsOutside(binary_search_cur, it)) { n         outside_record = binary_search_mid; n         if (dir_flag) n           binary_search_left = binary_search_mid; n         else n           binary_search_right = binary_search_mid; n       } else { n         if (dir_flag) n           binary_search_right = binary_search_mid; n         else n           binary_search_left = binary_search_mid; n       } n     } n     if (Cmp(inside_record, outside_record) > 0) n       swap(inside_record, outside_record); n     record.push_back(make_pair(inside_record, outside_record)); n   } n   sort(record.begin(), record.end(), n        [&](pair<double, double> k1, pair<double, double> k2) { n          return Cmp(k1.first, k2.first) < 0; n        }); n   double ans = Cmp(-record[0].first, 0.0) > 0 ? -record[0].first : 0.0; n   double fi = record[0].first, se = record[0].second; n   for (int i = 1; i < record.size(); ++i) { n     if (Cmp(se, record[i].first) < 0) { n       if (Cmp(ans, fabs(se)) > 0) ans = fabs(se); n       if (Cmp(ans, fabs(record[i].first)) > 0) ans = fabs(record[i].first); n       fi = record[i].first; n     } n     if (Cmp(record[i].second, se) > 0) se = record[i].second; n   } n   if (Cmp(ans, fabs(se)) > 0) ans = fabs(se); n   cout << sqrt(ans * ans + dis_div2 * dis_div2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n long long Pot(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long x = Pot(a, b / 2); n     return x * x % MOD; n   } else n     return Pot(a, b - 1) * a % MOD; n } n long long Inv(long long x) { return Pot(x, MOD - 2); } n int main() { n   ios_base::sync_with_stdio(0); n   string s; n   long long n, k; n   cin >> s >> k; n   n = s.size(); n   long long odp = 0; n   for (long long i = 0; i < n; ++i) { n     if (s[i] ==  0  || s[i] ==  5 ) { n       long long pot1 = Pot(2, n * k) - 1; n       if (pot1 < 0) pot1 += MOD; n       long long pot2 = Pot(2, n) - 1; n       if (pot2 < 0) pot2 += MOD; n       long long il = Pot(2, i) * pot1 % MOD; n       il = (il * Inv(pot2)) % MOD; n       odp = (odp + il) % MOD; n     } n   } n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   cout << 3 * n / 2 <<   n ; n   for (i = 2; i <= n; i += 2) cout << i <<    ; n   for (i = 1; i <= n; i += 2) cout << i <<    ; n   for (i = 2; i <= n; i += 2) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   char cc = getchar(); n   int cn = 0, flus = 1; n   while (cc <  0  || cc >  9 ) { n     if (cc ==  - ) flus = -flus; n     cc = getchar(); n   } n   while (cc >=  0  && cc <=  9 ) cn = cn * 10 + cc -  0 , cc = getchar(); n   return cn * flus; n } n const int N = 2e5 + 5; n struct E { n   int to, next; n } e[N << 1]; n int n, cnt, A, B, head[N]; n void add(int x, int y) { n   e[++cnt] = (E){y, head[x]}, head[x] = cnt, e[++cnt] = (E){x, head[y]}, n   head[y] = cnt; n } n void Dfs(int x, int fa, int dep) { n   (dep ^ 1) ? ++A : ++B; n   for (register int i = head[x]; i; i = e[i].next) n     if (e[i].to ^ fa) Dfs(e[i].to, x, dep ^ 1); n } n signed main() { n   n = gi(); n   int x, y; n   for (register int i = (2); i <= (n); ++i) x = gi(), y = gi(), add(x, y); n   Dfs(1, 1, 0); n   cout << min(A, B) - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static char a[55][55]; n bool vis[55][55]; n bool dfs(int i, int j, int px, int py, int n, int m) { n   bool flag = false; n   if (i > n || j > m) return false; n   if (i == 0 || j == 0) return false; n   vis[i][j] = 1; n   int xi[] = {1, 0, -1, 0}; n   int yi[] = {0, -1, 0, 1}; n   for (int k = 0; k < 4; k++) { n     int x = xi[k] + i; n     int y = yi[k] + j; n     if (x == px && y == py) continue; n     if (a[x][y] == a[i][j]) { n       if (vis[x][y]) { n         return true; n       } else n         flag = dfs(x, y, i, j, n, m); n       if (flag) { n         return true; n       } n     } n   } n   return false; n } n int main() { n   int n, m; n   char ch; n   scanf( %d%d , &n, &m); n   memset(a,  . , sizeof(a)); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &ch); n     for (int j = 1; j <= m; j++) { n       scanf( %c , &a[i][j]); n     } n   } n   memset(vis, 0, sizeof(vis)); n   bool flag = false; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (vis[i][j] == true) n         continue; n       else n         flag = dfs(i, j, 0, 0, n, m); n       if (flag) break; n     } n     if (flag) break; n   } n   if (flag) n     printf( Yes ); n   else n     printf( No ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n string tos(T a) { n   stringstream ss; n   string ret; n   ss << a; n   ss >> ret; n   return ret; n } n int n, a; n vector<int> arr; n int main() { n   while (cin >> n) { n     arr.clear(); n     for (int(i) = (0); (i) < (n); (i)++) { n       cin >> a; n       arr.push_back(a); n     } n     int boro = *max_element((arr).begin(), (arr).end()); n     if (boro == 1) { n       sort(arr.begin(), arr.end()); n       arr[arr.size() - 1] = 2; n     } else { n       for (int(i) = (0); (i) < (n); (i)++) { n         if (arr[i] == boro) { n           arr[i] = 1; n           break; n         } n       } n       sort(arr.begin(), arr.end()); n     } n     for (int(i) = (0); (i) < (n); (i)++) { n       if (i) cout <<    ; n       cout << arr[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105000; n int n, m; n int tin[MAXN], tout[MAXN]; n vector<int> g[MAXN]; n vector<int> gr[MAXN]; n vector<int> gg[MAXN]; n vector<int> vv; n bool used[MAXN]; n int sz[MAXN]; n int cursize; n int p[MAXN]; n bool has[MAXN]; n int ans; n void dfs(int v) { n   used[v] = true; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (!used[to]) dfs(to); n   } n   vv.push_back(v); n } n void dfs2(int v, int par) { n   used[v] = true; n   p[v] = par; n   sz[par]++; n   for (int i = 0; i < (int)gr[v].size(); i++) { n     int to = gr[v][i]; n     if (!used[to]) dfs2(to, par); n   } n } n void dfs3(int v) { n   used[v] = true; n   has[v] = true; n   for (int i = 0; i < (int)gg[v].size(); i++) { n     int to = gg[v][i]; n     if (!used[to]) dfs3(to); n   } n } n void dfs4(int v) { n   used[v] = true; n   cursize++; n   for (int i = 0; i < (int)gg[v].size(); i++) { n     int to = gg[v][i]; n     if (!used[to]) dfs4(to); n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int from, to; n     scanf( %d %d , &from, &to); n     g[from].push_back(to); n     gr[to].push_back(from); n     gg[from].push_back(to); n     gg[to].push_back(from); n   } n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       dfs(i); n     } n   } n   memset(used, 0, sizeof(used)); n   for (int i = 1; i <= n; i++) { n     int cur = vv[n - i]; n     if (!used[cur]) { n       dfs2(cur, cur); n       if (sz[cur] > 1) { n         has[cur] = true; n       } n     } n   } n   memset(used, 0, sizeof(used)); n   for (int i = 1; i <= n; i++) n     if (!used[i] && has[i]) { n       dfs3(i); n     } n   memset(used, 0, sizeof(used)); n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       cursize = 0; n       dfs4(i); n       if (has[i]) n         ans += cursize; n       else n         ans += cursize - 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long n = s.size() / 2 + s.size() % 2; n   long long x = n; n   cout << s[x - 1]; n   for (long long t = 1; t <= 100; t++) { n     if (x + t - 1 < s.size()) cout << s[x + t - 1]; n     if (x - t - 1 >= 0) cout << s[x - t - 1]; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n template <typename T> n ostream& operator<<(ostream& out, vector<T>& a) { n   for (auto& p : a) out << p <<    ; n   return out; n } n template <typename T> n istream& operator>>(istream& inp, vector<T>& a) { n   for (auto& p : a) inp >> p; n   return inp; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cout.precision(20); n   srand((signed)time(NULL)); n   long long n = 0, ans = 0; n   cin >> n; n   vector<pair<long long, long long>> arr(n); n   for (auto& p : arr) cin >> p.first >> p.second; n   map<long long, long long> gx, gy; n   for (long long i = 0; i < n; i++) { n     ans += gx[arr[i].first] + gy[arr[i].second]; n     gx[arr[i].first]++, gy[arr[i].second]++; n   } n   map<pair<long long, long long>, long long> gg; n   for (long long i = 0; i < n; i++) gg[arr[i]]++; n   for (auto& p : gg) ans -= p.second * (p.second - 1) / 2; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace math { n template <typename T> n T gcd(T a, T b) { n   if (a < 0) a = -a; n   if (b < 0) b = -b; n   while (b > 0) { n     T r = a % b; n     a = b; n     b = r; n   } n   return a; n } n }  // namespace math n template <typename C> n int SZ(const C& c) { n   return (int)c.size(); n } n template <size_t n, class... Types> n struct TupleHashHelper { n   static std::size_t calc(const std::tuple<Types...>& t) { n     using std::size_t; n     std::hash<typename std::tuple_element<n - 1, std::tuple<Types...>>::type> n         elemHash; n     return elemHash(std::get<n - 1>(t)) + n            31013 * TupleHashHelper<n - 1, Types...>::calc(t); n   } n }; n template <class... Types> n struct TupleHashHelper<0, Types...> { n   static std::size_t calc(const std::tuple<Types...>&) { return 0; } n }; n template <class... Types> n struct TupleHash { n   std::size_t operator()(const std::tuple<Types...>& t) const { n     static const size_t n = std::tuple_size<std::tuple<Types...>>::value; n     return TupleHashHelper<n, Types...>::calc(t); n   } n }; n class Solution { n  public: n   void solve(std::istream& in, std::ostream& out) { n     int n; n     in >> n; n     vector<int> x(n), y(n); n     for (int i = 0; i < n; ++i) in >> x[i] >> y[i]; n     unordered_map<tuple<int, int, int>, set<int>, TupleHash<int, int, int>> n         lines; n     for (int i = 0; i < n; ++i) n       for (int j = i + 1; j < n; ++j) { n         using math::gcd; n         int a = y[j] - y[i], b = x[i] - x[j]; n         int c = -a * x[i] - b * y[i]; n         int d = gcd(gcd(a, b), c); n         if (a < 0) { n           a = -a; n           b = -b; n           c = -c; n         } else if (a == 0 && b < 0) { n           b = -b; n           c = -c; n         } n         auto t = make_tuple(a / d, b / d, c / d); n         lines[t].insert(i); n         lines[t].insert(j); n       } n     long long res = (long long)n * (n - 1) * (n - 2) / 6; n     for (const auto& p : lines) { n       long long s = SZ(p.second); n       res -= s * (s - 1) * (s - 2) / 6; n     } n     out << res << endl; n   } n }; n void solve(std::istream& in, std::ostream& out) { n   out << std::setprecision(12); n   Solution solution; n   solution.solve(in, out); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   istream& in = cin; n   ostream& out = cout; n   solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int toInt(string s) { n   int v; n   istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const int dx[] = {-1, 1, 0, 0}; n const int dy[] = {0, 0, 1, -1}; n int n, k, ret; n bool us[11]; n int main() { n   cin >> n >> k; n   for (int i = (0); i < (n); ++i) { n     string s; n     bool f = true; n     cin >> s; n     for (int j = (0); j < (10); ++j) us[j] = false; n     for (int j = (0); j < (s.size()); ++j) us[s[j] -  0 ] = true; n     for (int j = (0); j < (k + 1); ++j) n       if (!us[j]) f = false; n     ret += f; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long l3, l4, l5; n   std::cin >> l3 >> l4 >> l5; n   long double pi = 3.141592653589793238462643383279; n   long double v3 = 1.0 / 3.0 * l3 * l3 * sqrtl(3.0) / 4.0 * l3 * sqrtl(2.0 / 3); n   long double v4 = 1.0 / 3.0 * l4 * l4 * l4 * sqrtl(2) / 2; n   long double v5 = 1.0 / 3.0 * l5 * l5 * sqrtl(25 + 10 * sqrtl(5)) / 4 * l5 * n                    sqrtl(4 * sinl(pi / 5) * sinl(pi / 5) - 1) / n                    (2 * sinl(pi / 5)); n   std::cout.precision(10); n   return std::cout << std::fixed << v3 + v4 + v5, 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p2(long long x) { return x * x; } n int main() { n   long long n; n   cin >> n; n   if (n <= 2) n     cout << -1 << endl; n   else if (!(n % 2)) n     cout << (n * n) / 4 - 1 <<     << (n * n) / 4ll + 1ll << endl; n   else n     cout << (n * n - 1) / 2ll <<     << (n * n + 1) / 2ll << endl; n   return 0; n } n


///Bismillahir Rahmanir Rahim n #include<bits/stdc++.h> n #define        u64             uint64_t n #define        ll              long long n #define        endl              n  n #define        PI              acos(-1) n #define        fi              first n #define        si              second n #define        mkp             make_pair n #define        pb              push_back n #define        set0(arr)       memset(arr, 0, sizeof(arr)) n #define        setinf(arr)     memset(arr, 126, sizeof(arr)) n #define        all(x)          (x).begin(),(x).end() n #define        sz(v)           ((ll)(v).size()) n #define        IOS             ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) n using namespace std; n using          pll        =      pair<ll, ll> ; n using          vl         =      vector<ll> ; n using          vpll       =      vector<pll> ; n using          mll        =      map<ll, ll> ; n using          mcl        =      map<char, ll> ; n using          msl        =      map<string,ll> ; n using          sl         =      set<ll> ; n using          sc         =      set<char> ; n using          dl         =      deque<ll> ; n const int N = 1e6+5 ; n  n ll      mod = 1e9+7 ; n vl      adj[N] ; n vpll    adjc[N] ; n ll      vis[N] ; n ll      arr[N] ; n  n ll sumd(ll a,ll b){return (((a%mod)+(b%mod))%mod);} n ll muld(ll a,ll b){return (((a%mod)*(b%mod))%mod);} n ll subd(ll a,ll b){return (((a%mod)-(b%mod)+mod)%mod);} n  n  n  n int main() n { n     IOS; n     ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18, cs; n     string   str ; n     char     ch ; n     double   db ; n     ll       l, r ; n     //code n  n     cin>>tc; n     for(cs=1;cs<=tc;cs++) n     { n         ll c1,c2,c3,a1,a2,a3,a4,a5; n         cin>>c1>>c2>>c3; n         cin>>a1>>a2>>a3>>a4>>a5; n         c1-=a1; n         c2-=a2; n         c3-=a3; n         if(c1<0||c2<0||c3<0) n         { n                cout<< NO n ;continue; n         } n         if(c1>=a4) n         { n                        if(c2>=a5){} n                        else n                        { n                            a5-=c2; n                            c3-=a5; n                            if(c3<0) n                            { n                                  cout<< NO n ;continue; n                            } n                        } n         } n         else n         { n                a4-=c1; n                c3-=a4; n                if(c3<0) n                { n                       cout<< NO n ;continue; n                } n                else n                { n                                   if(c2>=a5); n                else n               { n               a5-=c2; n               c3-=a5; n               if(c3<0) n               { n               cout<< NO n ;continue; n               } n               } n               } n         } n         cout<< YES n ; n  n     } n  n  n     //code n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long a, long long b, long long mod) { n   a %= mod; n   if (a < 0) a += mod; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b /= 2; n   } n   return ans; n } n long long pwr(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans *= a; n     a *= a; n     b /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   while (b) { n     long long temp = a; n     a = b; n     b = temp % b; n   } n   return a; n } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n long long modularInverse(long long a, long long m) { n   assert(false); n   return pwr(a, m - 2, m); n } n const int mod = 1000000007; n int DP[5001][5001]; n int PDP[5001][5001]; n int n, k, a, b; n void generatePDP(int u) { n   PDP[u][0] = DP[u][0]; n   for (int i = 1; i <= n; ++i) { n     PDP[u][i] = (PDP[u][i - 1] + DP[u][i]) % mod; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n >> a >> b >> k; n   for (int i = 1; i <= n; ++i) DP[0][i] = 1; n   DP[0][b] = 0; n   for (int i = 1; i <= k; ++i) { n     generatePDP(i - 1); n     for (int j = 1; j <= n; ++j) { n       if (j == b) { n         DP[i][b] = 0; n       } else if (j < b) { n         int dist = b - j; n         int start = max(1, j - dist + 1); n         int end = b - 1; n         if (start - 1 >= 1) n           DP[i][j] = PDP[i - 1][end] - PDP[i - 1][start - 1] - DP[i - 1][j]; n         else n           DP[i][j] = PDP[i - 1][end] - DP[i - 1][j]; n       } else if (j > b) { n         int dist = j - b; n         int start = b + 1; n         int end = min(j + dist - 1, n); n         if (start - 1 >= 1) n           DP[i][j] = PDP[i - 1][end] - PDP[i - 1][start - 1] - DP[i - 1][j]; n         else n           DP[i][j] = PDP[i - 1][end] - DP[i - 1][j]; n       } n       DP[i][j] %= mod; n       if (DP[i][j] < 0) DP[i][j] += mod; n     } n   } n   cout << DP[k][a] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll P = 998244353; n constexpr ll modPow(ll a, ll b, ll c) { n   if ((b & 1) == 1) return a * modPow(a, b - 1, c) % c; n   if (b == 0) return 1 % c; n   return modPow(a * a % c, b / 2, c); n } n void bitReverse(vector<int>& vec) { n   int n = vec.size(); n   int lg = 0; n   while ((1 << lg) < n) ++lg; n   for (int i = 0; i < n; ++i) { n     int t = 0; n     for (int j = 0; j < lg; ++j) { n       if (i & (1 << j)) t |= 1 << (lg - 1 - j); n     } n     if (i < t) swap(vec[i], vec[t]); n   } n } n void ntt(vector<int>& pol, bool inv) { n   bitReverse(pol); n   int n = pol.size(); n   int lg = 0; n   while ((1 << lg) < n) ++lg; n   ll nth_root = modPow(3, (P - 1) / n, P); n   if (inv) nth_root = modPow(nth_root, n - 1, P); n   vector<ll> pows(lg, nth_root); n   for (int i = lg - 2; i >= 0; --i) pows[i] = (pows[i + 1] * pows[i + 1]) % P; n   ll tmp = 1; n   for (int i = 0; i < n; ++i) tmp = (tmp * nth_root) % P; n   for (lg = 0; (1 << lg) < n; ++lg) { n     int h = 1 << lg; n     int len = h << 1; n     ll root = pows[lg]; n     for (int j = 0; j < n; j += len) { n       ll curr = 1; n       for (int i = j; i < j + h; ++i) { n         int tmp = (curr * pol[i + h]) % P; n         curr = (curr * root) % P; n         pol[i + h] = P - tmp + pol[i]; n         if (pol[i + h] >= P) pol[i + h] -= P; n         pol[i] += tmp; n         if (pol[i] >= P) pol[i] -= P; n       } n     } n   } n } n vector<int> polyMult(vector<int> a, vector<int> b) { n   int as = a.size(); n   int bs = b.size(); n   int n = 1; n   while (n < (as + bs)) n <<= 1; n   a.resize(n, 0); n   b.resize(n, 0); n   ntt(a, false); n   ntt(b, false); n   vector<int> c(n); n   int n_inv = modPow(n, P - 2, P); n   for (int i = 0; i < n; ++i) { n     ll prod = (ll)a[i] * b[i] % P; n     c[i] = prod * n_inv % P; n   } n   ntt(c, true); n   c.resize(as + bs - 1); n   return c; n } n const int N = 10 + 4 * (int)1e5; n ll fact[N]; n ll inv_fact[N]; n ll twop[N]; n ll choose(int a, int b) { n   if (b > a) return 0; n   ll div = inv_fact[b] * inv_fact[a - b] % P; n   return fact[a] * div % P; n } n vector<int> build(const vector<ll>& as, ll h) { n   int n = as.size(); n   int x = 0, y = 0; n   for (int i = 0; i < n && as[i] < h;) { n     int j = i; n     while (j < n && as[j] == as[i]) ++j; n     if (j - i == 1) n       ++x; n     else n       ++y; n     i = j; n   } n   n = x + 2 * y + 1; n   vector<int> vec1(n, 0), vec2(n, 0); n   for (int i = 0; i < n; ++i) { n     vec1[i] = choose(2 * y, i); n     vec2[i] = choose(x, i) * twop[i] % P; n   } n   vector<int> res = polyMult(vec1, vec2); n   while (res.back() == 0) res.pop_back(); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   fact[0] = 1; n   twop[0] = 1; n   inv_fact[0] = 1; n   for (int i = 1; i < N; ++i) { n     fact[i] = (i * fact[i - 1]) % P; n     twop[i] = (2 * twop[i - 1]) % P; n     inv_fact[i] = modPow(fact[i], P - 2, P); n   } n   int n, k; n   cin >> n >> k; n   vector<ll> as(n), bs(k); n   for (int i = 0; i < n; ++i) cin >> as[i]; n   for (int i = 0; i < k; ++i) cin >> bs[i]; n   sort(as.begin(), as.end()); n   vector<vector<int>> dp(k); n   for (int j = 0; j < k; ++j) dp[j] = build(as, bs[j]); n   int q; n   cin >> q; n   for (int qi = 0; qi < q; ++qi) { n     int t; n     cin >> t; n     ll res = 0; n     for (int j = 0; j < k; ++j) { n       int c = (t / 2) - bs[j] - 1; n       if (c >= 0 && c < dp[j].size()) { n         res += dp[j][c]; n       } n     } n     cout << (res % P) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long qpow(long long x, long long k) { n   return k == 0 ? 1 n                 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; n } n const int md = 1e9 + 7; n const int Maxn = 5005; n int n, k; n long long dp[Maxn][Maxn]; n int main() { n   scanf( %d%d , &n, &k); n   dp[0][0] = 1; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= i; j++) { n       dp[i][j] = (dp[i - 1][j] * j + dp[i - 1][j - 1] * (n - j + 1)) % md; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= min(n, k); i++) { n     ans = (ans + dp[k][i] * qpow(2, n - i)) % md; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[101][101]; n int min(int a, int b) { return a < b ? a : b; } n int max(int a, int b) { return a > b ? a : b; } n int main() { n   int n, m; n   int i, j; n   string s; n   cin >> n >> m; n   for (i = 0; i < n; ++i) { n     cin >> s; n     for (j = 0; j < m; ++j) { n       if (s[j] ==  W ) n         mat[i][j] = 0; n       else n         mat[i][j] = 1; n     } n   } n   int left = -1, right = 101, top = -1, bot = 101; n   int bcount = 0; n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       if (mat[i][j] == 1) { n         ++bcount; n         if (left == -1) n           left = j; n         else n           left = min(left, j); n         if (right == 101) n           right = j; n         else n           right = max(right, j); n         if (top == -1) n           top = i; n         else n           top = min(top, i); n         if (bot == 101) n           bot = i; n         else n           bot = max(bot, i); n       } n     } n   } n   int c = 0; n   if (bot == 101 && right == 101 && left == -1 && top == -1) n     c = 1; n   else if (bot == top && right == left) n     c = 0; n   else { n     int hor = bot - top + 1; n     int ver = right - left + 1; n     int side = max(hor, ver); n     if (side > n || side > m) n       c = -1; n     else { n       c = side * side - bcount; n     } n   } n   cout << c << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int MOD = 998244353; n int fact[N], factinv[N]; n int lgcput(int b, int p) { n   int a = 1; n   while (p) { n     if (p & 1) { n       a = (1LL * a * b) % MOD; n     } n     b = (1LL * b * b) % MOD; n     p /= 2; n   } n   return a; n } n int inv(int x) { return lgcput(x, MOD - 2); } n int main() { n   int n, k; n   cin >> n >> k; n   fact[0] = factinv[0] = 1; n   for (int i = 1; i <= n; i++) { n     fact[i] = (1LL * i * fact[i - 1]) % MOD; n     factinv[i] = (1LL * inv(i) * factinv[i - 1]) % MOD; n   } n   int c = n - k; n   if (k >= n) { n     cout <<  0 ; n     return 0; n   } n   int ans = 0; n   for (int i = 0; i <= c; i++) { n     int dead = n         (1LL * n          ((1LL * ((1LL * fact[c] * factinv[i]) % MOD) * factinv[c - i]) % MOD) * n          lgcput(c - i, n)) % n         MOD; n     if (i & 1) n       ans -= dead; n     else n       ans += dead; n     if (ans < 0) ans += MOD; n     if (ans >= MOD) ans -= MOD; n   } n   ans = (1LL * (1LL * (1LL * ans * fact[n] % MOD) * factinv[c] % MOD) * n          factinv[n - c]) % n         MOD; n   if (k == 0) { n     cout << ans; n     return 0; n   } n   ans *= 2; n   ans %= MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nMax = 200003; n int smen[nMax]; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(NULL); n   int n, k, q, st, dr; n   cin >> n >> k >> q; n   int m = nMax - 3; n   for (int i = 1; i <= n; i++) { n     cin >> st >> dr; n     smen[st]++; n     smen[dr + 1]--; n   } n   for (int i = 1; i <= m; i++) { n     smen[i] = smen[i - 1] + smen[i]; n   } n   for (int i = 1; i <= m; i++) { n     if (smen[i] >= k) { n       smen[i] = 1; n     } else { n       smen[i] = 0; n     } n   } n   for (int i = 1; i <= m; i++) { n     smen[i] = smen[i - 1] + smen[i]; n   } n   int x, y; n   for (int i = 1; i <= q; i++) { n     cin >> x >> y; n     cout << smen[y] - smen[x - 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> dp; n long long fastPow(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = (ret * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return ret; n } n long long cc(long long y) { n   if (dp.find(y) != dp.end()) return dp[y]; n   dp[y] = fastPow(2, y - 1); n   for (int i = 2; i * i <= y; i++) { n     if (!(y % i)) { n       dp[y] = (dp[y] - cc(i) + 1000000007) % 1000000007; n       if (i != y / i) { n         dp[y] = (dp[y] - cc(y / i) + 1000000007) % 1000000007; n       } n     } n   } n   dp[y]--; n   return dp[y]; n } n int main() { n   long long x, y; n   cin >> x >> y; n   if (!(y % x)) { n     y /= x; n     if (y == 1) { n       cout << 1 << endl; n       return 0; n     } n     cout << cc(y) << endl; n     return 0; n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int cases; n   scanf( %d , &cases); n   while (cases--) { n     int n, p; n     scanf( %d%d , &n, &p); n     for (int i = 2; i <= n; i++) { n       printf( %d %d n , 1, i); n     } n     for (int i = 2; i < n; i++) { n       printf( %d %d n , i, i + 1); n     } n     printf( %d %d n , n, 2); n     int count = 0, extra = 2 * n + p - 2 * (n - 1); n     for (int i = 2; i <= n; i++) { n       for (int j = i + 2; j <= n; j++) { n         if (i == 2 && j == n) break; n         printf( %d %d n , i, j); n         count++; n         if (count == extra) goto here; n       } n     } n   here: n     continue; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[31111], dp[31111][800]; n int main() { n   int i, j, n, d, u; n   scanf( %d%d , &n, &d); n   for (i = 0; i < n; i++) { n     scanf( %d , &u); n     p[u]++; n   } n   for (i = 0; i <= 30000; i++) { n     for (j = 0; j <= 650; j++) { n       dp[i][j] = -10000000; n     } n   } n   int ans = dp[d][300] = p[d]; n   for (i = d + 1; i <= 30000; i++) { n     for (j = 1; j <= 600; j++) { n       int pre = i - j - d + 300; n       if (pre < 0) break; n       if (pre >= i) continue; n       dp[i][j] = max(dp[pre][j], max(dp[pre][j + 1], dp[pre][j - 1])) + p[i]; n       ans = max(ans, dp[i][j]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n int dx[]{-1, 0, 0, 1}; n int dy[]{-1, -1, 1, 1}; n void solve() { n   string str; n   cin >> str; n   cout << str; n   reverse(str.begin(), str.end()); n   cout << str <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n   cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<  s. n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FastIO { n const int L = (1 << 20); n char buf[L], *S, *T; n inline char getchar() { n   if (S == T) { n     T = (S = buf) + fread(buf, 1, L, stdin); n     if (S == T) return EOF; n   } n   return *S++; n } n inline int read() { n   int s = 0, f = 1; n   char t = getchar(); n   while ( 0  > t || t >  9 ) { n     if (t ==  - ) f = -1; n     t = getchar(); n   } n   while ( 0  <= t && t <=  9 ) { n     s = (s << 1) + (s << 3) + t -  0 ; n     t = getchar(); n   } n   return s * f; n } n }  // namespace FastIO n using FastIO::read; n const int N = 55; n const int Mod = 998244353; n int dp[N][N][N * 2], sum, Sadd, Ssub; n int w[N], a[N], n, m; n int Inc(int x, int y) { return x + y >= Mod ? x + y - Mod : x + y; } n int Dec(int x, int y) { return x - y < 0 ? x - y + Mod : x - y; } n int Mul(int x, int y) { return (long long)x * y % Mod; } n int Fpow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = Mul(ans, a); n     b >>= 1; n     a = Mul(a, a); n   } n   return ans; n } n int Inv(int x) { return Fpow(x, Mod - 2); } n void Dp(int c) { n   memset(dp, 0, sizeof(dp)); n   dp[0][0][0] = 1; n   for (int i = 0; i < m; i++) { n     for (int k = 0; k <= i; k++) { n       int now = (a[c] ? w[c] + k : w[c] - k); n       if (now < 0) continue; n       for (int d = 0; d <= i; d++) { n         if (!dp[i][k][d]) continue; n         int Dw = sum + d - (i - d); n         Dw = Inv(Dw); n         int Up_add = Sadd + d; n         int Up_sub = Ssub - (i - d); n         if (a[c]) { n           dp[i + 1][k + 1][d + 1] = n               Inc(dp[i + 1][k + 1][d + 1], Mul(dp[i][k][d], Mul(now, Dw))); n           dp[i + 1][k][d + 1] = n               Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add - now, Dw))); n           dp[i + 1][k][d] = n               Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub, Dw))); n         } else { n           dp[i + 1][k + 1][d] = n               Inc(dp[i + 1][k + 1][d], Mul(dp[i][k][d], Mul(now, Dw))); n           dp[i + 1][k][d + 1] = n               Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add, Dw))); n           dp[i + 1][k][d] = n               Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub - now, Dw))); n         } n       } n     } n   } n   int Ans = 0; n   for (int k = 0; k <= m; k++) { n     int now = (a[c] ? w[c] + k : w[c] - k); n     if (now < 0) continue; n     for (int d = 0; d <= m; d++) { n       Ans = Inc(Ans, Mul(now, dp[m][k][d])); n     } n   } n   cout << Ans <<   n ; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) { n     w[i] = read(); n     if (a[i]) n       Sadd += w[i]; n     else n       Ssub += w[i]; n     sum += w[i]; n   } n   for (int i = 1; i <= n; i++) Dp(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int tp[250] = { n     0,    4,     10,    20,    35,    56,    83,    116,   155,   198,  244, n     292,  341,   390,   439,   488,   537,   586,   635,   684,   733,  782, n     831,  880,   929,   978,   1027,  1076,  1125,  1174,  1223,  1272, 1321, n     1370, 1419,  1468,  1517,  1566,  1615,  1664,  1713,  1762,  1811, 1860, n     1909, 1958,  2007,  2056,  2105,  2154,  2203,  2252,  2301,  2350, 2399, n     2448, 2497,  2546,  2595,  2644,  2693,  2742,  2791,  2840,  2889, 2938, n     2987, 3036,  3085,  3134,  3183,  3232,  3281,  3330,  3379,  3428, 3477, n     3526, 3575,  3624,  3673,  3722,  3771,  3820,  3869,  3918,  3967, 4016, n     4065, 4114,  4163,  4212,  4261,  4310,  4359,  4408,  4457,  4506, 4555, n     4604, 4653,  4702,  4751,  4800,  4849,  4898,  4947,  4996,  5045, 5094, n     5143, 5192,  5241,  5290,  5339,  5388,  5437,  5486,  5535,  5584, 5633, n     5682, 5731,  5780,  5829,  5878,  5927,  5976,  6025,  6074,  6123, 6172, n     6221, 6270,  6319,  6368,  6417,  6466,  6515,  6564,  6613,  6662, 6711, n     6760, 6809,  6858,  6907,  6956,  7005,  7054,  7103,  7152,  7201, 7250, n     7299, 7348,  7397,  7446,  7495,  7544,  7593,  7642,  7691,  7740, 7789, n     7838, 7887,  7936,  7985,  8034,  8083,  8132,  8181,  8230,  8279, 8328, n     8377, 8426,  8475,  8524,  8573,  8622,  8671,  8720,  8769,  8818, 8867, n     8916, 8965,  9014,  9063,  9112,  9161,  9210,  9259,  9308,  9357, 9406, n     9455, 9504,  9553,  9602,  9651,  9700,  9749,  9798,  9847,  9896, 9945, n     9994, 10043, 10092, 10141, 10190, 10239, 10288, 10337, 10386, 10435}; n int main() { n   int n; n   gn(n); n   if (n < 201) n     println(tp[n]); n   else n     println(tp[200] + (long long)(n - 200) * 49); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a; n   scanf( %d %d %*d , &n, &a); n   map<long long, int> cnt1; n   map<pair<int, int>, int> cnt2; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     int vx, vy; n     scanf( %*d %d %d , &vx, &vy); n     long long v = 1LL * a * vx - vy; n     ans += cnt1[v]; n     ans -= cnt2[{vx, vy}]; n     ++cnt1[v]; n     ++cnt2[{vx, vy}]; n   } n   printf( %lld n , ans * 2); n } n


#include <bits/stdc++.h> n using namespace std; n long long L[200005], R[200005], U[200005], D[200005]; n long long n; n string S; n long long x, y, sx = 0, sy = 0; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> S >> x >> y; n   S =  _  + S; n   L[0] = R[0] = U[0] = D[0] = 0; n   for (long long i = 1; i <= n; i++) { n     L[i] = L[i - 1]; n     R[i] = R[i - 1]; n     D[i] = D[i - 1]; n     U[i] = U[i - 1]; n     if (S[i] ==  L ) n       L[i]++, sx--; n     else if (S[i] ==  R ) n       R[i]++, sx++; n     else if (S[i] ==  U ) n       U[i]++, sy++; n     else if (S[i] ==  D ) n       D[i]++, sy--; n   } n   if (sx == x && sy == y) { n     cout << 0; n     return 0; n   } n   if ((sx + sy + x + y) % 2 != 0) { n     cout << -1; n     return 0; n   } n   long long s_ind = 1, g_ind = n + 1; n   while (g_ind > s_ind) { n     long long len = (g_ind + s_ind) / 2; n     bool b = 0; n     for (long long j = len; j <= n; j++) { n       long long l = L[n] - L[j] + L[j - len], r = R[n] - R[j] + R[j - len], n                 u = U[n] - U[j] + U[j - len], d = D[n] - D[j] + D[j - len]; n       long long curx = r - l, cury = u - d; n       if (abs(curx - x) + abs(cury - y) <= len) { n         b = 1; n         break; n       } n     } n     if (b) n       g_ind = len; n     else n       s_ind = len + 1; n   } n   if (s_ind <= n) n     cout << s_ind; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500023; n bool vis[N]; n vector<int> adj[N]; n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int c; n     cin >> c; n     v.push_back(c); n   } n   sort((v).begin(), (v).end()); n   long long int ans = 0; n   if (v[2] != v[1]) { n     for (int i = 2; i < n; i++) { n       if (v[i] == v[2]) n         ans++; n       else n         break; n     } n   } else if ((v[2] == v[1]) && (v[1] != v[0])) { n     long long int count = 0; n     for (int i = 1; i < n; i++) { n       if (v[i] == v[1]) n         count++; n       else n         break; n     } n     ans = (count * (count - 1)) / 2; n   } else { n     long long int count = 0; n     for (int i = 0; i < n; i++) { n       if (v[i] == v[0]) n         count++; n       else n         break; n     } n     ans = (count * (count - 1) * (count - 2)) / 6; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   int t = 0; n   while (t++ < T) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long fact(int x) { n   long long ans = 0; n   while (x > 0) { n     ans += x; n     x--; n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   vector<vector<char> > a(n, vector<char>(n)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   vector<int> ansi(n); n   vector<int> ansj(n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (a[i][j] ==  C ) { n         ansi[i]++; n         ansj[j]++; n       } n   long long count = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) count += fact(ansi[i] - 1); n   ans += count; n   count = 0; n   for (int i = 0; i < n; i++) count += fact(ansj[i] - 1); n   ans += count; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, a[300010]; n bool check(int mid) { n   int pre = mid + a[1] >= m ? 0 : a[1]; n   for (int i = 2; i <= n; i++) { n     if (a[i] + mid < pre) return false; n     if (a[i] <= pre) n       continue; n     else if (a[i] + mid >= m && (a[i] + mid) % m >= pre) n       continue; n     pre = a[i]; n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int l = 0, r = m - 1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define int unsigned long long n  n const int N = 100010; n int n, a[N]; n  n signed main(void) n { n     ios::sync_with_stdio(false); n     map <int, int> mp; n     int T; n     cin >> T; n     while(T--) n     { n         mp.clear(); n         int res = 0; n         int n; n         cin >> n; n         for(int i = 1; i <= n; i++) n         { n             cin >> a[i]; n             res += mp[a[i]] * (n - i + 1); n             mp[a[i]] += i; n         } n         cout << res << endl; n     } n     return 0; n } n /* n ———————————————— n 版权声明：本文为CSDN博主「zhbbbb！」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。 n 原文链接：https://blog.csdn.net/zhbbbbbb/article/details/117330722*/ n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,fast-math,unroll-loops ) n using namespace std; n const int maxn = 2e5 + 50; n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   char ch = nc(); n   int sum = 0; n   while (!(ch >=  0  && ch <=  9 )) ch = nc(); n   while (ch >=  0  && ch <=  9 ) sum = sum * 10 + ch - 48, ch = nc(); n   return sum; n } n int t, n; n string str; n int sum1[maxn], sum2[maxn]; n map<pair<int, int>, int> mp; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n >> str; n     str =      + str; n     n++; n     for (int i = 1; i <= n; i++) n       sum1[i] = sum1[i - 1] + (str[i] ==  L ) - (str[i] ==  R ), n       sum2[i] = sum2[i - 1] + (str[i] ==  U ) - (str[i] ==  D ); n     int ans = 0x3f3f3f3f, l = 0, r = 0; n     mp.clear(); n     for (int i = 1; i <= n; i++) { n       if (mp[make_pair(sum1[i], sum2[i])] != 0) { n         int tmp = mp[make_pair(sum1[i], sum2[i])]; n         if (i - tmp < ans) ans = i - tmp, l = tmp + 1, r = i; n       } n       mp[make_pair(sum1[i], sum2[i])] = i; n     } n     if (ans == 0x3f3f3f3f) n       printf( -1 n ); n     else n       printf( %d %d n , l - 1, r - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m; n long long n; n long long deg[1001001], result; n vector<vector<int> > e; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     deg[a]++; n     deg[b]++; n   } n   result = 0; n   for (int i = 1; i <= n; i++) result += (n - 1 - deg[i]) * deg[i]; n   result /= 2; n   result = n * (n - 1) * (n - 2) / 6 - result; n   cout << result; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 19; n int qpow(int a, int b, int p) { n   int res = 1; n   while (b) { n     if (b & 1) res = (long long)res * a % p; n     a = (long long)a * a % p, b >>= 1; n   } n   return res; n } n int n, m, k, p; n int fa[MAXN], size[MAXN]; n int getf(int node) { n   return fa[node] == node ? node : fa[node] = getf(fa[node]); n } n int main() { n   std::scanf( %d%d%d , &n, &m, &p); n   for (int i = 1; i <= n; ++i) fa[i] = i, size[i] = 1; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     std::scanf( %d%d , &u, &v); n     if (getf(u) != getf(v)) { n       size[getf(v)] += size[getf(u)]; n       fa[getf(u)] = getf(v); n     } n   } n   int ans = 1; n   for (int i = 1; i <= n; ++i) n     if (fa[i] == i) ++k, ans = (long long)ans * size[i] % p; n   if (k >= 2) n     ans = (long long)ans * qpow(n, k - 2, p) % p; n   else n     ans = 1; n   std::printf( %d n , ans % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long int> vec1, vec2; n   vector<long long int>::iterator it; n   long long int n, a, sz, s1 = 0, s2 = 0, s3 = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     s1 += a; n   } n   n--; n   for (int i = 0; i < n; i++) { n     cin >> a; n     s2 += a; n   } n   n--; n   for (int i = 0; i < n; i++) { n     cin >> a; n     s3 += a; n   } n   cout << s1 - s2 << endl << s2 - s3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2000000000; n const int maxn = 1000005; n int n; n int a[maxn]; n bool vis[maxn]; n int main() { n   long long ans = 0LL; n   scanf( %d , &n); n   for (int i = n; i >= 0; i--) { n     if (vis[i]) continue; n     int t = 0, s = 0; n     for (int j = 0; j < 20; j++) n       if (i & (1 << j)) s = j; n     for (int j = s - 1; j >= 0; j--) n       if (!(i & (1 << j))) t |= (1 << j); n     a[i] = t, a[t] = i; n     vis[i] = vis[t] = 1; n     ans += 2 * (i ^ t); n   } n   printf( %I64d n , ans); n   for (int i = 0; i < n; i++) printf( %d  , a[i]); n   printf( %d n , a[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fail[1000006]; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   fail[-1] = fail[0] = -1; n   for (int i = 1; i < n; ++i) { n     int ptr = i - 1; n     while (s[i] != s[fail[ptr] + 1] && ptr > 0) ptr = fail[ptr]; n     fail[i] = fail[ptr] + (s[i] == s[fail[ptr] + 1]); n   } n   for (int i = 0; i < n; ++i) { n     int le = i - fail[i]; n     int cc = (i + 1) / le; n     if ((i + 1) % le == 0) { n       if (cc / k - cc % k >= 0) n         cout << 1; n       else n         cout << 0; n     } else { n       if (cc / k - cc % k > 0) n         cout << 1; n       else n         cout << 0; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long min(long long x, long long y) { return (x < y) ? x : y; } n long long max(long long x, long long y) { return (x > y) ? x : y; } n const int N = 100009; n int x, y, n, m, i, u, w, z, sum, best, j, k, t, mx; n int a[N]; n string s, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   n = s.size(); n   while (i < n) { n     if (s[i] ==  d  && s[i + 1] ==  o  && s[i + 2] ==  t  && i != 0 && n         i != n - 3) { n       ans.push_back( . ); n       i += 2; n     } else if (s[i] ==  a  && s[i + 1] ==  t  && !x && i != 0 && i != n - 2) { n       x = 1; n       i++; n       ans.push_back( @ ); n     } else n       ans.push_back(s[i]); n     i++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> V[1005]; n int dis[1005], rdis[1005], G[1005][1005]; n void spfa(int s, int e) { n   queue<int> Q; n   memset(dis, 0x3f, sizeof(dis)); n   memset(rdis, 0x3f, sizeof(rdis)); n   Q.push(s); n   dis[s] = 0; n   while (!Q.empty()) { n     int f = Q.front(); n     for (int i = (0); i < ((int)(V[f]).size()); i++) { n       int nex = V[f][i]; n       if (dis[nex] > dis[f] + 1) { n         dis[nex] = dis[f] + 1; n         Q.push(nex); n       } n     } n     Q.pop(); n   } n   Q.push(e); n   rdis[e] = 0; n   while (!Q.empty()) { n     int f = Q.front(); n     for (int i = (0); i < ((int)(V[f]).size()); i++) { n       int nex = V[f][i]; n       if (rdis[nex] > rdis[f] + 1) { n         rdis[nex] = rdis[f] + 1; n         Q.push(nex); n       } n     } n     Q.pop(); n   } n } n int main() { n   int n, m, s, e; n   scanf( %d%d%d%d , &n, &m, &s, &e); n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     V[a].push_back(b); n     V[b].push_back(a); n     G[a][b] = G[b][a] = 1; n   } n   int ans = 0; n   spfa(s, e); n   for (int i = (1); i < (n + 1); i++) n     for (int j = (i + 1); j < (n + 1); j++) n       if (dis[i] + 1 + rdis[j] >= dis[e] && dis[j] + 1 + rdis[i] >= dis[e] && n           !G[i][j]) n         ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n typedef long long ll; n using namespace std; n  n int k; n  n int addk(int a,int b) n { n  int base,aw,bw,ret=0; n  for(base=1;base<=max(a,b);base*=k) n  { n   aw=a/base%k; n   bw=b/base%k; n   ret+=(aw+bw)%k*base; n  }  n  return ret; n } n  n int subk(int a,int b) n { n  int base,aw,bw,ret=0; n  for(base=1;base<=max(a,b);base*=k) n  { n   aw=a/base%k; n   bw=b/base%k; n   ret+=(aw-bw+k)%k*base; n  }  n  return ret; n } n  n int main() n { n  int T,n,y,i,r; n  scanf( %d ,&T); n  while(T--) n  { n   scanf( %d%d ,&n,&k); n   for(i=0;i<n;i++) n   { n    if(i==0) n     y=0; n    else if(i&1) n     y=subk(i-1,i); n    else n     y=subk(i,i-1); n    printf( %d n ,y); n    fflush(stdout); n    scanf( %d ,&r); n    if(r==1) n     break; n   } n  } n } n  n


#include <bits/stdc++.h> n using namespace std; n int n, k; n const int maxn = 1e5 + 10; n stack<int> max_seen; n int ans[maxn]; n vector<int> to[maxn]; n void calc(int a) { n   int s = max(a - k, 0), t = min(n, a + k + 1); n   if (max_seen.empty()) n     ans[a] += t - s; n   else n     ans[a] += t - max(max_seen.top(), s); n   max_seen.push(t); n   for (int i = 0; i < to[a].size(); i++) { n     ans[to[a][i]] = ans[a]; n     calc(to[a][i]); n   } n   max_seen.pop(); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (x > 0) to[x - 1].push_back(i); n   } n   for (int i = 0; i < n; i++) n     if (ans[i] == 0) calc(i); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; 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   long ans = 0; n   for (int i = 0; i < n; i++) { n     if ((s[i] -  0 ) % 2 == 0) ans += (i + 1); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 35; n int n, m; n int a[MAXN][MAXN]; n int part[MAXN][MAXN], partn = 0; n long long ans = 0; n int x[MAXN][2]; n int res[MAXN][MAXN]; n int c[MAXN]; n int u[MAXN]; n void read() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     char tmp[35]; n     scanf( %s , tmp + 1); n     for (int j = 1; j <= m; j++) { n       if (tmp[j] ==  . ) n         a[i][j] = 0; n       else if (tmp[j] >=  a ) n         a[i][j] = tmp[j] -  a  + 1; n       else n         a[i][j] = tmp[j] -  A  + 26 + 1; n     } n   } n } n void prepare() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] != 0 && !part[i][j]) { n         partn++; n         part[i][j] = part[i][j + 1] = part[i + 1][j] = part[i + 1][j + 1] = n             partn; n       } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] != 0) { n         int k = a[i][j]; n         if (x[k][0] == 0) n           x[k][0] = part[i][j]; n         else n           x[k][1] = part[i][j]; n       } n   for (int i = 1; i <= 28; i++) { n     if (x[i][0] > x[i][1]) x[i][0] ^= x[i][1] ^= x[i][0] ^= x[i][1]; n   } n } n bool chk(int p) { n   bool e[10][10]; n   memset(e, 0, sizeof(e)); n   for (int i = 1; i <= 28; i++) n     if (x[i][1] <= p) { n       int f = c[x[i][0]], g = c[x[i][1]]; n       if (f > g) f ^= g ^= f ^= g; n       if (e[f][g]) return false; n       e[f][g] = true; n     } n   return true; n } n void dfs(int p) { n   if (p > partn) { n     if (ans == 0) { n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= m; j++) n           if (a[i][j] != 0) res[i][j] = c[part[i][j]]; n     } n     ans += 5040; n     return; n   } n   for (int i = 0; i <= 6; i++) n     if (u[i] <= 1) { n       c[p] = i; n       if (!chk(p)) continue; n       u[i]++; n       dfs(p + 1); n       u[i]--; n       if (u[i] == 0) return; n     } n } n void work() { dfs(1); } n void print() { n   printf( %I64d n , ans); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] == 0) n         printf( . ); n       else n         printf( %d , res[i][j]); n     } n     printf(  n ); n   } n } n int main() { n   read(); n   prepare(); n   work(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   long long c = n, maxi = INT_MIN, mini = INT_MIN, t = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (maxi != a[i]) { n       maxi = a[i]; n       mini = a[i]; n       t = 1; n     } else { n       t++; n       c += t - 1; n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[305][305]; n vector<pair<int, int> > poss[90005]; n int dp[305][305]; n vector<int> col[305]; n int vis[305]; n int n, m, p; n void pr() {} n void solve() { n   memset(dp, 127, sizeof(dp)); n   ; n   scanf( %d %d %d , &n, &m, &p); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       scanf( %d , &ar[i][j]); n       poss[ar[i][j]].push_back(make_pair(i, j)); n       if (ar[i][j] == 1) { n         dp[i][j] = i + j; n       } n     } n   } n   for (int val = 2; val <= p; val++) { n     memset(vis, 0, sizeof(vis)); n     ; n     for (int i = 0; i < (int)(m); i++) { n       col[i].clear(); n     } n     for (int i = 0; i < poss[val].size(); i++) { n       pair<int, int> a = poss[val][i]; n       col[a.second].push_back(a.first); n     } n     for (int i = 0; i < poss[val - 1].size(); i++) { n       pair<int, int> a = poss[val - 1][i]; n       vis[a.first] = 1; n     } n     for (int i = 0; i < n; i++) { n       if (!vis[i]) continue; n       int best = 1e9; n       for (int j = 0; j < m; j++) { n         best++; n         if (ar[i][j] == val - 1) best = min(best, dp[i][j]); n         for (int k = 0; k < col[j].size(); k++) { n           dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i)); n         } n       } n       best = 1e9; n       for (int j = m; j >= 0; j--) { n         best++; n         if (ar[i][j] == val - 1) best = min(best, dp[i][j]); n         for (int k = 0; k < col[j].size(); k++) { n           dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i)); n         } n       } n     } n   } n   printf( %d , dp[poss[p][0].first][poss[p][0].second]); n } n int main() { n   int TC = 1; n   for (int ZZ = 1; ZZ <= TC; ZZ++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2E9; n const long long LINF = (long long)9E18; n const long double PI = acos(-1.0); n const long double EPS = 1E-11; n template <typename T> n inline T sqr(const T x) { n   return x * x; n } n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <typename T> n T mod(T a, T b) { n   a %= b; n   if (a < 0) a += b; n   return a; n } n template <typename T> n inline T rad(const T x) { n   return x * PI / 180; n } n template <typename T> n inline string toString(T x) { n   ostringstream oss; n   oss << x; n   return oss.str(); n } n inline long long toInt(const string& st) { n   istringstream istringstream(st); n   long long x; n   istringstream >> x; n   return x; n } n inline long double toDouble(const string& st) { n   istringstream istringstream(st); n   long double x; n   istringstream >> x; n   return x; n } n inline string toLower(string st) { n   for (int i = 0; i < (((int)(st).size())); ++i) st[i] = tolower(st[i]); n   return st; n } n inline string toUpper(string st) { n   for (int i = 0; i < (((int)(st).size())); ++i) st[i] = toupper(st[i]); n   return st; n } n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n int DEBUG = false; n const char DEBUG_PARAM[] =  __LOCAL_TESTING ; n const int MAXCLOCK = 100; n int aClock[MAXCLOCK], mClock[MAXCLOCK], nClock = 0, iClock = 0; n inline void tick(int maxTime = 10000) { n   if (DEBUG) { n     mClock[nClock] = maxTime; n     aClock[nClock++] = clock(); n   } n } n inline void untick(string name =  Execution Time ) { n   if (DEBUG) { n     int numClock = clock() - aClock[--nClock]; n     cout <<  @   << name <<  : [  n          << (int)((double)(numClock) / CLOCKS_PER_SEC * 1000) <<   ms] ; n     if (numClock > (int)(mClock[nClock])) n       cout <<   [TLE] ; n     else if (numClock > (int)(mClock[nClock] * 0.9)) n       cout <<   [almost TLE] ; n     cout << endl; n   } n } n const char IN[] =  _.in ; n const char OUT[] =  _.out ; n int ntest = 1, test = 1; n int n; n map<string, string> type; n pair<int, int> findDefRef(string w) { n   int d = 0, r = 0; n   while (d < ((int)(w).size()) && w[d] ==  & ) d++; n   while (r < ((int)(w).size()) && w[((int)(w).size()) - r - 1] ==  * ) r++; n   return make_pair(d, r); n } n string removeDefRef(string w) { n   pair<int, int> tmp = findDefRef(w); n   return w.substr(tmp.first, ((int)(w).size()) - tmp.first - tmp.second); n } n string findType(string original, int def, int ref) { n   if (type.count(original) == 0) { n     return  errtype ; n   } n   string nowType = type[original]; n   if (nowType ==  errtype ) { n     return nowType; n   } n   pair<int, int> tmp = findDefRef(nowType); n   nowType = removeDefRef(nowType); n   def += tmp.first, ref += tmp.second; n   if (def > ref) { n     return  errtype ; n   } else { n     ref -= def; n     def = 0; n   } n   for (int i = 0; i < (ref); ++i) nowType = nowType +  * ; n   return nowType; n } n inline void run() { n   type[ void ] =  void ; n   type[ errtype ] =  errtype ; n   for (test = 1; test <= ntest; ++test) { n     tick((int)2E3); n     cin >> n; n     string command, a, b; n     pair<int, int> tmp; n     int def, ref; n     for (int i = (1); i <= (n); ++i) { n       cin >> command; n       if (command ==  typedef ) { n         cin >> a >> b; n         tmp = findDefRef(a); n         def = tmp.first, ref = tmp.second; n         a = removeDefRef(a); n         type[b] = findType(a, def, ref); n       } else { n         cin >> a; n         tmp = findDefRef(a); n         def = tmp.first, ref = tmp.second; n         a = removeDefRef(a); n         cout << findType(a, def, ref) << endl; n       } n     } n     untick( #  + toString(test)); n   } n } n int main(int argc, char* argv[]) { n   if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) { n     DEBUG = true; n   } n   if (DEBUG) { n     tick((int)1E4); n     freopen(IN,  r , stdin); n   } n   run(); n   if (DEBUG) { n     untick( TOTAL ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int cnt[1001] = {0}, a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     cnt[a[i]] = i; n   } n   int vis[1001] = {0}; n   vector<int> res; n   for (int i = 0; i < n; i++) { n     if (cnt[a[i]] == i) res.push_back(a[i]); n   } n   cout << res.size() << endl; n   for (int i = 0; i < res.size(); ++i) cout << res[i] <<    ; n   cout << endl; n   return 0; n } n