malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 4010;   const long long mod = 1e9 + 9;   long long f[maxn * 2][maxn], n, w, b;   long long num[maxn * 2], inv[maxn * 2];   long long pp(long long a, long long b) {     if (!b) return 1;     long long cnt = pp(a, b / 2);     cnt = 1ll * cnt * cnt % mod;     if (b & 1) cnt = 1ll * cnt * a % mod;     return cnt;   }   long long calc(long long n, long long m) {     if (n < m) return 0;     return 1ll * num[n] * num[n - 1] % mod * inv[m - 1] % mod * inv[n - m] % mod;   }   int main() {     cin >> n >> w >> b;     f[1][1] = 1;     for (long long i = 2; i <= w + b; ++i)       for (long long j = 1; j <= min(n, i); ++j)         f[i][j] = (1ll * f[i - 1][j] * j + f[i - 1][j - 1]) % mod;     num[0] = 1;     for (long long i = 1; i <= w + b; ++i) num[i] = 1ll * num[i - 1] * i % mod;     inv[w + b] = pp(num[w + b], mod - 2);     for (long long i = w + b - 1; i >= 0; --i)       inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;     long long ans = 0;     for (long long i = 1; i < n - 1; ++i) {       long long cur = n - i - 1;       cur = 1ll * cur * calc(w, n - i) % mod * calc(b, i) % mod;       (ans += cur) %= mod;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 123456;   int s[N];   int main() {     int n, k, ans = 0;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , s + i);     }     for (int i = 0; i < n - k; ++i) {       ans = max(ans, s[i] + s[(n - k) * 2 - i - 1]);     }     printf( %d n , max(ans, s[n - 1]));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Edge {     int from, to;     long long capacity, cost;     Edge(int from, int to, long long capacity, long long cost) {       this->from = from;       this->to = to;       this->capacity = capacity;       this->cost = cost;     }   };   vector<vector<int>> adj;   vector<vector<long long>> cost, capacity;   const long long INF = 1e9;   void shortest_paths(int n, int v0, vector<long long>& d, vector<int>& p) {     d.assign(n, INF);     d[v0] = 0;     vector<int> m(n, 2);     deque<int> q;     q.push_back(v0);     p.assign(n, -1);     while (!q.empty()) {       int u = q.front();       q.pop_front();       m[u] = 0;       for (int v : adj[u]) {         if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) {           d[v] = d[u] + cost[u][v];           p[v] = u;           if (m[v] == 2) {             m[v] = 1;             q.push_back(v);           } else if (m[v] == 0) {             m[v] = 1;             q.push_front(v);           }         }       }     }   }   long long min_cost_flow(int N, vector<Edge> edges, long long K, int s, int t) {     adj.assign(N, vector<int>());     cost.assign(N, vector<long long>(N, 0));     capacity.assign(N, vector<long long>(N, 0));     for (Edge e : edges) {       adj[e.from].push_back(e.to);       adj[e.to].push_back(e.from);       cost[e.from][e.to] = e.cost;       cost[e.to][e.from] = -e.cost;       capacity[e.from][e.to] = e.capacity;     }     long long flow = 0;     long long cost = 0;     vector<int> p;     vector<long long> d;     while (flow < K) {       shortest_paths(N, s, d, p);       if (d[t] == INF) break;       long long f = K - flow;       int cur = t;       while (cur != s) {         f = min(f, capacity[p[cur]][cur]);         cur = p[cur];       }       flow += f;       cost += f * d[t];       cur = t;       while (cur != s) {         capacity[p[cur]][cur] -= f;         capacity[cur][p[cur]] += f;         cur = p[cur];       }     }     if (flow < K)       return -1;     else       return cost;   }   string s, x[105];   int y, val[256], n, temp[256];   vector<Edge> v;   int main() {     cin >> s;     for (int i = 0; i < s.size(); i++) {       val[s[i] -  a ]++;     }     scanf( %d , &n);     for (int i = 0; i < 26; i++) {       v.push_back(Edge(n + 1 + i, n + 1 + 26, val[i], 0));     }     for (int i = 1; i <= n; i++) {       cin >> x[i] >> y;       v.push_back(Edge(0, i, y, i));       for (int j = 0; j < x[i].size(); j++) {         temp[x[i][j] -  a ]++;       }       for (int j = 0; j < 26; j++) {         v.push_back(Edge(i, n + 1 + j, temp[j], 0));         temp[j] = 0;       }     }     printf( %lld n , min_cost_flow(n + 26 + 2, v, s.size(), 0, n + 26 + 1));   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using std::cerr;   using std::cin;   using std::cos;   using std::cout;   using std::endl;   using std::make_pair;   using std::max;   using std::min;   using std::next_permutation;   using std::pair;   using std::priority_queue;   using std::queue;   using std::random_shuffle;   using std::set;   using std::sin;   using std::sort;   using std::sqrt;   using std::stack;   using std::string;   using std::stringstream;   using std::swap;   using std::vector;   const long double PI = 3.14159265358979323846;   int main() {     int n, d, s;     long long sum = 0;     int add = 0;     cin >> n >> d >> s;     vector<pair<int, int> > can, ne;     for (int i = 0; i < n; ++i) {       int c, l, f;       cin >> c >> f >> l;       sum += c;       pair<int, int> p(f, c);       if (l < d) {         ne.push_back(p);         if (c) ++add;       } else         can.push_back(p);     }     sort(can.begin(), can.end());     int mr = 0;     long long fuel = 0;     while ((mr < can.size()) && (fuel + can[mr].first <= s)) {       fuel += can[mr].first;       ++mr;     }     vector<pair<int, int> > cacan, necan;     for (int i = 0; i < can.size(); ++i) {       if (can[i].second)         cacan.push_back(can[i]);       else         necan.push_back(can[i]);     }     vector<long long> cacans(cacan.size());     for (int i = 0; i < cacan.size(); ++i) {       if (i)         cacans[i] = cacans[i - 1] + cacan[i].first;       else         cacans[i] = cacan[i].first;     }     for (int k = 1; k <= cacan.size(); ++k) {       long long free = sum - (int)(cacan.size()) + k - add;       long long fu = cacans[k - 1];       if (fu > s) continue;       int num = necan.size() + ne.size() - add;       if (num <= free) {         if (mr < n) {           mr = n;           fuel = fu;         } else if (mr == n) {           if (fuel > fu) fuel = fu;         }         continue;       }       num -= free;       int mmr = n - num;       for (int i = 0; i < min(num, (int)necan.size()); ++i) {         if (fu + necan[i].first <= s) {           ++mmr;           fu += necan[i].first;         } else           break;       }       if (mmr > mr) {         mr = mmr;         fuel = fu;       } else if (mmr == mr)         if (fuel > fu) fuel = fu;     }     cout << mr <<     << fuel << endl;     return 0;   }
#include <bits/stdc++.h>   const int maxN = 1000005, p = 998244353;   int n, ans, fac[maxN], inv[maxN];   inline int power(int x, long long y) {     int res = 1;     for (; y; y >>= 1, x = (long long)x * x % p) {       if (y & 1) {         res = (long long)res * x % p;       }     }     return res;   }   inline int binomial(int x, int y) {     return (long long)fac[x] * inv[y] % p * inv[x - y] % p;   }   inline long long sign(int x) { return x & 1 ? -1 : 1; }   int main() {     scanf( %d , &n);     for (int i = fac[0] = 1; i <= n; i++) {       fac[i] = (long long)fac[i - 1] * i % p;     }     inv[n] = power(fac[n], p - 2);     for (int i = n; i; i--) {       inv[i - 1] = (long long)inv[i] * i % p;     }     for (int i = 1; i <= n; i++) {       ans = (ans + sign(i) * binomial(n, i) *                        power(3, (long long)n * (n - i) + i) * 2) %             p;     }     for (int i = 0; i < n; i++) {       ans = (ans + sign(n - i) * binomial(n, i) *                        (power(power(3, i) - 1, n) - power(3, (long long)n * i)) *                        3) %             p;     }     printf( %d n , (p - ans) % p);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   multimap<int, string> MM;   long long int BinSearch(long long int a[], vector<long long int>& tVec,                           long long int x, long long int y, long long int v) {     while (y > x + 1) {       long long int m = x + (y - x) / 2;       if (a[tVec[m]] >= v)         y = m;       else         x = m;     }     return y;   }   const int M = 1e9 + 7;   long long mod(long long x) { return ((x % M + M) % M); }   long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }   long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }   long long int Mod = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, i;     cin >> n;     vector<long long int> res;     while (n > 0) {       long long int num = n, m = 0, p = 1;       while (num > 0) {         if (num % 10 > 0) m = m + p;         p = p * 10;         num = num / 10;       }       res.push_back(m);       n = n - m;     }     cout << res.size() <<   n ;     for (i = 0; i < res.size(); i++) cout << res[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string convertTostr(int x) {     stringstream ss;     ss << x;     string st;     st = ss.str();     return st;   }   unsigned long long convertToint(string y) {     unsigned long long num;     stringstream ss(y);     ss >> num;     return num;   }   unsigned long long fpow(unsigned long long b, unsigned long long p,                           unsigned long long m) {     if (p == 0) return 1;     if (p == 1) return b % m;     unsigned long long res = fpow(b, p / 2, m) % m;     res = (res * res) % m;     if (p % 2 != 0) res = (res * (b % m)) % m;     return res;   }   int main() {     std::cin.tie(0);     std::ios::sync_with_stdio(false);     int n;     cin >> n;     vector<pair<int, int> > vec;     int x, y;     for (int i = 0; i < n; i++) {       cin >> x >> y;       vec.push_back(make_pair(x, y));     }     int cnt = 0;     for (int i = 0; i < n - 2; i++) {       int curX = vec[i].first, curY = vec[i].second;       int nX = vec[i + 1].first, nY = vec[i + 1].second;       int aX = vec[i + 2].first, aY = vec[i + 2].second;       if ((nX > curX && aY > nY) || (nX < curX && aY < nY) ||           (nY > curY && aX < nX) || (nY < curY && aX > nX))         cnt++;     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 30;   const int N = 5e4 + 10;   int y[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , y + i);     }     int low = 1, high = 1e9;     while (low != high) {       int mid = (low + high) / 2;       unordered_set<int> A;       bool found = false;       for (int i = 0; i < n; ++i) {         int cur = y[i];         while (cur > mid) cur >>= 1;         while (cur > 1 && A.find(cur) != A.end()) cur >>= 1;         if (A.find(cur) == A.end()) {           A.insert(cur);         } else {           found = true;           break;         }       }       if (found) {         low = mid + 1;       } else {         high = mid;       }     }     unordered_set<int> A;     for (int i = 0; i < n; ++i) {       int cur = y[i];       while (cur > high) cur >>= 1;       while (cur > 1 && A.find(cur) != A.end()) cur >>= 1;       if (A.find(cur) == A.end()) {         A.insert(cur);         printf( %d%c , cur,    n [i == n - 1]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k = 0, l = 0, n;     int a[1001], b[10001];     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < n; i++) {       cin >> b[i];     }     for (i = 0; i < n; i++) {       k = k | a[i];     }     for (i = 0; i < n; i++) {       l = l | b[i];     }     cout << k + l <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void debug(int a[], int n) {     cout <<  Array: [  ;     for (auto i = 0; i != n; i++) cout << a[i] <<    ;     cout <<  ] n ;   }   void debugv(vector<int>& v) {     cout <<  Vector: [  ;     for (auto i = 0; i != v.size(); i++) cout << v[i] <<    ;     cout <<  ] n ;   }   bool comp(pair<long long int, long long int> a,             pair<long long int, long long int> b) {     if (a.first < b.first || (a.first == b.first && a.second >= b.second))       return true;     return false;   }   long long int k[100000 + 2];   pair<long long int, long long int> power[100000 + 2];   void Solve() {     long long int n;     cin >> n;     long long int ans = 0, temp;     for (auto i = 0; i != n; i++) {       cin >> k[i];       long long int ma = 0;       for (auto j = 0; j != k[i]; j++) {         long long int p;         cin >> p;         ma = max(ma, p + 1 - j);       }       power[i].first = ma, power[i].second = k[i];     }     sort(power, power + n);     temp = ans = power[0].first;     for (auto i = 0; i != n; i++) {       if (temp >= power[i].first) {         temp += power[i].second;       } else {         ans += (power[i].first - temp);         temp = power[i].first + power[i].second;       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     cin >> T;     while (T--) {       Solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const unsigned long long inf = 0x3f3f3f3f3f3f3f3f;   const unsigned long long MAXN = 1e5 + 5;   unsigned long long cnt[MAXN];   vector<unsigned long long> adj[MAXN];   vector<unsigned long long> vis(MAXN, 0);   void dfs(unsigned long long v) {     vis[v] = 1;     if (adj[v].size() == 1 && v != 1) {       cnt[v] = 1;       return;     }     for (auto u : adj[v]) {       if (!vis[u]) {         dfs(u);         cnt[v] += cnt[u];       }     }     ++cnt[v];   }   unsigned long long nc2(unsigned long long n) {     if (n < 2) return 0;     return (n * (n - 1)) / 2;   }   long double nc3(unsigned long long n) {     if (n < 3) return 0;     return ((long double)n * (n - 1) * (n - 2)) / 6.0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     unsigned long long n;     cin >> n;     pair<pair<unsigned long long, unsigned long long>, unsigned long long> cst[n];     for (unsigned long long i = 0; i < n - 1; i++) {       unsigned long long u, v, l;       cin >> u >> v >> l;       adj[v].push_back(u);       adj[u].push_back(v);       cst[i + 1] = {{u, v}, l};     }     dfs(1);     long double ini = 0;     long double wgt[n];     fill(wgt, wgt + n, 0);     long double ttl = nc3(n);     for (unsigned long long i = 1; i < n; i++) {       unsigned long long u = cst[i].first.first, v = cst[i].first.second;       unsigned long long lft = min(cnt[u], cnt[v]);       unsigned long long rgt = n - lft;       wgt[i] += (long double)lft * nc2(rgt) + rgt * nc2(lft);       wgt[i] *= 2;       wgt[i] /= ttl;       ini += wgt[i] * cst[i].second;     }     unsigned long long q;     cin >> q;     cout << fixed << setprecision(12);     while (q--) {       unsigned long long idx, x;       cin >> idx >> x;       ini -= wgt[idx] * (cst[idx].second - x);       cst[idx].second = x;       long double d = ini;       cout << d <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int INF = 1000 * 1000 * 1000 + 7;   const long long LINF = INF * (long long)INF;   const int MOD = 1000 * 1000 * 1000 + 7;   const int MAX = 1000 * 1000 + 47;   long long getN(long long a) {     long long res = a;     while (a) {       res = res * 10 + a % 10;       a /= 10;     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int k, p;     cin >> k >> p;     long long res = 0;     for (int i = (1); i < (k + 1); i++) {       res = (res + getN(i)) % p;     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   int n, u, v, dp[maxn], d[2];   long long f[maxn], ans, sum;   vector<int> a[maxn];   void dfs(int nod, int pa, int x) {     d[x]++;     for (int i : a[nod]) {       if (i != pa) {         dfs(i, nod, x ^ 1);         dp[nod] += dp[i];         f[nod] += (f[i] + dp[i]);       }     }     for (int i : a[nod]) {       if (i != pa) {         ans += (f[nod] - f[i] - dp[i]) * dp[i];       }     }     ans += f[nod];     dp[nod]++;     return;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       a[u].push_back(v);       a[v].push_back(u);     }     dfs(1, 0, 0);     printf( %lld n , (ans + (1ll * d[0] * d[1])) / 2);   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     int a[n];     int b[n];     string s;     cin >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  a ) {         a[i] = 1;         b[i] = 0;       } else {         a[i] = 0;         b[i] = 1;       }       if (i) {         a[i] += a[i - 1];         b[i] += b[i - 1];       }     }     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if ((i == 0 && b[j] == a[j]) ||             (i != 0 && ((b[j] - b[i - 1]) == (a[j] - a[i - 1])))) {           cout << i + 1 <<     << j + 1 <<   n ;           return;         }       }     }     cout <<  -1 -1 n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int N;     cin >> N;     deque<int> D;     for (int i = 1; i <= N; ++i) D.push_back(i);     for (int k = 2; k <= N; ++k) {       int x = D.front();       D.pop_front();       for (int i = k - 1; i < N - 1; i += k) swap(x, D[i]);       D.push_back(x);     }     for (int i = 0; i < N; ++i) cout << D[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   int main() {     int i, cnt1 = 0, l, diff, a, c, fck;     scanf( %d%d , &a, &c);     l = a;     diff = 0;     while (l > 0) {       if (l % 3) diff += (3 - (l % 3)) * pow(3, cnt1);       l /= 3;       cnt1++;     }     cnt1 = 0;     l = 0;     while (c || diff) {       fck = c + diff;       l += fck % 3 * pow(3, cnt1);       cnt1++;       c /= 3;       diff /= 3;     }     printf( %d , l);   }
#include <bits/stdc++.h>   using namespace std;   typedef long long ll;         pair<int, int> intersect(pair<int, int> a, pair<int, int> b)   {       vector<pair<int, int>> v;       v.push_back(a);       v.push_back(b);       sort(v.begin(), v.end());       a = v[0];       b = v[1];          if(a.first >= b.first && a.second <= b.second) return a;       if(b.first >= a.first && b.second <= a.second) return b;          if(a.second >= b.first)       {           return {b.first, a.second};       }          return {1, 0};   }      int main() {       cin.tie(0);       cin.sync_with_stdio(0);             int t; cin>>t;       while(t--)       {           int n, m; cin>>n>>m;           string s; cin>>s;              pair<int, int> L[n], R[n];           int X[n];           int mn = 0, mx = 0;           int x = 0;           for(int i = 0; i < n; i++)           {               if(s[i] ==  + )               {                   x++;                   mx = max(mx, x);               }               else               {                   x--;                   mn = min(mn, x);               }                  L[i] = {mn, mx};               X[i] = x;           }              mn = 0, mx = 0;           for(int i = n - 1; i >= 0; i--)           {               if(s[i] ==  + )               {                   mx++;                   mn++;               }               else               {                   mx--;                   mn--;               }                  mx = max(mx, 0);               mn = min(0, mn);               R[i] = {mn, mx};           }              while(m--)           {               int l, r;               cin>>l>>r;                  --l;               --r;                     if(!l && r == n - 1)               {                   cout<<1<<  n ;                   continue;               }               if(!l)               {                   cout<<R[r + 1].second - R[r + 1].first + 1<<  n ;               }               else if(r == n - 1)               {                   cout<<L[l - 1].second - L[l - 1].first + 1<<  n ;               }               else               {                   int x = X[l - 1];                   pair<int, int> before = L[l - 1];                   pair<int, int> after  = R[r + 1];                      after.first  += x;                   after.second += x;                      int out = before.second - before.first + 1 + after.second - after.first + 1;                   pair<int, int> sec = intersect(before, after);                   out -= sec.second - sec.first + 1;                      cout<<out<<  n ;               }           }       }   }
#include <bits/stdc++.h>   const int N = 200100;   using namespace std;   vector<long long int> v[N];   long long int n, down[N], up[N], k, s[N], a[N];   long long int ans;   void f(long long int x, long long int p) {     s[x] = a[x];     for (int i = 0; i < v[x].size(); i++) {       long long int y = v[x][i];       if (y == p) continue;       f(y, x);       s[x] += s[y];       down[x] += down[y] + s[y];     }   }   void dfs(long long int x, long long int p) {     long long int mx = k + k - s[x];     for (int i = 0; i < v[x].size(); i++) {       long long int y = v[x][i];       if (y == p) continue;       up[y] = up[x] + down[x] - down[y] - s[y] + k + k - s[y];       dfs(y, x);       mx = max(mx, s[y]);     }     if (mx <= k) ans = max(ans, up[x] + down[x]);   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= k + k; i++) {       long long int x;       cin >> x;       a[x] = 1;     }     for (int i = 1; i < n; i++) {       long long int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     f(1, -1);     dfs(1, -1);     cout << ans << endl;   }
#include <bits/stdc++.h>      typedef long long ll;      using namespace std;      int main(){       ios::sync_with_stdio(0);       cin.tie(0);          ll t;       cin >> t;       while(t--){           ll n;           cin >> n;           ll x[n];           for(ll i=0; i<n; i++){               cin >> x[i];           }           vector<ll> v;           for(ll i=0; i<n; i++){               for(ll j=0; j<n; j++){                   if(i != j && find(v.begin(), v.end(), abs(x[i]-x[j])) == v.end()){                       v.push_back(abs(x[i]-x[j]));                   }               }           }           cout << v.size() <<   n ;       }   }