malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int n, k;   vector<vector<long long>> adj, leaf;   vector<long long> level, par, out;   vector<long long> counti;   void dfs(int src, int lvl, int p = -1) {     int child = 0;     level[src] = lvl;     for (auto it : adj[src]) {       if (it == p) continue;       par[it] = src;       child++;       dfs(it, lvl + 1, src);     }     if (!child) leaf.push_back({src, lvl, 0});     out[src] = child;   }   class comp {    public:     bool operator()(vector<long long> a, vector<long long> b) {       return a[1] - a[2] < b[1] - b[2];     }   };   void solve() {     cin >> n >> k;     adj.resize(n);     for (int i = 0, x, y; i < n - 1; i++) {       cin >> x >> y;       x--, y--;       adj[x].push_back(y);       adj[y].push_back(x);     }     level.resize(n);     par.resize(n);     out.resize(n);     dfs(0, 0);     vector<long long> total(n, 0);     priority_queue<vector<long long>, vector<vector<long long>>, comp> pq;     for (auto it : leaf) pq.push(it);     long long ans = 0;     while (!pq.empty() && k--) {       auto f = pq.top();       pq.pop();       long long node = f[0];       ans += f[1] - total[node];       if (node) {         total[par[node]] += f[2] + 1;         --out[par[node]];         if (!out[par[node]]) {           pq.push({par[node], f[1] - 1, total[par[node]]});         }       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 77, L = 17;   int n, q, tim = 1, root = 1;   int b[N], d[N], Par[N][L], St[N], En[N];   long long S[N << 2], lz[N << 2];   inline void Shift(int l, int r, int id) {     long long x = lz[id];     lz[id] = 0;     S[id] += (r - l) * x;     if (r - l > 1) lz[id << 1] += x, lz[id << 1 ^ 1] += x;   }   void Add(int ql, int qr, int x, int l = 1, int r = n + 1, int id = 1) {     Shift(l, r, id);     if (qr <= l || r <= ql) return;     if (ql <= l && r <= qr) {       lz[id] += x;       Shift(l, r, id);       return;     }     int mid = ((l + r) >> 1);     Add(ql, qr, x, l, mid, id << 1);     Add(ql, qr, x, mid, r, id << 1 ^ 1);     S[id] = S[id << 1] + S[id << 1 ^ 1];   }   long long Get(int ql, int qr, int l = 1, int r = n + 1, int id = 1) {     Shift(l, r, id);     if (qr <= l || r <= ql) return 0;     if (ql <= l && r <= qr) return S[id];     int mid = ((l + r) >> 1);     return Get(ql, qr, l, mid, id << 1) + Get(ql, qr, mid, r, id << 1 ^ 1);   }   vector<int> a[N];   void dfs(int v = 1) {     St[v] = tim++;     for (int i = 1; i < L; ++i) Par[v][i] = Par[Par[v][i - 1]][i - 1];     for (int u : a[v]) {       if (u == Par[v][0]) continue;       Par[u][0] = v;       d[u] = d[v] + 1;       dfs(u);     }     En[v] = tim;   }   inline bool isPar(int v, int u) { return St[v] <= St[u] && En[u] <= En[v]; }   inline int GetPar(int v, int k) {     for (int i = 0; i < L; ++i)       if (k & (1 << i)) v = Par[v][i];     return v;   }   inline int GetLca(int v, int u) {     if (d[v] < d[u]) swap(v, u);     v = GetPar(v, d[v] - d[u]);     if (v == u) return v;     for (int i = L - 1; i >= 0; --i)       if (Par[v][i] != Par[u][i]) v = Par[v][i], u = Par[u][i];     return Par[v][0];   }   inline int GetLca(int r, int v, int u) {     int t = GetLca(v, u);     if (isPar(r, t)) return t;     if (isPar(r, u) || isPar(r, v)) return r;     if (!isPar(t, r)) return t;     int x = GetLca(r, v);     int y = GetLca(r, u);     return (d[x] > d[y] ? x : y);   }   inline void Update(int r, int v, int u, int x) {     int t = GetLca(r, v, u);     if ((!isPar(t, r))) {       Add(St[t], En[t], x);       return;     }     Add(1, n + 1, x);     if (r == t) return;     int p = GetPar(r, d[r] - d[t] - 1);     Add(St[p], En[p], -x);   }   inline long long GetSum(int r, int v) {     if (!isPar(v, r)) return Get(St[v], En[v]);     long long res = Get(1, n + 1);     if (r == v) return res;     int p = GetPar(r, d[r] - d[v] - 1);     res -= Get(St[p], En[p]);     return res;   }   int main() {     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) scanf( %d , b + i);     for (int v, u, i = 1; i < n; ++i)       scanf( %d %d , &v, &u), a[v].push_back(u), a[u].push_back(v);     d[1] = 1;     dfs();     for (int i = 1; i <= n; ++i) Add(St[i], St[i] + 1, b[i]);     while (q--) {       int tp, v, u, x;       scanf( %d %d , &tp, &v);       if (tp == 1) {         root = v;       } else if (tp == 2) {         scanf( %d %d , &u, &x);         Update(root, v, u, x);       } else {         printf( %lld n , GetSum(root, v));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int Pow(long long int a, long long int b, long long int md,                     long long int ans = 1) {     for (; b; b >>= 1, a = a * a % md)       if (b & 1) ans = ans * a % md;     return ans % md;   }   const long long int MAXK = 1e6 + 10;   const long long int MAXN = 2e3 + 10;   const long long int INF = 8e18;   const long long int MOD = 1e9 + 7;   long long int dp[MAXN][MAXN], fact[MAXK], inv[MAXK], ans = 0, n, m, k;   long long int add(long long int x, long long int y) { return (x + y) % MOD; }   long long int mul(long long int x, long long int y) { return x * y % MOD; }   long long int C(long long int x, long long int y) {     if (y > x) return 0;     return fact[x] * inv[y] % MOD * inv[x - y] % MOD;   }   int main() {     scanf( %lld%lld%lld , &n, &m, &k);     if (m == 1) return !printf( %lld n , Pow(k, n, MOD));     fact[0] = inv[0] = 1;     for (long long int i = 1; i < MAXK; i++) {       fact[i] = fact[i - 1] * i % MOD;       inv[i] = Pow(fact[i], MOD - 2, MOD);     }     dp[0][0] = 1;     for (long long int i = 1; i <= n; i++) {       for (long long int j = 1; j <= i; j++) {         dp[i][j] = j * (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD;       }     }     for (long long int i = 1; i <= n; i++) {       for (long long int j = 0; j <= i; j++) {         ans = add(ans, mul(Pow(j, n * (m - 2) % MOD, MOD),                            mul(dp[n][i] * dp[n][i] % MOD,                                mul(C(k - i, i - j), mul(C(k, i), C(i, j))))));       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   #define pb push_back   #define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)   using namespace std;   typedef long long ll;   typedef long double ld;   typedef vector <ll> vll;   typedef pair<ll,ll> pll;   typedef vector <pair<ll,ll>> vpll;   typedef vector <vll> vvll;      const ll N=1e5+1;   ll t,n,k;      int main()   {       speed;       cin>>t;       while(t--)       {           cin>>n>>k;           vll ans;           cout<<n-(k+1)/2<<  n ;           for(int i=(k+1)/2;i<=n;i++)               if(i!=k)                   cout<<i<<   ;           cout<<  n ;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[200010];   long long vis[200100];   long long x, n, k, sum, y;   string s;   long long dfs(long long x, long long k, long long y) {     if (x == y) return k;     return dfs(a[x], k + 1, y);   }   long long poww(long long ba) {     if (ba == 0) return 1;     if (ba == 1) return 3;     if (ba % 2 == 0) return poww(ba / 2) * poww(ba / 2);     return poww(ba / 2) * poww(ba / 2) * 3;   }   int main() {     long long q;     cin >> q;     while (q--) {       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       for (int i = 1; i <= n; i++) {         cout << dfs(a[i], 0, i) + 1 <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010;   string s[maxn];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> s[i];     int ans = 6, cnt;     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         cnt = 0;         for (int k = 0; k < 6; k++)           if (s[i][k] != s[j][k]) cnt++;         ans = min(ans, (cnt + 1) / 2 - 1);       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     int t;     long long s, a, b, c;     scanf( %d , &t);     while (t--) {       scanf( %I64d %I64d %I64d %I64d , &s, &a, &b, &c);       printf( %I64d n , s / c + s / c / a * b);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int i, j;     cin >> s;     for (i = 0; i < s.size(); i++)       if (s[i] ==  . ) break;     if (s[i - 1] ==  9 ) {       cout <<  GOTO Vasilisa. ;       return 0;     }     if (s[i + 1] >=  5 ) s[i - 1]++;     for (j = 0; j < i; j++) cout << s[j];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, k, n2;     long long dip, cert, losers;     cin >> n >> k;     dip = n / (2 * (k + 1));     cert = k * dip;     losers = n - dip - cert;     cout << dip <<     << cert <<     << losers;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int a[25], b[25];   int dp[1 << 24];   int main() {     int n, k;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     scanf( %d , &k);     for (int i = 1; i <= k; i++) scanf( %d , &b[i]);     dp[0] = 1;     for (int i = 1; i < (1 << n); i++) {       int sum = 0, ans = 0;       for (int j = 0; j < n; j++) {         if ((i >> j) & 1) {           sum += a[j];           ans = (ans + dp[i ^ (1 << j)]) % mod;           if (sum > 1e9) sum = 1e9 + 1;         }       }       bool ok = 1;       for (int j = 1; j <= k; j++) {         if (sum == b[j]) ok = 0;       }       if (!ok) continue;       dp[i] = (dp[i] + ans) % mod;     }     printf( %d n , dp[(1 << n) - 1]);   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long x, long long y) {     if (x == 0) return y;     return gcd(y % x, x);   }   int main() {     int t;     long long r, b, k;     scanf( %d , &t);     while (t--) {       scanf( %I64d%I64d%I64d , &r, &b, &k);       long long Gcd = gcd(r, b);       r /= Gcd;       b /= Gcd;       if (r <= (k - 1) * b + 1 && b <= (k - 1) * r + 1)         printf( OBEY n );       else         printf( REBEL n );     }   }
#include <bits/stdc++.h>   using namespace std;   long long a[1000000], prime[1000000];   vector<long long> v[3000000];   int main() {     ios::sync_with_stdio(false);     cin.tie(), cout.tie();     long long e;     for (long long i = 3; i * i <= 1000000; i += 2)       if (a[i] == 0)         for (long long j = i * i; j <= 1000000; j = j + (i * 2)) a[j] = 1;     prime[0] = 2;     for (long long i = 3, j = 1; i <= 1000000; i += 2)       if (a[i] == 0) {         prime[j++] = i;         e = j;       }     long long n;     cin >> n;     long long b, c = 0, d = 1, f = 0, g = 0, h;     for (long long i = 0; i < n; i++) {       cin >> b;       for (long long j = 0;; j++) {         c = 0;         if (prime[j] > sqrt(b)) {           break;         }         if (b % prime[j] == 0) {           while (b % prime[j] == 0) {             c++;             b /= prime[j];           }         }         v[prime[j]].push_back(c);         g = max(g, prime[j]);       }       if (b > 1) {         g = max(g, b);         v[b].push_back(1);       }     }     for (long long i = 0; i < 210000; i++) {       long long y = prime[i];       if (v[y].size() == n - 1) {         sort(v[y].begin(), v[y].end());         long long x = v[y][0];         d = d * (pow(y, x));       }       if (v[y].size() == n) {         sort(v[y].begin(), v[y].end());         long long x = v[y][1];         d = d * (pow(y, x));       }     }     cout << d << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       long long n, k, s;       cin >> n >> k >> s;       if (s >= k && s <= k * (n - 1)) {         long long step = s / k;         long long x = s - k * step;         long long last = 1, fact = 1;         cout <<  YES  << endl;         for (int i = 0; i < k; i++) {           long long tmp = step;           if (x) {             tmp++;             x--;           }           last += tmp * fact;           cout << last <<    ;           fact *= -1;         }         cout << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5023;   int grundy[N], canCut[N], mex[N];   void init() {     memset(grundy, 0, sizeof(grundy));     memset(canCut, 0, sizeof(canCut));     for (int i = 1; i < N; i++) {       for (int j = 0; j < i; j++) {         mex[grundy[max(0, j - 1)] ^ grundy[max(0, i - j - 2)]] = i;       }       for (int j = 0;; j++) {         if (mex[j] != i) {           grundy[i] = j;           break;         }       }     }   }   bool firstWin(int x, int n) {     int sg = 0, len = 0;     for (int i = 0; i < n; i++) {       if (canCut[i] && abs(x - i) > 1) {         len++;       } else {         sg ^= grundy[len];         len = 0;       }     }     return sg == 0;   }   int main() {     string s;     while (cin >> s) {       init();       int n = s.size();       for (int i = 1; i + 1 < n; i++) {         if (s[i - 1] == s[i + 1]) canCut[i] = 1;       }       int ans = -1;       for (int i = 0; i < n; i++) {         if (canCut[i] && firstWin(i, n)) {           ans = i + 1;           break;         }       }       if (ans == -1)         printf( Second n );       else         printf( First n%d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     map<int, int> mp;     vector<int> ans;     int a;     for (int i = 0; i < n; i++) {       scanf( %d , &a);       mp[a] = 1;     }     n = m;     for (int i = 1; i <= n; i++) {       if (i > m) break;       if (mp[i] == 0) {         if (i <= m) {           ans.push_back(i);           m -= i;         }       }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int INF = 1000000000;   const ll LINF = 1001002003004005006ll;   int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};   template <class T>   bool chmax(T &a, const T &b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   bool chmin(T &a, const T &b) {     if (b < a) {       a = b;       return true;     }     return false;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (int i = 0; i < (int)v.size(); i++)       os << v[i] << (i + 1 == (int)v.size() ?    :    );     return os;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &v) {     for (T &x : v) is >> x;     return is;   }   signed main() {     int n;     cin >> n;     vector<int> bx;     for (int i = 0; i < (n); i++) {       cout <<  ?   << 0 <<     << i << endl;       int x;       cin >> x;       bx.push_back(x);     }     vector<int> px;     for (int i = 0; i < (n); i++) {       cout <<  ?   << i <<     << 0 << endl;       int x;       cin >> x;       px.push_back(x);     }     int cnt = 0;     vector<int> print_res;     for (int i = 0; i < (n); i++) {       bool f = true;       vector<int> b;       for (auto x : bx) b.push_back(x ^ i);       auto v = b;       sort(begin(v), end(v));       for (int j = 0; j < (n); j++)         if (v[j] != j) f = false;       if (!f) continue;       vector<int> p;       for (auto x : px) p.push_back(x ^ b[0]);       for (int j = 0; j < (n); j++)         if (p[b[j]] != j) f = false;       if (!f) continue;       cnt++;       print_res = p;     }     cout <<  !  << endl;     cout << cnt << endl;     cout << print_res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > h;   vector<vector<long long> > g;   long long p[500001][21];   long long volume[500001];   long long n, dt, ans, s, f;   void dfs(long long v, long long m) {     p[v][0] = m;     for (long long i = 0; i < g[v].size(); i++) {       long long k = g[v][i];       if (k != m) {         volume[k] = volume[v] + 1;         dfs(k, v);       }     }   }   void prepare() {     for (long long i = 0; i <= 500000; i++)       for (long long k = 1; k <= 20; k++) p[i][k] = -1;     dfs(1, -1);     for (long long k = 1; k <= 20; k++)       for (long long i = 1; i <= 500000; i++)         if (p[i][k - 1] != -1) p[i][k] = p[p[i][k - 1]][k - 1];     ans = 1;     cout << ans <<    ;     s = 1;     f = 2;   }   long long kanst(long long v, long long k) {     while (k > 0) {       long long k0 = log2(k);       v = p[v][k0];       k -= 1 << k0;     }     return v;   }   long long lca(long long a, long long b) {     if (a == b) return a;     if (volume[b] > volume[a]) swap(a, b);     a = kanst(a, volume[a] - volume[b]);     if (a == b) return a;     for (long long j = 20; j >= 0; j--) {       if (p[a][j] != p[b][j]) {         a = p[a][j];         b = p[b][j];       }     }     return p[a][0];   }   int main() {     cin >> n;     g.resize(200001);     vector<long long> d;     d.resize(n + 1);     for (long long i = 2; i <= n; i++) {       long long a;       cin >> a;       d[i] = i;       g[i].push_back(a);       g[a].push_back(i);     }     prepare();     for (long long k = 3; k <= n; k++) {       if (volume[s] + volume[k] - 2 * volume[lca(s, k)] > ans) {         ans = volume[s] + volume[k] - 2 * volume[lca(s, k)];         f = k;       }       if (volume[f] + volume[k] - 2 * volume[lca(f, k)] > ans) {         ans = volume[f] + volume[k] - 2 * volume[lca(f, k)];         s = k;       }       cout << ans <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   char s[5005], t[5005];   long long DP[5005][5005];   int main(int argc, char** argv) {     scanf( %s%s , s, t);     int lens = strlen(s);     int lent = strlen(t);     long long ans = 0;     for (int i = 1; i <= lens; i++) {       for (int j = 1; j <= lent; j++) {         DP[i][j] = DP[i][j - 1];         if (s[i - 1] == t[j - 1])           DP[i][j] = (DP[i][j] + DP[i - 1][j - 1] + 1) % mod;       }       ans = (ans + DP[i][lent]) % mod;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using ::std::cin;   using ::std::cout;   using ::std::endl;   using ::std::multiset;   using ::std::pair;   using ::std::vector;   int main() {     ::std::ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     vector<pair<int, int>> tr;     for (int i = 0; i < m; ++i) {       int t, r;       cin >> t >> r;       while (!tr.empty() && tr.back().second <= r) tr.pop_back();       tr.push_back({t, r});     }     tr.push_back({-1, 0});     multiset<int> s;     for (int i = 0; i < tr[0].second; ++i) s.insert(a[i]);     for (int i = 0; i < static_cast<int>(tr.size()); ++i) {       for (int j = tr[i].second - 1; j >= tr[i + 1].second; --j) {         if (tr[i].first == 2) {           a[j] = *s.begin();           s.erase(s.begin());         } else {           a[j] = *--s.end();           s.erase(--s.end());         }       }     }     for (int x : a) cout << x <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   int ans[N];   void solve() {     int n;     cin >> n;     int d = 0;     for (int i = 0; i <= 30; i++) {       ans[i] = (1 << i);     }     int sum = 0;     for (int i = 0; i <= 30; i++) {       sum += ans[i];       if (sum >= n) {         d = i;         break;       }     }     int extra = sum - n;     for (int i = d; i >= 1; i--) {       long long need = (ans[i] - ans[i - 1] + 0ll) * (d - i + 1ll);       if (extra > need) {         extra -= need;         for (int j = d; j >= i; j--) ans[j] = ans[i - 1];       } else {         int dv = (extra / (d - i + 1));         int c = (extra % (d - i + 1));         for (int j = d; j >= i; j--) ans[j] -= dv;         for (int j = i; j < i + c; j++) ans[j]--;         break;       }     }     cout << d <<   n ;     for (int i = 1; i <= d; i++) cout << ans[i] - ans[i - 1] <<    ;     cout <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tt;     cin >> tt;     while (tt--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   const double PI = 3.1415926535897932384626;   const int KL = 1e6;   const long long MOD = 1e9 + 7;   using namespace std;   void show(complex<double> a) {     cout << fixed << setprecision(10) << a.real() <<   +   << a.imag() <<  i           << endl;   }   complex<double> rot(complex<double> a, complex<double> o, double alpha) {     return (a - o) * polar(1.0, alpha * PI / 180.0) + o;   }   complex<double> intersection(complex<double> a, complex<double> b, double h) {     double m = (a.imag() - b.imag()) / (a.real() - b.real());     double p = a.imag() - m * a.real();     complex<double> ret((h - p) / m, h);     return ret;   }   double h, w;   double alpha;   int main() {     cin >> w >> h >> alpha;     if (w < h) swap(h, w);     if (alpha >= 90) alpha = 180 - alpha;     double wt = sqrt(w * w + h * h);     if (alpha == 0) {       cout << fixed << setprecision(10) << w * h << endl;       return 0;     }     complex<double> a(-w / 2.0, h / 2.0);     complex<double> b(w / 2.0, h / 2.0);     complex<double> d(w / 2.0, -h / 2.0);     complex<double> o(0, 0);     double mx_angle = 2 * abs(arg(b)) * 180.0 / PI;     if (alpha >= mx_angle) {       cout << fixed << setprecision(10) << h * h / sin(alpha * PI / 180.0)            << endl;       return 0;     }     complex<double> a1 = rot(a, o, alpha);     complex<double> b1 = rot(b, o, alpha);     complex<double> c1 = rot(d, o, alpha);     complex<double> A = intersection(a1, b1, h / 2);     complex<double> B = intersection(b1, c1, h / 2);     double d1 = A.real() - a.real();     double d2 = b.real() - B.real();     double wt1 = d1 / cos(alpha * PI / 180.0);     double wt2 = d2 / sin(alpha * PI / 180.0);     double area =         d1 * wt1 * sin(alpha * PI / 180.0) + d2 * wt2 * cos(alpha * PI / 180.0);     double ans = w * h - area;     cout << fixed << setprecision(10) << ans << endl;     return 0;   }