#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n template <class T = int> n inline T readInt(); n inline double readDouble(); n inline int readUInt(); n inline int readChar(); n inline void readWord(char *s); n inline bool readLine(char *s); n inline bool isEof(); n inline int getChar(); n inline int peekChar(); n inline bool seekEof(); n inline void skipBlanks(); n inline std::string readString(); n template <class T> n inline void writeInt(T x, char end = 0, int len = -1); n inline void writeChar(int x); n inline void writeWord(const char *s); n inline void writeDouble(double x, int len = 0); n inline void flush(); n static struct buffer_flusher_t { n   ~buffer_flusher_t() { flush(); } n } buffer_flusher; n using namespace std; n template <typename T> n inline void pv(const std::vector<T> &vector, char separator =    , n                char end =   n ) { n   for (int i = 0; i < vector.size(); ++i) { n     if (i != 0) { n       writeChar(separator); n     } n     writeInt(vector[i]); n   } n   writeChar(end); n } n inline void init() { n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n static const int buf_size = 4 * 4096; n static unsigned char buf[buf_size]; n static int buf_len = 0, buf_pos = 0; n inline string readString() { n   const int MAX = 1e6 + 42; n   static char buf[MAX]; n   readWord(buf); n   return string(buf); n } n inline bool isEof() { n   if (buf_pos == buf_len) { n     buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin); n     if (buf_pos == buf_len) return 1; n   } n   return 0; n } n inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; } n inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; } n inline bool seekEof() { n   int c; n   while ((c = peekChar()) != -1 && c <= 32) buf_pos++; n   return c == -1; n } n inline void skipBlanks() { n   while (!isEof() && buf[buf_pos] <= 32U) buf_pos++; n } n inline int readChar() { n   int c = getChar(); n   while (c != -1 && c <= 32) c = getChar(); n   return c; n } n inline int readUInt() { n   int c = readChar(), x = 0; n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return x; n } n template <class T> n inline T readInt() { n   int second = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) n     second = -1, c = getChar(); n   else if (c ==  + ) n     c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return second == 1 ? x : -x; n } n inline double readDouble() { n   int second = 1, c = readChar(); n   double x = 0; n   if (c ==  - ) second = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   if (c ==  . ) { n     c = getChar(); n     double coef = 1; n     while ( 0  <= c && c <=  9 ) x += (c -  0 ) * (coef *= 1e-1), c = getChar(); n   } n   return second == 1 ? x : -x; n } n inline void readWord(char *second) { n   int c = readChar(); n   while (c > 32) *second++ = c, c = getChar(); n   *second = 0; n } n inline bool readLine(char *second) { n   int c = getChar(); n   while (c !=   n  && c != -1) *second++ = c, c = getChar(); n   *second = 0; n   return c != -1; n } n static int write_buf_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_buf_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0; n   write_buf[write_buf_pos++] = x; n } n inline void flush() { n   if (write_buf_pos) n     fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0; n } n template <class T> n inline void writeInt(T x, char end, int output_len) { n   if (x < 0) writeChar( - ), x = -x; n   char second[24]; n   int n = 0; n   while (x || !n) second[n++] =  0  + x % 10, x /= 10; n   while (n < output_len) second[n++] =  0 ; n   while (n--) writeChar(second[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *second) { n   while (*second) writeChar(*second++); n } n inline void writeDouble(double x, int output_len) { n   if (x < 0) writeChar( - ), x = -x; n   int t = (int)x; n   writeInt(t), x -= t; n   writeChar( . ); n   for (int i = output_len - 1; i > 0; i--) { n     x *= 10; n     t = std::min(9, (int)x); n     writeChar( 0  + t), x -= t; n   } n   x *= 10; n   t = std::min(9, (int)(x + 0.5)); n   writeChar( 0  + t); n } n const int MAXN = 3e5 + 42; n const int MOD = 1e9 + 7; n void solve() { n   int n = readInt(); n   vector<pair<char, int>> oper; n   vector<int> pos; n   for (int i = 0; i < 2 * n; ++i) { n     char c = readChar(); n     if (false) cerr <<  c =   << c << endl; n     if (c ==  + ) { n       oper.push_back({ + , 0}); n       pos.push_back(oper.size() - 1); n       continue; n     } else { n       int cost = readInt(); n       oper.push_back({ - , cost}); n       if (pos.empty()) { n         writeWord( NO n ); n         return; n       } n       int x = pos.back(); n       if (false) cerr << x << endl; n       pos.pop_back(); n       oper[x].second = cost; n     } n   } n   set<int> have; n   vector<int> ans; n   for (auto [op, c] : oper) { n     if (false) cerr << op <<     << c << endl; n     if (op ==  + ) { n       have.insert(c); n       ans.push_back(c); n     } else { n       auto u = *have.begin(); n       if (u != c) { n         writeWord( NO n ); n         return; n       } n       have.erase(have.begin()); n     } n   } n   writeWord( YES n ); n   pv(ans); n } n signed main() { n   init(); n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using ull = unsigned long long; n using ll = long long; n using ld = long double; n using namespace std; n const ll mod = 998244353; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, k, t; n   cin >> n >> k >> m >> t; n   deque<bool> cur(n); n   cur[k - 1] = 1; n   while (t--) { n     int f, pos; n     cin >> f >> pos; n     if (f == 1) { n       cur.insert(cur.begin() + (pos - 1), 0); n       for (int i = 0; i < ((int)(cur).size()); i++) { n         if (cur[i]) { n           cout << ((int)(cur).size()) <<     << i + 1 <<   n ; n           break; n         } n       } n     } else { n       int loc = -1; n       for (int i = 0; i < ((int)(cur).size()); i++) { n         if (cur[i]) { n           loc = i + 1; n           break; n         } n       } n       if (loc <= pos) n         while (((int)(cur).size()) > pos) cur.pop_back(); n       else n         while (pos--) cur.pop_front(); n       for (int i = 0; i < ((int)(cur).size()); i++) { n         if (cur[i]) { n           cout << ((int)(cur).size()) <<     << i + 1 <<   n ; n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maze[1500][1500]; n pair<int, int> was[1500][1500]; n int n, m; n int sr, sc; n inline int rem(int x, int y) { n   x %= y; n   return (x < 0) ? x + y : x; n } n bool dfs(int r, int c) { n   int rm = rem(r, n), cm = rem(c, m); n   if (maze[rm][cm] == 1) { n     return was[rm][cm] != pair<int, int>(r, c); n   } n   int rmn, cmn; n   if (maze[rm][cm] == 0) { n     maze[rm][cm] = 1; n     was[rm][cm] = pair<int, int>(r, c); n     rmn = rem(r, n), cmn = rem(c - 1, m); n     if (maze[rmn][cmn] != -1 && dfs(r, c - 1)) return true; n     rmn = rem(r, n), cmn = rem(c + 1, m); n     if (maze[rmn][cmn] != -1 && dfs(r, c + 1)) return true; n     rmn = rem(r + 1, n), cmn = rem(c, m); n     if (maze[rmn][cmn] != -1 && dfs(r + 1, c)) return true; n     rmn = rem(r - 1, n), cmn = rem(c, m); n     if (maze[rmn][cmn] != -1 && dfs(r - 1, c)) return true; n   } n   return false; n } n void TheSolution(istream& input, ostream& output) { n   input >> n >> m; n   string s; n   memset(maze, -1, sizeof(maze)); n   for (int i = 0; i < n; i++) { n     input >> s; n     for (int j = 0; j < m; j++) { n       if (s[j] ==  . ) maze[i][j] = 0; n       if (s[j] ==  S ) { n         sr = i; n         sc = j; n         maze[i][j] = 0; n       } n       was[i][j] = pair<int, int>(-1000000, -1000000); n     } n   } n   output << (dfs(sr, sc) ?  Yes  :  No ); n } n int main() { n   TheSolution(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct node { n   int cnt; n   node* bit[3]; n   node() { n     bit[0] = bit[1] = NULL; n     cnt = 0; n   } n }; n node* root; n void Insertt(int x) { n   node* cur = root; n   for (int i = 28; i >= 0; --i) { n     if (cur->bit[(x >> i) & 1] == NULL) cur->bit[(x >> i) & 1] = new node(); n     cur = cur->bit[(x >> i) & 1]; n     ++(cur->cnt); n   } n } n void Erasee(int x) { n   node* cur = root; n   for (int i = 28; i >= 0; --i) { n     --(cur->bit[(x >> i) & 1]->cnt); n     if (cur->bit[(x >> i) & 1]->cnt == 0) { n       cur->bit[(x >> i) & 1] = NULL; n       return; n     } else n       cur = cur->bit[(x >> i) & 1]; n   } n } n int Query(int p, int l) { n   int res = 0; n   node* cur = root; n   for (int i = 28; i >= 0; --i) { n     if (((l >> i) & 1) == 0) { n       if (cur->bit[(p >> i) & 1] != NULL) n         cur = cur->bit[(p >> i) & 1]; n       else n         return res; n     } else { n       if (cur->bit[(p >> i) & 1] != NULL) res += cur->bit[(p >> i) & 1]->cnt; n       if (cur->bit[(1 ^ ((p >> i) & 1))] != NULL) n         cur = cur->bit[(1 ^ ((p >> i) & 1))]; n       else n         return res; n     } n   } n   return res; n } n int main() { n   scanf( %d , &n); n   root = new node(); n   for (int i = 1; i <= n; ++i) { n     int type, p, l; n     scanf( %d%d , &type, &p); n     if (type == 1) Insertt(p); n     if (type == 2) Erasee(p); n     if (type == 3) { n       scanf( %d , &l); n       printf( %d n , Query(p, l)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long labs(long long a) { return a < 0 ? (-a) : a; } n long long max(long long a, long long b) { return a > b ? a : b; } n long long min(long long a, long long b) { return a < b ? a : b; } n struct Fenwik { n   vector<long long> t; n   long long n; n   void Init(long long nn) { n     n = nn; n     t.assign(n, 0); n   } n   long long Sum(long long r) { n     long long result = 0; n     for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; n     return result; n   } n   void Inc(long long i, long long delta) { n     for (; i < n; i = (i | (i + 1))) t[i] += delta; n   } n   long long Sum(long long l, long long r) { return Sum(r) - Sum(l - 1); } n   void Init(const vector<long long>& a) { n     Init(a.size()); n     for (int(i) = 0; (i) < (n); (i)++) Inc(i, a[i]); n   } n }; n vector<Fenwik> f; n vector<int> g; n vector<int> gn; n long long gs; n map<long long, pair<int, int> > ids; n long long h, m, n; n long long gt; n long long F(long long x) { n   long long pos = (gn[gt] + x) % gs; n   long long sum = 0; n   if (pos < gn[gt]) { n     sum = f[g[gt]].Sum(gn[gt], gs - 1) + f[g[gt]].Sum(0, pos); n   } else { n     sum = f[g[gt]].Sum(gn[gt], pos); n   } n   return x + 1 - sum; n } n long long UpperBound(long long l, long long r, long long f) { n   while (l < r) { n     long long m = (l + r) / 2; n     if (!(f < F(m))) { n       l = m + 1; n     } else { n       r = m; n     } n   } n   return l; n } n long long res = 0; n void Add(long long id, long long t) { n   gt = t; n   long long lb = UpperBound(0, gs - 1, 0); n   res += lb; n   long long gg = g[t]; n   long long nn = (gn[t] + lb) % gs; n   f[gg].Inc(nn, 1); n   ids[id] = make_pair(gg, nn); n } n void Delete(long long id) { f[ids[id].first].Inc(ids[id].second, -1); } n int main() { n   cin >> h >> m >> n; n   g.resize(h, -1); n   gn.resize(h, 0); n   long long group = 0; n   for (int(i) = 0; (i) < (h); (i)++) { n     if (g[i] != -1) continue; n     g[i] = group; n     gs = 1; n     for (long long j = 1; (i + j * m) % h != i; j++) { n       g[(i + j * m) % h] = group; n       gn[(i + j * m) % h] = j; n       gs = j + 1; n     } n     group++; n   } n   f.resize(group); n   for (int(i) = 0; (i) < (group); (i)++) { n     f[i].Init(gs); n   } n   char c; n   long long id; n   long long t; n   for (int(i) = 0; (i) < (n); (i)++) { n     cin >> c >> id; n     if (c ==  + ) { n       cin >> t; n       Add(id, t); n     } else { n       Delete(id); n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MAXN = 1e6 + 7; n const double EPS = 1e-8; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> cnt(m, 0); n   long long d = n % m; n   for (long long i = 0; i < m; i++) { n     cnt[i * i % m] += n / m; n     if (i && i <= d) { n       cnt[i * i % m]++; n     } n   } n   long long res = 0; n   for (long long i = 0; i < m; i++) { n     res += cnt[i] * cnt[(m - i) % m]; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n char buffer[200]; n int main() { n   int n; n   string data; n   while (scanf( %d , &n) != EOF) { n     getchar(); n     for (int i = 0; i < n; i++) { n       gets(buffer); n       data = string(buffer); n       int firstM = data.find( miao. ); n       int lastL = data.rfind( lala. ); n       if (firstM != data.npos && n           (lastL == data.npos || lastL != data.size() - 5) && firstM == 0) { n         printf( Rainbow s n ); n       } else if ((firstM == data.npos || firstM != 0) && lastL != data.npos && n                  lastL == data.size() - 5) { n         printf( Freda s n ); n       } else { n         printf( OMG>.< I don t know! n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long double PI = 3.1415926535898; n const long long LLMAX = LLONG_MAX; n int a[(int)2e5 + 5], b[(int)2e5 + 5], c[(int)2e5 + 5]; n int n; n vector<vector<int> > v((int)2e5 + 5); n int type10 = 0; n int type01 = 0; n long long ans = 0; n int one[(int)2e5 + 5] = {0}; n int zero[(int)2e5 + 5] = {0}; n void solve(int parent, int node) { n   if (b[node] != c[node]) { n     if (b[node]) n       one[node]++; n     else n       zero[node]++; n   } n   if (parent) a[node] = min(a[node], a[parent]); n   for (int i = 0; i < v[node].size(); i++) { n     if (v[node][i] == parent) n       continue; n     else { n       solve(node, v[node][i]); n       one[node] += one[v[node][i]]; n       zero[node] += zero[v[node][i]]; n     } n   } n   int dec = min(one[node], zero[node]); n   one[node] -= dec; n   zero[node] -= dec; n   ans += (long long)a[node] * 2 * dec; n } n int main() { n   int x, y; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     if (b[i] == 0 && c[i] == 1) n       type01++; n     else if (b[i] == 1 && c[i] == 0) n       type10++; n   } n   for (int i = 1; i < n; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   solve(0, 1); n   if (type01 != type10) { n     cout << -1 <<   n ; n   } else { n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n int main() { n   cin >> a >> b; n   long long ans = (b * (b - 1)) / 2; n   ans %= 1000000007; n   ans = n       (a * ans + ((b * ans) % 1000000007) * ((a * (a + 1) / 2) % 1000000007)) % n       1000000007; n   cout << ans; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define mp make_pair n #define fi first n #define se second n #define pb push_back n #define pf push_front n #define ppb pop_back n #define ppf pop_front n #define sz(v) (v).size() n #define all(v) (v).begin(),(v).end() n #define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++) n #define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) n #define Time (double)clock()/CLOCKS_PER_SEC n typedef long long ll; n typedef unsigned long long ull; n typedef double db; n typedef long double ldb; n typedef pair<int,int> pi; n template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;} n template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;} n template <class T> T sqr(T x) {return x*x;} n template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);} n template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;} n const int inf=1000000000,mod=1000000007; n const ll infll=1000000000000000000ll,modll=ll(mod); n const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0); n //#define mydef n //#define LOCAL n #ifdef mydef n #define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) n #endif n #define debug n #define putnum(x) cout<<#x<< :  <<x<<endl n #define putvec(v) cout<<#v<< :  ;REP(i,0,sz(v)) cout<<v[i]<<   ;cout<<endl n template <class T> inline void read(T &x){ n  T w=1;x=0;char c=getchar(); n  while(c< 0 ||c> 9 ) {if(c== - )w=-1;c=getchar();} n  while(c>= 0 &&c<= 9 ) {x=(x<<3)+(x<<1)+(c&15);c=getchar();} n  x*=w; n } n template <class T> inline void write(T x) { n  if(x<0) putchar( - ),x=~x+1; n  if(x) write(x/10),putchar(x%10+ 0 ); n } n  n const int maxn=200005; n int n; n ll sum[2],a[maxn]; n vector<ll> odd,even; n  n void solve() { n  odd.clear(); n  even.clear(); n  cin>>n; n  for(int i=1;i<=n;i++) { n   cin>>a[i]; n   if(a[i]&1ll) odd.pb(a[i]); n   else even.pb(a[i]); n  } n  sort(all(odd)); n  sort(all(even)); n   n  sum[0]=sum[1]=0ll; n  int p=0; n  for(int i=0;i<n;i++) { n   ll A=even.empty()?-infll:even.back(); n   ll B=odd.empty()?-infll:odd.back();   n   if(A>B) { n    if(!p) sum[p]+=A; n    even.ppb(); n   } else { n    if(p) sum[p]+=B; n    odd.ppb(); n   } n   p^=1; n  } n  cout<<(sum[0]>sum[1]? Alice :(sum[0]==sum[1]? Tie : Bob ))<<endl; n  return; n } n  n int main() { n  #ifdef LOCAL n   freopen( input.in , r ,stdin); n   freopen( output.out , w ,stdout); n  #endif n  int T; n  cin>>T; n  while(T--) solve(); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vpi = vector<pair<int, int>>; n using pi = pair<int, int>; n const int nax = 1e5 + 7; n const int mod = 1e9 + 7; n const int inf = numeric_limits<int>::max() - (int)9e6; n struct monotonic_queue { n   queue<int> qu; n   deque<int> mx, mn; n   void push(int v) { n     qu.push(v); n     while (mx.size() && mx.back() < v) mx.pop_back(); n     mx.push_back(v); n     while (mn.size() && mn.back() > v) mn.pop_back(); n     mn.push_back(v); n   } n   void pop() { n     if (mx.front() == qu.front()) mx.pop_front(); n     if (mn.front() == qu.front()) mn.pop_front(); n     qu.pop(); n   } n   int front() const { return qu.front(); } n   int max() const { return mx.front(); } n   int min() const { return mn.front(); } n   int size() const { return qu.size(); } n   bool empty() { return qu.empty(); } n }; n int n; n void test() { n   cin >> n; n   int s, l; n   cin >> s >> l; n   vi v(n); n   for (int i = 0; i < (int)n; i++) cin >> v[i]; n   monotonic_queue q; n   vi g(n, n); n   for (int i = n - 1, j = n - 1; i >= 0; i--) { n     while (j >= 0 and (q.empty() or (((q.max() - q.min()) <= s)))) { n       q.push(v[j--]); n     } n     g[i] = (((q.max() - q.min()) <= s) ? j : j + 1); n     if (((int)(q).size())) q.pop(); n   } n   while (((int)(q).size())) q.pop(); n   vi dp(n + 1, inf); n   dp[0] = 0; n   for (int i = l, j = 0; i <= n; i++) { n     q.push(dp[i - l]); n     while (((int)(q).size()) and j <= g[i - 1]) q.pop(), j++; n     if (((int)(q).size())) dp[i] = q.min() + 1; n   } n   cout << (dp[n] >= inf ? -1 : dp[n]); n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   while (T--) { n     test(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int a[20]; n long long ans, p[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, limit = 1e18; n void dfs(int num, int len) { n   if (num == 1) { n     long long res = 1; n     for (int i = 1; i <= len; ++i) { n       for (int j = 1; j < a[i]; ++j) { n         if (limit / p[i] <= res) { n           return; n         } n         res *= p[i]; n       } n     } n     ans = min(ans, res); n     return; n   } n   for (int i = 2; i <= num; ++i) { n     if (num % i == 0) { n       a[len + 1] = i; n       dfs(num / i, len + 1); n     } n   } n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << 1 << endl; n     return 0; n   } else if (n == 2) { n     cout << 2 << endl; n     return 0; n   } n   ans = limit; n   for (int i = 1; i <= n; ++i) { n     if (n % i == 0) { n       a[1] = i; n       dfs(n / i, 1); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, T; n int l[200010], r[200010], s[200010], t[200010], id[200010]; n int f[1010][1010]; n int ans[200010]; n int x[200010], y[200010]; n inline int cmp(int x, int y) { return l[x] > l[y]; } n int main() { n   scanf( %d%d%d , &n, &m, &T); n   for (int i = 1; i <= m; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i <= T; i++) n     scanf( %d%d%d%d , &l[i], &r[i], &s[i], &t[i]), id[i] = i; n   sort(id + 1, id + T + 1, cmp); n   memset(f, 63, sizeof(f)); n   int last = 1; n   for (int i = m; i; i--) { n     f[x[i]][y[i]] = f[y[i]][x[i]] = i; n     for (int j = 1; j <= n; j++) { n       f[x[i]][j] = min(f[x[i]][j], f[y[i]][j]); n       f[y[i]][j] = min(f[y[i]][j], f[x[i]][j]); n     } n     while (last <= T && l[id[last]] == i) { n       int u = id[last++]; n       ans[u] = f[s[u]][t[u]] <= r[u]; n     } n   } n   for (int i = 1; i <= T; i++) puts(ans[i] ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 0x3f3f3f3f3f3f3f3fLL; n struct Edge { n   int u, v; n   long long cap[2], flow; n }; n vector<int> d, act; n bool bfs(int s, int t, vector<vector<int>>& adj, vector<Edge>& E, n          long long lim = 1) { n   queue<int> Q; n   d = vector<int>(adj.size(), -1); n   d[t] = 0; n   Q.push(t); n   for (Q.push(t); Q.size() and d[s] == -1; Q.pop()) { n     int u = Q.front(); n     for (int i = (0); i < int(adj[u].size()); i++) { n       int e = adj[u][i], v = (E[e].u ^ E[e].v ^ u); n       if (((E[e].u == v) ? E[e].cap[0] - E[e].flow : E[e].cap[1] + E[e].flow) >= n               lim and n           d[v] == -1) { n         d[v] = d[u] + 1; n         Q.push(v); n       } n     } n   } n   return d[s] >= 0; n } n long long dfs(int u, int t, long long bot, vector<vector<int>>& adj, n               vector<Edge>& E) { n   if (bot == 0) return 0; n   if (u == t) return bot; n   for (; act[u] < int(adj[u].size()); ++act[u]) { n     int e = adj[u][act[u]]; n     if (d[u] == d[(E[e].u ^ E[e].v ^ u)] + 1) { n       long long inc = dfs((E[e].u ^ E[e].v ^ u), t, n                           min(bot, ((E[e].u == u) ? E[e].cap[0] - E[e].flow n                                                   : E[e].cap[1] + E[e].flow)), n                           adj, E); n       if (inc) { n         E[e].flow += ((E[e].u == u) ? (inc) : (-(inc))); n         return inc; n       } n     } n   } n   return 0; n } n long long maxflow(int s, int t, vector<vector<int>>& adj, vector<Edge>& E, n                   int F = 0) { n   for (auto& e : E) e.flow = 0; n   long long flow = 0, bot; n   for (int lim = (1 << F); lim >= 1; lim >>= 1) { n     while (bfs(s, t, adj, E, lim)) { n       act = vector<int>(adj.size(), 0); n       while ((bot = dfs(s, t, oo, adj, E))) flow += bot; n     } n   } n   return flow; n } n inline void addEdge(int x, int y, vector<vector<int>>& adj, vector<Edge>& E, n                     long long c, long long rc = 0) { n   Edge e; n   e.u = x; n   e.v = y; n   e.flow = 0; n   e.cap[0] = c; n   e.cap[1] = rc; n   adj[x].push_back(int((E).size())); n   adj[y].push_back(int((E).size())); n   E.push_back(e); n } n long long anss = 0; n void gomory_hu(vector<vector<int>>& adj, vector<Edge>& E, vector<int>& parent, n                vector<vector<long long>>& ans) { n   int n = int((adj).size()); n   parent.assign(n, 0); n   ans.assign(n, vector<long long>(n, oo)); n   for (int i = (1); i < int(n); i++) { n     long long f = maxflow(i, parent[i], adj, E); n     anss += f; n     bfs(i, parent[i], adj, E); n     for (int j = (i + 1); j < int(n); j++) n       if (d[j] == -1 and parent[j] == parent[i]) parent[j] = i; n     ans[i][parent[i]] = ans[parent[i]][i] = f; n     for (int j = (0); j < int(i); j++) n       ans[i][j] = ans[j][i] = min(f, ans[parent[i]][j]); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> adj(n); n   vector<Edge> E; n   for (int i = (0); i < int(m); i++) { n     int a, b, c; n     cin >> a >> b >> c; n     --a, --b; n     addEdge(a, b, adj, E, c, c); n   } n   vector<int> par; n   vector<vector<long long>> d; n   gomory_hu(adj, E, par, d); n   cout << anss << endl; n   int best = 0; n   vector<int> res(n); n   for (int i = 0; i < n; ++i) { n     res[i] = i; n   } n   for (int i = 0; i < 1; ++i) { n     vector<int> order; n     order.push_back(i); n     vector<int> used(n, 0); n     used[i] = true; n     int score = 0; n     while (int((order).size()) != n) { n       int best = -1, index = -1; n       for (int j = 0; j < n; ++j) { n         if (!used[j] && d[order.back()][j] > best) { n           best = d[order.back()][j]; n           index = j; n         } n       } n       score += best; n       order.push_back(index); n       used[index] = true; n     } n     if (score > best) { n       best = score; n       res = order; n     } n   } n   for (int i = 0; i < res.size(); ++i) { n     printf( %d  , res[i] + 1); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const int maxn = 1000010; n const long long mod = 1e9 + 7; n char str1[maxn], str2[maxn]; n int ans[maxn]; n long long L1[maxn], R1[maxn], L2[maxn], R2[maxn]; n int main() { n   while (scanf( %s , str1 + 1) != -1) { n     scanf( %s , str2 + 1); n     int len1 = strlen(str1 + 1); n     int len2 = strlen(str2 + 1); n     if (len1 - 1 != len2) { n       printf( 0 n ); n       continue; n     } n     L1[0] = 0; n     L2[0] = 0; n     R1[len1 + 1] = 0; n     R2[len2 + 1] = 0; n     for (int i = 1; i <= len1; i++) L1[i] = (L1[i - 1] * 26 + str1[i]) % mod; n     for (int i = len1; i >= 1; i--) R1[i] = (R1[i + 1] * 26 + str1[i]) % mod; n     for (int i = 1; i <= len2; i++) L2[i] = (L2[i - 1] * 26 + str2[i]) % mod; n     for (int i = len2; i >= 1; i--) R2[i] = (R2[i + 1] * 26 + str2[i]) % mod; n     int len = 0; n     for (int i = 1; i <= len1; i++) { n       if (L1[i - 1] == L2[i - 1] && R1[i + 1] == R2[i]) ans[len++] = i; n     } n     printf( %d n , len); n     for (int i = 0; i < len; i++) { n       if (i == len - 1) n         printf( %d n , ans[i]); n       else n         printf( %d  , ans[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 100000; n const int MAXQ = 200000; n int nq; n int qkind[MAXQ], qx[MAXQ], qy[MAXQ], qans[MAXQ]; n int n; n map<pair<int, int>, int> mp; n int px[MAXN], py[MAXN], pl[MAXN], pr[MAXN]; n map<long long, vector<int>> byrad; n map<pair<int, int>, vector<pair<int, int>>> matches; n map<pair<int, int>, vector<pair<int, int>>> singles; n map<pair<int, int>, vector<int>> queries; n void run() { n   scanf( %d , &nq); n   for (int i = (0); i < (nq); ++i) scanf( %d%d%d , &qkind[i], &qx[i], &qy[i]); n   n = 0; n   mp.clear(); n   for (int i = (0); i < (nq); ++i) { n     if (qkind[i] == 1) { n       int id = n++; n       pair<int, int> key = make_pair(qx[i], qy[i]); n       assert(!mp.count(key)); n       mp[key] = id; n       px[id] = qx[i], py[id] = qy[i], pl[id] = i, pr[id] = -1; n     } n     if (qkind[i] == 2) { n       pair<int, int> key = make_pair(qx[i], qy[i]); n       assert(mp.count(key)); n       int id = mp[key]; n       mp.erase(key); n       pr[id] = i; n     } n     if (qkind[i] == 3) { n       qans[i] = ((int)(mp).size()); n     } n   } n   for (auto it = mp.begin(); it != mp.end(); ++it) { n     int id = it->second; n     pr[id] = nq; n   } n   mp.clear(); n   for (int i = (0); i < (n); ++i) n     byrad[(long long)px[i] * px[i] + (long long)py[i] * py[i]].push_back(i); n   for (auto it = byrad.begin(); it != byrad.end(); ++it) { n     vector<int> lst = it->second; n     for (int ii = (0); ii < (((int)(lst).size())); ++ii) n       for (int jj = (ii + 1); jj < (((int)(lst).size())); ++jj) { n         int i = lst[ii], j = lst[jj]; n         int l = max(pl[i], pl[j]), r = min(pr[i], pr[j]); n         if (l > r) continue; n         assert(px[i] != px[j] || py[i] != py[j]); n         int cx = px[i] + px[j], cy = py[i] + py[j], g = gcd(cx, cy); n         cx /= g, cy /= g; n         matches[make_pair(cx, cy)].push_back(make_pair(l, r)); n       } n   } n   for (int i = (0); i < (n); ++i) { n     int cx = px[i], cy = py[i], g = gcd(cx, cy); n     cx /= g, cy /= g; n     singles[make_pair(cx, cy)].push_back(make_pair(pl[i], pr[i])); n   } n   for (int i = (0); i < (nq); ++i) n     if (qkind[i] == 3) { n       int cx = qx[i], cy = qy[i], g = gcd(cx, cy); n       cx /= g, cy /= g; n       queries[make_pair(cx, cy)].push_back(i); n     } n   for (auto it = queries.begin(); it != queries.end(); ++it) { n     vector<int> cqueries = it->second; n     vector<pair<int, int>> cmatches = matches[it->first]; n     vector<pair<int, int>> csingles = singles[it->first]; n     vector<pair<int, int>> evt; n     for (int i = (0); i < (((int)(cmatches).size())); ++i) n       evt.push_back(make_pair(cmatches[i].first, -2)), n           evt.push_back(make_pair(cmatches[i].second + 1, +2)); n     for (int i = (0); i < (((int)(csingles).size())); ++i) n       evt.push_back(make_pair(csingles[i].first, -1)), n           evt.push_back(make_pair(csingles[i].second + 1, +1)); n     sort(cqueries.begin(), cqueries.end()); n     sort(evt.begin(), evt.end()); n     int at = 0, sum = 0; n     for (int i = (0); i < (((int)(cqueries).size())); ++i) { n       int id = cqueries[i]; n       while (at < ((int)(evt).size()) && id >= evt[at].first) n         sum += evt[at++].second; n       qans[id] += sum; n     } n   } n   for (int i = (0); i < (nq); ++i) n     if (qkind[i] == 3) printf( %d n , qans[i]); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int i, j, k, m, n, max_length, length; n   char string[101]; n   for (n = 0, scanf( %s , string); string[n]; n++) n     ; n   for (m = 1, max_length = 0; m < n; m++) { n     for (i = m - 1; i >= 0; i--) { n       for (j = i, k = m, length = 0; k && string[j] == string[k]; n            j--, k--, length++) n         ; n       if (max_length < length) { n         max_length = length; n       } n     } n   } n   printf( %d , max_length); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int K; n   cin >> K; n   int sinir = 2 * K * K; n   int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 31, 37, 39}; n   set<int> mynums; n   int i; n   for (i = 0; i < sizeof(primes) / sizeof(int); i++) { n     mynums.clear(); n     int t1 = 1, t2 = 1, t = 1; n     for (int j = 0; j <= i; j++) t *= primes[j]; n     for (int j = 0; j <= i / 4; j++) t1 *= primes[j] * primes[i - j]; n     for (int j = i / 4 + 1; j < i - i / 4; j++) t2 *= primes[j]; n     mynums.insert(t); n     for (int y = 0; y < 30; y++) { n       for (int j = 0; j <= i; j++) { n         for (set<int>::iterator it = mynums.begin(); it != mynums.end(); it++) { n           if (*it * primes[j] <= sinir) { n             mynums.insert(*it * primes[j]); n           } n         } n       } n     } n     if (mynums.size() > K) break; n     if (t1 > 1) mynums.insert(t1); n     if (t2 > 1) mynums.insert(t2); n     for (int y = 0; y < 30; y++) { n       for (int j = 0; j <= i; j++) { n         for (set<int>::iterator it = mynums.begin(); it != mynums.end(); it++) { n           if (*it * primes[j] <= sinir) { n             mynums.insert(*it * primes[j]); n           } n         } n       } n     } n     if (mynums.size() <= K / 2) continue; n     if (mynums.size() > K) break; n   } n   set<int>::iterator it = mynums.end(); n   it--; n   cout << *it; n   i = 1; n   for (it--; it != mynums.begin() && i < K; it--, i++) cout <<     << *it; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n char s[504][504]; n int vis[504][504]; n vector<pair<char, pair<int, int> > > v; n vector<pair<int, int> > ord; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n void dfs(int x, int y) { n   ord.push_back(make_pair(x, y)); n   for (int i = 0; i < 4; i++) n     if (x + dx[i] < n and y + dy[i] < m and x + dx[i] >= 0 and n         y + dy[i] >= 0 and s[x + dx[i]][y + dy[i]] !=  #  and n         !vis[x + dx[i]][y + dy[i]]) { n       vis[x + dx[i]][y + dy[i]] = 1; n       dfs(x + dx[i], y + dy[i]); n     } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; i++) scanf( %s , &s[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (s[i][j] ==  . ) v.push_back(make_pair( B , make_pair(i, j))); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (!vis[i][j] and s[i][j] !=  # ) { n         ord.clear(); n         vis[i][j] = 1; n         dfs(i, j); n         for (int k = ord.size() - 1; k >= 1; k--) { n           v.push_back(make_pair( D , ord[k])); n           v.push_back(make_pair( R , ord[k])); n         } n       } n   printf( %d n , v.size()); n   for (int i = 0; i < v.size(); i++) n     printf( %c %d %d n , v[i].first, v[i].second.first + 1, n            v[i].second.second + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   char S[110]; n   scanf( %s , S); n   int len = strlen(S); n   sort(S, S + len); n   for (int i = 0; i < len - 1; i++) { n     if (S[i + 1] - S[i] != 1) { n       printf( NO n ); n       return; n     } n   } n   printf( YES n ); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sqr(long long int x) { return x * x; } n inline long long int add(long long int a, long long int b) { n   long long int c = a + b; n   if (c >= 1000000007) c -= 1000000007; n   return c; n } n inline long long int mul(long long int a, long long int b) { n   return (a * 1ll * b) % 1000000007; n } n inline long long int sub(long long int a, long long int b) { n   long long int c = a - b; n   if (c < 0) c += 1000000007; n   return c; n } n const int nax = 1000 * 1000 + 10; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   auto ok = [](vector<int> &v, int pos) { n     swap(v[pos + 1], v[pos + 2]); n     swap(v[pos], v[pos + 1]); n   }; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> v(n); n     vector<pair<int, int>> x(n); n     for (int i = 0; i < n; ++i) { n       cin >> v[i]; n       x[i] = {v[i], i}; n     } n     sort(x.begin(), x.end()); n     for (int i = 0; i < n; ++i) { n       v[x[i].second] = i; n     } n     int cnt = 0; n     for (int i = 0; i < n; ++i) { n       for (int j = i + 1; j < n; ++j) { n         cnt += (v[j] < v[i]); n       } n     } n     if (cnt & 1) { n       for (int i = 0; i < n - 1; ++i) { n         if (x[i].first == x[i + 1].first) { n           swap(v[x[i].second], v[x[i + 1].second]); n           break; n         } n       } n     } n     vector<int> ans; n     for (int i = 0; i < n - 2; ++i) { n       int pos = min_element(v.begin() + i, v.end()) - v.begin(); n       while (pos > i + 1) { n         ok(v, pos - 2); n         ans.push_back(pos - 2); n         pos -= 2; n       } n       if (pos != i) { n         ok(v, i); n         ok(v, i); n         ans.push_back(i); n         ans.push_back(i); n       } n     } n     for (int i = 0; i < 3; ++i) { n       if (is_sorted(v.begin(), v.end())) break; n       ok(v, n - 3); n       ans.push_back(n - 3); n     } n     if (!is_sorted(v.begin(), v.end())) { n       cout << -1 <<   n ; n     } else { n       cout << ans.size() <<   n ; n       for (int i = 0; i < ans.size(); ++i) cout << ans[i] + 1 <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     string str; n     cin >> str; n     bool flag = true; n     if (n < 3) { n       if (n == 1) { n         if (str[0] ==  1 ) n           cout <<  Yes  << endl; n         else n           cout <<  No  << endl; n       } else { n         if (str !=  00  && str !=  11 ) n           cout <<  Yes  << endl; n         else n           cout <<  No  << endl; n       } n       continue; n     } n     for (int i = 0; i < n - 2; i++) { n       int ans = 0; n       for (int j = i; j < i + 3; j++) { n         ans += str[j] -  0 ; n       } n       if (ans == 1) n         continue; n       else if (ans == 0 || ans == 3) { n         flag = false; n         break; n       } else { n         if (str[i + 1] !=  0 ) { n           flag = false; n           break; n         } n       } n     } n     if (str[0] ==  0  && str[1] ==  0  && str[2] ==  1 ) flag = false; n     if (str[n - 1] ==  0  && str[n - 2] ==  0  && str[n - 3] ==  1 ) n       flag = false; n     if (flag) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct SCC { n   vector<pair<int, int>> minmax; n   int size; n   SCC(vector<int> v) { n     minmax.resize(v.size()); n     for (int i = 0; i < (int)v.size(); i++) { n       minmax[i].first = v[i]; n       minmax[i].second = v[i]; n     } n     size = 1; n   } n   bool operator<(SCC const& other) const { n     for (int i = 0; i < (int)minmax.size(); i++) { n       if (other.minmax[i].first < minmax[i].second) return false; n     } n     return true; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   set<SCC> s; n   for (int i = 0; i < n; i++) { n     vector<int> v(k); n     for (int i = 0; i < k; i++) { n       cin >> v[i]; n     } n     SCC scc(v); n     auto a = s.lower_bound(scc); n     auto b = s.upper_bound(scc); n     if (a != b) { n       auto x = a; n       while (x != b) { n         for (int K = 0; K < k; K++) { n           scc.minmax[K].first = min(scc.minmax[K].first, x->minmax[K].first); n           scc.minmax[K].second = max(scc.minmax[K].second, x->minmax[K].second); n         } n         scc.size += x->size; n         x++; n       } n       s.erase(a, b); n     } n     s.insert(scc); n     cout << s.rbegin()->size <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct he { n   int ind; n   int val; n   he(int ind_ = 0, int val_ = 0) : ind(ind_), val(val_) {} n   bool operator<(const he oth) const { return val > oth.val; } n }; n int n, m, k; n vector<int> p[200200], c[200200]; n int minv[200200], maxv[200200]; n int np[200200], nc[200200]; n int ans[200200]; n queue<int> q; n priority_queue<he> d_cand, id_cand; n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     if (a == 0) { n       minv[i] = 0; n       maxv[i] = k + 1; n     } else { n       minv[i] = a; n       maxv[i] = a; n     } n   } n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     p[b].push_back(a); n     c[a].push_back(b); n   } n   for (int i = 1; i <= n; ++i) { n     np[i] = p[i].size(); n     if (np[i] == 0) { n       maxv[i] = min(k, maxv[i]); n       q.push(i); n     } n   } n   int reached = 0; n   while (!q.empty()) { n     int ind = q.front(); n     q.pop(); n     ++reached; n     for (int x : c[ind]) { n       maxv[x] = min(maxv[x], maxv[ind] - 1); n       --np[x]; n       if (np[x] == 0) q.push(x); n     } n   } n   if (reached < n) { n     cout <<  -1 n ; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     nc[i] = c[i].size(); n     if (nc[i] == 0) { n       id_cand.push(he(i, minv[i])); n     } n   } n   for (int step = 1; step <= k; ++step) { n     while (!id_cand.empty() && id_cand.top().val <= step) { n       int x = id_cand.top().ind; n       id_cand.pop(); n       d_cand.push(he(x, maxv[x])); n     } n     if (d_cand.empty()) { n       cout <<  -1 n ; n       return 0; n     } n     int added = 0; n     while (!d_cand.empty() && d_cand.top().val == step) { n       ++added; n       int x = d_cand.top().ind; n       d_cand.pop(); n       ans[x] = step; n       for (int y : p[x]) { n         --nc[y]; n         minv[y] = max(minv[y], step + 1); n         if (nc[y] == 0) { n           id_cand.push(he(y, minv[y])); n         } n       } n     } n     if (added == 0) { n       int x = d_cand.top().ind; n       d_cand.pop(); n       if (maxv[x] < step) { n         cout <<  -1 n ; n         return 0; n       } n       ans[x] = step; n       for (int y : p[x]) { n         --nc[y]; n         minv[y] = max(minv[y], step + 1); n         if (nc[y] == 0) { n           id_cand.push(he(y, minv[y])); n         } n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int num; n     cin >> num; n     mp[num]++; n   } n   if (mp.size() != 2) { n     cout <<  NO ; n     exit(0); n   } n   if (mp.begin()->second != mp.rbegin()->second) { n     cout <<  NO ; n     exit(0); n   } n   cout <<  YES n ; n   cout << mp.begin()->first <<     << mp.rbegin()->first; n   return 0; n } n


