malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a < b) {       swap(a, b);     }     if (a % b == 0) {       return b;     }     return gcd(b, a % b);   }   vector<vector<int>> gr;   vector<int> used;   void dfs(int v) {     used[v] = 1;     for (auto i : gr[v]) {       if (!used[i]) {         dfs(i);       }     }   }   void solution() {     long int n;     cin >> n;     vector<long long> mass(n);     long long minn = 1e10;     long long minn_ind = 0;     for (int i = 0; i < n; i++) {       cin >> mass[i];       if (mass[i] < minn) {         minn = mass[i];         minn_ind = i;       }     }     gr = vector<vector<int>>(n);     used = vector<int>(n, 0);     for (int i = 0; i < n; i++) {       if (gcd(mass[i], minn) == minn) {         gr[i].push_back(minn_ind);         gr[minn_ind].push_back(i);       }     }     dfs(minn_ind);     vector<long long> sorted = mass;     sort(sorted.begin(), sorted.end());     vector<long long> get_ans;     for (int i = 0; i < n; i++) {       if (mass[i] != sorted[i] || i == minn_ind) {         get_ans.push_back(i);       }     }     bool ans = 1;     for (auto i : get_ans) {       if (!used[i]) {         ans = 0;         break;       }     }     if (ans) {       cout <<  YES ;     } else {       cout <<  NO ;     }     cout << endl;   }   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       solution();     }   }
#include <bits/stdc++.h>   using namespace std;   int grundy[] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6,  2, 1, 8, 7,  5,                   9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};   int main() {     int n;     scanf( %d , &n);     int ones = n;     bool vis[100100] = {false};     vector<int> v;     for (long long i = 2; i * i <= n; i++) {       if (vis[i]) continue;       int size = 0;       for (long long j = i; j <= n; j = j * i) {         if (j * j <= n) vis[j] = true;         size++;         ones--;       }       v.push_back(grundy[size]);     }     ones %= 2;     for (int i = 0; i < (int)v.size(); i++) ones = ones ^ v[i];     if (ones)       cout <<  Vasya  << endl;     else       cout <<  Petya  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long xyp(long long x, long long y) {     if (y == 0) return 1LL;     if (y == 1) return x;     if (y % 2) {       long long p = xyp(x, y - 1);       return (x * p) % 1000000007;     }     long long p = xyp(x, y / 2);     return (p * p) % 1000000007;   }   long long inv(long long x) { return xyp(x, 1000000007 - 2); }   long long fact[10000007];   long long pr[10000007];   vector<long long> rp;   void init() {     fact[0] = 1;     for (long long i = 1; i < 10000007; i++) {       fact[i] = (fact[i - 1] * i) % 1000000007;     }   }   long long ncr(long long n, long long r) {     if (r < 0) return 0LL;     if (r == 0 || n == r) return 1LL;     if (r == 1 || ((n - r) == 1)) return n;     long long p =         (((fact[n] * inv(fact[r])) % 1000000007) * inv(fact[n - r])) % 1000000007;     return p;   }   void sieve() {     memset(pr, 1, sizeof(pr));     pr[0] = pr[1] = 0;     for (long long i = 2; i < 10000007; i++) {       if (pr[i]) {         rp.push_back(i);         for (long long j = i * i; j < 10000007; j += i) {           pr[j] = 0;         }       }     }   }   void solve() {     long long n, k;     cin >> n >> k;     long long a[n + 1], b[n + 1];     long long flag = 1;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       b[i] = a[i];       if (a[i] == k) flag = 0;     }     if (flag) {       cout <<  no n ;       return;     }     sort(b + 1, b + n + 1);     if (b[(n + 1) / 2] == k) {       cout <<  yes n ;       return;     }     if (n == 1) {       if (a[1] == k)         cout <<  yes n ;       else         cout <<  no n ;       return;     }     if (n == 2) {       if (b[1] == k)         cout <<  yes n ;       else         cout <<  no n ;       return;     }     vector<long long> v;     for (long long i = 1; i <= n; i++) {       if (a[i] >= k) {         v.push_back(i);       }     }     flag = 0;     for (long long i = 0; i < v.size() - 1; i++) {       if ((v[i + 1] - v[i]) <= 2) {         flag = 1;       }     }     if (flag)       cout <<  yes n ;     else       cout <<  no n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int testcases;     cin >> testcases;     while (testcases--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1.0);   const long double EPS = 1e-14;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   const long long p = 97;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(9);     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     vector<vector<long long>> pr(n, vector<long long>(6, INF));     pr[0] = vector<long long>(6, -1);     for (long long i = 1; i < n; i++) {       for (long long j = 1; j <= 5; j++) {         if (pr[i - 1][j] == INF) continue;         for (long long z = 1; z <= 5; z++) {           if (a[i] > a[i - 1]) {             if (z > j) {               pr[i][z] = j;             }           } else if (a[i] < a[i - 1]) {             if (z < j) {               pr[i][z] = j;             }           } else {             if (z != j) {               pr[i][z] = j;             }           }         }       }     }     long long last = -1;     for (long long i = 1; i <= 5; i++) {       if (pr[n - 1][i] != INF) last = i;     }     if (last == -1) {       cout << -1;       return 0;     }     long long num = n - 1;     vector<long long> ans;     while (num != -1) {       ans.push_back(last);       last = pr[num][last];       num--;     }     reverse(ans.begin(), ans.end());     for (long long i = 0; i < ans.size(); i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include<bits/stdc++.h>   using namespace std;   int main()   {          int n,i,j,cnt,ara2[20000],m;       string ss;       cin>>m;       for(i=0;i<m;i++)       {       cin>>n;       cin>>ss;          if((ss[0]== 2 )&&(ss[n-1]== 0 )&&(ss[n-2]== 2 )&&(ss[n-3]== 0 ))       {           cout<< Yes <<endl;       }       else if((ss[0]== 2 )&&(ss[n-1]== 0 )&&(ss[n-2]== 2 )&&(ss[1]== 0 ))       {           cout<< Yes <<endl;       }       else if((ss[0]== 2 )&&(ss[1]== 0 )&&(ss[2]== 2 )&&(ss[n-1]== 0 ))       {           cout<< Yes <<endl;       }       else if((ss[0]== 2 )&&(ss[1]== 0 )&&(ss[2]== 2 )&&(ss[3]== 0 ))       {           cout<< Yes <<endl;       }       else if((ss[n-1]== 0 )&&(ss[n-2]== 2 )&&(ss[n-3]== 0 )&&(ss[n-4]== 2 ))       {           cout<< Yes <<endl;       }       else       {           cout<< No <<endl;       }   }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int nCases;     cin >> nCases;     for (int i = 0; i < nCases; i++) {       long long nCandy = 0;       cin >> nCandy;       if (nCandy == 1 || nCandy == 2) {         cout << 0 << endl;       } else if (nCandy % 2 == 0) {         cout << (nCandy / 2) - 1 << endl;       } else {         cout << nCandy / 2 << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 300005, B = 35;   int n;   char t[N];   struct AC {     struct Trie {       int ch[26], fail, c, c2;     } A[N];     int siz[B], root[N], top, cnt;     string str[N];     void build(int RT) {       queue<int> q;       q.push(RT);       while (!q.empty()) {         int x = q.front(), F, k;         q.pop();         for (int i = 0; i < 26; i++)           if (A[x].ch[i]) {             F = A[x].fail;             k = A[x].ch[i];             while (F && !A[F].ch[i]) F = A[F].fail;             if (F)               A[k].fail = A[F].ch[i];             else               A[k].fail = RT;             q.push(k);             A[k].c2 = A[A[k].fail].c2 + A[k].c;           }       }     }     int merge(int x, int y) {       if (!x || !y) return x + y;       A[x].c = A[x].c | A[y].c;       for (int i = 0; i < 26; i++) A[x].ch[i] = merge(A[x].ch[i], A[y].ch[i]);       return x;     }     void Add(string s) {       root[++top] = ++cnt;       int x = root[top], len = s.length(), k;       for (int i = 0; i < len; i++) {         k = s[i] -  a ;         if (!A[x].ch[k]) A[x].ch[k] = ++cnt;         x = A[x].ch[k];       }       A[x].c++;       siz[top] = 1;       build(root[top]);       while (top && siz[top] == siz[top - 1]) {         merge(root[top - 1], root[top]);         build(root[top - 1]);         siz[top - 1] *= 2;         top--;       }     }     int Ask(string s) {       int len = s.length(), sum = 0;       for (int i = 1; i <= top; i++) {         int x = root[i], k;         for (int j = 0; j < len; j++) {           k = s[j] -  a ;           while (x && !A[x].ch[k]) x = A[x].fail;           if (x)             x = A[x].ch[k];           else             x = root[i];           sum += A[x].c2;         }       }       return sum;     }   } IN, OUT;   int main() {     scanf( %d , &n);     int tp;     while (n--) {       scanf( %d %s , &tp, t);       if (tp == 1)         IN.Add(t);       else if (tp == 2)         OUT.Add(t);       else         cout << IN.Ask(t) - OUT.Ask(t) <<   n  << flush;     }   }
#include <bits/stdc++.h>   using namespace std;   typedef long long ll;      inline int read(){       int x=0,f=1;       char ch=getchar();       while(ch< 0 ||ch> 9 ){           if(ch== - )               f=-1;           ch=getchar();       }       while(ch>= 0 &&ch<= 9 ){           x=(x<<1)+(x<<3)+(ch^48);           ch=getchar();       }       return x*f;   }   const int maxn = 3e4 + 9;    int t;   int a[3];   int solve(){    int l = a[0], m = a[1], r = a[2];    int avg = (l + m + r) / 3;    //printf( avg: %d n , avg);    int ans = 0;    if (l < avg){     if (r > avg){      if (m > avg){       ans += (m - avg) * 2 + r - avg;       return ans;      }      else if (m < avg){       ans += avg - l + (avg - m) * 2;       return ans;      }      else{       ans += avg - l;       return ans;      }     }     else if (r < avg){      ans += (avg - l) * 2 + avg - r;      return ans;     }     else{      ans += (avg - l) * 2;      return ans;     }    }    else if (l == avg){     if (m == avg){      return ans;     }     else if (m < avg){      ans += (avg - m) * 2;      return ans;     }     else{      ans += m - avg;      return ans;     }    }    else{     if (m < avg){      if (r > avg){       ans += (r - avg) * 2 + l - avg;       return ans;      }      else if (r < avg){       ans += (avg - r) * 2 + avg - m;       return ans;      }      else{       ans += avg - m;       return ans;      }     }     else if (m > avg){      ans += (l - avg) * 2 + m - avg;      return ans;     }     else{      ans = (l - avg) * 2;      return ans;     }    }    return ans;   }   int main(){       t = read();       while(t--){        int n = read();        int m;        a[0] = a[1] = a[2] = 0;        for (int i = 1; i <= n; i++){         m = read();         a[m % 3]++;     }     //sort(a, a + 3);     int ans = solve();     printf( %d n , ans);    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353, N = 20, M = 1 << 5;   int n, m;   int f[2][N][N][N][N][2][2], (*nxt)[N][N][N][2][2] = f[1],                               (*cur)[N][N][N][2][2] = *f;   int snx[M], sny[M], spx[M], spy[M], A;   void add(int &x, int y) {     x += y - mod;     x += x >> 31 & mod;   }   int main() {     scanf( %d%d , &n, &m);     A = (1 << n) - 1;     int x, y;     for (int i = 0; i < n; i++) {       scanf( %d%d , &x, &y);       if (x < 0)         snx[1 << i] -= x;       else         spx[1 << i] += x;       if (y < 0)         sny[1 << i] -= y;       else         spy[1 << i] += y;     }     for (int i = 1; i <= A; i++)       spx[i] = spx[i - (i & -i)] + spx[i & -i],       spy[i] = spy[i - (i & -i)] + spy[i & -i],       snx[i] = snx[i - (i & -i)] + snx[i & -i],       sny[i] = sny[i - (i & -i)] + sny[i & -i];     ******nxt = 1;     for (; m; m >>= 1) {       int c = m & 1, tmp;       swap(nxt, cur);       memset(nxt, 0, sizeof *f);       for (int ppx = 0; ppx <= spx[A]; ppx++)         for (int ppy = 0; ppy <= spy[A]; ppy++)           for (int pnx = 0; pnx <= snx[A]; pnx++)             for (int pny = 0; pny <= sny[A]; pny++)               for (int ox = 0; ox < 2; ox++)                 for (int oy = 0; oy < 2; oy++)                   if (tmp = cur[ppx][pnx][ppy][pny][ox][oy])                     for (int d = 0; d <= A; d++) {                       int npx = ppx + spx[d], npy = ppy + spy[d],                           nnx = pnx + snx[d], nny = pny + sny[d];                       if (((npx ^ nnx) | (npy ^ nny)) & 1) continue;                       add(nxt[npx >> 1][nnx >> 1][npy >> 1][nny >> 1]                              [(npx ^ c) & 1 ? !c : ox][(npy ^ c) & 1 ? !c : oy],                           tmp);                     }     }     printf( %d n , ((******nxt) + mod - 1) % mod);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   char s[N];   int main() {     int t;     scanf( %d , &t);     while (t--) {       map<long long, long long> mp;       long long n;       scanf( %lld , &n);       scanf( %s , s);       long long sum = 0, ans = 0;       long long x;       for (int i = 1; i <= n; i++) {         x = s[i - 1] -  0  - 1;         sum += x;         if (sum == 0) ans++;         ans += mp[sum];         mp[sum]++;       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int par[200005], vis[200005], tree[200005];   set<pair<int, int> > par_change;   void dfs(int u, vector<int> next[], int dfs_par) {     vis[u] = 1;     if (par[u] == u) {       par_change.insert({1, u});     }     for (int i = 0; i < next[u].size(); ++i) {       int v = next[u][i];       if (!vis[v]) {         dfs(v, next, u);       } else if (v != dfs_par) {         if (par[u] == v) {           par_change.insert({2, u});         } else {           par_change.insert({2, v});         }       }     }     vis[u] = 2;   }   int main() {     int n, cnt = 0;     scanf( %d , &n);     vector<int> next[n + 5];     for (int i = 1; i <= n; ++i) {       scanf( %d , &par[i]);       tree[i] = par[i];       if (i != par[i]) {         next[i].push_back(par[i]);         next[par[i]].push_back(i);       }     }     for (int i = 1; i <= n; ++i) {       if (!vis[i]) {         dfs(i, next, 0);       }     }     int flag = 0;     tree[par_change.begin()->second] = par_change.begin()->second;     auto it1 = par_change.begin(), it2 = ++it1;     it1 = par_change.begin();     for (; it2 != par_change.end(); ++it2, ++it1) {       tree[it2->second] = it1->second;     }     for (int i = 1; i <= n; ++i) {       if (tree[i] != par[i]) {         ++cnt;       }     }     printf( %d n , cnt);     for (int i = 1; i <= n; ++i) {       printf( %d  , tree[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e6 + 10;   int cnt[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int t;       scanf( %d , &t);       ++cnt[t];     }     int ans = 0;     for (int i = 0; i < maxn - 1; i++) {       cnt[i + 1] += (cnt[i] / 2);       cnt[i] %= 2;       ans += cnt[i];     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void swap(int ar[], int i, int j) {     int aux = ar[j];     ar[j] = ar[i];     ar[i] = ar[j];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     int Ma[n][m];     int Mb[n][m];     for (int i = 0; i < n; i++)       for (int k = 0; k < m; k++) cin >> Ma[i][k];     for (int i = 0; i < n; i++)       for (int k = 0; k < m; k++) {         int aux;         cin >> aux;         if (aux <= Ma[i][k]) {           int aux2 = Ma[i][k];           Ma[i][k] = aux;           Mb[i][k] = aux2;         } else           Mb[i][k] = aux;       }     for (int i = 0; i < n; i++)       for (int k = 1; k < m; k++)         if (Ma[i][k] <= Ma[i][k - 1] || Mb[i][k] <= Mb[i][k - 1]) {           cout <<  Impossible ;           return 0;         }     for (int k = 0; k < m; k++)       for (int i = 1; i < n; i++)         if (Ma[i][k] <= Ma[i - 1][k] || Mb[i][k] <= Mb[i - 1][k]) {           cout <<  Impossible ;           return 0;         }     cout <<  Possible ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   vector<pair<int, int> > opt[18 * MAX];   map<pair<int, int>, vector<pair<int, int> > > mp;   int par[MAX];   int col[MAX];   int sz[MAX];   int an[MAX];   vector<int> roll_back_num;   vector<pair<pair<int, pair<int, int> >, pair<int, pair<int, int> > > >       roll_back;   void insert(int t, int l, int r, int a, int b, int c, int d) {     if (l > b || r < a) {       return;     }     if (l >= a && r <= b) {       opt[t].push_back(make_pair(c, d));       return;     }     int mid = (l + r) / 2;     insert(t * 2, l, mid, a, b, c, d);     insert(t * 2 + 1, mid + 1, r, a, b, c, d);   }   int not_cor;   int find(int x) {     while (x != par[x]) {       x = par[x];     }     return x;   }   int color(int x) {     int ans = 0;     while (x != par[x]) {       ans ^= col[x];       x = par[x];     }     return (ans ^ col[x]);   }   void unite(int a, int b) {     int ra = find(a), rb = find(b);     int ca = color(a), cb = color(b);     if (ra == rb) {       if (ca == cb) {         not_cor++;         roll_back_num.push_back(-1);       }     } else {       if (sz[ra] > sz[rb]) {         swap(a, b);         swap(ra, rb);         swap(ca, cb);       }       roll_back.push_back({{ra, {ra, sz[ra]}}, {rb, {col[ra], sz[rb]}}});       par[ra] = rb;       sz[rb] += sz[ra];       if (ca == cb) {         col[ra] ^= 1;       }     }   }   void solve(int t, int l, int r) {     int curn = roll_back_num.size();     int cur = roll_back.size();     for (auto it : opt[t]) {       unite(it.first, it.second);     }     if (l == r) {       if (not_cor) {         an[l] = 0;       } else {         an[l] = 1;       }     } else {       int mid = (l + r) / 2;       solve(2 * t, l, mid);       solve(2 * t + 1, mid + 1, r);     }     while (roll_back_num.size() > curn) {       not_cor += roll_back_num.back();       roll_back_num.pop_back();     }     while (roll_back.size() > cur) {       pair<pair<int, pair<int, int> >, pair<int, pair<int, int> > > tmp =           roll_back.back();       par[tmp.first.first] = tmp.first.second.first;       sz[tmp.first.first] = tmp.first.second.second;       col[tmp.first.first] = tmp.second.second.first;       sz[tmp.second.first] = tmp.second.second.second;       roll_back.pop_back();     }   }   int main() {     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= q; i++) {       int a, b;       scanf( %d %d , &a, &b);       if (a > b) {         swap(a, b);       }       vector<pair<int, int> >& tmp = mp[make_pair(a, b)];       if (tmp.empty() || (((tmp.back()).second) != -1)) {         tmp.push_back(make_pair(i, -1));       } else {         (tmp.back()).second = (i - 1);       }     }     for (auto it : mp) {       for (auto it1 : it.second) {         if (it1.second == -1) {           it1.second = q;         }         insert(1, 1, q, it1.first, it1.second, it.first.first, it.first.second);       }     }     for (int i = 1; i <= n; i++) {       sz[i] = 1;       par[i] = i;     }     solve(1, 1, q);     for (int i = 1; i <= q; i++) {       if (an[i] == 0) {         printf( NO n );       } else {         printf( YES n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxn = 100006;   const int mxm = 100006 * 2;   int n, m;   int fe[mxn] = {};   int ne[mxm];   int ev[mxm];   bool v[mxm];   int el = 1;   void add1(int L, int R) {     ++el;     ne[el] = fe[L];     fe[L] = el;     ev[el] = R;   }   bool vis[mxn];   int dfs(int x) {     if (vis[x]) return 0;     vis[x] = true;     int last = 0;     for (int i = fe[x]; i; i = ne[i])       if (!v[i]) {         v[i] = v[i ^ 1] = true;         int t = dfs(ev[i]);         if (t) {           printf( %d %d %d n , x, ev[i], t);         } else {           if (last) {             printf( %d %d %d n , last, x, ev[i]);             last = 0;           } else {             last = ev[i];           }         }       }     return last;   }   void EXEC() {     cin >> n >> m;     if (m % 2 == 1) {       cout <<  No solution  << endl;       return;     }     for (int i = 0; i < m; ++i) {       int L, R;       scanf( %d%d , &L, &R);       add1(L, R);       add1(R, L);     }     dfs(1);   }   int main() {     EXEC();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct people {     long long a, b, id;   } p[300010];   long long n, s[300010], t;   bool v[300010], ans[300010];   bool cmp(people a, people b) { return a.a == b.a ? a.b > b.b : a.a > b.a; }   long double check(people a, people b) {     return (long double)a.a * b.a * (b.b - a.b) / (b.a - a.a) / b.b / a.b;   }   int main() {     scanf( %lld , &n);     for (int i = 1; i <= n; i++) scanf( %lld%lld , &p[i].a, &p[i].b), p[i].id = i;     sort(p + 1, p + n + 1, cmp);     s[1] = 1, t = 1;     long long maxb = p[1].b;     for (int i = 2; i <= n; i++)       if (p[i].b <= maxb)         v[i] = 1;       else         maxb = p[i].b;     for (int i = 2; i <= n; i++) {       if (v[i] || check(p[i], p[s[t]]) > 0) continue;       while (t > 1 && check(p[i], p[s[t]]) < check(p[s[t - 1]], p[s[t]])) t--;       s[++t] = i;     }     for (int i = 1; i <= t; i++) {       ans[p[s[i]].id] = 1;       for (int j = s[i] + 1; j <= n && p[s[i]].a == p[j].a && p[s[i]].b == p[j].b;            j++)         ans[p[j].id] = 1;     }     for (int i = 1; i <= n; i++)       if (ans[i]) printf( %d  , i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     cout << n / 2 * 2 + (n + 1) / 2 << endl;     for (int i = 2; i <= n; i += 2) cout << i <<    ;     for (int i = 1; i <= n; i += 2) cout << i <<    ;     for (int i = 2; i <= n; i += 2) cout << i <<    ;   }