malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   vector<int> factor[100001];   void precal() {     for (int i = (1); i < (100001); ++i)       for (int j = (i); j < (100001); j += (i)) factor[j].push_back(i);   }   int expo(int a, int b) {     if (b == 0) return 1;     if (b == 1) return a % 1000000007;     int c = expo(a, b >> 1);     c = ((long long)c * c) % 1000000007;     if (b & 1) c = ((long long)c * a) % 1000000007;     return c;   }   int main(int argc, char **argv) {     int N;     scanf( %d , &N);     vector<int> A(N);     for (int i = (0); i < (N); ++i) scanf( %d , &A[i]);     sort(A.begin(), A.end());     precal();     int res = 0;     for (int f = (1); f < (100001); ++f) {       int numFactor = factor[f].size();       int bound[numFactor];       int cur = 0;       for (int i = (0); i < (numFactor); ++i) {         int lo = cur, hi = N - 1;         int nextFactor = (i + 1 == numFactor) ? (1 << 29) : factor[f][i + 1];         while (lo <= hi) {           int mi = (lo + hi) / 2;           if (A[mi] >= nextFactor)             hi = mi - 1;           else             lo = mi + 1;         }         bound[i] = lo - cur;         cur = lo;       }       if (!bound[numFactor - 1]) continue;       int countWays = 1;       for (int i = (0); i < (numFactor - 1); ++i)         countWays = ((long long)countWays * expo(i + 1, bound[i])) % 1000000007;       countWays = ((long long)countWays *                    (((long long)expo(numFactor, bound[numFactor - 1]) +                      1000000007 - expo(numFactor - 1, bound[numFactor - 1])) %                     1000000007)) %                   1000000007;       res = (res + countWays) % 1000000007;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct inf {     long long t, w;   };   struct djk {     long long n, v;     bool operator>(const djk &i) const { return v < i.v; }     bool operator<(const djk &i) const { return v > i.v; }   } n;   struct edge {     long long n1, n2, t;     bool operator<(const edge &i) const { return t < i.t; }   };   long long nodes, edges, n1, n2, weight, nn, totes, p1, p2, ports;   long long isp[100005], cp[100005], pd[100005], rnk[100005], head[100005];   edge elist[100005];   vector<inf> nlist[100005];   priority_queue<djk> viser;   long long ufind(long long n) {     if (head[n] != n) head[n] = ufind(head[n]);     return head[n];   }   void uni(long long n1, long long n2) {     p1 = ufind(n1);     p2 = ufind(n2);     if (rnk[p1] > rnk[p2])       head[p1] = p2;     else if (rnk[p2] > rnk[p1])       head[p2] = p1;     else       head[p1] = p2, rnk[p1]++;   }   int main() {     scanf( %I64d%I64d , &nodes, &edges);     for (int i = 1; i <= nodes; i++) pd[i] = -1, head[i] = i, rnk[i] = 1;     for (int i = 1; i <= edges; i++) {       scanf( %I64d%I64d%I64d , &n1, &n2, &weight);       nlist[n1].push_back({n2, weight});       nlist[n2].push_back({n1, weight});       elist[i] = {n1, n2, weight};     }     scanf( %I64d , &ports);     for (int i = 1; i <= ports; i++) {       scanf( %I64d , &n1);       isp[n1] = 1;       viser.push({n1, 0});       pd[n1] = 0;       cp[n1] = n1;     }     while (!viser.empty()) {       n = viser.top();       viser.pop();       if (pd[n.n] > n.v) continue;       for (int i = 0; i < nlist[n.n].size(); i++) {         nn = nlist[n.n][i].t;         if (pd[nn] == -1 || pd[nn] > nlist[n.n][i].w + pd[n.n]) {           pd[nn] = nlist[n.n][i].w + pd[n.n];           cp[nn] = cp[n.n];           viser.push({nn, pd[nn]});         }       }     }     for (int i = 1; i <= edges; i++)       elist[i].t += pd[elist[i].n1] + pd[elist[i].n2];     sort(elist + 1, elist + 1 + edges);     for (int i = 1; i <= edges; i++) {       if (ufind(cp[elist[i].n1]) != ufind(cp[elist[i].n2])) {         uni(cp[elist[i].n1], cp[elist[i].n2]);         totes += elist[i].t;       }     }     if (!isp[1] && pd[1] != -1) totes += pd[1];     printf( %I64d n , totes);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 55;   const int INF = 1 << 20;   bool dp1[MAXN][MAXN][26] = {false};   bool dp2[MAXN][MAXN][26] = {false};   int dp[MAXN][MAXN];   char s1[MAXN], s2[MAXN];   string tr[MAXN];   int n;   void cal(char* s, bool f[MAXN][MAXN][26]) {     int len = strlen(s);     for (int i = 0; i < len; i++) {       f[i][i][s[i] -  a ] = true;     }     for (int L = 1; L <= len; L++) {       for (int i = 0; i + L - 1 < len; i++) {         int j = i + L - 1;         for (int k = i; k < j; k++) {           for (int c = 0; c < n; c++) {             if (f[i][k][tr[c][3] -  a ] && f[k + 1][j][tr[c][4] -  a ]) {               f[i][j][tr[c][0] -  a ] = true;             }           }         }       }     }   }   int main() {     cin >> s1 >> s2;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> tr[i];     }     cal(s1, dp1);     cal(s2, dp2);     int len1 = strlen(s1);     int len2 = strlen(s2);     for (int i = 0; i <= len1; i++)       for (int j = 0; j <= len2; j++) dp[i][j] = INF;     dp[0][0] = 0;     for (int i = 0; i < len1; i++)       for (int ii = i; ii < len1; ii++)         for (int c = 0; c < 26; c++)           if (dp1[i][ii][c])             for (int j = 0; j < len2; j++)               for (int jj = j; jj < len2; jj++)                 if (dp2[j][jj][c])                   dp[ii + 1][jj + 1] = min(dp[ii + 1][jj + 1], dp[i][j] + 1);     int ans = (dp[len1][len2] == INF ? -1 : dp[len1][len2]);     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   long long me(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;   }   int main() {     c_p_c();     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       long long b[n];       for (long long i = 0; i < n; i++) cin >> a[i];       for (long long i = 0; i < n; i++) cin >> b[i];       long long c[n];       multiset<long long> s;       long long l = 0;       vector<long long> v;       long long g = 0;       long long temp = 0;       long long g1 = 0;       for (long long i = 0; i < n; i++) {         c[i] = b[i] - a[i];         if (c[i] == 0 && g1 == 1) {           break;         }         if (c[i] < 0) {           cout <<  NO n ;           l = 1;           break;         }         if (c[i] != 0 && (temp == c[i] || g == 0)) {           a[i] = a[i] + c[i];           temp = c[i];           g = 1;           g1 = 1;         }       }       if (l == 1) {         continue;       }       long long flg = 0;       for (long long i = 0; i < n; i++) {         if (a[i] != b[i]) {           flg = 1;           break;         }       }       if (flg == 1) {         cout <<  NO n ;       } else {         cout <<  YES n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t, n, k, c = 0, d = 0;     cin >> n >> k;     map<long long, long long> m;     for (int i = 0; i < n; ++i) {       cin >> t;       m[t]++;     }     auto it2 = m.begin();     for (auto it = m.begin(); it != m.end(); ++it) {       it2 = it;       it2++;       t = it->first;       if (it2 == m.end()) {         c += it->second;         break;       }       d = it2->first;       if (t + k < d) c += it->second;     }     cout << c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T sqr(T a) {     return a * a;   }   int n;   int a[21000];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int best = -1000000000;     for (int i = 3; i <= n; i++)       if (n % i == 0) {         int g = n / i;         for (int j = 0; j < g; j++) {           int cur = 0;           for (int k = j; k < n; k += g) cur += a[k];           best = max(cur, best);         }       }     cout << best;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   struct Edge {     int src, dst;     int weight;     Edge(int src_, int dst_, int weight_)         : src(src_), dst(dst_), weight(weight_) {}     Edge(int dst_, int weight_) : src(-2), dst(dst_), weight(weight_) {}   };   bool operator<(const Edge &e, const Edge &f) {     return e.weight != f.weight ? e.weight > f.weight : e.dst < f.dst;   }   void dijkstra(const vector<vector<Edge> > &g, int start, vector<int> &dist) {     int n = g.size();     dist.assign(n, INF);     dist[start] = 0;     vector<int> prev(n, -1);     priority_queue<Edge> q;     for (q.push(Edge(-2, start, 0)); !q.empty();) {       Edge e = q.top();       q.pop();       if (prev[e.dst] != -1) continue;       prev[e.dst] = e.src;       for (typeof((g[e.dst]).begin()) f = ((g[e.dst]).begin());            f != (g[e.dst]).end(); ++f) {         if (dist[f->dst] > e.weight + f->weight) {           dist[f->dst] = e.weight + f->weight;           q.push(Edge(f->src, f->dst, e.weight + f->weight));         }       }     }   }   int main() {     int n;     int x0, y0, x1, y1;     vector<pair<int, pair<int, int> > > v;     cin >> y0 >> x0 >> y1 >> x1;     cin >> n;     for (int(i) = 0; (i) < (int)(n); ++(i)) {       int o, a, b;       cin >> o >> a >> b;       v.push_back(make_pair((o), (make_pair((a), (b)))));     }     sort((v).begin(), (v).end());     map<pair<int, int>, int> m;     int t = 0;     static const int dy[4] = {0, -1, -1, -1}, dx[4] = {-1, 0, -1, 1};     vector<vector<Edge> > g;     for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {       for (int(j) = (int)(i->second.first); (j) <= (int)(i->second.second);            ++(j)) {         if (m.count(make_pair((i->first), (j)))) continue;         int q = m[make_pair((i->first), (j))] = t++;         g.push_back(vector<Edge>());         for (int(d) = 0; (d) < (int)(4); ++(d))           if (m.count(make_pair((i->first + dy[d]), (j + dx[d])))) {             g[q].push_back(                 Edge(q, m[make_pair((i->first + dy[d]), (j + dx[d]))], 1));           }       }     }     vector<vector<Edge> > gg(t);     for (int(i) = 0; (i) < (int)(t); ++(i))       for (typeof((g[i]).begin()) j = ((g[i]).begin()); j != (g[i]).end(); ++j) {         gg[j->src].push_back(Edge(j->src, j->dst, 1));         gg[j->dst].push_back(Edge(j->dst, j->src, 1));       }     int r = -1;     if (!m.count(make_pair((y0), (x0))))       r = -999;     else if (!m.count(make_pair((y1), (x1))))       r = -999;     else {       vector<int> d;       dijkstra(gg, m[make_pair((y0), (x0))], d);       r = d[m[make_pair((y1), (x1))]];       if (r == INF) r = -1;     }     cout << r << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr <<  [   << *it <<   =   << a <<   ]  ;     err(++it, args...);   }   template <typename Tk, typename Tv>   ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) {     os <<  {  << p.first <<  ,  << p.second <<  } ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& p) {     os <<  [  ;     for (T x : p) os << x <<    ;     os <<  ]  <<   n ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& p) {     os <<  {  ;     for (T x : p) os << x <<    ;     os <<  }  <<   n ;     return os;   }   template <typename Tk, typename Tv>   ostream& operator<<(ostream& os, const map<Tk, Tv>& p) {     os <<  {  ;     for (pair<Tk, Tv> x : p) os << x <<    ;     os <<  }  <<   n ;     return os;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t = 1;     cin >> t;     while (t--) {       long long int n, k, l;       cin >> n >> k >> l;       vector<long long int> depth(n), time(2 * k);       for (long long int i = 0; i <= k; i++) {         time[i] = i;       }       long long int ct = 1;       for (long long int i = 0; i < n; i++) {         cin >> depth[i];       }       for (long long int i = k - 1; i > 0; i--) {         time[k + (ct++)] = i;       }       bool dp[2 * k][n];       memset(dp, false, sizeof(dp));       for (long long int i = 0; i < 2 * k; i++) {         if (depth[0] + time[i] <= l) {           dp[i][0] = true;         }       }       for (long long int i = 0; i < n - 1; i++) {         if (depth[i] + k <= l) {           if (i == 0) {             for (long long int j = 0; j < 2 * k; j++) {               dp[j][i] = true;             }           } else {             bool find_true = false;             for (long long int j = 0; j < 2 * k; j++) {               if (dp[j][i]) {                 find_true = true;               }             }             if (find_true) {               for (long long int j = 0; j < 2 * k; j++) {                 dp[j][i] = true;               }             }           }         }         for (long long int j = 0; j < 2 * k; j++) {           if (dp[j][i] &&               (depth[i + 1] + (long long int)time[(j + 1) % (2 * k)] <= l)) {             dp[(j + 1) % (2 * k)][i + 1] = true;           }         }       }       bool pass = false;       for (long long int i = 0; i < 2 * k; i++) {         if (dp[i][n - 1]) {           pass = true;           break;         }       }       cout << (pass ?  YES n  :  NO n );     }     return 0;   }
#include <bits/stdc++.h>   int max(int a, int b) {     if (a > b) return a;     return b;   }   int min(int a, int b) {     if (a < b)       return a;     else       return b;   }   char s1[600000], s2[600000];   int z[600000], y[600000];   struct student {     int l;     int r;   } a[600000];   int main() {     int i;     int n, m, p;     int j;     scanf( %d%d%d , &n, &m, &p);     for (i = 0; i <= n + 1; i++) {       a[i].l = i - 1;       a[i].r = i + 1;     }     getchar();     gets(s1 + 1);     gets(s2 + 1);     int num = 0;     int len = strlen(s1 + 1);     for (i = 1; i <= len; i++) {       if (s1[i] ==  ( )         z[num++] = i;       else {         y[z[--num]] = i;         y[i] = z[num];       }     }     len = strlen(s2 + 1);     for (i = 1; i <= len; i++) {       if (s2[i] ==  R )         p = a[p].r;       else if (s2[i] ==  L )         p = a[p].l;       else {         a[a[max(p, y[p])].r].l = a[min(p, y[p])].l;         a[a[min(p, y[p])].l].r = a[max(p, y[p])].r;         a[min(p, y[p])].r = a[max(p, y[p])].r;         p = a[p].r;       }       if (p < 1) p = a[p].r;       if (p > n) p = a[p].l;     }     for (i = a[0].r; i <= n; i = a[i].r) {       printf( %c , s1[i]);     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   const int sieve_size = 5e6;   void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int &x : a) cin >> x;     vector<int> primes;     vector<bool> sieve(sieve_size);     for (int i = 2; i < int((sieve).size()); i++) {       if (sieve[i]) continue;       primes.push_back(i);       for (int j = i + i; j < int((sieve).size()); j += i) {         sieve[j] = true;       }     }     auto get_decomp = [](int x) {       vector<int> decomp;       for (int j = 2; j * j <= x; j++) {         if (x % j == 0) {           decomp.push_back(j);           x /= j;           while (x % j == 0) x /= j;         }       }       if (x != 1) decomp.push_back(x);       return decomp;     };     set<int> occupied_primes;     vector<int> b(n);     bool a_coprime = true;     for (int i = 0, primes_idx = 0; i < n; i++) {       if (a_coprime) {         vector<int> decomp = get_decomp(a[i]);         for (int x : decomp) {           if (occupied_primes.count(x)) {             a_coprime = false;             break;           }         }         if (!a_coprime) {           for (a[i]++;; a[i]++) {             decomp = get_decomp(a[i]);             bool ok = true;             for (int x : decomp) {               if (occupied_primes.count(x)) {                 ok = false;                 break;               }             }             if (ok) break;           }         }         for (int x : decomp) occupied_primes.insert(x);         b[i] = a[i];       } else {         while (occupied_primes.count(primes[primes_idx])) primes_idx++;         b[i] = primes[primes_idx++];       }     }     for (int x : b) cout << x <<    ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int tcs = 1;     for (int tc = 1; tc <= tcs; tc++) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, h;   long long l[200010], r[200010];   long long dsum[200010], rsum[200010];   int main() {     scanf( %lld%lld , &n, &h);     for (long long i = (long long)(0); i < (long long)(n); i++)       scanf( %lld%lld , l + i, r + i);     for (long long i = (long long)(0); i < (long long)(n - 1); i++)       dsum[i + 1] = dsum[i] + l[i + 1] - r[i];     for (long long i = (long long)(0); i < (long long)(n); i++)       rsum[i + 1] = rsum[i] + r[i] - l[i];     long long res = h;     for (long long i = (long long)(0); i < (long long)(n - 1); i++) {       long long lb = 0, ub = n;       while (ub - lb > 1) {         long long mid = (lb + ub) / 2;         if (dsum[mid] - dsum[i] < h)           lb = mid;         else           ub = mid;       }       res = (res > h + rsum[ub] - rsum[i] ? res : h + rsum[ub] - rsum[i]);     }     res = (res > h + r[n - 1] - l[n - 1] ? res : h + r[n - 1] - l[n - 1]);     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Act {     int l, r;     int mx;     double pb;     vector<Act *> son;     static int dp_size;     double dp[5005];     bool operator<(const Act &act) const {       if (l < act.l) {         return true;       } else if (l > act.l) {         return false;       } else if (r > act.r) {         return true;       }       return false;     }     bool contains(Act &act) { return act.l >= l && act.r <= r; }     double p(int profit) {       if (profit < 0) return 0.0;       if (profit > dp_size) return 1.0;       return dp[profit];     }   };   int Act::dp_size = 0;   int n, q;   int *a;   int **m;   int *lg;   Act act[5005];   void dfs(Act &act) {     for (int i = 0; i < act.son.size(); i++) {       dfs(*act.son[i]);     }     for (int i = 0; i <= Act::dp_size; i++) {       act.dp[i] = 0.0;     }     for (int i = 0; i <= Act::dp_size; i++) {       double p = (i == 0 ? 0.0 : act.pb);       double np = 1.0 - act.pb;       for (int j = 0; j < act.son.size(); j++) {         Act *son = act.son[j];         p *= son->p(act.mx - son->mx + i - 1);         np *= son->p(act.mx - son->mx + i);       }       act.dp[i] = p + np;     }   }   int main() {     cin >> n >> q;     a = new int[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     lg = new int[n + 1];     lg[1] = 0;     for (int i = 2; i <= n; i++) {       lg[i] = lg[i / 2] + 1;     }     m = new int *[lg[n] + 1];     for (int i = 0; i <= lg[n]; i++) {       m[i] = new int[n];     }     for (int i = 0; i < n; i++) {       m[0][i] = a[i];     }     for (int i = 1; i <= lg[n]; i++) {       int psz = 1 << (i - 1);       int nsz = 1 << i;       for (int j = 0; j + nsz <= n; j++) {         m[i][j] = max(m[i - 1][j], m[i - 1][j + psz]);       }     }     q++;     for (int i = 0; i < q; i++) {       if (i == q - 1) {         act[i].l = 1;         act[i].r = n;         act[i].pb = 0.0;       } else {         cin >> act[i].l >> act[i].r >> act[i].pb;       }       act[i].l--, act[i].r--;       int tlg = lg[act[i].r - act[i].l + 1];       int tsz = 1 << tlg;       act[i].mx = max(m[tlg][act[i].l], m[tlg][act[i].r - tsz + 1]);     }     sort(act, act + q);     Act::dp_size = q;     for (int i = 1; i < q; i++) {       int j = i - 1;       while (!act[j].contains(act[i])) j--;       act[j].son.push_back(act + i);     }     dfs(act[0]);     double ans = 0.0;     for (int i = 0; i <= Act::dp_size; i++) {       ans += (act[0].mx + i) * (act[0].p(i) - act[0].p(i - 1));     }     printf( %.8lf , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, j, k, l, n, a, b, ans = 0;   int gcd(int a, int b) {     if (a < b) swap(a, b);     return (b == 0) ? a : gcd(b, a % b);   }   struct st {     int first;     long double second;   };   int main() {     std::ios_base::sync_with_stdio(0);     string s;     cin >> s;     n = s.size();     list<int> left, right;     j = 0;     for (auto i : s) {       if (i ==  l ) {         left.push_front(++j);       } else {         right.push_front(++j);       }     }     reverse(right.begin(), right.end());     for (auto i : right) cout << i <<   n ;     for (auto i : left) cout << i <<   n ;     fflush(stdin);     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, q;     std::cin >> n >> q;     int ans[q], servers[n], server;     for (server = 0; server < n; ++server) {       servers[server] = 0;     }     int index, t, k, d, planner[n];     for (int i = 0; i < q; ++i) {       server = 0;       index = 0;       std::cin >> t >> k >> d;       while (index < k && server < n) {         if (servers[server] <= t) {           planner[index++] = server;           servers[server] = t;         }         ++server;       }       if (index == k) {         ans[i] = k;         for (index = 0; index < k; ++index) {           ans[i] += planner[index];           servers[planner[index]] += d;         }       } else {         ans[i] = -1;       }     }     for (int a : ans) {       std::cout << a << std::endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       vector<int> sml, big;       for (int i = 1, j = n; i < j; i++, j--) {         printf( ? %d %d n , i, j);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  < ) {           sml.push_back(i);           big.push_back(j);         }         if (c[0] ==  > ) {           sml.push_back(j);           big.push_back(i);         }         if (c[0] ==  = ) {           sml.push_back(i);           big.push_back(j);         }       }       if (n % 2) {         big.push_back(ceil(n / 2.0));         sml.push_back(ceil(n / 2.0));       }       int mn = sml[0];       for (int i = 1; i < sml.size(); i++) {         printf( ? %d %d n , mn, sml[i]);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  > ) {           mn = sml[i];         }       }       int mx = big[0];       for (int i = 1; i < big.size(); i++) {         printf( ? %d %d n , mx, big[i]);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  < ) {           mx = big[i];         }       }       printf( ! %d %d n , mn, mx);       fflush(stdout);     }   }