#include<cstdio> n #include<iostream> n #include<cstring> n #include<string> n #include<cmath> n #include<map> n #include<algorithm> n #include<vector> n #include<set> n #define Quick ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) n #define ll long long n #define INF 0x3f3f3f3f n #define PI acos(-1) n #define mem(a, b) memset(a, b, sizeof a) n using namespace std; n const int N = 1e6 + 3; n const int mod =1e9 + 7;  n int a[N]; n ll dp[N]; n int vis[N]; n int tot = 0; n  n int main() n { n  Quick; n  int n; cin >> n; n  cout << (n - 1) % 9 + 1 << endl;; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int par[N], ran[N]; n struct DSU { n   DSU() { n     for (int i = 1; i < N; ++i) par[i] = i, ran[i] = 1; n   } n   int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } n   bool unite(int a, int b) { n     a = find(a), b = find(b); n     if (a == b) return false; n     ran[a] += ran[a] == ran[b]; n     if (ran[a] < ran[b]) swap(a, b); n     par[b] = a; n     return true; n   } n }; n int a[N]; n int n, q; n vector<int> primes; n int spf[N]; n void sieve() { n   for (int i = 2; i < N; ++i) { n     if (spf[i] == 0) spf[i] = i, primes.push_back(i); n     int sz = primes.size(); n     for (int j = 0; j < sz && i * primes[j] < N && primes[j] <= spf[i]; ++j) { n       spf[i * primes[j]] = primes[j]; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   sieve(); n   cin >> n >> q; n   DSU ds; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     int x = a[i]; n     while (x > 1) { n       ds.unite(spf[a[i]], spf[x]); n       x /= spf[x]; n     } n   } n   set<pair<int, int>> pary; n   for (int i = 1; i <= n; ++i) { n     int x = a[i]; n     vector<int> v; n     while (x > 1) { n       v.push_back(spf[x]); n       x /= spf[x]; n     } n     x = a[i] + 1; n     while (x > 1) { n       v.push_back(spf[x]); n       x /= spf[x]; n     } n     v.resize(unique(v.begin(), v.end()) - v.begin()); n     for (auto &j : v) { n       j = ds.find(j); n     } n     for (auto j : v) { n       for (auto k : v) { n         if (j < k) pary.insert({j, k}); n       } n     } n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     x = spf[a[x]], y = spf[a[y]]; n     if (ds.find(x) == ds.find(y)) n       cout << 0 << endl; n     else { n       x = ds.find(x); n       y = ds.find(y); n       if (x > y) swap(x, y); n       if (pary.count({x, y})) n         cout << 1 << endl; n       else n         cout << 2 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T extgcd(T a, T b, T& x, T& y) { n   for (T u = y = 1, v = x = 0; a;) { n     T q = b / a; n     swap(x -= q * u, u); n     swap(y -= q * v, v); n     swap(b -= q * a, a); n   } n   return b; n } n template <class T> n T mod_inv(T a, T m) { n   T x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n long long mod_pow(long long a, long long n, long long mod) { n   long long ret = 1; n   long long p = a % mod; n   while (n) { n     if (n & 1) ret = ret * p % mod; n     p = p * p % mod; n     n >>= 1; n   } n   return ret; n } n template <long long mod, long long primitive_root> n class NTT { n  public: n   long long get_mod() const { return mod; } n   void _ntt(vector<long long>& a, long long sign) { n     const long long n = ((long long)(a).size()); n     const long long g = primitive_root; n     long long h = (long long)mod_pow(g, (mod - 1) / n, mod); n     if (sign == -1) h = (long long)mod_inv(h, mod); n     long long i = 0; n     for (long long j = 1; j < n - 1; ++j) { n       for (long long k = n >> 1; k > (i ^= k); k >>= 1) n         ; n       if (j < i) swap(a[i], a[j]); n     } n     for (long long m = 1; m < n; m *= 2) { n       const long long m2 = 2 * m; n       const long long base = mod_pow(h, n / m2, mod); n       long long w = 1; n       for (long long x = 0; x < (m); x++) { n         for (long long s = x; s < n; s += m2) { n           long long u = a[s]; n           long long d = a[s + m] * w % mod; n           a[s] = u + d; n           if (a[s] >= mod) a[s] -= mod; n           a[s + m] = u - d; n           if (a[s + m] < 0) a[s + m] += mod; n         } n         w = w * base % mod; n       } n     } n     for (auto& x : a) n       if (x < 0) x += mod; n   } n   void ntt(vector<long long>& input) { _ntt(input, 1); } n   void intt(vector<long long>& input) { n     _ntt(input, -1); n     const long long n_inv = mod_inv(((long long)(input).size()), mod); n     for (auto& x : input) x = x * n_inv % mod; n   } n   vector<long long> convolution(const vector<long long>& a, n                                 const vector<long long>& b) { n     long long ntt_size = 1; n     while (ntt_size < ((long long)(a).size()) + ((long long)(b).size())) n       ntt_size *= 2; n     vector<long long> _a = a, _b = b; n     _a.resize(ntt_size); n     _b.resize(ntt_size); n     ntt(_a); n     ntt(_b); n     for (long long i = 0; i < (ntt_size); i++) { n       (_a[i] *= _b[i]) %= mod; n     } n     intt(_a); n     return _a; n   } n   vector<long long> powup(const vector<long long>& a, long long exponent) { n     long long ntt_size = 1; n     while (ntt_size < (((long long)(a).size()) * exponent)) ntt_size *= 2; n     vector<long long> _a = a; n     _a.resize(ntt_size); n     ntt(_a); n     for (long long i = 0; i < (ntt_size); i++) { n       _a[i] = mod_pow(_a[i], exponent, mod); n     } n     intt(_a); n     return _a; n   } n }; n vector<long long> mul(vector<long long> a, vector<long long> b, long long mod) { n   for (auto& x : a) x %= mod; n   for (auto& x : b) x %= mod; n   NTT<167772161, 3> ntt1; n   NTT<469762049, 3> ntt2; n   NTT<1224736769, 3> ntt3; n   auto x = ntt1.convolution(a, b); n   auto y = ntt2.convolution(a, b); n   auto z = ntt3.convolution(a, b); n   const long long m1 = ntt1.get_mod(), m2 = ntt2.get_mod(), m3 = ntt3.get_mod(); n   const long long m1_inv_m2 = mod_inv<long long>(m1, m2); n   const long long m12_inv_m3 = mod_inv<long long>(m1 * m2, m3); n   const long long m12_mod = m1 * m2 % mod; n   vector<long long> ret(((long long)(x).size())); n   for (long long i = 0; i < (((long long)(x).size())); i++) { n     long long v1 = (y[i] - x[i]) * m1_inv_m2 % m2; n     if (v1 < 0) v1 += m2; n     long long v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3; n     if (v2 < 0) v2 += m3; n     long long constants3 = (x[i] + m1 * v1 + m12_mod * v2) % mod; n     if (constants3 < 0) constants3 += mod; n     ret[i] = constants3; n   } n   return ret; n } n vector<long long> exppoly(vector<long long> base, long long e, long long mod) { n   vector<long long> ans = {1}; n   while (e > 0) { n     if (e % 2 == 1) { n       ans = mul(ans, base, mod); n       e--; n     } else { n       base = mul(base, base, mod); n       e /= 2; n     } n   } n   return ans; n } n const long long MOD = 1000000007LL; n int main() { n   ios_base::sync_with_stdio(false); n   long long a, b, k, t; n   cin >> a >> b >> k >> t; n   vector<long long> v; n   for (long long x = 0; x < 2 * k + 1; x++) v.push_back(1); n   vector<long long> got = exppoly(v, t, MOD); n   long long len = got.size(); n   vector<long long> suf = got; n   for (long long x = len - 2; x >= 0; x--) suf[x] = (suf[x] + suf[x + 1]) % MOD; n   long long ans = 0; n   for (long long x = 0; x < len; x++) { n     long long me = b + x; n     long long youneed = max(0LL, me + 1 - a); n     if (youneed >= len) continue; n     ans += (got[x] * suf[youneed]); n     ans %= MOD; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n bool dp[505][505]; n int c[505]; n int N, K, a[505], b[505]; n long long s1, s2; n int main() { n   ios::sync_with_stdio(false); n   cin >> N >> K; n   for (int i = 1; i <= N; ++i) cin >> a[i] >> b[i], s1 += a[i], s2 += b[i]; n   dp[0][0] = 1; n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < K; ++j) c[j] = 0, dp[i + 1][j] = dp[i][j]; n     if (a[i + 1] + b[i + 1] >= K) n       for (int j = 0; j < K; ++j) { n         if (!dp[i][j]) continue; n         int l, r; n         l = max(1, K - b[i + 1]); n         r = min(K - 1, a[i + 1]); n         if (l <= r) { n           int St, Ed; n           St = j + l; n           Ed = j + r; n           St %= K; n           Ed %= K; n           c[St]++; n           if (Ed >= St) { n             c[Ed + 1]--; n           } else { n             c[0]++; n             c[Ed + 1]--; n           } n         } n       } n     for (int j = 0; j < K; ++j) { n       if (j) c[j] += c[j - 1]; n       dp[i + 1][j] |= c[j]; n     } n   } n   long long res = 0; n   for (int j = 0; j < K; ++j) { n     if (!dp[N][j]) continue; n     long long l1, l2; n     l1 = s1, l2 = s2; n     l1 -= j; n     l2 -= (K - j) % K; n     long long cur = bool(j) + (l1 / K) + (l2 / K); n     res = max(res, cur); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char ch; n   int n, cnt = 0; n   long long x, a; n   scanf( %d%I64d , &n, &x); n   for (int i = 0; i < n; i++) { n     while (isspace(ch = getchar())) n       ; n     scanf( %I64d , &a); n     if (ch ==  + ) n       x += a; n     else { n       if (x < a) n         cnt++; n       else n         x -= a; n     } n   } n   cout << x <<     << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int cnt[N]; n int n, k; n int cut[N]; n vector<pair<int, int> > arr; n struct Sub { n   bool operator()(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) { n     if (a.first != b.first) n       return a.first < b.first; n     else n       return (cnt[a.second.second] / (a.second.first + 1)) < n              (cnt[b.second.second] / (b.second.first + 1)); n   } n }; n bool ForSort(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } n int main() { n   scanf( %d%d , &n, &k); n   int tx; n   for (int i = 1; i <= n; i++) scanf( %d , &tx), cnt[tx]++; n   for (int i = 1; i <= 200000; i++) n     if (cnt[i] > 0) arr.push_back({cnt[i], i}); n   sort(arr.begin(), arr.end(), ForSort); n   priority_queue<pair<int, pair<int, int> >, n                  vector<pair<int, pair<int, int> > >, Sub> n       pq; n   int len = 1, ex = arr[0].first; n   cut[arr[0].second] = 1; n   cout << arr[0].second <<    ; n   pq.push(pair<int, pair<int, int> >(arr[0].first, n                                      pair<int, int>(1, arr[0].second))); n   int i = 1; n   pair<int, pair<int, int> > u = n       pair<int, pair<int, int> >(2, pair<int, int>(10, 1)); n   pair<int, pair<int, int> > v = n       pair<int, pair<int, int> >(2, pair<int, int>(4, 2)); n   while (len < k) { n     pair<int, pair<int, int> > u = pq.top(); n     if (i >= arr.size()) { n       if (cut[u.second.second] + 1 <= cnt[u.second.second]) { n         len++; n         cut[u.second.second] = u.second.first + 1; n         pq.pop(); n         cout << u.second.second <<    ; n         pq.push(pair<int, pair<int, int> >( n             cnt[u.second.second] / (u.second.first + 1), n             pair<int, int>(u.second.first + 1, u.second.second))); n       } else n         pq.pop(); n     } else { n       if (u.second.first + 1 <= cnt[u.second.second] && n           cnt[u.second.second] / (u.second.first + 1) > arr[i].first) { n         cut[u.second.second] = u.second.first + 1; n         cout << u.second.second <<    ; n         len++; n         pq.pop(); n         pq.push(pair<int, pair<int, int> >( n             cnt[u.second.second] / (u.second.first + 1), n             pair<int, int>(u.second.first + 1, u.second.second))); n       } else { n         ex = min(ex, arr[i].first); n         cut[arr[i].second] = 1; n         cout << arr[i].second <<    ; n         len++; n         pq.push(pair<int, pair<int, int> >(arr[i].first, n                                            pair<int, int>(1, arr[i].second))); n         i++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n long long n, a[N], t; n map<long long, bool> vis; n int solve() { n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld , &n); n     vis.clear(); n     bool imp = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &a[i]); n       long long nw = (i + a[i]) % n; n       if (nw < 0) nw += n; n       if (vis[nw]) { n         imp = 1; n       } n       vis[nw] = 1; n     } n     if (imp) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n int main() { return solve(); } n


