malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int N;   int A[2000000], B[2000000];   int l[2000000], r[2000000], c[2000000];   int main() {     scanf( %d , &N);     int mxi = 0;     for (int Ni = 0; Ni < N; Ni++) {       scanf( %d , &A[Ni]);       if (A[Ni] > A[mxi]) mxi = Ni;     }     for (int Ni = 0; Ni < N; Ni++) B[Ni] = A[(Ni + mxi) % N];     B[N] = B[0];     for (int Ni = N - 1; Ni >= 0; Ni--) {       r[Ni] = Ni + 1;       while (r[Ni] < N && B[Ni] > B[r[Ni]]) r[Ni] = r[r[Ni]];       if (r[Ni] < N && B[Ni] == B[r[Ni]]) c[Ni] = c[r[Ni]] + 1, r[Ni] = r[r[Ni]];     }     for (int Ni = 1; Ni < N; Ni++) {       l[Ni] = Ni - 1;       while (l[Ni] > 0 && B[Ni] > B[l[Ni]]) l[Ni] = l[l[Ni]];       if (l[Ni] > 0 && B[Ni] == B[l[Ni]]) l[Ni] = l[l[Ni]];     }     long long Ans = 0;     for (int Ni = 0; Ni < N; Ni++) {       if (B[Ni] == B[0])         ;       else if (l[Ni] == 0 && r[Ni] == N)         Ans++;       else         Ans += 2;       Ans += c[Ni];     }     printf( %I64d n , Ans);   }
#include <bits/stdc++.h>   using namespace std;   void start() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   long long ans = INT_MAX;   void solve(long long a, long long b, long long k, long long steps) {     if (a == 1 && b == 1) {       ans = min(ans, steps);       return;     }     if (a - b > 0) solve(a - b, b, k, steps + 1);     if (b - a > 0) solve(a, b - a, k, steps + 1);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     start();     long long n;     cin >> n;     for (long long i = 1; i < n; i++) {       solve(i, n - i, n, 1);     }     if (ans == INT_MAX)       cout <<  0             <<   n ;     else       cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, m, k;     cin >> n >> m >> k;     vector<pair<long long int, long long int> > F(n), S(m);     long long int maxDay = 0;     for (long long int i = 0; i < n; i++) {       cin >> F[i].first;       F[i].second = i + 1;       maxDay = max(maxDay, F[i].first);     }     for (long long int i = 0; i < m; i++) {       cin >> S[i].first;       S[i].second = i + 1;     }     long long int f_i = 0, s_i = 0;     sort(F.begin(), F.begin() + F.size());     sort(S.begin(), S.begin() + S.size());     vector<long long int> res;     for (long long int i = 0; i <= maxDay; i++) {       long long int use = 0;       while (use < k && f_i < F.size() && F[f_i].first == i) {         f_i++;         use++;       }       if (use == k && f_i < F.size() && F[f_i].first == i) {         cout <<  -1 ;         return 0;       }       while (s_i < S.size() && S[s_i].first < i) s_i++;       while (use < k && s_i < S.size() && S[s_i].first >= i) {         res.push_back(S[s_i].second);         use++;         s_i++;       }       while (use < k && f_i < F.size() && F[f_i].first > i) {         use++;         f_i++;       }     }     for (long long int i = maxDay + 1; s_i < S.size(); i++) {       long long int use = 0;       while (s_i < S.size() && S[s_i].first < i) s_i++;       while (use < k && s_i < S.size() && S[s_i].first >= i) {         res.push_back(S[s_i].second);         s_i++;         use++;       }     }     cout << res.size() <<   n ;     for (auto x : res) cout << x <<    ;   }
#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:400000000 )   int n, m;   struct space {     int from, to, dist;     space(int f, int t) : from(f), to(t) {       if (f > t) {         dist = 0;       } else if (f == 1 || t == n) {         dist = t - f + 1;       } else {         dist = (t - f) / 2 + 1;       }     }     bool operator<(const space& b) const {       return dist > b.dist || dist == b.dist && from < b.from;     }   };   set<space> S;   int carP[1000010], carL[1000010], carR[1000010], posToCar[1000010];   int main() {     cin >> n >> m;     S.insert(space(1, n));     for (int i = 0; i < m + 1; i++) {       if (i == m) break;       int t, id;       cin >> t >> id;       if (t == 1) {         space sp = *S.begin();         S.erase(S.begin());         int F = sp.from, T = sp.to, pos;         if (F == 1 && T == n)           pos = 1;         else if (F == 1)           pos = 1;         else if (T == n)           pos = n;         else           pos = (F + T) / 2;         cout << pos << endl;         carP[id] = pos;         carL[id] = F - 1;         carR[id] = T + 1;         posToCar[pos] = id;         carR[posToCar[F - 1]] = pos;         carL[posToCar[T + 1]] = pos;         S.insert(space(F, pos - 1));         S.insert(space(pos + 1, T));       } else {         int F = carL[id], P = carP[id], T = carR[id];         S.erase(space(F + 1, P - 1));         S.erase(space(P + 1, T - 1));         carR[posToCar[F]] = T;         carL[posToCar[T]] = F;         S.insert(space(F + 1, T - 1));       }     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename _T, typename _T2>   string to_string(pair<_T, _T2> x) {     return  <  + to_string(x.first) +  ,   + to_string(x.second) +  > ;   }   template <typename _T>   string to_string(vector<_T> vec) {     string s =  [  ;     for (auto el : vec) s += to_string(el) +  ,  ;     s[s.size() - 1] =  ] ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T>   string to_string(set<_T> vec) {     string s =  {  ;     for (auto el : vec) s += to_string(el) +  ,  ;     s[s.size() - 1] =  } ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T, typename _T2>   string to_string(map<_T, _T2> vec) {     string s =  {  ;     for (auto el : vec)       s +=  mp[  + to_string(el.first) +  ]=   + to_string(el.second) +  ,  ;     s[s.size() - 1] =  } ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T>   inline void _DBG(const char *s, _T x) {     cerr << s <<   =   << to_string(x) <<   n ;   }   template <typename _T, typename... args>   void _DBG(const char *s, _T x, args... a) {     while (*s !=  , ) cerr << *s++;     cerr <<   =   << to_string(x) <<   n ;     _DBG(s + 1, a...);   }   mt19937 rng___(chrono::steady_clock::now().time_since_epoch().count());   const long long MOD = 998244353;   const long long LLINF = 1e18 + 7;   const int N = 2e5 + 8;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     ;     int t;     cin >> t;     while (t--) {       int n[3], len;       cin >> n[0] >> n[1] >> n[2];       len = n[0] + n[1] + n[2] + 1;       string ans;       if (n[0] > 0) {         ans += string(n[0] + 1,  0 );         if (n[2] > 0) {           n[1]--;           ans += string(n[2] + 1,  1 );         }         while (n[1]--) {           if (ans.back() ==  1 )             ans +=  0 ;           else             ans +=  1 ;         }         cout << ans <<   n ;       } else {         ans += string(n[2] + 1,  1 );         while (n[1]--) {           if (ans.back() ==  1 )             ans +=  0 ;           else             ans +=  1 ;         }         cout << ans <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     string s;     cin >> n >> m;     cin >> s;     int pos = 0;     for (int i = 1; i < n; i++) {       if (s.substr(0, i) == s.substr(n - i, i)) {         pos = i;       }     }     cout << s;     for (int i = 1; i < m; i++) {       cout << s.substr(pos);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 7, n = 7, maxm = 101;   int m, cntblk = maxn, f[maxn], deg[maxn];   bool used[maxm];   class ed {    public:     int target, id;     char type;     ed(int t, int i, char ty) : target(t), id(i), type(ty){};   };   class ans {    public:     int id;     char type;     ans(int i, char ty) : id(i), type(ty){};   };   vector<ed> edge[maxn];   vector<ans> answer;   inline int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }   void euler(int x, int in_id, char in_type) {     for (vector<ed>::iterator vi = edge[x].begin(); vi != edge[x].end(); vi++) {       if (used[(*vi).id]) continue;       used[(*vi).id] = true;       euler((*vi).target, (*vi).id, (*vi).type);     }     if (in_id >= 0) answer.push_back(ans(in_id, in_type));   }   int main() {     cin >> m;     for (int i = 0; i < n; i++) f[i] = i, deg[i] = 0;     for (int i = 1, x, y; i <= m; i++) {       cin >> x >> y;       deg[x]++;       deg[y]++;       edge[x].push_back(ed(y, i,  + ));       edge[y].push_back(ed(x, i,  - ));       if (getf(x) == getf(y)) continue;       f[getf(x)] = getf(y);       cntblk--;     }     int tmp = 0, s = -1;     for (int i = 0; i < n; i++) {       cntblk -= (deg[i] == 0);       tmp += (deg[i] & 1);       if (deg[i] > 0 && (s == -1 || deg[i] & 1)) s = i;     }     if (cntblk != 1 || !(tmp == 0 || tmp == 2)) {       puts( No solution );       return 0;     }     memset(used, false, sizeof(used));     euler(s, -1,  + );     for (vector<ans>::reverse_iterator vi = answer.rbegin(); vi < answer.rend();          vi++) {       printf( %d %c n , (*vi).id, (*vi).type);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 200020;   int a[maxn], b[maxn], pos[maxn], ans;   unsigned long long hsh, qpow[200020] = {1}, sum;   int n, m;   struct node {     unsigned long long a, tot;   } tr[maxn << 2];   void pushup(int p) {     tr[p].tot = tr[p << 1].tot + tr[p << 1 | 1].tot;     tr[p].a = tr[p << 1].a * qpow[tr[p << 1 | 1].tot] + tr[p << 1 | 1].a;   }   void update(int l, int r, int pos, int p, int k) {     if (l == r) {       if (!k)         tr[p].tot -= 1;       else         tr[p].tot += 1;       tr[p].a = k;       return;     }     int mid = l + r >> 1;     if (pos <= mid) update(l, mid, pos, p << 1, k);     if (pos > mid) update(mid + 1, r, pos, p << 1 | 1, k);     pushup(p);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       hsh = hsh * 23 + a[i];       qpow[i] = qpow[i - 1] * 23;       sum += qpow[i - 1];     }     for (int i = 1; i <= m; i++) scanf( %lld , &b[i]), pos[b[i]] = i;     for (int i = 1; i <= m; i++) {       if (i > n) update(1, m, pos[i - n], 1, 0);       update(1, m, pos[i], 1, i);       int d = i - n;       if (d >= 0 && tr[1].a == d * sum + hsh) ++ans;     }     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int n;     cin >> n;     if (n % 2 == 0)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       long long int b[(2 * n) + 1];       b[1] = 1;       b[2 * n] = 2 * n;       long long int i = n + 1;       long long int j = 2;       long long int k = 2;       long long int flag = 1;       while (k < 2 * n) {         if (flag == 1) {           b[i] = k;           b[i + 1] = k + 1;           k = k + 2;           i = i + 2;           flag = 0;         } else {           b[j] = k;           b[j + 1] = k + 1;           k = k + 2;           j = j + 2;           flag = 1;         }       }       for (long long int i = 1; i <= 2 * n; i++) cout << b[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e2 + 5;   const int MOD = 1e9 + 7;   long long cnt[N], x;   int n, tmp;   vector<vector<long long> > matMul(vector<vector<long long> > &A,                                     vector<vector<long long> > &B) {     vector<vector<long long> > C(N, vector<long long>(N, 0));     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         for (int k = 0; k < N; k++) {           C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % MOD) % MOD;         }       }     }     return C;   }   vector<vector<long long> > matPow(vector<vector<long long> > &A, int b) {     if (b == 1) return A;     vector<vector<long long> > ans = matPow(A, b >> 1);     ans = matMul(ans, ans);     if (b & 1) ans = matMul(ans, A);     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> x;     for (int i = 0; i < n; i++) {       cin >> tmp;       cnt[tmp]++;     }     if (x == 0) {       cout <<  1 n ;       return 0;     }     vector<vector<long long> > res(N, vector<long long>(N, 0));     for (int i = 1; i <= 100; i++) {       res[0][i - 1] = cnt[i];       if (i < 100) res[i][i - 1] = 1;     }     res[0][100] = 1;     res[100][100] = 1;     res = matPow(res, x);     long long ans = (res[0][0] + res[0][100]) % MOD;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 505;   const long long inf = 1e18;   inline int read() {     int x = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return x;   }   int n, m, now;   long long ans, Pow[100];   bitset<N> f[2][64][N], g, tmp;   int main() {     n = read();     m = read();     int u, v, w;     for (int i = 1; i <= m; ++i) {       u = read();       v = read();       w = read();       f[w][0][u][v] = 1;     }     for (int i = 1; i <= 60; ++i)       for (int u = 1; u <= n; ++u)         for (int v = 1; v <= n; ++v) {           if (f[0][i - 1][u][v]) f[0][i][u] |= f[1][i - 1][v];           if (f[1][i - 1][u][v]) f[1][i][u] |= f[0][i - 1][v];         }     if (f[0][60][1].count()) {       puts( -1 );       return 0;     }     now = 0;     tmp[1] = 1;     Pow[0] = 1;     for (int i = 1; i <= 60; ++i) Pow[i] = Pow[i - 1] << 1;     for (int i = 60; i >= 0; --i) {       g.reset();       for (int j = 1; j <= n; ++j)         if (tmp[j]) g |= f[now][i][j];       if (g.count()) {         now ^= 1;         tmp = g;         ans += Pow[i];       }     }     if (ans > inf)       puts( -1 );     else       printf( %lld , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int n, fa[1200000], sf[1200000], len[1200000], f[1200000], g[1200000],       diff[1200000], c[1200000][30], p, np, cnt;   char s[1200000], a[1200000];   void upd(int &x, int y) { x = (x + y) % mod; }   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = 1; i <= n / 2; ++i) {       a[i * 2 - 1] = s[i];       a[i * 2] = s[n - i + 1];     }     fa[0] = 1;     len[0] = 0;     fa[1] = 1;     len[1] = -1;     np = cnt = 1;     f[0] = 1;     for (int i = 1; i <= n; ++i) {       int w = a[i] -  a ;       for (p = np; a[i] != a[i - len[p] - 1]; p = fa[p])         ;       if (!c[p][w]) {         int q = fa[p], now = ++cnt;         len[now] = len[p] + 2;         for (; a[i] != a[i - len[q] - 1]; q = fa[q])           ;         fa[now] = c[q][w];         diff[now] = len[now] - len[fa[now]];         c[p][w] = np = now;         sf[now] = diff[now] == diff[fa[now]] ? sf[fa[now]] : fa[now];       } else         np = c[p][w];       for (int q = np; q; q = sf[q]) {         g[q] = f[i - len[sf[q]] - diff[q]];         if (diff[q] == diff[fa[q]]) upd(g[q], g[fa[q]]);         upd(f[i], g[q]);       }       if (i & 1) f[i] = 0;     }     printf( %d n , f[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5000 + 5;   const int inf = 0x3f3f3f3f;   const int Max = 200000;   template <class T>   inline void getin(T& num) {     char c;     bool flag = 0;     num = 0;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag = 1;     while (c >=  0  && c <=  9 ) {       num = num * 10 + c - 48;       c = getchar();     }     if (flag) num = -num;   }   int n, k, a, x, y, m;   queue<int> num[N];   set<pair<int, int> > output;   vector<pair<int, int> > Ans;   int main() {     getin(n);     int sum = 0, ans = 0;     for (int i = 1; i <= n; i++) {       getin(k), getin(a), sum += k;       getin(x), getin(y), getin(m);       num[i].push(a);       int ret = 0;       for (int j = 2; j <= k; j++) {         int nxt = (1ll * a * x % m + y) % m;         if (nxt < a) ret++;         num[i].push(a = nxt);       }       ans = max(ans, ret);     }     printf( %d n , ans);     if (sum > Max) return 0;     for (int i = 1; i <= n; i++) {       output.insert(make_pair(num[i].front(), i));       num[i].pop();     }     int last = -inf, p;     for (int i = 1; i <= sum; i++) {       set<pair<int, int> >::iterator it =           output.lower_bound(make_pair(last, -inf));       if (it == output.end()) ans++, it = output.begin();       Ans.push_back(*it);       last = (*it).first;       p = (*it).second;       output.erase(it);       if (!num[p].empty()) {         output.insert(make_pair(num[p].front(), p));         num[p].pop();       }     }     for (int i = 0; i < sum; i++) printf( %d %d n , Ans[i].first, Ans[i].second);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 50 + 5;   const int oo = 1000000000;   bool u[N][N];   int n, m;   int ans = -1;   int a[N][N], l[N][N];   int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   char s[N];   struct pis {     int x, y;     pis() {}     pis(int x, int y) : x(x), y(y) {}   } f[10 * N * N];   inline int Solve(int x, int y) {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) l[i][j] = oo;     int front = 0, top = 1;     f[1] = pis(x, y);     l[x][y] = 0;     while (front < top) {       pis p = f[++front];       u[p.x][p.y] = 0;       for (int i = 0; i < 4; i++) {         int dx = p.x + d[i][0];         int dy = p.y + d[i][1];         if (dx >= 1 && dx <= n && dy >= 1 && dy <= m) {           int dis = a[p.x][p.y] != a[dx][dy];           if (l[p.x][p.y] + dis < l[dx][dy]) {             l[dx][dy] = l[p.x][p.y] + dis;             if (!u[dx][dy]) {               u[dx][dy] = 1;               f[++top] = pis(dx, dy);             }           }         }       }     }     int MAX = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && l[i][j] > MAX) MAX = l[i][j];     return MAX;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s , s);       for (int j = 1; j <= m; j++) {         if (s[j - 1] ==  B ) ans = oo;         a[i][j] = s[j - 1] ==  W  ? 0 : 1;       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) ans = min(ans, Solve(i, j));     printf( %d n , ans + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 1e5 + 7;   int n, x;   int main() {     cin >> n >> x;     int ans = 0, num = 0;     if (n == 2 && x == 0) {       puts( NO );       return 0;     }     puts( YES );     if (n == 1)       printf( %d , x);     else if (n == 2)       printf( %d %d , 0, x);     else if (n == 3) {       int t1 = 1 << 18;       int t2 = 1 << 19;       int t3 = x ^ t1 ^ t2;       printf( %d %d %d , t1, t2, t3);     } else {       for (int i = 1;; i++) {         if (i == x) continue;         ans ^= i;         printf( %d  , i);         num++;         if (num == n - 3) break;       }       int t1 = 1 << 18;       int t2 = 1 << 19;       int t3 = x ^ ans ^ t1 ^ t2;       printf( %d %d %d , t1, t2, t3);       ans ^= t1 ^ t2 ^ t3;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 mt_rand(time(0));   const int N = 105;   int t, n;   char a[N], b[N], c[N];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %s%s%s , a, b, c);       n = 0;       while (a[n]) n++;       bool ok = 1;       for (int i = 0; i < n; i++) {         if (c[i] == a[i] || c[i] == b[i])           ;         else           ok = 0;       }       if (!ok)         printf( NO n );       else         printf( YES n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string str;   int memo[100005][3];   int dp(int Idx, int prev) {     if (Idx == (int)str.size()) return 0;     int choice1 = 1000000;     if (memo[Idx][prev] != -1) return memo[Idx][prev];     if (prev == 0) {       if (isupper(str[Idx]))         choice1 = dp(Idx + 1, 0) + 1;       else         choice1 = dp(Idx + 1, 0);     } else {       if (isupper(str[Idx])) {         choice1 = min(dp(Idx + 1, 0) + 1, dp(Idx + 1, 1));       } else         choice1 = min(dp(Idx + 1, 0), dp(Idx + 1, 1) + 1);     }     return memo[Idx][prev] = choice1;   }   int main() {     cin >> str;     memset(memo, -1, sizeof(memo));     int ans = dp(0, 1);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   double p;   bool in_memo[200 + 1][230 + 1][2][1 << 8];   double memo[200 + 1][230 + 1][2][1 << 8];   double E(int k, int lead_count, bool lead_parity, int mask) {     double& ans = memo[k][lead_count][lead_parity][mask];     if (not in_memo[k][lead_count][lead_parity][mask]) {       if (k == 0) {         ans = mask ? __builtin_ctz(mask) : 8 + (lead_parity ? 0 : lead_count);       } else {         ans = p / 100 *                   E(k - 1,                     bool((mask & (1 << 7)) >> 7) == lead_parity ? lead_count + 1                                                                 : 1,                     (mask & (1 << 7)) >> 7, (mask * 2) & 0b11111111) +               (100 - p) / 100 *                   E(k - 1,                     mask == 0b11111111 ? (lead_parity ? lead_count : 1)                                        : lead_count,                     lead_parity ^ (mask == 0b11111111), (mask + 1) & 0b11111111);       }       in_memo[k][lead_count][lead_parity][mask] = true;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int x, k;     cin >> x >> k >> p;     memset(in_memo, false, sizeof in_memo);     cout << fixed << setprecision(7)          << E(k, __builtin_ctz((x >> 8) & 1 ? (x >> 8) + 1 : x >> 8),               (x >> 8) & 1, x & 0b11111111)          <<   n ;     return 0;   }