malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     scanf( %d , &n);     vector<int> cnt(20, 0);     for (int i = 0; i < n; i++) {       scanf( %d , &x);       for (int b = 0; b < 20; b++) {         if (1 & (x >> b)) cnt[b]++;       }     }     long long ans = 0;     for (int i = 0; i < n; i++) {       long long val = 0;       for (int b = 0; b < 20; b++) {         if (cnt[b] > 0) {           cnt[b]--;           val |= 1 << b;         }       }       ans += val * val;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 3e5 + 10;   long long N, L, R, X, arr[maxn], ans;   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> N >> L >> R >> X;     for (register long long i = 1; i <= N; ++i) {       cin >> arr[i];     }     for (register long long i = 0; i < (1 << N); ++i) {       long long cnt = __builtin_popcount(i);       if (i < 2) continue;       vector<long long> v;       for (register long long j = 0; j < N; ++j) {         if (i & 1 << j) {           v.push_back(arr[j + 1]);         }       }       sort(v.begin(), v.end());       if (v.back() - v.front() < X) {         continue;       }       long long sum = accumulate(v.begin(), v.end(), 0);       if (sum < L || sum > R) {         continue;       }       ++ans;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     int n, r1 = 0, r2 = 0;     cin >> n;     vector<int> r(n);     for (int &i : r) cin >> i;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       if (a != r[i]) {         if (a)           r1++;         else           r2++;       }     }     if (!r2)       cout <<  -1 n ;     else if (r2 > r1)       cout <<  1 n ;     else       cout << (int)ceil((r1 + 1.0) / r2) <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   char s[45][20] = {        Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe ,        Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler ,        Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan ,        Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield ,        Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley ,        Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge ,        Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy ,        Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan };   int n;   void solve() {     cin >> n;     cout << s[n - 1];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   bool setmax(T &_a, T _b) {     if (_b > _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   bool setmin(T &_a, T _b) {     if (_b < _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   T gcd(T _a, T _b) {     return _b == 0 ? _a : gcd(_b, _a % _b);   }   int a[110][4], n, m;   int main() {     cin >> n >> m;     int cur = 1;     memset(a, 0, sizeof(a));     for (int i = int(1); i <= int(n); ++i) {       if (cur > m) break;       a[i][0] = cur++;       if (cur > m) break;       a[i][3] = cur++;     }     for (int i = int(1); i <= int(n); ++i) {       if (cur > m) break;       a[i][1] = cur++;       if (cur > m) break;       a[i][2] = cur++;     }     vector<int> ans;     for (int i = int(1); i <= int(n); ++i) {       ans.push_back(a[i][1]);       ans.push_back(a[i][0]);       ans.push_back(a[i][2]);       ans.push_back(a[i][3]);     }     for (int x : ans)       if (x > 0) cout << x <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int OO = 2e7;   const double EPS = (1e-7);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   bool sort_p(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     int n;     cin >> n;     vector<int> a(n);     vector<int> b(n, 0);     vector<bool> s(n + 1);     vector<int> t(n + 1);     bool k = false, q = false;     for (int i = 0; i < (int)(n); ++i) {       cin >> a[i];       if (a[i] == 0) k = true;       if (a[i] != 0 && k) q = true;       s[n - a[i]] = true;       t[a[i]]++;     }     if (k) {       for (int i = 0; i < (int)(n); ++i)         if (a[i] != 0) {           cout <<  Impossible ;           return 0;         }     }     if (k) {       cout <<  Possible n ;       for (int i = 0; i < (int)(n); ++i) cout << 1 <<    ;       return 0;     }     long long int sum = 0;     for (int i = 0; i < (int)(n); ++i) {       sum = t[i];       if (n - i != 0 && t[i] != 0 && sum % (n - i)) {         cout <<  Impossible ;         return 0;       }     }     vector<pair<int, int>> p;     for (int i = 0; i < (int)(n); ++i) {       pair<int, int> r = make_pair(n - a[i], i);       p.push_back(r);     }     sort(((p).begin()), ((p).end()), sort_p);     int l = 1;     int j = 0;     for (int i = 0; i < ((int)((p).size())); ++i) {       for (j = 0; j < p[i].first; j++) {         b[p[i + j].second] = l;       }       i = i + j - 1;       l++;     }     if (sum > n) {       cout <<  Impossible ;       return 0;     }     cout <<  Possible n ;     for (int i = 0; i < (int)(n); ++i) cout << b[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   int n, m;   bool used[MAXN];   set<int> graph[MAXN];   stack<int> path;   vector<int> cycle;   int fake;   vector<pair<int, int> > edges;   vector<pair<int, int> > answer;   void EulerCycle(int x) {     cycle.clear();     edges.clear();     path.push(x);     while (path.empty() == false) {       used[x] = true;       if (graph[x].empty() == false) {         path.push(*graph[x].begin());         int y = *graph[x].begin();         graph[x].erase(y);         graph[y].erase(x);         x = y;       } else {         cycle.push_back(x);         path.pop();         if (path.empty() == false) x = path.top();       }     }     for (int i = 0; i < cycle.size() - 1; i++) {       edges.push_back({cycle[i], cycle[i + 1]});     }   }   bool isFake(pair<int, int> x) {     if (x.first == fake || x.second == fake) return true;     return false;   }   void solve(int x) {     EulerCycle(x);     for (int i = 0; i < edges.size(); i++) {       bool toDelete;       if (isFake(edges[i]) == true) {         continue;       }       if (i % 2 != 0)         toDelete = true;       else         toDelete = false;       if (i != 0 && isFake(edges[i - 1]) == true) toDelete = false;       if (i == 0 && isFake(edges[edges.size() - 1]) == true) toDelete = false;       if (i != edges.size() - 1 && isFake(edges[i + 1]) == true) toDelete = false;       if (i == edges.size() - 1 && isFake(edges[0]) == true) toDelete = false;       if (toDelete == false) {         answer.push_back(edges[i]);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       graph[u].insert(v);       graph[v].insert(u);     }     fake = -1;     for (int i = 1; i <= n; i++) {       if (graph[i].size() % 2 != 0) {         fake = n + 1;         graph[i].insert(fake);         graph[fake].insert(i);       }     }     for (int x = 1; x <= n + 1; x++) {       if (used[x] == false) {         solve(x);       }     }     cout << answer.size() <<   n ;     for (int i = 0; i < answer.size(); i++) {       cout << answer[i].first <<     << answer[i].second <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int a, b, maxa;   void use_battery(int s) {     if (s == 1) a = min(a + 1, maxa);     --b;   }   void use_accum() { --a; }   int main() {     int ans = 0;     int n;     cin >> n >> b >> a;     maxa = a;     vector<int> s(n);     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < n; i++) {       if (a == 0 && b == 0)         break;       else if (a == 0)         use_battery(s[i]);       else if (b == 0)         use_accum();       else if (s[i] == 1 && a < maxa)         use_battery(s[i]);       else         use_accum();       ans++;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int d[1000001] = {0};   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0, a, b; i < m; ++i) scanf( %d%d , &a, &b), d[a]++, d[b]++;     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long de = d[i], ade = n - 1 - de;       ans += de * (de - 1) + ade * (ade - 1) - de * ade;     }     cout << ans / 6 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[200];   int n, x;   vector<pair<int, int> > res;   int main() {     int sum = 0;     cin >> n;     cin >> a[0].first;     a[0].second = 0;     sum += a[0].first;     for (int i = 1; i < n; i++) {       cin >> a[i].first;       a[i].second = i;       sum += a[i].first;     }     if (a[0].first == 0 || sum < n - 1) {       cout << -1;       return 0;     }     sort(a + 1, a + n);     reverse(a + 1, a + n);     int x = 0;     int y = 1;     cout << n - 1 << endl;     while (y < n) {       cout << a[x].second + 1 <<     << a[y].second + 1 << endl;       y++;       a[x].first--;       if (a[x].first == 0) x++;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int m, x, y, a, b;       cin >> x >> y >> a >> b;       m = abs(x - a) + abs(y - b);       if (x != a && y != b) m = m + 2;       cout << m <<   n ;     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( 03 )   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <class T, class T2>   T _f_pow(T a, T2 b) {     T res = 1LL;     while (b > 0) {       if (b & 1) res = (res * a);       a = (a * a);       b >>= 1;     }     return (res);   }   template <class T>   T gcd(T a, T b) {     while (a && b)       if (a > b)         (a %= b);       else         (b %= a);     return a + b;   }   template <class T>   T lcm(T a, T b) {     return (a * b) / gcd(a, b);   }   int n, m;   long long par[(int)2e6 + 5], sz[(int)2e6 + 5], cmp, mxComp;   void init() {     iota(par, par + n + 1, 0);     fill(sz, sz + n + 1, 1);     cmp = n;     mxComp = 1;   }   int Find(int u) { return u == par[u] ? u : par[u] = Find(par[u]); }   int join(int u, int v) {     u = Find(u);     v = Find(v);     if (u == v) return 0;     if (sz[u] > sz[v]) swap(u, v);     par[u] = v;     sz[v] += sz[u];     mxComp = max(mxComp, sz[v]);     cmp--;     return 1;   }   int from[(int)2e6 + 5], to[(int)2e6 + 5], cst[(int)2e6 + 5],       sorted[(int)2e6 + 5];   int f, s;   int krus() {     init();     sort(sorted, sorted + m, [](int a, int b) { return cst[a] < cst[b]; });     int ret = 0;     for (int k = 0; k < m; k++) {       int e = sorted[k];       if (join(from[e], to[e])) {         if (Find(f) == Find(s)) {           cout << cst[e] << endl;           return 0;         }         ret += cst[e];       }     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     cin >> n >> m;     int tm;     m--;     cin >> f >> s >> tm;     for (int i = 0, _n = (m); i < _n; i++) {       cin >> from[i] >> to[i] >> cst[i];       sorted[i] = i;     }     if (krus()) cout << ((int)1e9) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 1000000;   const int OPAND = 2, OPOR = 3, OPXOR = 4, OPNOT = 5;   int n;   int op[MAXN];   vector<int> ch[MAXN];   int res[MAXN];   int ans[MAXN];   int dfsres(int at) {     if (op[at] == 0 || op[at] == 1) {       res[at] = op[at];     } else if (op[at] == OPNOT) {       res[at] = 1 - dfsres(ch[at][0]);     } else if (op[at] == OPAND) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = a == 1 && b == 1 ? 1 : 0;     } else if (op[at] == OPOR) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = a == 1 || b == 1 ? 1 : 0;     } else if (op[at] == OPXOR) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = (a == 1) ^ (b == 1) ? 1 : 0;     } else {       assert(false);     }     return res[at];   }   void dfsans(int at, int fix, bool neg) {     if (op[at] == 0 || op[at] == 1) {       ans[at] = fix != -1 ? fix : 1 - op[at];       if (fix == -1 && neg) ans[at] = 1 - ans[at];     } else if (op[at] == OPNOT) {       dfsans(ch[at][0], fix, !neg);     } else if (op[at] == OPAND) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i],                fix != -1                 ? fix                : res[ch[at][1 - i]] == 0 ? (neg ? 1 : 0)                                          : -1,                neg);     } else if (op[at] == OPOR) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i],                fix != -1                 ? fix                : res[ch[at][1 - i]] == 1 ? (neg ? 0 : 1)                                          : -1,                neg);     } else if (op[at] == OPXOR) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i], fix != -1 ? fix : -1,                res[ch[at][1 - i]] == 1 ? !neg : neg);     } else {       assert(false);     }   }   void run() {     scanf( %d , &n);     for (int i = (0); i < (n); ++i) {       char buff[10];       scanf( %s , buff);       if (strcmp(buff,  AND ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPAND;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  OR ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPOR;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  XOR ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPXOR;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  NOT ) == 0) {         int a;         scanf( %d , &a);         --a;         op[i] = OPNOT;         ch[i].push_back(a);       } else if (strcmp(buff,  IN ) == 0) {         int val;         scanf( %d , &val);         op[i] = val;       } else {         printf( err: %s n , buff);         assert(false);       }     }     dfsres(0);     dfsans(0, -1, false);     for (int i = (0); i < (n); ++i)       if (op[i] == 0 || op[i] == 1) printf( %d , ans[i]);     puts(  );   }   int main() {     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long tab[12], pr[12];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     long long n, m, w, x;     for (int z = 0; z < q; z++) {       cin >> n >> m;       x = 1;       for (long long i = 1; i <= 11; i++) {         tab[i] = (tab[i - 1] + (m % 10)) % 10;         pr[i] = pr[i - 1] + tab[i];         if (tab[i] == tab[1] && i > 1) {           x = i - 1;           break;         }       }       w = pr[x] * (n / (x * m));       n %= x * m;       w += pr[n / m];       cout << w <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ar[30005];   int diff[30005];   int main() {     int t;     cin >> t;     while (t--) {       string ans =  YES ;       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> ar[i];         diff[i] = 0;       }       for (int i = 2; i <= n; i++) {         if (ar[i] < ar[i - 1]) {           diff[1] += ar[i - 1] - ar[i];           diff[i] -= ar[i - 1] - ar[i];         }       }       for (int i = 1; i <= n; i++) {         diff[i] += diff[i - 1];         ar[i] -= diff[i];         if (ar[i] < 0 || (i > 1 && ar[i] < ar[i - 1])) {           ans =  NO ;         }       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = 0;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return f ? x : -x;   }   long long n, m, ans, s = 1, num;   bool exia[20][20], exib[20][20], s1[20][20], s2[20][20];   signed main() {     n = read(), m = read();     for (long long i = 1; i <= n; i++) {       long long x = read(), y = read();       exia[x][y] = exia[y][x] = 1;     }     for (long long i = 1; i <= m; i++) {       long long x = read(), y = read();       exib[x][y] = exib[y][x] = 1;     }     for (long long i = 1; i <= 9; i++) {       for (long long j = 1; j <= 9; j++) {         if (i == j) continue;         for (long long k = 1; k <= 9; k++) {           if (i == k || j == k) continue;           if (exia[i][j] && exib[i][k]) {             s1[i][j] = s2[i][k] = 1;             ans = i;             if (num && num != i) s = 0;             num = i;           }         }       }     }     if (s)       printf( %lld , ans);     else {       ans = 0;       for (long long i = 1; i <= 9; i++) {         for (long long j = 1; j <= 9; j++) {           if (i == j) continue;           if (s1[i][j] && s1[j][i]) {             ans = -1;             break;           }           if (s2[i][j] && s2[j][i]) {             ans = -1;             break;           }         }       }       printf( %lld , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b == 1) return a;     long long int r = power(a, b / 2) % 1000000007;     if (b % 2 == 0)       return (r * r) % 1000000007;     else       return (((r * r) % 1000000007) * a) % 1000000007;   }   int prime[1000009];   int sumprime[1000009];   void sieve(int n) {     prime[0] = prime[1] = 1;     for (int i = 2; i <= sqrt(n); i++) {       if (!prime[i]) {         for (int j = 2 * i; j <= n; j = j + i) {           prime[j]++;         }       }     }     for (int i = 1; i <= n; i++) {       if (!prime[i]) {         sumprime[i] = sumprime[i - 1] + 1;       } else {         sumprime[i] = sumprime[i - 1];       }     }   }   bool comp(const pair<long double, long long int> &a,             const pair<long double, long long int> &b) {     if (a.first == b.first) return a.second < b.second;     return (a.first < b.first);   }   const int N = 5 * 1e5 + 9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int m, n;     cin >> n >> m;     vector<pair<long double, long long int>> v;     for (int i = 1; i <= m; i++) {       long double d = abs((m + 1) / 2.0 - i);       v.push_back({d, i});     }     sort(v.begin(), v.end(), comp);     for (int i = 0; i < n; i++) {       int val = i % m;       cout << v[val].second <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, m;   struct point {     int x, y;     point() {}     point(int x, int y) : x(x), y(y) {}     point operator-(point a) { return point(x - a.x, y - a.y); }     bool operator<(const point& a) const { return x == a.x ? y < a.y : x < a.x; }   } a[N], b[N];   long long dot(point a, point b) {     return (long long)a.x * b.x + (long long)a.y * b.y;   }   long long cross(point a, point b) {     return (long long)a.x * b.y - (long long)a.y * b.x;   }   long long dist2(point a, point b) { return dot(a - b, a - b); }   vector<point> convex_hull(point* p, int n) {     sort(p + 1, p + 1 + n);     vector<point> convex;     for (int i = 1; i <= n; ++i) {       for (int j; (j = convex.size()) > 1 &&                   cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0;            convex.pop_back())         ;       convex.push_back(p[i]);     }     int old = convex.size();     for (int i = n - 1; i; --i) {       for (int j; (j = convex.size()) > old &&                   cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0;            convex.pop_back())         ;       convex.push_back(p[i]);     }     convex.pop_back();     return convex;   }   struct p3 {     long long x, y, z;     p3() {}     p3(long long x, long long y, long long z) : x(x), y(y), z(z) {}     bool operator<(const p3& a) const {       return x == a.x ? y == a.y ? z < a.z : y < a.y : x < a.x;     }   };   long long f(point a, point b) { return dot(a, b) + cross(a, b); }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &a[i].x, &a[i].y);     }     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &b[i].x, &b[i].y);     }     vector<point> convex_a = convex_hull(a, n);     vector<point> convex_b = convex_hull(b, m);     if (convex_a.size() != convex_b.size()) {       return puts( NO ), 0;     } else {       map<p3, int> appeared;       for (int i = 0; i < convex_a.size(); ++i) {         int j = i + 1 == convex_a.size() ? 0 : i + 1;         int k = i == 0 ? convex_a.size() - 1 : i - 1;         ++appeared[p3(dist2(convex_a[k], convex_a[i]),                       f(convex_a[k] - convex_a[i], convex_a[j] - convex_a[i]),                       dist2(convex_a[j], convex_a[i]))];       }       for (int i = 0; i < convex_b.size(); ++i) {         int j = i + 1 == convex_b.size() ? 0 : i + 1;         int k = i == 0 ? convex_b.size() - 1 : i - 1;         --appeared[p3(dist2(convex_b[k], convex_b[i]),                       f(convex_b[k] - convex_b[i], convex_b[j] - convex_b[i]),                       dist2(convex_b[j], convex_b[i]))];       }       bool same = true;       for (map<p3, int>::iterator it = appeared.begin(); it != appeared.end();            ++it) {         if (it->second != 0) {           same = false;         }       }       return puts(same ?  YES  :  NO ), 0;     }     return 0;   }