#include <bits/stdc++.h> n using namespace std; n void file() {} n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n } n const int N = 510 + 20; n const double PI = acos(-1.0); n bool cmp(complex<double> a, complex<double> b) { n   if (a.real() == b.real()) return a.imag() < b.imag(); n   return a.real() < b.real(); n } n bool cmp_doub(double a, double b) { n   if (fabs(a - b) <= 1e-9) { n     return 1; n   } n   return 0; n } n bool is_correct(complex<double> p1, complex<double> p2, complex<double> p3, n                 complex<double> p4) { n   vector<double> dists; n   double dist1 = (hypot((p1 - p2).imag(), (p1 - p2).real())); n   double dist2 = (hypot((p2 - p4).imag(), (p2 - p4).real())); n   double dist3 = (hypot((p3 - p4).imag(), (p3 - p4).real())); n   double dist4 = (hypot((p3 - p1).imag(), (p3 - p1).real())); n   double dist5 = (hypot((p2 - p3).imag(), (p2 - p3).real())); n   double dist6 = (hypot((p1 - p4).imag(), (p1 - p4).real())); n   dists.push_back(dist1); n   dists.push_back(dist2); n   dists.push_back(dist3); n   dists.push_back(dist4); n   dists.push_back(dist5); n   dists.push_back(dist6); n   sort(((dists).begin()), ((dists).end())); n   if (dists[0] > 0 && cmp_doub(dists[0], dists[1]) && n       cmp_doub(dists[1], dists[2]) && cmp_doub(dists[2], dists[3]) && n       cmp_doub(dists[3], dists[2])) { n     if (cmp_doub(dists[4], dists[5]) && !cmp_doub(dists[5], dists[0])) { n       return 1; n     } n   } n   return 0; n } n complex<double> rotate(complex<double> a, complex<double> b, int c) { n   for (int i = 0; i < c; i++) { n     a = (((((a) - (b))) * exp(complex<double>(0, PI / 2.0))) + b); n   } n   return a; n } n int main() { n   file(); n   fast(); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     vector<pair<complex<double>, complex<double>>> points; n     for (int j = 0; j < 4; j++) { n       int x, y, z, m; n       cin >> x >> y >> z >> m; n       points.push_back({complex<double>(x, y), complex<double>(z, m)}); n     } n     int ans = 1e9; n     for (int j1 = 0; j1 < 4; j1++) { n       for (int j2 = 0; j2 < 4; j2++) { n         for (int j3 = 0; j3 < 4; j3++) { n           for (int j4 = 0; j4 < 4; j4++) { n             vector<complex<double>> points1(4); n             points1[0] = rotate(points[0].first, points[0].second, j1); n             points1[1] = rotate(points[1].first, points[1].second, j2); n             points1[2] = rotate(points[2].first, points[2].second, j3); n             points1[3] = rotate(points[3].first, points[3].second, j4); n             if (is_correct(points1[0], points1[1], points1[2], points1[3])) { n               ans = min(ans, j1 + j2 + j3 + j4); n             } n           } n         } n       } n     } n     if (ans == 1e9) ans = -1; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[300001]; n const long long mod = 1000000007; n int main(int argc, const char* argv[]) { n   string S; n   cin >> S; n   int n = (int)S.size(); n   string A(n,  2 ); n   int scr = 0; n   for (int i = 0; i < n; ++i) { n     char a = S[(i + n - 1) % n]; n     char b = S[i]; n     char c = S[(i + 1) % n]; n     if (a == b && b == c) n       A[i] =  0 ; n     else if (a == b && b != c) { n       ++scr; n       A[i] =  1 ; n       A[(i + 1) % n] =  0 ; n     } else if (a != b && b == c) n       A[i] =  0 ; n   } n   int fst = 0; n   while (fst < n && !(A[fst] !=  2  && A[(fst + 1) % n] ==  2 )) ++fst; n   if (fst == n) { n     if (A[0] ==  2 ) n       cout << n / 2 << endl; n     else n       cout << scr << endl; n     return 0; n   } n   int j = 0; n   for (int i = 0; i < n; ++i) { n     char a = A[(i + fst) % n]; n     char b = A[(i + fst + 1) % n]; n     if (a ==  2  && b !=  2 ) { n       scr += (i - j + 1) / 2; n     } n     if (a !=  2  && b ==  2 ) j = i; n   } n   cout << scr << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   int n, a, b, k; n   scanf( %d%d%d%d , &n, &a, &b, &k); n   int tot = 0; n   cin >> s; n   int prev = -1; n   for (int i = 0; i < n; i++) n     if (s[i] ==  1 ) { n       tot += (i - prev - 1) / b; n       prev = i; n     } n   tot += (n - prev - 1) / b; n   int ans = tot - a + 1; n   printf( %d n , ans); n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       cnt = 0; n     } else { n       cnt++; n     } n     if (cnt == b && ans) { n       ans--; n       printf( %d  , i + 1); n       cnt = 0; n     } n   } n   printf(  n ); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n const int inf = 0x3f3f3f3f; n const ll linf = 0x3f3f3f3f3f3f3f3f; n const ll mod = 1e9 + 7; n //const ll mod = 998244353; n   n ll ksm(ll x,ll k) { n  ll ret=1; n  while(k) { n   if(k&1) ret=ret*x%mod; n   x=x*x%mod,k>>=1; n  } n  return ret; n } n   n ll prime[10000005],pcnt,phi[10000005]; n bool mark[10000005]; n inline void pre(ll n) { n  mark[1]=1; n  for(ll i=2;i<=n;i++) { n   if(!mark[i]) prime[++pcnt]=i/*,phi[i]=i-1*/; n   for(ll j=1;j<=pcnt;j++) { n    ll k=i*prime[j]; n    if(k>n) break; n    mark[k]=1; n    if(i%prime[j]==0) { n //    phi[k]=phi[i]*prime[j]; n     break; n    } n //   else phi[k]=phi[i]*(prime[j]-1); n   } n  } n } n  n struct data { n  int u,v,val; n  data(int _u=0,int _v=0,int _val=0):u(_u),v(_v),val(_val) {} n  bool operator < (const data &x) const { n   if(val==x.val) return u<x.u; n   return val<x.val; n  } n }a[505*505]; n int fa[505*505],c[505*505]; n vector <int> u,v; n  n int getfa(int x) { n  return x==fa[x] ? x:fa[x]=getfa(fa[x]); n } n  n int main() { n  ios::sync_with_stdio(false); n  cin.tie(nullptr); n  int T=1; n // cin>>T; n  while(T--) { n   int n; cin>>n; n   for(int i=1;i<=n*n;i++) fa[i]=i; n   int cnt=0; n   for(int i=1;i<=n;i++) { n    for(int j=1;j<=n;j++) { n     int x; cin>>x; n     if(i==j) c[i]=x; n     if(i>=j) continue; n     a[++cnt]=data(i,j,x); n    } n   } n   sort(a+1,a+cnt+1); n   int k=n; n   for(int i=1;i<=cnt;i++) { n    int fu=getfa(a[i].u),fv=getfa(a[i].v); n    if(fu==fv) continue; n    if(fu>n && c[fu]==a[i].val) { n     fa[fv]=fu,u.push_back(fv),v.push_back(fu); n     continue; n    } n    if(fv>n && c[fv]==a[i].val) { n     fa[fu]=fv,u.push_back(fu),v.push_back(fv); n     continue; n    } n    k++; n    fa[fu]=fa[fv]=k; n    c[k]=a[i].val; n    u.push_back(fu),v.push_back(k); n    u.push_back(fv),v.push_back(k); n   } n   cout<<k<<  n ; n   for(int i=1;i<=k;i++) cout<<c[i]<<   ; n   cout<<  n ; n   cout<<k<<  n ; n   for(int i=0;i<u.size();i++) cout<<u[i]<<   <<v[i]<<  n ; n  } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b); n bool prime(int n); n long long sumdigit(long long n); n bool ispowerof2(int n); n long long onesinbinary(long long n); n long long power(long long a, long long b); n bool cmp(pair<int, int> x, pair<int, int> y) { return x.second < y.second; } n bool cmp(int x, int y) { return x > y; } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     char arr[2][n]; n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < n; j++) { n         cin >> arr[i][j]; n       } n     } n     int ans = 0; n     vector<int> v; n     for (int i = 0; i < n; i++) { n       if (arr[0][i] ==  0  && arr[1][i] ==  0 ) n         ans++; n       else if (arr[0][i] ==  0  || arr[1][i] ==  0 ) n         ans += 2; n       else { n         v.push_back(i); n       } n     } n     int x = v.size(); n     for (int i = 0; i < x; i++) { n       if (v[i] < n - 1 && v[i] > 0) { n         if (arr[0][v[i] - 1] ==  0  && arr[1][v[i] - 1] ==  0 ) { n           ans++; n         } else if (arr[0][v[i] + 1] ==  0  && arr[1][v[i] + 1] ==  0 ) { n           ans++; n           arr[0][v[i] + 1] =  1 , arr[1][v[i] + 1] =  1 ; n         } n       } else if (v[i] == n - 1) { n         if (arr[0][v[i] - 1] ==  0  && arr[1][v[i] - 1] ==  0 ) { n           ans++; n         } n       } else if (v[i] == 0) { n         if (arr[0][v[i] + 1] ==  0  && arr[1][v[i] + 1] ==  0 ) { n           ans++; n           arr[0][v[i] + 1] =  1 , arr[1][v[i] + 1] =  1 ; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n long long onesinbinary(long long n) { n   long long cnt = 0; n   while (n) { n     n = n & (n - 1); n     cnt++; n   } n   return cnt; n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool prime(int n) { n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long sumdigit(long long n) { n   int c = 0; n   while (n > 0) { n     c += n % 10; n     n /= 10; n   } n   return c; n } n bool ispowerof2(int n) { n   if (n == 0) return false; n   return (!(n & ((n) & (n - 1)))); n } n long long power(long long a, long long b) { n   if (b == 0) return 1; n   long long res = 1; n   while (b) { n     if (b % 2 == 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int p, c = 0; n   cin >> p; n   long long int x = p; n   long long int n = std::numeric_limits<int>::digits - 1; n   std::string s; n   s.reserve(n + 1); n   do s.push_back(((x >> n) & 1) +  0 ); n   while (--n > -1); n   for (auto x : s) n     if (x ==  1 ) c++; n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Str { n   int l; n   char *s; n } str[50010 << 2]; n int tot; n char s[3100000]; n int k; n int n, l[50010], r[50010]; n char ans[3100000]; n int cut(int pos) { n   for (int i = 0; i < tot; i++) n     if (pos == 0) { n       tot++; n       for (int k = tot - 1; k > i; k--) str[k] = str[k - 1]; n       return i; n     } else if (str[i].l <= pos) n       pos -= str[i].l; n     else { n       tot += 2; n       for (int k = tot - 1; k > i + 1; k--) str[k] = str[k - 2]; n       str[i + 2].l = str[i].l - pos; n       str[i + 2].s = str[i].s + pos; n       str[i].l = pos; n       return i + 1; n     } n   return tot++; n } n void getans(int h, int t) { n   int l = 0; n   for (int i = 0; t >= 0 && i < tot; i++) n     if (str[i].l <= h) { n       h -= str[i].l; n       t -= str[i].l; n     } else { n       for (int k = max(0, h); k < str[i].l && k <= t; k++) n         ans[l++] = str[i].s[k]; n       int tmp = min(str[i].l, t); n       h -= tmp; n       t -= tmp; n     } n } n void deal(int d, int len) { n   if (d < 0) { n     Str tmp; n     tmp.l = len; n     tmp.s = s + 1; n     str[tot++] = tmp; n     return; n   } n   if (r[d] >= len) { n     deal(d - 1, len); n     return; n   } n   int c = r[d] - l[d] + 1; n   if (r[d] + c >= len) { n     deal(d - 1, r[d]); n     Str tmp; n     tmp.l = len - r[d]; n     tmp.s = new char[len - r[d]]; n     int c_2 = c / 2; n     int D = (tmp.l >= c_2) ? c : (tmp.l * 2); n     getans(l[d], l[d] + D - 1); n     for (int i = 0; i < tmp.l; i++) n       if (i < c_2) n         tmp.s[i] = ans[i * 2 + 1]; n       else n         tmp.s[i] = ans[(i - c_2) << 1]; n     str[tot++] = tmp; n   } else { n     deal(d - 1, len - c); n     int pos = cut(r[d] + 1); n     Str tmp; n     tmp.l = c; n     tmp.s = new char[c]; n     int c_2 = c / 2; n     getans(l[d], r[d]); n     for (int i = 0; i < c; i++) n       if (i < c_2) n         tmp.s[i] = ans[i * 2 + 1]; n       else n         tmp.s[i] = ans[(i - c_2) << 1]; n     str[pos] = tmp; n   } n } n int main() { n   scanf( %s , s + 1); n   scanf( %d , &k); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &l[i], &r[i]); n   tot = 1; n   str[0].l = 1; n   str[0].s = new char[1]; n   str[0].s[0] =    ; n   deal(n - 1, k); n   getans(0, k); n   ans[k + 1] = 0; n   printf( %s n , ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   while (cin >> n) { n     long long i = 5, cnt = 1, x, ans = 0, y, z = 5, m; n     if (n < 5 || n == 5) { n       if (n == 1) n         cout <<  Sheldon  << endl; n       else if (n == 2) n         cout <<  Leonard  << endl; n       else if (n == 3) n         cout <<  Penny  << endl; n       else if (n == 4) n         cout <<  Rajesh  << endl; n       else if (n == 5) n         cout <<  Howard  << endl; n       continue; n     } n     while (i < n || i == n) { n       x = i; n       if (i == n) { n         x = 0; n         break; n       } else { n         z += z; n         i += z; n       } n       cnt = cnt * 2; n     } n     if (x == 0) n       cout <<  Howard  << endl; n     else { n       y = n - x; n       if (y < 1) y *= -1; n       ans = y / cnt; n       ans++; n       if (y % cnt == 0) ans--; n       if (ans == 1) n         cout <<  Sheldon  << endl; n       else if (ans == 2) n         cout <<  Leonard  << endl; n       else if (ans == 3) n         cout <<  Penny  << endl; n       else if (ans == 4) n         cout <<  Rajesh  << endl; n       else if (ans == 5) n         cout <<  Howard  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n int ans[N], vis[N]; n vector<int> G[N], s[N], tmp; n void dfs(int u, int fa) { n   int sz = s[u].size(); n   for (int i = 1; i <= sz; i++) vis[i] = 0; n   for (int j : s[u]) { n     if (ans[j] && ans[j] <= sz) vis[ans[j]] = 1; n   } n   for (int j = 1; j <= sz; j++) { n     if (!vis[j]) tmp.push_back(j); n   } n   int cur = 0; n   for (int j : s[u]) { n     if (!ans[j]) ans[j] = tmp[cur++]; n   } n   tmp.clear(); n   for (int v : G[u]) { n     if (v == fa) continue; n     dfs(v, u); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int cnt; n     scanf( %d , &cnt); n     for (int j = 1; j <= cnt; j++) { n       int x; n       scanf( %d , &x); n       s[i].push_back(x); n     } n   } n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, 0); n   int mx = 0; n   for (int i = 1; i <= m; i++) { n     if (!ans[i]) ans[i] = 1; n     mx = max(mx, ans[i]); n   } n   printf( %d n , mx); n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Time = 12622780800ll; n int i, j, k, n, ch, s, m, h, day, date, month, Fg; n long long t; n struct cc { n   int s, m, h, day, date, month, year; n   int Js() { n     if (month == 4 || month == 6 || month == 9 || month == 11) return 30; n     if (month != 2) return 31; n     if (year % 400 == 0 || (year % 100 && year % 4 == 0)) return 29; n     return 28; n   } n   int count() { n     if (year % 400 == 0 || (year % 100 && year % 4 == 0)) return 366; n     return 365; n   } n   void work() { n     if (s == 60) { n       s = 0; n       m++; n     } n     if (m == 60) { n       h++; n       m = 0; n     } n     if (h == 24) { n       date++; n       day = day % 7 + 1; n       h = 0; n     } n     if (date == Js() + 1) { n       month++; n       date = 1; n     } n     if (month == 13) { n       year++; n       month = 1; n     } n   } n   void operator+=(const long long &ad) { n     long long a = ad; n     while (s && a) { n       a--; n       if (++s == 60) work(); n     } n     while (m && a >= 60) { n       a -= 60; n       if (++m == 60) work(); n     } n     while (h && a >= 3600) { n       a -= 3600; n       if (++h == 24) work(); n     } n     while (date != 1 && a >= 86400) { n       a -= 86400; n       day = day % 7 + 1; n       if (++date == Js() + 1) work(); n     } n     while (month != 1 && a >= Js() * 86400) { n       a -= Js() * 86400; n       day = (day + Js() - 1) % 7 + 1; n       if (++month == 13) work(); n     } n     while (year % 400 && a >= count() * 86400) { n       a -= count() * 86400; n       day = (day + count() - 1) % 7 + 1; n       year++; n     } n     year += (a / Time) * 400; n     a %= Time; n     while (a >= count() * 86400) { n       a -= count() * 86400; n       day = (day + count() - 1) % 7 + 1; n       year++; n     } n     while (a >= Js() * 86400) { n       a -= Js() * 86400; n       day = (day + Js() - 1) % 7 + 1; n       if (++month == 13) work(); n     } n     while (a >= 86400) { n       a -= 86400; n       day = day % 7 + 1; n       if (++date == Js() + 1) work(); n     } n     while (a >= 3600) { n       a -= 3600; n       if (++h == 24) work(); n     } n     while (a >= 60) { n       a -= 60; n       if (++m == 60) work(); n     } n     while (a) { n       a--; n       if (++s == 60) work(); n     } n   } n } a; n void R(long long &x) { n   x = 0; n   ch = getchar(); n   while (ch <  0  ||  9  < ch) ch = getchar(); n   while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n } n void W(long long x) { n   if (x >= 10) W(x / 10); n   putchar(x % 10 +  0 ); n } n bool check() { n   if (month == -1 || month == a.month) n     if ((day == -1 && date == -1) || (day == a.day || date == a.date)) n       if (h == -1 || h == a.h) n         if (m == -1 || m == a.m) n           if (s == -1 || s == a.s) return 1; n   return 0; n } n int main() { n   scanf( %d%d%d%d%d%d , &s, &m, &h, &day, &date, &month); n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     R(t); n     t++; n     Fg = 0; n     a.s = a.m = a.h = 0; n     a.day = 4; n     a.date = a.month = 1; n     a.year = 1970; n     a += t; n     while (a.s && !check()) { n       t++; n       if (++a.s == 60) a.work(); n     } n     while (s != -1 && a.s != s) { n       t++; n       a.s++; n     } n     while (a.m && !check()) { n       t += 60; n       if (++a.m == 60) a.work(); n     } n     while (m != -1 && a.m != m) { n       t += 60; n       a.m++; n     } n     while (a.h && !check()) { n       t += 3600; n       if (++a.h == 24) a.work(); n     } n     while (h != -1 && a.h != h) { n       t += 3600; n       a.h++; n     } n     while (!check()) { n       t += 86400; n       a.day = a.day % 7 + 1; n       a.date++; n       a.work(); n     } n     W(t); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     if (n == 1 || n == 2) n       cout <<  -1 n ; n     else if (n == 3) n       cout <<  210 n ; n     else { n       if (n % 6 == 1) { n         cout <<  1 ; n         for (int i = 3; i < n; i++) cout <<  0 ; n         cout <<  20 n ; n       } n       if (n % 6 == 2) { n         cout <<  1 ; n         for (int i = 3; i < n - 1; i++) cout <<  0 ; n         cout <<  200 n ; n       } n       if (n % 6 == 3) { n         cout <<  1 ; n         for (int i = 3; i < n - 1; i++) cout <<  0 ; n         cout <<  110 n ; n       } n       if (n % 6 == 4) { n         cout <<  1 ; n         for (int i = 3; i < n; i++) cout <<  0 ; n         cout <<  50 n ; n       } n       if (n % 6 == 5) { n         cout <<  1 ; n         for (int i = 3; i < n; i++) cout <<  0 ; n         cout <<  80 n ; n       } n       if (n % 6 == 0) { n         cout <<  1 ; n         for (int i = 3; i < n - 1; i++) cout <<  0 ; n         cout <<  170 n ; n       } n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n bool ok[100005]; n typedef long long int ll; n int gcd(int a ,int b) n { n     if(b==0) n     return a; n     else if(a==0) n     return a; n     else n  n      return gcd(b,a%b); n } n int main() n { n     int n; n     cin>>n; n     ll prod=1; n     for(int i=1;i<n;i++) n     { n         if(gcd(n,i)==1) n         { n             ok[i]=1; n             prod=(prod*i)%n; n         } n     } n     if(prod!=1) n     ok[prod]=0; n  n     cout<<count(ok+1,ok+n,1)<<endl; n     for(int i=1;i<n;i++) n     { n         if(ok[i]) n         cout<<i<<   ; n     } n     return 0; n  n }


