malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, n1, n2, limiter = 1, fight = 0, temp;     cin >> n >> n1;     queue<int> p1, p2;     for (int i = 1; i <= n1; i++) {       cin >> temp;       p1.push(temp);       limiter *= i;     }     cin >> n2;     for (int i = 1; i <= n2; i++) {       cin >> temp;       p2.push(temp);       limiter *= (n1 + i);     }     limiter *= (n + 1);     while (1) {       if (fight > limiter) {         cout << -1;         break;       } else if (p1.empty()) {         cout << fight <<     << 2;         break;       } else if (p2.empty()) {         cout << fight <<     << 1;         break;       } else {         if (p1.front() > p2.front()) {           p1.push(p2.front());           p1.push(p1.front());           p1.pop();           p2.pop();           fight++;         } else {           p2.push(p1.front());           p2.push(p2.front());           p2.pop();           p1.pop();           fight++;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int max_n = 200002, inf = 1111111111;   const int max_p = 500005;   int n, q, a[max_n], z[max_p], cnt[max_p], used[max_n];   long long ans;   int get_cnt(int x) {     int res = 0;     for (int i = 1; i * i <= x; ++i) {       if (x % i == 0) {         int d = i;         res += cnt[d] * z[d];         d = x / i;         if (i < d) {           res += cnt[d] * z[d];         }       }     }     return res;   }   void add(int x, int f) {     for (int i = 1; i * i <= x; ++i) {       if (x % i == 0) {         int d = i;         cnt[d] += f;         d = x / i;         if (i < d) {           cnt[d] += f;         }       }     }   }   int main() {     for (int i = 2; i < max_p; ++i) {       int x = i;       for (int j = 2; j * j <= x; ++j) {         if (x % j == 0) {           x /= j;           ++z[i];           if (x % j == 0) {             z[i] = 0;             x = 0;             break;           }         }       }       if (x == 0) {         continue;       }       if (x > 1) {         ++z[i];       }       if (z[i] % 2 == 1) {         z[i] = 1;       } else {         z[i] = -1;       }     }     scanf( %d%d , &n, &q);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     int sz = 0;     while (q--) {       int x;       scanf( %d , &x);       --x;       int res;       if (used[x]) {         add(a[x], -1);         used[x] = 0;         --sz;         ans -= (sz - get_cnt(a[x]));       } else {         used[x] = 1;         ans += sz - get_cnt(a[x]);         add(a[x], 1);         ++sz;       }       printf( %I64d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void aent(long long int *a, long long int n) {     for (long long int i = 0; i < n; i++) cin >> a[i];   }   string arrtostr(long long int *a, long long int n) {     ostringstream os;     for (long long int i = 0; i < n; i++) os << a[i];     string str(os.str());     return str;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n;     cin >> n;     vector<long long int> v;     for (long long int i = 0; i < n; i++) {       long long int a;       cin >> a;       v.push_back(a);     }     long long int s = 0, e = n - 1;     string ss =   ;     long long int mi = LLONG_MIN;     while (s <= e) {       if (s == e) {         if (mi < v[s]) ss +=  L ;         break;       }       long long int st = v[s];       long long int ed = v[e];       if (st < mi && ed < mi) break;       if (st == ed) {         string s1 =  L , s2 =  R ;         int rs = s;         if (st < mi) break;         int chk = st;         while (s + 1 < e) {           if (v[s + 1] > chk) {             s1 +=  L ;             chk = v[s + 1];             s++;           } else             break;         }         chk = ed;         while (e - 1 > rs) {           if (v[e - 1] > chk) {             s2 +=  R ;             chk = v[e - 1];             e--;           } else             break;         }         if (s1.size() >= s2.size()) {           ss += s1;         } else           ss += s2;         cout << ss.size() <<   n ;         cout << ss;         return 0;       }       if (ed < st) {         if (ed > mi) {           ss +=  R ;           e--;           mi = ed;         } else {           if (st > mi) {             ss +=  L ;             s++;             mi = st;           } else             break;         }       } else {         if (st > mi) {           ss +=  L ;           s++;           mi = st;         } else {           if (ed > mi) {             ss +=  R ;             e--;             mi = ed;           } else             break;         }       }     }     cout << ss.size() <<   n ;     cout << ss;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, i;       cin >> n;       int a[n];       map<long int, int> mp;       for (i = 0; i < n; i++) {         cin >> a[i];         mp[a[i]]++;       }       cout << mp.size() << endl;     }   }
#include <bits/stdc++.h>   const int N = (int)1e3 + 5;   int n;   int a[N];   int b[N];   int u[N];   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   int main() {     int ts;     scanf( %d , &ts);     while (ts--) {       for (int i = 0; i < N; ++i) {         u[i] = 0;       }       scanf( %d , &n);       for (int i = 0; i < n; ++i) {         scanf( %d , &a[i]);       }       int g = 0;       for (int p = 1; p <= n; ++p) {         int mx = -1;         int ind = -1;         for (int i = 0; i < n; ++i) {           if (!u[i]) {             if (mx < gcd(g, a[i])) {               mx = gcd(g, a[i]);               ind = i;             }           }         }         assert(ind != -1);         u[ind] = 1;         g = gcd(g, a[ind]);         b[p] = a[ind];       }       for (int i = 1; i <= n; ++i) {         printf( %d  , b[i]);       }       printf(  n );     }   }
#include <bits/stdc++.h>   using namespace std;      void ng() { cout << -1 <<   n ; }      void solve() {       int N; cin >> N;       map<int, int> M;       vector<int> A(N);       for (auto &x: A) {           cin >> x; M[x]++;       }       for (auto &[key, val]: M) {           if (val > (N + 1) / 2) return ng();       }       int l = 0;       vector<pair<int, int>> V;       for (int i = 0; i < N-1; i++) {           if (A[i] == A[i+1]) {               V.emplace_back(A[l], A[i]);               l = i+1;           }       }       V.emplace_back(A[l], A[N-1]);       M.clear();       int sum = V.size();       int sum2 = 0;       map<int, int> P;       for (auto &[c, d]: V) {           if (c == d) P[c]++, sum2++;           else M[c]++, M[d]++;       }       sum -= sum2;       for (auto &[key, val]: P) {           if (sum - M[key] + 1 + (sum2 - val) < val) {               int a = val - (sum - M[key] + 1 + (sum2 - val));               cout << V.size() - 1 + a <<   n ;               return;           }       }       cout << V.size() - 1 <<   n ;   }      int main() {       cin.tie(nullptr);       ios::sync_with_stdio(false);       int T; cin >> T;       while (T--) solve();       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[100];   int sol[1000000];   void prueba(int ind) {     sol[0] = ind;     priority_queue<pair<int, int> > q;     for (int i = 1; i <= m; i++) {       if (ind == i) {         if (a[i] > 1) q.push(pair<int, int>(a[i] - 1, i));       } else {         q.push(pair<int, int>(a[i], i));       }     }     for (int i = 1; i < n; i++) {       pair<int, int> p = q.top();       q.pop();       if (i < n - 1) {         if (p.second != sol[i - 1]) {           sol[i] = p.second;           p.first--;           if (p.first) q.push(p);         } else if (int(q.size()) == 0) {           return;         } else {           pair<int, int> pbis = q.top();           q.pop();           sol[i] = pbis.second;           pbis.first--;           if (pbis.first) q.push(pbis);           q.push(p);         }       } else {         if (p.second != sol[i - 1] and p.second != sol[0]) {           sol[i] = p.second;           p.first--;           if (p.first) q.push(p);         } else if (int(q.size()) == 0) {           return;         } else {           pair<int, int> pbis = q.top();           q.pop();           if (pbis.second != sol[i - 1] and pbis.second != sol[0]) {             sol[i] = pbis.second;             pbis.first--;             if (pbis.first) q.push(pbis);             q.push(p);           } else if (int(q.size()) == 0) {             return;           } else {             pair<int, int> pbisbis = q.top();             q.pop();             sol[i] = pbisbis.second;             pbisbis.first--;             if (pbisbis.first) q.push(pbisbis);             q.push(p);             q.push(pbis);           }         }       }     }     for (int i = 0; i < n; i++) {       if (i > 0) cout <<    ;       cout << sol[i];     }     cout << endl;     exit(0);   }   int main() {     cin >> n >> m;     int suma = 0;     for (int i = 1; i <= m; i++) {       cin >> a[i];       suma += a[i];     }     if (suma < n) {       cout << -1 << endl;       exit(0);     }     for (int i = 1; i <= m; i++) prueba(i);     cout << -1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int nmax = 5e4 + 5;   int l[nmax], r[nmax];   int main() {     int n, x;     scanf( %d %d , &n, &x);     for (int i = 0; i < n; ++i) {       scanf( %d %d , l + i, r + i);     }     long long res = 0;     for (int i = 0; i < n;) {       if (!(l[i] <= x && r[i] >= x)) {         int lc = 0, rc = 0;         int lc2 = 0, rc2 = 0;         int j = i;         while ((lc < 1 || rc < 1) && lc2 < 2 && rc2 < 2) {           if (r[j] <= x) ++lc;           if (l[j] >= x) ++rc;           if (r[j] < x) ++lc2;           if (l[j] > x) ++rc2;           ++j;         }         if (lc && rc) {           if (l[i] > x) {             res += l[i] - x;           }           if (r[i] < x) {             res += x - r[i];           }           ++i;         } else if (lc2 == 2) {           int nxt = 0;           for (int k = i; k < j; ++k) {             nxt = max(nxt, l[k]);             if (r[k] < x) {               nxt = max(nxt, r[k]);             }           }           res += x - nxt;           x = nxt;         } else if (rc2 == 2) {           int nxt = mod;           for (int k = i; k < j; ++k) {             nxt = min(nxt, r[k]);             if (l[k] > x) {               nxt = min(nxt, l[k]);             }           }           res += nxt - x;           x = nxt;         }       } else {         ++i;       }     }     printf( %lld n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     register char c = getchar();     bool f = 0;     while (c !=  -  && (c <  0  || c >  9 )) c = getchar();     if (c ==  - ) c = getchar(), f = 1;     while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar();     x = f ? -x : x;     return;   }   template <class T>   inline void write(T x) {     if (!x) {       putchar(48);       return;     }     if (x < 0) x = -x, putchar( - );     int len = -1, z[20];     while (x > 0) z[++len] = x % 10, x /= 10;     for (register int i = len; i >= 0; i--) putchar(z[i] + 48);     return;   }   const int N = 10010;   struct node {     int to, nxt, w;   } g[N * 10];   int lst[N], gl;   void add(int x, int y, int z) {     g[++gl] = (node){y, lst[x], z};     lst[x] = gl;   }   vector<pair<int, int> > E[N];   vector<int> E2[N];   bool vis1[N], visn[N];   void dfs(int x) {     vis1[x] = 1;     for (int i = 0; i < E[x].size(); i++) {       int v = E[x][i].first;       if (!vis1[v]) dfs(v);     }   }   void dfs2(int x) {     visn[x] = 1;     for (int i = 0; i < E2[x].size(); i++) {       int v = E2[x][i];       if (!visn[v]) dfs2(v);     }   }   int cnt[N], dis[N], n, m;   queue<int> q;   bool vis[N];   bool spfa() {     q.push(1);     memset(dis, 127 / 3, sizeof(dis));     dis[1] = 0;     while (!q.empty()) {       int u = q.front();       q.pop();       vis[u] = 0;       for (int i = lst[u]; i; i = g[i].nxt) {         int v = g[i].to;         if (dis[v] > dis[u] + g[i].w) {           dis[v] = dis[u] + g[i].w;           if ((++cnt[v]) == n) return 0;           if (!vis[v]) {             vis[v] = 1;             q.push(v);           }         }       }     }     return 1;   }   int ans[N];   int main() {     read(n), read(m);     for (int i = 1, x, y; i <= m; i++) {       read(x), read(y);       E[x].push_back(make_pair(y, i));       E2[y].push_back(x);     }     dfs(1);     dfs2(n);     for (int u = 1; u <= n; u++)       for (auto v : E[u])         if (vis1[u] && visn[v.first]) {           add(v.first, u, 2);           add(u, v.first, -1);         }     if (!spfa())       puts( No );     else {       puts( Yes );       int flg = 0;       for (int u = 1; u <= n; u++)         for (auto v : E[u])           if (vis1[u] && visn[v.first])             ans[v.second] = dis[u] - dis[v.first];           else             ans[v.second] = 1;       for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   bool prime[N];   int phi[N], deg[N];   void sieve();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     sieve();     int n, k;     cin >> n >> k;     priority_queue<pair<int, int>, vector<pair<int, int>>,                    greater<pair<int, int>>>         q;     for (int i = 3; i <= n; ++i) {       if (deg[i] == 0) {         q.push({phi[i], i});       }       for (int j = 2 * i; j <= n; j += i) {         ++deg[j];       }     }     long long ans = (k == 1 ? 1 : 2);     while (k--) {       auto cur = q.top();       q.pop();       int i = cur.second;       ans += phi[i];       for (int j = 2 * i; j <= n; j += i) {         --deg[j];         if (deg[j] == 0) {           q.push({phi[j], j});         }       }     }     cout << ans <<   n ;   }   void sieve() {     for (int i = 2; i < N; ++i) {       prime[i] = true;       phi[i] = i;     }     for (int i = 2; i < N; ++i) {       if (prime[i]) {         for (int j = i; j < N; j += i) {           prime[j] = false;           phi[j] /= i;           phi[j] *= (i - 1);         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 100010;   int n, m, k, q;   vector<int> G[maxN];   int pre[maxN], s[maxN], e[maxN], cnt[maxN], t;   bool nroot[maxN];   void dfs(int cur) {     s[cur] = ++t;     cnt[cur] = 1;     for (int i = 0; i < (int)G[cur].size(); i++) {       int nxt = G[cur][i];       dfs(nxt);       cnt[cur] += cnt[nxt];     }     e[cur] = t;   }   int main(int argc, char** argv) {     cin >> n >> m >> k >> q;     int a, b;     for (int i = 0; i < k; i++) {       cin >> a >> b;       if (pre[b]) {         G[pre[b]].push_back(a);         nroot[a] = true;       }       pre[b] = a;     }     for (int i = 1; i <= n; i++) {       if (!nroot[i]) dfs(i);     }     for (int i = 0; i < q; i++) {       cin >> a >> b;       cout << ((s[a] < s[pre[b]] && e[a] >= e[pre[b]]) ? cnt[a] : 0) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<string> tt(n);     for (int i = 0; i < n; ++i) {       cin >> tt[i];     }     int const INF = 1000000000;     vector<int> dp(k + 1, INF), ndp(k + 1, INF);     dp[0] = 0;     for (int day = 0; day < n; ++day) {       vector<int> prefix(m + 1);       for (int i = 0; i < m; ++i) {         prefix[i + 1] = prefix[i] + (tt[day][i] ==  1 );       }       vector<int> update(m + 1, INF);       for (int arr = 0; arr <= m; ++arr) {         for (int dep = arr; dep <= m; ++dep) {           int plus_time = dep - arr;           int plus_sk = prefix[m] - (prefix[dep] - prefix[arr]);           update[plus_sk] = min(update[plus_sk], plus_time);         }       }       for (int sk = 0; sk <= k; ++sk) {         for (int plus_sk = 0; plus_sk <= m && sk + plus_sk <= k; ++plus_sk) {           ndp[sk + plus_sk] = min(ndp[sk + plus_sk], dp[sk] + update[plus_sk]);         }       }       dp.assign(k + 1, INF);       dp.swap(ndp);     }     int result = INF;     for (int sk = 0; sk <= k; ++sk) {       result = min(result, dp[sk]);     }     cout << result << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, id = 0;   int Hash[333], L[333];   map<string, int> M;   vector<int> v;   int kmp(int Len) {     int len = v.size();     int lps[len], I = 0, num = 0;     lps[0] = 0;     for (int i = 1; i < len; i++) {       if (v[I] != v[i]) I = -1;       lps[i] = ++I;     }     int i = 0, j = 0;     while (i < n) {       if (Hash[i] == v[j]) {         i++;         j++;       } else {         if (j == 0)           i++;         else {           if (j == lps[j])             j = 0;           else             j = lps[j - 1];         }       }       if (j == len) {         num++;         j = 0;       }     }     if (num == 1) num = 0;     return (Len - 1) * num;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     int Len = 0, res = 0, len;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s;       if (!M[s]) {         M[s] = ++id;       }       Hash[i] = M[s];       L[i] = s.length();       Len += L[i];     }     Len += (n - 1);     for (int i = 0; i < n; i++) {       v.clear();       len = 0;       for (int j = i; j < n; j++) {         v.push_back(Hash[j]);         len += L[j];         res = max(res, kmp(len));       }     }     cout << Len - res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int E = 1e18 + 7;   const long long int MOD = 1000000007;   int main() {     long long int n;     cin >> n;     while (n--) {       string s;       cin >> s;       bool P = true;       bool same = true;       for (int i = 0; i < s.size(); i++) {         if (s[i] != s[0]) {           same = false;         }         if (s[i] != s[s.size() - 1 - i]) {           P = false;         }       }       if (!P) {         cout << s <<   n ;       } else if (same) {         cout << -1 <<   n ;       } else {         for (int i = 1; i < s.size(); i++) {           if (s[i] != s[i - 1]) {             swap(s[i], s[i - 1]);           }         }         cout << s <<   n ;       }     }     return 0;   }
#include<bits/stdc++.h>   using namespace std;   #define fo(a,b,c) for(int a=b;a<=c;++a)   #define fr(a,b,c) for(int a=b;a>=c;--a)   #define ll long long   #define pq priority_queue   int t,n,a[30010];   int cnt[3];   bool check()   {    if(cnt[1]==cnt[2]&&cnt[1]==cnt[0])     return false;    return true;    }   int main()   {    scanf( %d ,&t);     while(t--)    {     memset(cnt,0,sizeof(cnt));     int ans=0;     scanf( %d ,&n);     fo(i,1,n)      scanf( %d ,&a[i]);     fo(i,1,n)     {      if(a[i]%3==0)cnt[0]++;      else if(a[i]%3==1)cnt[1]++;      else cnt[2]++;      }     int sum=n/3,i=0;     while(check())     {      int pre=(i+2)%3;      if(cnt[i]<sum)      {       cnt[pre]-=abs(sum-cnt[i]);       ans+=abs(sum-cnt[i]);       cnt[i]=sum;      }      i=(i+1)%3;     }      printf( %d n ,ans);    }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int xa, ya, xb, yb, n, count = 0;     cin >> xa >> ya >> xb >> yb;     if (xa > xb) {       int t = xa;       xa = xb;       xb = t;     }     if (ya > yb) {       int t = ya;       ya = yb;       yb = t;     }     cin >> n;     int x[n], y[n], r[n];     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i] >> r[i];     }     for (int i = xa; i <= xb; i++) {       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - ya) * (y[h] - ya));         if (diff <= r[h]) {           count++;           break;         }       }       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - yb) * (y[h] - yb));         if (diff <= r[h]) {           count++;           break;         }       }     }     for (int j = (ya + 1); j < yb; j++) {       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - xa) * (x[h] - xa) + (y[h] - j) * (y[h] - j));         if (diff <= r[h]) {           count++;           break;         }       }       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - xb) * (x[h] - xb) + (y[h] - j) * (y[h] - j));         if (diff <= r[h]) {           count++;           break;         }       }     }     int ans = (2 * (xb - xa + yb - ya)) - count;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, a;     cin >> n >> a;     int arr[n + 1];     int cnt = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     for (int i = 1; i <= n; i++) {       if (arr[i]) {         int temp = i - a;         int d = a - temp;         if (d > n || d < 1 || arr[d] == arr[i]) cnt++;       }     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct point {     long long x, y;   };   long long cross(point a, point b, point c) {     return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);   }   int main() {     point a, b, c;     cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;     long long ans = cross(a, b, c);     if (ans > 0)       cout <<  LEFT n ;     else if (ans < 0)       cout <<  RIGHT n ;     else       cout <<  TOWARDS n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   long long po(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   void dfs(vector<vector<long long> > &adj, long long s, long long e,            vector<long long> &v, vector<bool> &vis) {     if (s == e) return;     vis[s] = true;     for (auto vx : adj[s]) {       if (!vis[vx]) {         dfs(adj, vx, e, v, vis);       }     }     v.push_back(s);   }   void solve() {     long long n, m, a, b;     cin >> n >> m >> a >> b;     vector<vector<long long> > adj(n + 1);     while (m--) {       long long x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     vector<long long> v1;     vector<long long> v2;     vector<bool> vis(n + 1, false);     dfs(adj, a, b, v1, vis);     for (int i = 0; i < n + 1; i++) vis[i] = false;     dfs(adj, b, a, v2, vis);     sort((v1).begin(), (v1).end());     sort((v2).begin(), (v2).end());     long long n1 = 0, n2 = 0;     for (long long i = 0; i < v1.size(); i++) {       if (!binary_search(v2.begin(), v2.end(), v1[i]) and v1[i] != a) {         n1++;       }     }     for (long long i = 0; i < v2.size(); i++) {       if (!binary_search(v1.begin(), v1.end(), v2[i]) and v2[i] != b) n2++;     }     cout << n1 * n2 <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }