malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, k;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     vector<vector<int>> g(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     vector<int> color(n);     vector<int> d(n);     vector<int> sub(n);     int iter = 0;     function<void(int, int, int, int)> dfs_to_color = [&](int v, int p, int c,                                                           int pr) {       color[v] = c;       for (int i : g[v]) {         if (i == p || color[i] != pr) continue;         dfs_to_color(i, v, c, pr);       }     };     function<void(int, int, int)> dfs_on_color = [&](int v, int p, int c) {       if (~p)         d[v] = d[p] ^ 1;       else         d[v] = 0;       sub[v] = 1;       for (int i : g[v]) {         if (color[i] != c || i == p) continue;         dfs_on_color(i, v, c);         sub[v] += sub[i];       }     };     vector<pair<int, int>> ret;     function<void(vector<int>)> solve = [&](vector<int> nodes) {       int sz = nodes.size();       if (sz <= 4) return;       int root = nodes[0];       int c = color[root];       dfs_on_color(root, -1, c);       int pr = -1;       while (true) {         int nxt = -1;         for (int i : g[root]) {           if (color[i] != c || i == pr) continue;           if (sub[i] > sz / 2) nxt = i;         }         if (nxt == -1) break;         pr = root;         root = nxt;       }       for (int i : g[root]) {         if (color[i] != c) continue;         iter++;         dfs_to_color(i, root, iter, c);       }       map<int, vector<int>> to_color;       for (int i : nodes) {         if (i == root) continue;         to_color[color[i]].push_back(i);       }       int pos = -1;       int cd = 0;       for (auto it : to_color) {         int id = it.first;         vector<int> nxt = it.second;         solve(nxt);         for (int i : nxt) color[i] = id;         dfs_on_color(nxt[0], -1, id);         if ((int)nxt.size() > cd) {           cd = nxt.size();           pos = id;         }       }       for (auto it : to_color) {         int id = it.first;         vector<int> vertices = it.second;         vector<int> to_root;         if (id != pos)           to_root = vertices;         else {           vector<int> cnt(2);           for (int i : vertices) cnt[d[i]]++;           int chosen = cnt[1] < cnt[0];           for (int i : vertices) {             if (d[i] == chosen) to_root.push_back(i);           }         }         for (int i : to_root) {           ret.emplace_back(i, root);         }       }     };     vector<int> foo(n);     iota(foo.begin(), foo.end(), 0);     solve(foo);     assert((int)ret.size() <= 10 * n);     cout << ret.size() << endl;     for (auto i : ret) {       cout << i.first + 1 <<     << i.second + 1 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   const long long MAXN = 1e6 + 5;   long long n, m, t;   struct NODE {     long long a, b;   } v[MAXN];   long long cmp1(NODE a, NODE b) { return a.a < b.a; }   long long a[MAXN], b[MAXN];   long long q[MAXN];   int main() {     cin >> t;     while (t--) {       cin >> m >> n;       for (long long i = 1; i <= n; i++) cin >> v[i].a >> v[i].b;       sort(v + 1, v + 1 + n, cmp1);       for (long long i = 1; i <= n; i++)         a[i] = v[i].a, b[i] = v[i].b, q[i] = q[i - 1] + a[i];       long long ans = 0;       for (long long i = 1; i <= n; i++) {         long long nowb = b[i];         long long index = lower_bound(a + 1, a + 1 + n, nowb) - a;         if (index == n + 1) {           ans = max(ans, a[i] + (m - 1) * b[i]);           continue;         }         if (index <= i) {           long long can = n - index + 1;           if (can >= m) {             ans = max(ans, q[n] - q[n - m]);             continue;           }           ans = max(ans, q[n] - q[index - 1] + b[i] * (m - can));         } else {           long long can = n - index + 1;           if (can >= m - 1) {             ans = max(ans, q[n] - q[n - m + 1] + a[i]);             continue;           }           ans = max(ans, q[n] - q[index] + a[i] + b[i] * (m - can - 1) +                              max(b[i], a[index]));         }       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, i, j, k, dances = 0, a[250][2];     cin >> n >> m;     dances = m + n - 1;     for (i = 0; i < m; i++) {       a[i][0] = 1;       a[i][1] = i + 1;     }     k = 2;     for (j = i; j < dances; j++) {       a[j][0] = k;       a[j][1] = 1;       k++;     }     cout << dances <<   n ;     for (i = 0; i < dances; i++) {       cout << a[i][0] <<     << a[i][1] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 + 100;   const long double eps = 1e-11;   const long double pi = acos(-1.0L);   int myrand() { return rand(); }   unsigned rdtsc() {     unsigned ans;     asm( rdtsc  :  =a (ans));     return ans;   }   int rnd(int x) { return myrand() % x; }   void precalc() {}   const int maxn = 1e5;   int n;   string str;   char s[maxn];   int dp[5][maxn];   bool read() {     if (scanf( %s , s) < 1) {       return false;     }     str = string(s);     n = strlen(s);     return true;   }   void solve() {     memset(dp, 0, sizeof(dp));     dp[2][n - 2] = 1;     dp[3][n - 3] = 1;     for (int i = n - 4; i >= 5; i--) {       for (int j = 2; j <= 3; j++) {         if (dp[j ^ 1][i + j]) {           dp[j][i] = 1;         }         if (i + j * 2 <= n) {           string s1 = str.substr(i, j);           string s2 = str.substr(i + j, j);           if (s1 != s2 && dp[j][i + j]) {             dp[j][i] = 1;           }         }       }     }     set<string> ans;     for (int i = 5; i < n; i++) {       for (int j = 2; j <= 3; j++) {         if (i + j > n) {           continue;         }         if (i + j == n - 1) {           continue;         }         string cur = str.substr(i, j);         if (dp[2][i + j] || dp[3][i + j] || n == i + j) {           ans.insert(cur);         }       }     }     printf( %d n , ((int)((ans).size())));     for (string s : ans) {       printf( %s n , s.c_str());     }   }   int main() {     srand(rdtsc());     precalc();     while (true) {       if (!read()) {         break;       }       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class mat {    public:     long long a, b, c, d;   };   mat a;   long long n, k, l, m, step, res;   long long power(long long a, long long b);   mat mult(mat a, mat b);   mat power1(mat a, long long n);   int main() {     cin >> n >> k >> l >> m;     step = power(2, n);     a.a = a.b = a.c = 1;     a.d = 0;     a = power1(a, n + 1);     res = 1;     while (l > 0) {       if (k % 2)         res = (res * ((step + m - a.a) % m)) % m;       else         res = (res * a.a) % m;       k /= 2;       l--;     }     if (k > 0)       cout << 0;     else       cout << res % m;     return 0;   }   long long power(long long a, long long b) {     long long res;     if (b == 0) return 1;     res = power(a, b / 2) % m;     res = (res * res) % m;     if (b % 2) res = (res * a) % m;     return res;   }   mat mult(mat a, mat b) {     mat res;     res.a = (a.a * b.a + a.b * b.c) % m;     res.b = (a.a * b.b + a.b * b.d) % m;     res.c = (a.c * b.a + a.d * b.c) % m;     res.d = (a.c * b.b + a.d * b.d) % m;     return res;   }   mat power1(mat a, long long n) {     mat res, a1;     if (n == 0) {       res.a = res.d = 1;       res.b = res.c = 0;       return res;     }     res = power1(a, n / 2);     res = mult(res, res);     if (n % 2) {       a1.a = a1.b = a1.c = 1;       a1.d = 0;       res = mult(res, a1);     }     return res;   }
#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1000000007;   const long long int ll_MAX = 1e15;   long long int fast_expo(long long int x, long long int p) {     if (p == 0)       return 1;     else if (p % 2 == 0) {       long long int t = fast_expo(x, p / 2) % MOD;       return (t * t) % MOD;     } else       return (x * (fast_expo(x, p - 1)) % MOD) % MOD;   }   long long int invMod(long long int num) {     return fast_expo(num, MOD - 2) % MOD;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long int t_c;     t_c = 1;     for (long long int t_i = 0; t_i < t_c; ++t_i) {       string str;       cin >> str;       long long int n = str.size();       long long int mini[27][n], maxi[27][n];       memset(mini, -1, sizeof(mini));       memset(maxi, -1, sizeof(maxi));       for (long long int k = 1; k < 27; ++k) {         long long int cnt[26]{}, num = 0, j = 0;         for (long long int i = 0; i < n; ++i) {           while (j < n && num != k) {             long long int pos = str[j] -  a ;             if (cnt[pos] == 0) {               num++;             }             cnt[pos]++;             j++;           }           if (num == k) mini[k][i] = j - 1;           long long int pos = str[i] -  a ;           if (cnt[pos] == 1) {             num--;           }           cnt[pos]--;         }       }       for (long long int k = 1; k < 27; ++k) {         long long int cnt[26]{}, num = 0, j = 0;         for (long long int i = 0; i < n; ++i) {           while (j < n && num != k + 1) {             long long int pos = str[j] -  a ;             if (cnt[pos] == 0) {               num++;             }             cnt[pos]++;             j++;           }           if (num == k + 1)             maxi[k][i] = j - 2;           else if (num == k)             maxi[k][i] = n - 1;           long long int pos = str[i] -  a ;           if (cnt[pos] == 1) {             num--;           }           cnt[pos]--;         }       }       long long int ans[27]{}, num = 0;       for (long long int k = 1; k < 27; ++k) {         for (long long int i = 0; i < n; ++i) {           if (mini[k][i] != -1) ans[k] += maxi[k][i] - mini[k][i] + 1;         }         if (ans[k] > 0) {           num = k;         }       }       cout << num << endl;       for (long long int i = 1; i <= num; ++i) cout << ans[i] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, tot, arr[105], sum[100050][105], a[100050], s[105];   bool Solved(int x, int y) {     for (int i = 1; i <= tot; ++i)       if (sum[y][i] - sum[x - 1][i] < s[i]) return false;     return true;   }   int main() {     long long ans;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     int t = k;     for (int i = 2; i * i <= t; ++i)       if (t % i == 0) {         arr[++tot] = i;         for (; t % i == 0; t /= i) s[tot]++;       }     if (t > 1) arr[++tot] = t, s[tot] = 1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= tot; ++j)         for (; a[i] % arr[j] == 0; a[i] /= arr[j]) sum[i][j]++;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= tot; ++j) sum[i][j] += sum[i - 1][j];     ans = 0;     for (int i = 1, L, R, mid; i <= n; ++i) {       L = i - 1;       R = n + 1;       while (L + 1 < R) {         mid = (L + R) / 2;         if (Solved(i, mid))           R = mid;         else           L = mid;       }       ans += n - R + 1;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, m, x, y, it;     int64_t r, min, max, ser, k;     std::cin >> n >> m >> k >> x >> y;     std::vector<std::vector<int64_t>> serg(n);     if (n == 1) {       min = k / m;       max = min + (k % m != 0);       ser = min + (k % m - y >= 0);     } else {       r = k % (2 * m * (n - 1));       min = k / (2 * m * (n - 1));       for (int i = 0; i < n; i++) {         serg[i].resize(m);       }       for (int i = 0; i < m; i++) {         serg[0][i] = min;         serg[n - 1][i] = min;       }       for (int i = 1; i < n - 1; i++) {         for (int j = 0; j < m; j++) {           serg[i][j] = 2 * min;         }       }       for (int i = 0; r != 0; i++) {         if (i >= n) {           it = 2 * n - i - 2;         } else {           it = i;         }         for (int j = 0; r != 0 && j < m; j++) {           serg[it][j]++;           r--;         }       }       ser = serg[x - 1][y - 1];       min += 4;       max = 0;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           max = std::max(max, serg[i][j]);           min = std::min(min, serg[i][j]);         }       }     }     std::cout << max <<     << min <<     << ser;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     int n = s.size();     vector<int> seq(n + 1, 0);     vector<int> ind;     for (int i = 1; i <= n; i++) {       if (s[i - 1] ==  )  || s[i - 1] ==  # ) {         seq[i] = seq[i - 1] - 1;         if (s[i - 1] ==  # ) {           ind.push_back(i - 1);         }       } else {         seq[i] = seq[i - 1] + 1;       }       if (seq[i] < 0) {         cout << -1 << endl;         return 0;       }     }     if (seq[n] == 0) {       for (int i = 0; i < ind.size(); i++) {         cout << 1 << endl;       }     } else {       seq[ind[ind.size() - 1]] -= seq[n];       int val = seq[n];       for (int i = ind[ind.size() - 1] + 1; i <= n; i++) {         if (s[i - 1] ==  )  || s[i - 1] ==  # ) {           seq[i] = seq[i - 1] - 1;         } else {           seq[i] = seq[i - 1] + 1;         }         if (seq[i] < 0) {           cout << -1 << endl;           return 0;         }       }       for (int i = 0; i < (int)ind.size() - 1; i++) {         cout << 1 << endl;       }       cout << 1 + val << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   bool vis[maxn];   int n, m, k, p[maxn], a[maxn];   char s[maxn][maxn];   void func(int x, int y) {     --m;     if (x <= y) {       s[m][x] = s[m][y] =  / ;     } else {       s[m][x] = s[m][y] =     ;     }   }   int main() {     scanf( %d , &n);     m = n + 1, k = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &p[i]);       for (int j = 1; j <= n; j++) {         s[i][j] =  . ;       }     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         int c = 0, v = i;         while (!vis[v]) {           a[++c] = v;           vis[v] = 1;           v = p[v];         }         if (c <= 1) continue;         reverse(a + 1, a + c + 1);         if (k == 0) {           func(a[1], a[1]);           k = a[c];           for (int i = 2; i <= c; i++) {             func(a[i], a[i - 1]);           }           continue;         }         for (int i = 1; i <= c; i++) {           if (k <= a[i] && a[i] <= a[i == c ? 1 : i + 1]) {             func(a[i == c ? 1 : i + 1], a[i]);             s[m][k] =     ;             for (int j = i + 2; j <= c; j++) {               func(a[j], a[j - 1]);             }             for (int j = (i == c) + 1; j < i; j++) {               func(a[j], a[j == 1 ? c : j - 1]);             }             func(k, a[i == 1 ? c : i - 1]);             break;           } else if (k >= a[i] && a[i] >= a[i == c ? 1 : i + 1]) {             func(a[i == c ? 1 : i + 1], a[i]);             s[m][k] =  / ;             for (int j = i + 2; j <= c; j++) {               func(a[j], a[j - 1]);             }             for (int j = 1; j < i; j++) {               func(a[j], a[j == 1 ? c : j - 1]);             }             func(k, a[i == 1 ? c : i - 1]);             break;           }         }       }     }     printf( %d n , k == 0 ? n : n - 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         putchar(s[i][j]);       }       putchar(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int size(long long a) {     int c = 0;     while (a != 0) {       c++;       a /= 10;     }     return c;   }   int main() {     long long a, b, c;     cin >> a >> b;     long long s = size(b), ns = 1;     for (int i = 1; i <= s; i++) ns *= 10;     s = ns;     while (b != 0) {       long long f = b % 10;       b /= 10;       s /= 10;       a += s * f;     }     cout << a;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   const int MOD = 1e9 + 7;   const double PI = 3.141592653589793238460;   long long power(long long a, long long b) {     long long res = 1;     if (a == 0) return 0;     if (a == 1) return 1;     for (; b > 0; b >>= 1) {       if (b & 1) res = (res * a);       if (res > MOD) res %= MOD;       a = (a * a);       if (a > MOD) a = a % MOD;     }     return res;   }   bool isPrime[1000000 + 1];   vector<long long> prime;   void seive(int limit) {     isPrime[0] = isPrime[1] = 1;     for (int i = 2; i * i <= limit; i++) {       if (!isPrime[i])         for (int j = i * i; j <= limit; j += i) {           isPrime[j] = 1;         }     }     for (int i = 2; i <= limit; i++)       if (!isPrime[i]) prime.push_back(i);   }   int ls;   int maxi = 0;   int mark[300000];   int visited[300000];   void dfsl(int x, vector<int> adj[], int n, int p = -1, int mx = 0) {     if (mx > maxi) {       maxi = mx;       ls = x;     }     for (int y : adj[x]) {       if (y == p) continue;       dfsl(y, adj, n, x, mx + 1);     }   }   int parent[300000];   pair<int, int> dfs(int x, vector<int> adj[], int d = 0, int p = -1) {     pair<int, int> res = {d, x};     parent[x] = p;     for (int y : adj[x]) {       if (y == p) continue;       res = max(res, dfs(y, adj, d + 1, x));     }     return res;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0);     int tt;     tt = 1;     while (tt--) {       int n;       cin >> n;       vector<int> adj[n + 1];       for (int i = 0; i < n - 1; i++) {         int u, v;         cin >> u >> v;         adj[v].push_back(u);         adj[u].push_back(v);       }       pair<int, int> da = dfs(1, adj);       pair<int, int> db = dfs(da.second, adj);       int a = da.second;       vector<int> v;       int x = db.second;       int b = x;       while (x != a) {         v.push_back(x);         x = parent[x];       }       v.push_back(a);       if (v.size() == n) {         cout << n - 1 <<   n ;         cout << a <<     << v[1] <<     << b;         return 0;       }       queue<int> q;       for (int i = 0; i < v.size(); i++) q.push(v[i]);       vector<int> d(n + 1, -1);       for (int i = 0; i < v.size(); i++) d[v[i]] = 0;       while (!q.empty()) {         int x = q.front();         q.pop();         for (int y : adj[x]) {           if (d[y] == -1) {             d[y] = d[x] + 1;             q.push(y);           }         }       }       pair<int, int> ans = {0, 0};       for (int i = 1; i <= n; i++) {         ans = max(ans, {d[i], i});       }       cout << (int)v.size() - 1 + ans.first <<   n ;       cout << a <<     << ans.second <<     << b <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, a;     cin >> x >> y;     vector<int> v;     vector<int> v2;     for (int i = 0; i < x; i++) {       cin >> a;       v.push_back(a);     }     for (int i = 0; i < y; i++) {       cin >> a;       v2.push_back(a);     }     int key = 0, ct = 0, ii = 0, jj = 0, sum = 0;     for (; ii < x; ii++) {       key += v[ii];       for (; jj <= y;) {         if (sum > key) {           break;         }         if (sum == key) {           ct++;           key = 0;           sum = 0;           break;         }         sum += v2[jj++];         if (sum == key) {           ct++;           key = 0;           sum = 0;           break;         }         if (sum > key) {           break;         }       }     }     cout << ct << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, t = 0, a, e = 0;   int main() {     cin >> n >> k;     for (long long i = 1; i <= n; i++) {       cin >> a;       if (i > 1 && t - (n - i) * a * (i - e - 1) < k) {         cout << i <<   n ;         e++;       } else         t += a * (i - 1 - e);     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxx = 1e6 + 100;   vector<pair<int, int> > in[maxx], out[maxx];   struct node {     int l;     int r;     long long sum;     long long num;   } p[maxx << 2];   int n, m, k;   void init() {     for (int i = 0; i <= n; i++) {       in[i].clear();       out[i].clear();     }   }   inline void pushup(int cur) {     p[cur].num = p[cur << 1].num + p[cur << 1 | 1].num;     p[cur].sum = p[cur << 1].sum + p[cur << 1 | 1].sum;   }   inline void build(int l, int r, int cur) {     p[cur].l = l;     p[cur].r = r;     p[cur].num = 0;     p[cur].sum = 0;     if (l == r) return;     int mid = l + r >> 1;     build(l, mid, cur << 1);     build(mid + 1, r, cur << 1 | 1);   }   inline void update(int num, int pos, int cur) {     int L = p[cur].l;     int R = p[cur].r;     if (L == R) {       p[cur].num += num;       p[cur].sum += (long long)num * (long long)L;       return;     }     int mid = L + R >> 1;     if (pos <= mid)       update(num, pos, cur << 1);     else       update(num, pos, cur << 1 | 1);     pushup(cur);   }   inline long long query(int num, int cur) {     int L = p[cur].l;     int R = p[cur].r;     if (L == R) return (long long)L * min(p[cur].num, (long long)num);     if (num <= p[cur << 1].num)       return query(num, cur << 1);     else       return (p[cur << 1].sum + query(num - p[cur << 1].num, cur << 1 | 1));   }   int main() {     int l, r, x, y;     while (~scanf( %d%d%d , &n, &k, &m)) {       init();       for (int i = 1; i <= m; i++) {         scanf( %d%d%d%d , &l, &r, &x, &y);         in[l].push_back(make_pair(x, y));         out[r].push_back(make_pair(x, y));       }       long long ans = 0;       build(1, maxx, 1);       for (int i = 1; i <= n; i++) {         for (int j = 0; j < in[i].size(); j++)           update(in[i][j].first, in[i][j].second, 1);         ans += query(k, 1);         for (int j = 0; j < out[i].size(); j++)           update(-out[i][j].first, out[i][j].second, 1);       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       if (n == 1 || n == 2)         printf( 1 n );       else if (n == 3)         printf( 2 n );       else {         if (n % 2 == 1)           printf( %d n , (n - 1) / 2 + 1);         else           printf( %d n , n / 2);       }     }     return 0;   }