malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int Lower[1000000 + 1], Higher[1000000 + 1];   char Used[1000000 + 1];   long long SumNat(long long n) {     long long p = n, q = n + 1;     if (p % 2)       q /= 2;     else       p /= 2;     return (p) * (q);   }   long long PrimePi(long long n) {     long long v = sqrt(n + 1e-9), p, temp, q, j, end, i, d, t;     for (int i = 0; i <= 1000000; i++) Used[i] = 0;     Higher[1] = n - 1;     for (p = 2; p <= v; p++) {       Lower[p] = p - 1;       Higher[p] = (n / p) - 1;     }     for (p = 2; p <= v; p++) {       if (Lower[p] == Lower[p - 1]) continue;       temp = Lower[p - 1];       q = p * p;       Higher[1] -= Higher[p] - temp;       j = 1 + (p & 1);       end = (v <= n / q) ? v : n / q;       for (i = p + j; i <= 1 + end; i += j) {         if (Used[i]) continue;         d = i * p;         if (d <= v)           Higher[i] -= Higher[d] - temp;         else {           t = n / d;           Higher[i] -= Lower[t] - temp;         }       }       if (q <= v)         for (i = q; i <= end; i += p * j) Used[i] = 1;       for (i = v; i >= q; i--) {         t = i / p;         Lower[i] -= Lower[t] - temp;       }     }     return Higher[1];   }   bool prime[10000000 + 1];   long long piii[10000000 + 1];   long long Prim(long long n) {     if (n <= 10000000) return piii[n];     return PrimePi(n);   }   int main() {     long long n, i, j;     long long answer;     for (i = 2; i * i <= 10000000; i++)       if (!prime[i])         for (j = i * i; j <= 10000000; j += i) prime[j] = true;     for (i = 2; i <= 10000000; i++) {       piii[i] = piii[i - 1];       if (!prime[i]) piii[i]++;     }     cin >> n;     for (i = 1;; i++) {       if (i * i * i > n) break;     }     answer = Prim(i - 1);     for (i = 2; i < (n / i); i++)       if (!prime[i]) answer += (Prim(n / i) - Prim(i));     cout << answer;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, ans, seq[4], d[3005][3005], MaxT[3005][3005], Max[3005][3005], row;   vector<vector<int>> g;   bool comp(int a, int b) { return d[row][a] > d[row][b]; }   bool compT(int a, int b) { return d[a][row] > d[b][row]; }   void Dijkstra(int s) {     priority_queue<pair<int, int>> pq;     pq.push({0, s});     while (!pq.empty()) {       int dist = -pq.top().first, x = pq.top().second;       pq.pop();       if (d[s][x] != -1) continue;       d[s][x] = dist;       for (int adj : g[x]) {         if (d[s][adj] != -1) continue;         pq.push({-dist - 1, adj});       }     }   }   void sol(int A, int B, int C, int D) {     if (d[A][B] == -1 || d[B][C] == -1 || d[C][D] == -1) return;     if (d[A][B] + d[B][C] + d[C][D] > ans) {       ans = d[A][B] + d[B][C] + d[C][D];       seq[0] = A;       seq[1] = B;       seq[2] = C;       seq[3] = D;     }   }   int main() {     cin >> n >> m;     g.resize(n + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         d[i][j] = -1;       }     }     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       g[a].push_back(b);     }     for (int i = 1; i <= n; i++) Dijkstra(i);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         MaxT[i][j] = j;         Max[i][j] = j;       }     }     for (row = 1; row <= n; row++) {       sort(Max[row] + 1, Max[row] + n + 1, comp);       sort(MaxT[row] + 1, MaxT[row] + n + 1, compT);     }     for (int B = 1; B <= n; B++) {       for (int C = 1; C <= n; C++) {         if (B == C) continue;         for (int i = 1; i < 3; i++) {           for (int j = 1; j < 3; j++) {             int A = MaxT[B][i], D = Max[C][j];             if (A == B || A == C || A == D || D == B || D == C) continue;             sol(A, B, C, D);           }         }       }     }     for (int i = 0; i < 4; i++) cout << seq[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   priority_queue<long long int, vector<long long int>, greater<long long int> > q;   int main() {     int n;     scanf( %d , &n);     long long int x, sum = 0, ans = 0;     for (int i = 1; i <= n; i++) {       scanf( %lld , &x);       q.push(x);     }     while (q.size()) {       x = q.top();       q.pop();       if (sum > x)         ans++;       else         sum += x;     }     cout << n - ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1e5 + 23;   const int INF = 1e9 + 23;   const int MOD = 1e9 + 7;   struct Mada {     long long x, h, p;   };   Mada mada[MAX_N];   stack<Mada> Dane;   int dp[MAX_N];   int n;   bool cmp(Mada a, Mada b) { return a.x < b.x; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> mada[i].x >> mada[i].h;       mada[i].p = i;     }     sort(mada + 1, mada + 1 + n, cmp);     dp[mada[n].p] = 1;     Dane.push(mada[n]);     for (int i = n - 1; i >= 1; i--) {       long long res = 0;       while (!Dane.empty()) {         if (Dane.top().x > mada[i].h + mada[i].x - 1) {           break;         }         res += dp[Dane.top().p];         Dane.pop();       }       dp[mada[i].p] = res + 1;       Dane.push(mada[i]);     }     for (int i = 1; i <= n; i++) {       cout << dp[i] <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char rep;   void query(long long x, long long y) {     cout <<  ?   << x <<     << y << endl;     cin >> rep;     return;   }   void solve() {     long long x = 0;     long long y = 1;     while (true) {       query(x, y);       if (rep ==  x ) {         break;       }       x = y;       y = y * 2;     }     long long l = x + 1;     long long r = y;     while (l != r) {       long long med = (l + r) / 2;       query(med, y);       if (rep ==  x ) {         l = med + 1;       } else {         r = med;       }     }     cout <<  !   << l << endl;     return;   }   int main() {     string s;     while (true) {       cin >> s;       if (s[0] ==  s ) {         solve();       } else {         break;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void checkMin(T& a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkMax(T& a, T b) {     if (b > a) a = b;   }   const int N = 22;   const long long INF = 1LL << 60;   int n, m, a[N], b[N];   long long dp[N][N], tmp[N][N], f[N][N], ans[N][N];   void DP(int n) {     for (int t = 0; t <= n; t++) {       for (int i = 0; i <= n; i++) dp[0][i] = INF;       dp[0][t] = 0;       for (int i = 1; i <= n; i++)         for (int j = 0; j <= n; j++) {           dp[i][j] = INF;           if (j > 0) checkMin(dp[i][j], dp[i - 1][j - 1] + a[i]);           if (j < n) checkMin(dp[i][j], dp[i - 1][j + 1] + b[i]);         }       for (int i = 0; i <= n; i++) f[t][i] = dp[n][i];     }   }   void fast_mult(int n, int m) {     for (int i = 0; i <= n; i++)       for (int j = 0; j <= n; j++) ans[i][j] = INF;     for (int i = 0; i <= n; i++) ans[i][i] = 0;     while (m > 0) {       if (m & 1) {         for (int i = 0; i <= n; i++)           for (int j = 0; j <= n; j++) tmp[i][j] = ans[i][j], ans[i][j] = INF;         for (int k = 0; k <= n; k++)           for (int i = 0; i <= n; i++)             for (int j = 0; j <= n; j++) checkMin(ans[i][j], tmp[i][k] + f[k][j]);       }       for (int i = 0; i <= n; i++)         for (int j = 0; j <= n; j++) tmp[i][j] = f[i][j], f[i][j] = INF;       for (int k = 0; k <= n; k++)         for (int i = 0; i <= n; i++)           for (int j = 0; j <= n; j++) checkMin(f[i][j], tmp[i][k] + tmp[k][j]);       m >>= 1;     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     DP(n);     fast_mult(n, m);     cout << ans[0][0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 69, INF = 1e9 + 85, MOD = 1e9 + 7;   int n, k, a, b, q, arr[MAXN], seg[2][MAXN << 2];   void update(int indx, int val, int type, int l = 0, int r = n, int id = 1) {     if (indx < l or indx >= r) return;     if (r - l == 1) {       seg[type][id] = val;       return;     }     int mid = (l + r) >> 1;     update(indx, val, type, l, mid, id << 1 | 0);     update(indx, val, type, mid, r, id << 1 | 1);     seg[type][id] = seg[type][id << 1 | 0] + seg[type][id << 1 | 1];   }   int get(int st, int en, int type, int l = 0, int r = n, int id = 1) {     if (l >= en or r <= st) return 0;     if (l >= st and r <= en) return seg[type][id];     int mid = (l + r) >> 1;     return get(st, en, type, l, mid, id << 1 | 0) +            get(st, en, type, mid, r, id << 1 | 1);   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k >> a >> b >> q;     while (q--) {       int type, day, order;       cin >> type;       if (type == 1) {         cin >> day >> order;         arr[day] += order;         update(day - 1, min(arr[day], a), 0);         update(day - 1, min(arr[day], b), 1);       } else {         cin >> day;         cout << get(0, day - 1, 1) + get(day + k - 1, n, 0) <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t{};     cin >> t;     while (t--) {       long long int n{};       cin >> n;       long long int a[n];       for (long long int i = 0; i < n; i++) cin >> a[i];       bool visit[n];       memset(visit, 0, sizeof(visit));       vector<pair<long long int, long long int>> vec{};       for (long long int i = 0; i < n; i++) {         for (long long int j = 0; j < n; j++) {           if (i != j && a[i] != a[j] && (!visit[i] || !visit[j])) {             visit[i] = 1;             visit[j] = 1;             vec.push_back({i + 1, j + 1});           }         }       }       if (vec.size() == n - 1) {         cout <<  YES  <<   n ;         for (long long int i = 0; i < n - 1; i++) {           cout << vec[i].first <<     << vec[i].second <<   n ;         }       } else {         cout <<  NO  <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, p, q;     scanf( %d %d %d , &n, &p, &q);     char word[n + 1];     scanf( %s , word);     if (n == p + q) {       printf( 2 n );       for (int i = 0; i < p; ++i) {         printf( %c , word[i]);       }       printf(  n );       for (int i = p; i < n; ++i) {         printf( %c , word[i]);       }       printf(  n );       return 0;     } else if (n % p != 0 && n % q != 0) {       int z = 0, i;       for (i = 0; i < n; ++i) {         int x = n - (p * i);         if (x % q == 0 && x > 0) {           z = i;           break;         }       }       if (z == 0) {         printf( -1 n );         return 0;       }       int y = (n - (z * p)) / q;       printf( %d n , z + y);       for (i = 0; i < z; ++i) {         for (int j = 0; j < p; ++j) {           int f = p * i + j;           printf( %c , word[f]);         }         printf(  n );       }       for (i = 0; i < y; ++i) {         int f = z * p;         for (int j = 0; j < q; ++j) {           int g = f + q * i + j;           printf( %c , word[g]);         }         printf(  n );       }       return 0;     } else {       int s;       if (n % p == 0) {         s = p;       } else {         s = q;       }       printf( %d n , n / s);       for (int i = 0; i < n / s; ++i) {         for (int j = 0; j < s; ++j) {           int x = i * s + j;           printf( %c , word[x]);         }         printf(  n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a, b, c, ans, x[4];   int main() {     cin >> a >> b >> c;     x[0] = a + b + c;     x[1] = a + c + c + a;     x[2] = b + c + c + b;     x[3] = b + b + a + a;     ans = x[0];     for (int i = 1; i < 4; i++) {       if (ans > x[i]) ans = x[i];     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   const int maxn = 100 + 5;   using namespace std;   int val[maxn][maxn];   void doit(int x1, int y1, int x2, int y2) {     for (int i = x1; i <= x2; i++) {       for (int j = y1; j <= y2; j++) {         val[i][j]++;       }     }   }   int main() {     memset(val, 0, sizeof(val));     int n;     cin >> n;     while (n--) {       int x, y, u, v;       cin >> x >> y >> u >> v;       doit(x, y, u, v);     }     int sum = 0;     for (int i = 1; i <= 100; i++)       for (int j = 1; j <= 100; j++) sum += val[i][j];     cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   inline bool mina(A &first, B second) {     return (first > second) ? (first = second, 1) : 0;   }   template <class A, class B>   inline bool maxa(A &first, B second) {     return (first < second) ? (first = second, 1) : 0;   }   const int MAXN = 1e5 + 5;   int N, K;   int ans[MAXN];   long long minimum(long long prev, long long num) {     long long start = prev + 1;     long long end = start + num - 1;     return (start + end) * num / 2;   }   int main() {     cin >> N >> K;     int ll = 1, rr = N;     while (ll <= rr) {       int mid = (ll + rr) / 2;       if (mid + minimum(mid, K - 1) > N) {         rr = mid - 1;       } else {         ll = mid + 1;       }     }     if (rr == 0) {       cout <<  NO  << endl;       return 0;     }     ans[0] = rr;     int left = N - rr;     int now = 1;     while (left) {       if (now >= K) {         cout <<  NO  << endl;         return 0;       } else if (now + 1 == K) {         ans[now] = left;         if (left > ans[now - 1] && left <= 2 * ans[now - 1]) {           break;         } else {           cout <<  NO  << endl;           return 0;         }       } else {         if (2LL * ans[now - 1] + minimum(ans[now - 1] * 2, K - now - 1) > left) {           int ll = ans[now - 1] + 1;           int rr = 2LL * ans[now - 1];           while (ll <= rr) {             int mid = (ll + rr) / 2;             if (mid + minimum(mid, K - now - 1) > left) {               rr = mid - 1;             } else {               ll = mid + 1;             }           }           if (rr < ans[now - 1] + 1) {             cout <<  NO  << endl;             return 0;           }           ans[now] = rr;           left -= ans[now];           now++;         } else {           ans[now] = ans[now - 1] * 2;           left -= ans[now];           now++;         }       }     }     cout <<  YES  << endl;     for (int i = 0; (i) < (K); ++(i)) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   const int MX = 10010896;   const int lmt = 3164;   const int N = 10000001;   using namespace std;   long long int extgcd(long long int a, long long int b, long long int& x,                        long long int& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     } else {       int g = extgcd(b, a % b, y, x);       y -= a / b * x;       return g;     }   }   long long int modpow(long long int a, long long int b) {     long long int res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   inline void solve() {     long long int n, m;     cin >> n >> m;     long long int a[n], b[m];     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     for (long long int i = 0; i < m; i++) {       cin >> b[i];     }     long long int ans = 0;     bool flag2 = false;     for (ans = 0; ans <= 512; ans++) {       long long int ci = ans;       for (long long int i = 0; i < n; i++) {         flag2 = false;         for (long long int j = 0; j < m; j++) {           if (((a[i] & b[j]) | ci) <= ans) {             flag2 = true;             ci = (a[i] & b[j]) | ci;             break;           }         }         if (!flag2) break;       }       if (flag2) {         cout << ans <<   n ;         return;       }     }   }   int main() {     cout << fixed << setprecision(12);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1, tc = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int max_height(long long n) { return (long long)(sqrt(24 * n + 1) - 1) / 6; }   int main() {     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       int pyramids = 0;       while (1) {         long long height = max_height(n);         if (!height) break;         long long int cards = (3 * height + 1) * height / 2;         pyramids += n / cards;         n -= n / cards * cards;       }       cout << pyramids << endl;     }     return 0;   }
#include <bits/stdc++.h>   const double eps = 1e-7, PI = 3.1415926;   const int N = 2e5 + 10;   using namespace std;   long long n, q, m, k, x, y, a[N], mx = -1, mem[N][3], vis[N][3], mn = 1e9, sum,                                     t;   char c[N];   string s, s1, s2;   map<int, int> mp;   vector<int> vec;   long long dp(long long i, int cur) {     if (i <= 0 || i > n) return 0;     if (vis[i][cur] == x) return -1e17;     if (mem[i][cur] != -1) return mem[i][cur];     vis[i][cur] = x;     long long p1 = 0, p2 = 0;     if (cur == 0)       p1 += a[i] + dp(i + a[i], cur ^ 1);     else       p1 += a[i] + dp(i - a[i], cur ^ 1);     return mem[i][cur] = p1;   }   int main() {     cin >> n;     memset(mem, -1, sizeof mem);     for (int i = 2; i <= n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       a[1] = i;       mem[1][0] = -1;       x++;       long long ans = dp(1, 0);       if (ans >= 0)         cout << ans << endl;       else         cout << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int ph[N];   int main() {     string s;     cin >> s;     int n = s.size();     for (int i = 1; i <= n; i++)       ph[i] = ph[i - 1] + (s[i - 1] >=  A  && s[i - 1] <=  Z  ? 1 : 0);     int ans = 1e5;     for (int i = 0; i <= n; i++) {       ans = min(ans, i - ph[i] + ph[n] - ph[i]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int x;     cin >> x;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << 0 <<   n ;       return 0;     }     sort(a.begin(), a.end());     if (x < a[0]) {       cout << a[0] - x + a[n - 2] - a[0] <<   n ;       return 0;     }     if (x > a[n - 1]) {       cout << x - a[n - 1] + a[n - 1] - a[1] <<   n ;       return 0;     }     int ans1 = a[n - 2] - a[0] + min((int)abs(x - a[0]), (int)abs(x - a[n - 2]));     int ans2 = a[n - 1] - a[1] + min((int)abs(x - a[1]), (int)abs(x - a[n - 1]));     cout << min(ans1, ans2) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout.precision(15);     long long int t;     cin >> t;     while (t--) {       string s;       cin >> s;       long long int x;       cin >> x;       long long int n = (long long int)s.length();       vector<int> v(n, 2);       for (long long int i = x; i < n - x; i++) {         if (s[i - x] ==  1  && s[i + x] ==  1 )           v[i] = 1;         else           v[i] = 0;       }       for (long long int i = 0; i < x; i++) {         if ((i + x) < n && s[i + x] ==  0 )           v[i] = 0;         else           v[i] = 1;       }       for (long long int i = n - x; i < n; i++) {         if ((i - x) >= 0 && s[i - x] ==  0 )           v[i] = 0;         else           v[i] = 1;       }       string ans;       for (long long int i = 0; i < n; i++) {         if (i - x >= 0 && v[i - x] == 1)           ans +=  1 ;         else if (i + x < n && v[i + x] == 1)           ans +=  1 ;         else           ans +=  0 ;       }       if (ans != s)         cout <<  -1 n ;       else {         string a;         for (long long int i = 0; i < n; i++) a += ( 0  + v[i]);         cout << a <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   char buf[1 << 21], *p1 = buf, *p2 = buf;   template <class T>   void read(T &x) {     x = 0;     int c = getchar();     int flag = 0;     while (c <  0  || c >  9 ) flag |= (c ==  - ), c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     if (flag) x = -x;   }   template <class T>   T _max(T a, T b) {     return b < a ? a : b;   }   template <class T>   T _min(T a, T b) {     return a < b ? a : b;   }   template <class T>   bool checkmax(T &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   bool checkmin(T &a, T b) {     return b < a ? a = b, 1 : 0;   }   const int N = 82;   int n;   int l[N], r[N], foo[N];   int arr[N << 1], top = 0;   int L, R;   double f[N][N], g[N][N];   double ans[N][N];   void init() {     read(n);     for (int i = 1; i <= n; ++i) {       read(l[i]);       read(r[i]);       arr[++top] = l[i];       arr[++top] = r[i];       foo[i] = r[i] - l[i];     }     sort(arr + 1, arr + top + 1);     top = unique(arr + 1, arr + top + 1) - arr - 1;   }   double calcl(int idx) {     return 1.0 * _min(_max(0, L - l[idx]), foo[idx]) / foo[idx];   }   double calcr(int idx) {     return 1.0 * _min(_max(0, r[idx] - R), foo[idx]) / foo[idx];   }   double calcmid(int idx) { return 1 - calcl(idx) - calcr(idx); }   void run(int idx, int bar) {     L = arr[bar], R = arr[bar + 1];     if (L < l[idx] || R > r[idx]) return;     memset(f, 0, sizeof f);     f[0][0] = 1;     int tot = 0;     for (int i = 1; i <= n; ++i) {       if (i == idx) continue;       ++tot;       double cl = calcl(i), cr = calcr(i), cm = calcmid(i);       for (int j = tot; j >= 0; --j) {         for (int k = tot - j; k >= 0; --k) {           if (j + k < tot)             f[j][k] *= cm;           else             f[j][k] = 0;           if (j) f[j][k] += f[j - 1][k] * cl;           if (k) f[j][k] += f[j][k - 1] * cr;         }       }     }     double co = 1.0 * (R - L) / foo[idx];     for (int i = 0; i <= tot; ++i) {       for (int j = 0; i + j <= tot; ++j) {         int num = tot - i - j + 1;         double res = f[i][j] / num * co;         ans[idx][i + 1] += res;         ans[idx][n - j + 1] -= res;       }     }   }   void solve() {     for (int i = 1; i <= n; ++i) {       for (int j = 1; j < top; ++j) {         run(i, j);       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         ans[i][j] += ans[i][j - 1];         printf( %.8lf%c , ans[i][j],    n [j == n]);       }     }   }   int main() {     init();     solve();     return 0;   }