#include <bits/stdc++.h> n using namespace std; n int inf = 1000000000, mxx, mxy, mnx = inf, mny = inf, n, m, i, j, r = 0, n     x[2000001], y[2000001]; n char a[3001][3001], c[3001][3001]; n bool ok(int mxx, int mxy, int mnx, int mny) { n   for (int i = 0; i < r; i++) n     if (x[i] < mnx || x[i] > mxx || y[i] < mny || y[i] > mxy || n         (x[i] != mxx && x[i] != mnx && y[i] != mxy && y[i] != mny)) n       return false; n   return (mxx - mnx == mxy - mny && mxx < n && mxy < m && mnx >= 0 && mny >= 0); n } n void go(int mxx, int mxy, int mnx, int mny) { n   int i, j; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       if (i >= mnx && i <= mxx && j >= mny && j <= mxy && n           ((i == mxx || i == mnx) || (j == mxy || j == mny)) && a[i][j] !=  w ) n         c[i][j] =  + ; n       else n         c[i][j] = a[i][j]; n     } n } n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  w ) { n         x[r] = i; n         y[r] = j; n         r++; n       } n     } n   for (i = 0; i < r; i++) { n     if (x[i] > mxx) mxx = x[i]; n     if (y[i] > mxy) mxy = y[i]; n     if (x[i] < mnx) mnx = x[i]; n     if (y[i] < mny) mny = y[i]; n   } n   for (i = 0; i <= m; i++) { n     if (ok(mxx, i + (mxx - mnx), mnx, i)) { n       go(mxx, i + (mxx - mnx), mnx, i); n       for (i = 0; i < n; i++) n         for (j = 0; j < m; j++) { n           cout << c[i][j]; n           if (j == m - 1) cout << endl; n         } n       return 0; n     } n   } n   for (i = 0; i <= n; i++) { n     if (ok(i + (mxy - mny), mxy, i, mny)) { n       go(i + (mxy - mny), mxy, i, mny); n       for (i = 0; i < n; i++) n         for (j = 0; j < m; j++) { n           cout << c[i][j]; n           if (j == m - 1) cout << endl; n         } n       return 0; n     } n   } n   cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 11, MAXL = 100000 + 10; n struct State { n   int len, fa, num[MAXN], tr[26]; n   State() : len(0), fa(0) { n     memset(num, 0, sizeof(num)); n     memset(tr, 0, sizeof(tr)); n   } n   State(int len, int fa, int numr, int ref) : len(len), fa(fa) { n     memset(num, 0, sizeof(num)); n     memset(tr, 0, sizeof(tr)); n     num[ref] = numr; n   } n } SAM[MAXL * 2]; n int n, tot = 1, c[MAXN][2]; n string str[MAXN]; n void Construct_SAM(const string &str, int ref) { n   int ns = str.size(); n   for (int cp = 1, pre = 1, i = 0, id = str[i] -  a ; i < ns; n        id = str[++i] -  a , pre = cp) { n     if (SAM[cp].tr[id] && SAM[SAM[cp].tr[id]].len == SAM[cp].len + 1) { n       cp = SAM[cp].tr[id]; n       ++SAM[cp].num[ref]; n       continue; n     } n     cp = ++tot; n     SAM[cp] = State(SAM[pre].len + 1, 0, 1, ref); n     for (; pre && !SAM[pre].tr[id]; SAM[pre].tr[id] = cp, pre = SAM[pre].fa) n       ; n     if (pre != 0) n       if (SAM[pre].len + 1 == SAM[SAM[pre].tr[id]].len) n         SAM[cp].fa = SAM[pre].tr[id]; n       else { n         int tp = SAM[pre].tr[id]; n         SAM[++tot] = State(SAM[pre].len + 1, SAM[tp].fa, 0, 0); n         memcpy(SAM[tot].tr, SAM[tp].tr, sizeof(SAM[tp].tr)); n         SAM[cp].fa = SAM[tp].fa = tot; n         for (; pre && SAM[pre].tr[id] == tp; n              SAM[pre].tr[id] = tot, pre = SAM[pre].fa) n           ; n       } n     else n       SAM[cp].fa = 1; n   } n } n void init() { n   cin >> str[0]; n   c[0][0] = 1, c[0][1] = MAXL; n   Construct_SAM(str[0], 0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> str[i] >> c[i][0] >> c[i][1]; n     Construct_SAM(str[i], i); n   } n } n vector<int> l[MAXL]; n void Calc(int id) { n   for (int i = MAXL - 1; i >= 0; --i) n     for (int j = 0; j < (int)l[i].size(); ++j) n       SAM[SAM[l[i][j]].fa].num[id] += SAM[l[i][j]].num[id]; n } n bool Check(int x) { n   for (int i = 0; i <= n; ++i) n     if (!(SAM[x].num[i] >= c[i][0] && SAM[x].num[i] <= c[i][1])) return false; n   return true; n } n void solve() { n   for (int i = 1; i <= tot; ++i) l[SAM[i].len].push_back(i); n   for (int i = 0; i <= n; ++i) Calc(i); n   long long CC = 0; n   for (int i = 2; i <= tot; ++i) n     if (Check(i)) CC += SAM[i].len - SAM[SAM[i].fa].len; n   cout << CC << endl; n } n int main() { n   init(); n   solve(); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long int> st; n set<long long int>::iterator it; n long long int cnt[200010]; n int main() { n   long long int n, a, b, x, y, z, p, m, d, mn, mx, t, i; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld , &n); n     p = 0; n     for (i = 1; i <= n; i++) { n       scanf( %lld , &a); n       st.insert(a); n       cnt[a]++; n       p = max(p, cnt[a]); n     } n     x = st.size(); n     if (x == 1) { n       if (p >= 2) { n         cout << 1 << endl; n       } else n         cout << 0 << endl; n     } else { n       x = x - 1; n       if (p <= x) n         cout << p << endl; n       else { n         d = p - x; n         if (d > 1) n           cout << x + 1 << endl; n         else n           cout << x << endl; n       } n     } n     for (i = 1; i <= n; i++) cnt[i] = 0; n     st.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool prime[1500000 + 10]; n bool a[1500000 + 10]; n int func(int N, int x) { n   int i, ans = 0; n   if (x == 2) return 0; n   if (a[1]) return 1; n   if (x >= 1500000) return -1; n   for (i = 2; i <= x - 1; i++) n     if (prime[i]) n       if (a[i]) n         ans++; n       else n         return -1; n   return ans; n } n int main(void) { n   int i, j, N, x, tmp; n   for (i = 2; i < 1500000; i++) prime[i] = true; n   for (i = 2; i * i < 1500000; i++) n     if (prime[i]) n       for (j = i * i; j < 1500000; j += i) prime[j] = false; n   scanf( %d%d , &N, &x); n   for ((i) = 0; (i) < (int)(N); (i)++) { n     scanf( %d , &tmp); n     if (tmp < 1500000) a[tmp] = true; n   } n   int ans = func(N, x); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task(); n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   task(); n   return 0; n } n vector<int> prefix_function(string s) { n   int n = s.length(); n   vector<int> p(n, 0); n   for (int i = 1; i < n; ++i) { n     int j = p[i - 1]; n     while (j > 0 && s[i] != s[j]) j = p[j - 1]; n     if (s[i] == s[j]) p[i] = j + 1; n   } n   return p; n } n string s[3]; n void task() { n   cin >> s[0] >> s[1] >> s[2]; n   sort(s, s + 3); n   int ans = 1e9 + 10; n   do { n     string ss = s[0]; n     { n       vector<int> p = prefix_function(s[1] +  !  + s[0]); n       int mx = 0; n       for (int i = s[1].length(); i < ((int)((p).size())); ++i) n         mx = max(mx, p[i]); n       if (p.back() != ((int)((s[1]).size())) && mx != ((int)((s[1]).size()))) n         ss += s[1].substr(p.back(), ((int)((s[1]).size())) - p.back()); n     } n     { n       vector<int> p = prefix_function(s[2] +  !  + ss); n       int mx = 0; n       for (int i = s[2].length(); i < ((int)((p).size())); ++i) n         mx = max(mx, p[i]); n       if (p.back() != ((int)((s[2]).size())) && mx != ((int)((s[2]).size()))) n         ss += s[2].substr(p.back(), ((int)((s[2]).size())) - p.back()); n       ans = min(ans, ((int)((ss).size()))); n     } n   } while (next_permutation(s, s + 3)); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n const int maxn = 3e5 + 10; n const int maxv = 1e3 + 10; n const double eps = 1e-9; n std::vector<int> G[maxn]; n int ans[maxn]; n void inv(int i) { ans[i] ^= 1; } n bool bad(int u) { n   int cnt = 0; n   for (int i = 0; i < G[u].size(); i++) { n     cnt += ans[u] == ans[G[u][i]]; n   } n   return cnt >= 2; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u > v) swap(u, v); n     G[v].push_back(u); n     G[u].push_back(v); n   } n   stack<int> s; n   for (int i = 1; i <= n; i++) { n     if (bad(i)) s.push(i); n   } n   while (s.size()) { n     int u = s.top(); n     s.pop(); n     if (bad(u)) inv(u); n     for (int i = 0; i < G[u].size(); i++) { n       int v = G[u][i]; n       if (bad(G[u][i])) s.push(v); n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d , ans[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[105]; n string S; n multiset<string> st; n vector<string> v; n inline bool cmp(string a, string b) { return a.length() < b.length(); } n int cnt[26]; n char ans[105]; n int f[105][26]; n int main() { n   scanf( %d , &n); n   if (n <= 3) { n     if (n == 1) { n       printf( ? 1 1 n ), fflush(stdout); n       scanf( %s , s); n       printf( ! %s n , s); n       return 0; n     } n     printf( ? 2 %d n , n), fflush(stdout); n     for (int i = 1; i <= n * (n - 1) / 2; i++) { n       scanf( %s , s); n       S = s; n       sort(S.begin(), S.end()); n       st.insert(S); n     } n     printf( ? 1 %d n , n), fflush(stdout); n     for (int i = 1; i <= n * (n + 1) / 2; i++) { n       scanf( %s , s); n       S = s; n       sort(S.begin(), S.end()); n       if (st.find(S) == st.end()) n         v.push_back(S); n       else n         st.erase(st.find(S)); n     } n     sort(v.begin(), v.end(), cmp); n     printf( !  ); n     for (int i = 0; i < n; i++) { n       if (i == 0) { n         printf( %c , v[i][0]); n         continue; n       } n       for (int j = 0; j < i + 1; j++) cnt[v[i][j] -  a ]++; n       for (int j = 0; j < i; j++) cnt[v[i - 1][j] -  a ]--; n       for (int j = 0; j < 26; j++) { n         if (cnt[j]) { n           cnt[j] = 0, printf( %c , j +  a ); n           break; n         } n       } n     } n     puts(  ); n     return 0; n   } n   int m = (n + 1) / 2; n   printf( ? 2 %d n , m), fflush(stdout); n   for (int i = 1; i <= m * (m - 1) / 2; i++) { n     scanf( %s , s); n     S = s; n     sort(S.begin(), S.end()); n     st.insert(S); n   } n   printf( ? 1 %d n , m), fflush(stdout); n   for (int i = 1; i <= m * (m + 1) / 2; i++) { n     scanf( %s , s); n     S = s; n     sort(S.begin(), S.end()); n     if (st.find(S) == st.end()) n       v.push_back(S); n     else n       st.erase(st.find(S)); n   } n   sort(v.begin(), v.end(), cmp); n   for (int i = 0; i < m; i++) { n     if (i == 0) { n       ans[i + 1] = v[i][0]; n       continue; n     } n     for (int j = 0; j < i + 1; j++) cnt[v[i][j] -  a ]++; n     for (int j = 0; j < i; j++) cnt[v[i - 1][j] -  a ]--; n     for (int j = 0; j < 26; j++) { n       if (cnt[j]) { n         cnt[j] = 0, ans[i + 1] = j +  a ; n         break; n       } n     } n   } n   printf( ? 1 %d n , n), fflush(stdout); n   for (int i = 1; i <= n * (n + 1) / 2; i++) { n     scanf( %s , s); n     int len = strlen(s); n     for (int j = 0; j < len; j++) { n       f[len][s[j] -  a ]++; n     } n   } n   for (int i = m; i; i--) { n     for (int j = 0; j < 26; j++) { n       int x = f[i][j] - f[i - 1][j]; n       for (int k = i; k < n + 1 - i; k++) x -= ans[k] == j +  a ; n       if (x) { n         ans[n + 1 - i] = j +  a ; n         break; n       } n     } n   } n   printf( !  ); n   for (int i = 1; i <= n; i++) printf( %c , ans[i]); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n vector<long long> A, B, C; n int main() { n   scanf( %lld %lld , &a, &b); n   for (long long i = 1; i <= sqrt(a); i++) n     if (a % i == 0) A.push_back(i); n   for (long long i = 1; i <= sqrt(b); i++) n     if (b % i == 0) B.push_back(i); n   for (long long i = 1; i <= sqrt(a + b); i++) n     if ((a + b) % i == 0) C.push_back(i); n   long long ans; n   for (long long i : C) { n     for (long long j : A) { n       if (j <= i && a / j <= (a + b) / i) ans = 2 * i + (2 * (a + b) / i); n     } n     for (long long j : B) { n       if (j <= i && b / j <= (a + b) / i) ans = 2 * i + (2 * (a + b) / i); n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a, b, e; n char s[5]; n long long ans; n int c; n int main() { n   int n, f; n   scanf( %d , &n); n   while (n--) { n     scanf( %s %d , &s, &f); n     if (s[0] ==  0  && s[1] ==  1 ) n       a.push_back(f); n     else if (s[0] ==  1  && s[1] ==  0 ) n       b.push_back(f); n     else if (s[0] ==  0  && s[1] ==  0 ) n       e.push_back(f); n     else { n       c++; n       ans += f; n     } n   } n   int temp = c + 2 * min((int)a.size(), (int)b.size()); n   c += min((int)a.size(), (int)b.size()); n   if (!a.empty()) sort(a.rbegin(), a.rend()); n   if (!b.empty()) sort(b.rbegin(), b.rend()); n   for (int i = 0; i < min((int)a.size(), (int)b.size()); ++i) { n     ans += a[i]; n     ans += b[i]; n   } n   if (a.size() > b.size()) { n     for (int i = min(a.size(), b.size()); i < a.size(); ++i) e.push_back(a[i]); n   } else if (b.size() > a.size()) { n     for (int i = min(a.size(), b.size()); i < b.size(); ++i) e.push_back(b[i]); n   } n   if (!e.empty()) sort(e.rbegin(), e.rend()); n   for (int i = 0; i < e.size(); ++i) { n     if (((temp + 2) / 2) <= c) { n       ans += e[i]; n       temp++; n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( Sisend.txt ); n int n, c; n bool b[100005]; n vector<int> v, v1; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cerr.tie(0); n   cin >> n; n   for (int(i) = (0); ((i)) < ((n)); ((i))++) { n     cin >> c; n     v.emplace_back(c); n   } n   sort(v.rbegin(), v.rend()); n   int l = 1, r = v.size() / 2 + 1; n   int vas = 0; n   v1.emplace_back(v[0]); n   while (r < v.size()) { n     while (r < v.size() and v[r] == v[l]) r++; n     v1.emplace_back(v[r]), v1.emplace_back(v[l]); n     b[r] = 1; n     vas++; n     l++; n     r++; n   } n   while (l < v.size()) { n     if (!b[l]) { n       v1.emplace_back(v[l]); n     } n     l++; n   } n   cout << vas << endl; n   for (int i : v1) cout << i <<    ; n } n


///****In the name of ALLAH, most Gracious, most Compassionate****// n #include<bits/stdc++.h> n  n #define Input                   freopen( in.txt , r ,stdin) n #define Output                  freopen( out.txt , w ,stdout) n #define ll                      long long int n #define ull                     unsigned long long int n #define pii                     pair<int,int> n #define pll                     pair<ll,ll> n #define sc                      scanf n #define scin(x)                 sc( %d ,&(x)) n #define scin2(x,y)              sc( %d %d ,&(x),&(y)) n #define scln(x)                 sc( %lld ,&(x)) n #define scln2(x,y)              sc( %lld %lld ,&(x),&(y)) n #define pf                      printf n #define all(a)                  (a.begin()),(a.end()) n #define UNIQUE(X)               (X).erase(unique(all(X)),(X).end()) n #define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) n #define ms(a,b)                 memset(a,b,sizeof(a)) n #define pb(a)                   push_back(a) n #define mp                      make_pair n #define db                      double n #define EPS                     10E-10 n #define ff                      first n #define ss                      second n #define sqr(x)                  (x)*(x) n #define vi                      vector<int> n #define vl                      vector<ll> n #define vii                     vector<vector<int> > n #define vll                     vector<vector<ll> > n #define DBG                     pf( HI n ) n #define MOD                     1000000007 n #define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) n #define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++) n #define CASE(t)                 printf( Case %d:  ,t) n #define CASEl(t)                printf( Case %d: n ,t) n #define intlimit                2147483690 n #define longlimit               9223372036854775800 n #define infinity                (1<<28) n #define gcd(a,b)                __gcd(a,b) n #define lcm(a,b)                (a)*(b)/gcd(a,b) n #define mxx                     123456789 n #define PI                      2*acos(0.0) n #define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++) n #define rev(i,a,b)              for(__typeof(i) i=a;i>=b;i--) n #define coutv(v)                for(auto it:v)cout<<it<<   ;cout<<  n ; n #define cinv(v)                 for(auto &it:v)cin>>it; n #define srt(v)                  sort(v.begin(),v.end()); n #define rsrt(v)                 sort(v.rbegin(),v.rend()); n #define rvs(v)                  reverse(v.begin(),v.end()); n  n using namespace std; n /**     toint, tostring, BigMod, Power , sieve, Primefactorize ,frequency in n!     **/ n //ll toint(string s){ll n=0,k=1;for(int i=s.size()-1; i>=0; i--){n += ((s[i]- 0 )*k);k*=10;}return n;} n //string tostring(ll x){string s=  ;while(x){s += (x%10)+ 0 ;x/=10;}reverse(s.begin(),s.end());return s;} n //ll BigMod(ll a,ll b){if(b == 0)return 1%MOD;else if(b%2 == 0){ll temp=BigMod(a,b/2);return ((temp%MOD)*(temp%MOD))%MOD;}else{return ((a%MOD)*BigMod(a,b-1)%MOD)%MOD;}} n //ll Power(ll a,ll n){ll ret=1;for(ll i=1 ; i<=n ; i++)ret = ((ret%MOD)*(a%MOD))%MOD;return ret;} n //bool isPrime(ll n){for(ll i=2 ; i*i<=n ; i++){if(n%i == 0)return 0;}return 1;} n //vector<ll>Prime; n //bool mark[10000003]; n //void sieve(ll n){ll i,j;mark[1]=1;for(i=4; i<=n; i+=2)mark[i]=1;Prime.push_back(2);for(i=3; i<=n; i+=2){if(!mark[i]){Prime.push_back(i);if(i*i<=n){for(j=i*i; j<=n; j+=(i*2))mark[j]=1;}}}} n //map<ll,ll>Factor; n //void Primefactorize(ll n){for(ll i=0; i<Prime.size() && Prime[i]*Prime[i]<=n; i++){if(n%Prime[i] == 0){while(n%Prime[i] == 0){Factor[Prime[i]]++;n/=Prime[i];}}}if(n>1){Factor[n]++;}} n //ll frequency(ll n,ll factor)/** Frequency of a factor in n! **/{ll cnt=0;while(n){cnt += (n/factor);n /= factor;}return cnt;} n /**     Order Set       **/ n //#include <ext/pb_ds/assoc_container.hpp> n //using namespace __gnu_pbds; n //template<typename T> using orderset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; n //orderset<int> s ; //orderset<int>::iterator it ; n //orderset<int> X; //X.insert(1); //X.insert(2); //X.insert(4); //X.insert(8); //X.insert(16); n //cout<<*X.find_by_order(0)<<endl; // 2 //cout<<*X.find_by_order(2)<<endl; // 4 //cout<<*X.find_by_order(4)<<endl; // 16 //cout<<(end(X)==X.find_by_order(6))<<endl; // true n //cout<<X.order_of_key(-5)<<endl;  // 0 //cout<<X.order_of_key(1)<<endl;   // 0 //cout<<X.order_of_key(3)<<endl;   // 2 //cout<<X.order_of_key(4)<<endl;   // 2 //cout<<X.order_of_key(400)<<endl; // 5 n  n struct custom_hash {    ///For unordered map or set n     static uint64_t splitmix64(uint64_t x) { n         x += 0x9e3779b97f4a7c15; n         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n         x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n         return x ^ (x >> 31); n     } n  n     size_t operator()(uint64_t x) const { n         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); n         return splitmix64(x + FIXED_RANDOM); n     } n }; n  n /** Debugging Tool **/ n #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } n void err(istream_iterator<string> it) {} n template<typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) n { n     cerr << *it <<   =   << a << endl; n     err(++it, args...); n } n /// n  n inline int add(int a, int b) {a += b; return a >= MOD ? a - MOD : a;} n inline int sub(int a, int b) {a -= b; return a < 0 ? a + MOD : a;} n inline int mul(int a, int b) {return (ll) a * b % MOD;} n int Set(int N, int pos) {return  N = N | (1<<pos);} n int Reset(int N, int pos) {return  N = N & ~(1<<pos);} n bool Cheek(int N, int pos) {return  (bool)(N & (1<<pos));} n  n //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n //int my_rand(int l,int r) n //{ n //    return uniform_int_distribution<int>(l , r) (rng); n //} n  n //string small =  abcdefghijklmnopqrstuvwxyz ; n //string capital =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ; n  n ///------------------Graph Moves------------------- n ///const int fx[] = {+1,-1,+0,+0}; n ///const int fy[] = {+0,+0,+1,-1}; n ///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King s move n ///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///King s move n ///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///Knight s move n ///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///Knight s move n  n ll s[5009],added[5009]; n  n void Solve(int t) n { n     ll i,j,k,n,ans=0; n     scln(n); n     for(i=1 ; i<=n ; i++) n         scln(s[i]); n  n     for(i=1 ; i<=n ; i++){ n         if(s[i] - added[i] <= 1){ n             for(j=i+2 ; j<=min(n , i+s[i]) ; j++) n                 added[j]++; n             if(added[i] >= s[i]){ n                 ll xtra = added[i] - (s[i] - 1); n                 added[i+1] += xtra; n             } n             continue; n         } n  n         ll temp = (s[i] - 1) - added[i]; n         added[i] += temp; n         ans += temp; n  n //        error(i , temp); n         for(j=i+2 ; j<=min(n , i+s[i]) ; j++){ n             added[j]++; n         } n  n         if(added[i] >= s[i]){ n             ll xtra = added[i] - (s[i] - 1); n             added[i+1] += xtra; n         } n     } n     pf( %lld n ,ans); n  n     for(i=1 ; i<=n+2 ; i++) n         s[i] = added[i] = 0; n } n  n int main() n { n     int t,T; n     T = 1; n     cin>>T; n     RUN_CASE(t,T) n     { n         Solve(t); n     } n     return 0; n } n  n /** Stuff u should look for n     * Avoid overflow n     * Corner(Small) case n     * Don t get stuck on one approach n     * Don t forget to clean used memory in each case n **/ n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n const long long BIG_INF = 1e18; n const long long MOD = 1e9 + 7; n const int maxn = 1e5 + 5; n vector<int> g[maxn]; n int p[maxn], max_height, deep_v; n void dfs(int v, int height) { n   if (height > max_height) { n     max_height = height; n     deep_v = v; n   } n   for (int j = 0; j < (int)g[v].size(); j++) { n     int to = g[v][j]; n     dfs(to, height + 1); n   } n } n void out(int v) { n   if (v) out(p[v]); n   ; n   cout << v <<    ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u; n     cin >> u; n     p[i] = u; n     g[u].push_back(i); n   } n   dfs(0, 0); n   vector<int> ans; n   int v = deep_v; n   while (1) { n     while (v && (int)g[p[v]].size() == 1) v = p[v]; n     if (!v) break; n     int par = p[v]; n     g[par].erase(find(g[par].begin(), g[par].end(), v)); n     int to = g[par][0]; n     ans.push_back(v); n     g[to].push_back(v); n     p[v] = to; n   } n   out(deep_v); n   cout <<   n ; n   reverse(ans.begin(), ans.end()); n   cout << (int)ans.size() <<   n ; n   for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char a[19]; n   int i, c = 0, t = 0, x = 0; n   scanf( %s , &a); n   for (i = 0; a[i] !=   0 ; i++) { n     t = t + a[i]; n   } n   x = a[0] - 1 + (i - 1) *  9 ; n   if (t >= x) n     printf( %s , a); n   else { n     if (a[1] !=  9 ) { n       for (i = 0; a[i] !=   0 ; i++) { n         if (c > 0) n           printf( 9 ); n         else { n           if (a[i] >  1 ) printf( %c , a[i] - 1); n           c++; n         } n       } n     } else { n       c = 0; n       printf( %c , a[0]); n       for (i = 1; a[i] !=   0 ; i++) { n         if (c == 0) { n           if (a[i + 1] ==  9 ) n             printf( 9 ); n           else { n             printf( 8 ); n             c++; n           } n         } else n           printf( 9 ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long a[1006]; n long long fac[1000006]; n long long power(long long a, long long i) { n   if (i == 0) return 1 % 1000000007; n   long long t = power(a, i / 2); n   long long ans = t * t % 1000000007; n   if (i % 2 == 1) ans = ans * a % 1000000007; n   return ans; n } n long long work(long long m, long long i) { n   return ((fac[m] % 1000000007) * n           (power(fac[i] * fac[m - i] % 1000000007, 1000000007 - 2) % n            1000000007)) % n          1000000007; n } n int main() { n   fac[0] = 1; n   for (int i = 1; i < 1000006; i++) fac[i] = (fac[i - 1] * i) % 1000000007; n   long long ans = 1; n   long long sum = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   for (int i = n; i >= 1; i--) { n     ans = ans * work(sum - 1, a[i] - 1) % 1000000007; n     sum -= a[i]; n   } n   cout << ans; n   return 0; n } n