malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 650;   int kd[MAXN], edges[100010][2];   int idA[MAXN], idB[MAXN];   int g[MAXN][MAXN];   int P;   inline int advPow(int a, int b) {     int ret = 1;     while (b) {       if (b & 1) ret = (long long)ret * a % P;       a = (long long)a * a % P;       b >>= 1;     }     return ret;   }   inline int inverse(int a) { return advPow(a, P - 2); }   int gauss(int N) {     int ret = 1;     for (int col = 1; col <= N; col++) {       int pos = 0;       for (int row = col; row <= N; row++)         if (g[row][col]) {           pos = row;           break;         }       if (pos && pos != col) {         for (int i = 1; i <= N; i++) swap(g[pos][i], g[col][i]);         ret *= -1;       }       if (pos == 0) return 0;       int inv = inverse(g[col][col]);       for (int row = 1; row <= N; row++) {         if (!g[row][col] || row == col) continue;         int cur = (long long)g[row][col] * inv % P;         for (int i = col; i <= N; i++)           g[row][i] = (g[row][i] - (long long)g[col][i] * cur % P + P) % P;       }     }     for (int i = 1; i <= N; i++) ret = (long long)ret * g[i][i] % P;     return (ret + P) % P;   }   struct Node {     int v, nxt;   } d[100010];   int head[MAXN], etot;   inline void addEdge(int a, int b) {     etot++;     d[etot].v = b;     d[etot].nxt = head[a];     head[a] = etot;   }   int rin[MAXN], rout[MAXN];   int plans[MAXN][MAXN];   queue<int> que;   int main() {     int N, M;     scanf( %d%d%d , &N, &M, &P);     for (int i = 1; i <= M; i++) {       int a, b;       scanf( %d%d , &a, &b);       if (a == b) continue;       rin[b]++, rout[a]++;       addEdge(a, b);     }     for (int i = 1; i <= N; i++)       if (!rin[i]) que.push(i);     for (int i = 1; i <= N; i++) plans[i][i] = 1;     int cnt1 = 0, cnt2 = 0;     for (int i = 1; i <= N; i++)       if (!rin[i]) idA[++cnt1] = i;     for (int i = 1; i <= N; i++)       if (!rout[i]) idB[++cnt2] = i;     if (cnt1 != cnt2) {       puts( 0 );       return 0;     }     while (!que.empty()) {       int u = que.front();       que.pop();       for (int e = head[u]; e; e = d[e].nxt) {         rin[d[e].v]--;         for (int i = 1; i <= N; i++) (plans[i][d[e].v] += plans[i][u]) %= P;         if (!rin[d[e].v]) que.push(d[e].v);       }     }     for (int i = 1; i <= cnt1; i++)       for (int j = 1; j <= cnt2; j++) g[i][j] = plans[idA[i]][idB[j]];     printf( %d n , gauss(cnt1));   }
#include <bits/stdc++.h>   using namespace std;   int main() {     double r, x, y, x1, y1;     cin >> r >> x >> y >> x1 >> y1;     double dist = sqrt(pow(x - x1, 2) + pow(y - y1, 2));     double ans = dist / (2 * r);     cout << ceil(ans) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 3e5;   int a[MAX] = {};   int main() {     int t;     cin >> t;     while (t--) {       char c;       long long s;       cin >> c >> s;       long long a1 = 0;       for (int i = 0; i < 18; i++) {         a1 = a1 * 2 + s % 2;         s /= 10;       }       if (c ==  + ) {         a[a1]++;       } else if (c ==  - ) {         a[a1]--;       } else {         cout << a[a1] <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> l[100007];   int parent[100007];   int color[100007];   int k;   int p;   int dfs(int u, int c) {     int sz = l[u].size();     {       for (int i = 0; i < sz; i++) {         if (color[u] - color[l[u][i]] >= k && color[l[u][i]] != 0) {           parent[l[u][i]] = u;           return u;         } else if (color[l[u][i]] == 0) {           color[l[u][i]] = c + 2;           parent[l[u][i]] = u;           return dfs(l[u][i], c + 1);         }       }     }   }   int main() {     int n, m;     cin >> n >> m >> k;     int a, b;     for (int i = 0; i < m; i++) {       cin >> a >> b;       l[a].push_back(b);       l[b].push_back(a);     }     color[1] = 1;     int u = dfs(1, 0);     int v = parent[u];     int cycle = 1;     while (v != u) {       v = parent[v];       cycle++;     }     cout << cycle << endl;     cout << u <<    ;     v = parent[u];     while (v != u) {       cout << v <<    ;       v = parent[v];     }   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   int n, m, k;   int p[40];   struct query {     int l, r, x;   };   struct query q[500005];   int max0[500005], max1[500005];   long long dp[500005];   long long s[500005];   void solve() {     cin >> n >> k >> m;     p[0] = 1;     for (int i = 1; i < 40; i++) p[i] = (p[i - 1] << 1);     n++;     for (int(i) = (1); (i) <= (m); ++(i)) cin >> q[i].l >> q[i].r >> q[i].x;     m++;     q[m].l = n;     q[m].r = n;     q[m].x = 0;     long long ans = 1;     for (int(j) = (0); (j) <= (k - 1); ++(j)) {       for (int(i) = (0); (i) <= (n); ++(i)) {         max0[i] = max1[i] = -1;       }       for (int(i) = (1); (i) <= (m); ++(i)) {         if (q[i].x & p[j]) {           if (max1[q[i].l] < q[i].r) max1[q[i].l] = q[i].r;         } else {           if (max0[q[i].r] < q[i].l) max0[q[i].r] = q[i].l;         }       }       for (int i = 0; i <= n; i++) s[i] = 0;       int L0 = 0;       int R1 = -1;       dp[0] = 1;       s[0] = 1;       for (int i = 1; i <= n; i++) {         R1 = max(R1, max1[i]);         if (i <= R1) {           dp[i] = 0;         } else {           dp[i] = (L0 == 0 ? s[i - 1] : s[i - 1] - s[L0 - 1]);           dp[i] %= mod;         }         s[i] = (s[i - 1] + dp[i]) % mod;         L0 = max(L0, max0[i]);       }       ans = (ans * dp[n]) % mod;     }     if (ans < 0) ans += mod;     cout << (ans) <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int T = 1;     for (int t = 1; t <= T; t++) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   bool dentro[410000];   int main() {     int n, k;     scanf( %d %d , &n, &k);     int vazio = 0;     int v[n];     vector<int> proximo(n, inf);     vector<int> mapa(n, -1);     for (int i = 0; i < n; i++) {       scanf( %d , &v[i]);       if (mapa[v[i]] != -1) {         proximo[mapa[v[i]]] = i;       }       mapa[v[i]] = i;     }     int resp = 0;     priority_queue<pair<int, int> > q;     int qt = 0;     for (int i = 0; i < n; i++) {       if (dentro[v[i]]) {         q.push(pair<int, int>(proximo[i], v[i]));         continue;       }       if (qt < k) {         q.push(pair<int, int>(proximo[i], v[i]));         qt++;         dentro[v[i]] = true;         resp++;         continue;       }       pair<int, int> aux = q.top();       q.pop();       resp++;       int u = aux.second;       dentro[u] = false;       dentro[v[i]] = true;       q.push(pair<int, int>(proximo[i], v[i]));     }     printf( %d n , resp);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mapping[200001];   int main(int narg, char **arg) {     int h, m, n;     cin >> h >> m >> n;     int hh = 0, occup_size = 0;     while (true) {       mapping[hh] = occup_size;       hh += m;       hh %= h;       occup_size++;       if (!hh) break;     }     int n_occup = h / occup_size;     map<int, int> occup[n_occup];     for (int i = 0; i < occup_size; i++) mapping[i] = mapping[i * n_occup];     long long counter = 0;     map<int, pair<int, int> > positions;     for (int i = 0; i < n; i++) {       char op;       int id, hash_value;       cin >> op >> id;       int i_occup, i_start;       if (op ==  + ) {         cin >> hash_value;         i_occup = hash_value % n_occup;         i_start = mapping[hash_value / n_occup];         while (true) {           map<int, int>::iterator it = occup[i_occup].upper_bound(i_start);           if (it == occup[i_occup].begin()) {             occup[i_occup][i_start] = i_start;             it = occup[i_occup].begin();           } else {             --it;             if (it->second >= i_start - 1) {               if (it->second == occup_size - 1) {                 counter += occup_size - i_start;                 i_start = 0;                 continue;               }               it->second++;               counter += it->second - i_start;               i_start = it->second;             } else {               it = occup[i_occup].insert(make_pair(i_start, i_start)).first;             }           }           map<int, int>::iterator it2 = it;           ++it2;           if (it2 != occup[i_occup].end()) {             if (it->second + 1 == it2->first) {               it->second = it2->second;               occup[i_occup].erase(it2);             }           }           break;         }         positions[id] = make_pair(i_occup, i_start);       } else {         pair<int, int> p = positions[id];         i_occup = p.first;         i_start = p.second;         positions.erase(id);         map<int, int>::iterator it = occup[i_occup].upper_bound(i_start);         --it;         if (it->first == i_start) {           int last = it->second;           occup[i_occup].erase(it);           if (last != i_start) occup[i_occup][i_start + 1] = last;         } else if (it->second == i_start)           it->second--;         else {           int last = it->second;           it->second = i_start - 1;           occup[i_occup][i_start + 1] = last;         }       }     }     cout << counter << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, ans;   int x[1100000], pw[1100000];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &x[i]);     sort(x + 1, x + n + 1);     pw[0] = 1;     for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 2 % 1000000007;     for (int i = 1; i < n; i++)       ans = (ans + 1LL * (x[i + 1] - x[i]) * (pw[i] - 1) % 1000000007 *                        (pw[n - i] - 1)) %             1000000007;     printf( %d n , (ans + 1000000007) % 1000000007);   }
#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, i, j, alp[27] = {0}, flg = 1;       cin >> n;       string s[n + 1];       for (i = 0; i < n; i++) cin >> s[i];       for (i = 0; i < n; i++) {         for (j = 0; j < s[i].size(); j++) {           alp[s[i][j] * 1 - 96]++;         }       }       for (i = 1; i < 27; i++) {         if (alp[i] != 0) {           if (alp[i] % n != 0) {             flg = 0;             break;           }         }       }       if (flg == 1)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 10000 + 10;   int a[maxn];   int main() {     int i, j, k, m, n;     cin >> n;     int sum = 0;     while (n > 0) {       if (n % 8 == 1) sum++;       n /= 8;     }     cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> edge[200010];   int dp[200010][2][2];   int leaf[200010];   void dfsLeaf(int u) {     if (edge[u].size() == 0) {       leaf[u] = 1;       return;     }     for (int i = 0; i < edge[u].size(); ++i) {       int v = edge[u][i];       dfsLeaf(v);       leaf[u] += leaf[v];     }   }   void dfsDp(int u) {     if (edge[u].size() == 0) {       dp[u][0][1] = dp[u][1][0] = dp[u][0][0] = dp[u][1][1] = 1;       return;     }     dp[u][0][0] = 0;     dp[u][0][1] = 0;     dp[u][1][0] = 1;     dp[u][1][1] = 1000000000;     for (int i = 0; i < edge[u].size(); ++i) {       int v = edge[u][i];       dfsDp(v);       dp[u][0][0] = max(dp[u][0][0], dp[v][1][0] + leaf[u] - leaf[v]);       dp[u][0][1] += dp[v][1][1];       dp[u][1][0] += dp[v][0][0] - 1;       dp[u][1][1] = min(dp[u][1][1], dp[v][0][1]);     }   }   int main() {     int u, v;     while (cin >> n) {       for (int i = 1; i < n; ++i) {         cin >> u >> v;         edge[u].push_back(v);       }       memset(leaf, 0, sizeof(leaf));       dfsLeaf(1);       dfsDp(1);       cout << dp[1][0][0] <<     << dp[1][0][1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int x = 0, y = 0;     int n = b.length();     while (x < n && a[x] == b[x]) x++;     while (y < n && a[n - y] == b[n - 1 - y]) y++;     int res = x + y + 2 - (n + 1);     res = max(0, min(res, n + 1));     cout << res << endl;     int start = n - y + 1;     for (int i = 0; i < res; i++) cout << (start + i) <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma optimize( Ofast )   using namespace std;   using ll = long long;   const int N = 2e5 + 5;   vector<int> g[N];   bool vis[N];   int cnt;   void dfs(int s) {     ++cnt;     vis[s] = true;     for (auto it : g[s]) {       if (!vis[it]) {         dfs(it);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       g[i + 1].push_back(x);       g[x].push_back(i + 1);     }     deque<ll> d;     for (int i = 1; i <= n; ++i) {       if (!vis[i]) {         cnt = 0;         dfs(i);         d.push_back(cnt);       }     }     sort(d.rbegin(), d.rend());     if (d.size() == 1) d.push_back(0);     ll ans = (d[0] + d[1]) * (d[0] + d[1]);     for (int i = 2; i < d.size(); ++i) {       ans += d[i] * d[i];     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, j, n, t, sum = 0, k, c1 = 1;   int main() {     cin >> n;     vector<string> c(n);     vector<string> s(n);     for (i = 0; i < n; i++) {       cin >> c[i] >> s[i];     }     for (i = 1; i < n; i++) {       k = 0;       for (j = i - 1; j >= 0; j--) {         if (c[i] == c[j] && s[i] == s[j]) {           k++;           if (k != 0) {             break;           }         }       }       if (k == 0) {         c1++;       }     }     cout << c1;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int testCases = 1;     cin >> testCases;     while (testCases--) {       int n;       cin >> n;       vector<int> a(n);       for (int i = 0; i < n; i++) {         cin >> a[i];       }       if (n % 2 == 0) {         for (int i = 0; i < n; i += 2) {           cout << a[i + 1] <<     << -a[i] <<    ;         }       } else {         for (int i = 0; i < n - 3; i += 2) {           cout << a[i + 1] <<     << -a[i] <<    ;         }         if (a[n - 3] + a[n - 2] != 0) {           cout << a[n - 1] <<     << a[n - 1] <<     << -(a[n - 3] + a[n - 2]);         } else if (a[n - 2] + a[n - 1] != 0) {           cout << -(a[n - 2] + a[n - 1]) <<     << a[n - 3] <<     << a[n - 3];         } else {           cout << a[n - 2] <<     << -(a[n - 1] + a[n - 3]) <<     << a[n - 2];         }       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   long long mul[1005], c[1005][1005], f[1005][1005][2][3], chose[1005];   long long ans;   int main() {     scanf( %d%d , &n, &k);     mul[0] = 1;     for (int i = 1; i <= n; ++i) mul[i] = mul[i - 1] * (long long)i % 1000000007;     for (int i = 0; i <= 1000; ++i) c[i][0] = 1;     for (int i = 1; i <= 1000; ++i)       for (int j = 1; j <= 1000; ++j)         c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 1000000007;     for (int i = 0; i <= n; ++i) f[i][0][1][0] = 1;     for (int i = 1; i <= n; ++i)       for (int j = 0; j <= i; ++j) {         f[i][j][0][0] = f[i - 1][j][0][0] + f[i - 1][j][0][1] +                         f[i - 1][j][1][0] + f[i - 1][j][1][1];         f[i][j][0][0] %= 1000000007;         if (j) f[i][j][0][1] = f[i - 1][j - 1][0][0] + f[i - 1][j - 1][0][1];         f[i][j][0][1] %= 1000000007;         if (j)           f[i][j][0][2] = f[i - 1][j - 1][0][0] + f[i - 1][j - 1][0][1] +                           f[i - 1][j - 1][1][0] + f[i - 1][j - 1][1][1];         f[i][j][0][2] %= 1000000007;         f[i][j][1][0] = f[i - 1][j][0][2] + f[i - 1][j][1][2];         f[i][j][1][0] %= 1000000007;         if (j) f[i][j][1][1] = f[i - 1][j - 1][0][2];         f[i][j][1][1] %= 1000000007;         if (j) f[i][j][1][2] = f[i - 1][j - 1][0][2] + f[i - 1][j - 1][1][2];         f[i][j][1][2] %= 1000000007;       }     for (int i = 0; i <= n; ++i) {       chose[i] = f[n][i][0][0] + f[n][i][0][1] + f[n][i][1][0] + f[n][i][1][1];       chose[i] %= 1000000007;     }     for (int i = k, opt = 1; i <= n; ++i, opt = -opt) {       ans += chose[i] * mul[n - i] % 1000000007 * c[i][k] % 1000000007 * opt;       ans = (ans % 1000000007 + 1000000007) % 1000000007;     }     printf( %I64d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long inf = 1e9;   int main() {     std::ios::sync_with_stdio(false);     int n;     cin >> n;     long long noemer = 0;     for (long long i = 1; i <= n; i++) {       noemer += (n + 1 - i) * (n + 1 - i);     }     string s1, s2;     cin >> s1;     cin >> s2;     double ans = 0.0;     long long teller = 0;     for (char ch =  A ; ch <=  Z ; ch++) {       long long x, y, z;       x = 0;       y = 0;       z = 0;       for (int i = n - 1; i >= 0; i--) {         if (s2[i] == ch) {           z += n - i;         }       }       x = z;       if (s1[0] == ch) {         if (teller >= inf) {           ans += 1.0 * teller / noemer;           teller = 0;         }         teller += x;       }       for (int i = 1; i < n; i++) {         if (s2[i - 1] == ch) {           y += i;           z -= (n + 1 - i);         }         long long now = x - y + z;         if (s1[i] == ch) {           if (teller >= inf) {             ans += 1.0 * teller / noemer;             teller = 0;           }           teller += now;         }         x = now;       }     }     ans += 1.0 * teller / noemer;     cout << fixed << setprecision(12) << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, c, arr[5], count = 0;     cin >> t;     for (int i = 0; i < t; i++) {       cin >> arr[0] >> arr[1] >> arr[2];       sort(arr, arr + 3);       c = arr[0];       b = arr[1];       a = arr[2];       if (a > 0) {         a--;         count++;       }       if (b > 0) {         b -= 1;         count++;       }       if (c > 0) {         c--;         count++;       }       if (a > 0 && b > 0) {         a--;         b--;         count++;       }       if (a > 0 && c > 0) {         a--;         c--;         count++;       }       if (b > 0 && c > 0) {         c--;         b--;         count++;       }       if (a > 0 && b > 0 && c > 0) {         c--;         a--;         b--;         count++;       }       cout << count << endl;       count = 0;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const double PI = 3.141592653589793238463;   const int N = 1e5 + 100;   vector<int> v[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int x;     cin >> x;     int r, l;     for (int i = 1; i < x; i++) {       cin >> r >> l;       v[r].push_back(l);       v[l].push_back(r);     }     bool ans = true;     for (int i = 1; i <= x; i++) {       if (v[i].size() == 2) {         ans = false;         break;       }     }     cout << (ans ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   struct edge {     int u, v, st, nt;   } e[MAXN * 2];   vector<int> lis;   int hd[MAXN], etot, used[MAXN], in[MAXN], u[MAXN], v[MAXN];   void addedge(int u, int v, int st) {     e[etot].u = u;     e[etot].v = v;     e[etot].st = st;     e[etot].nt = hd[u];     hd[u] = etot++;   }   void dfs(int p) {     used[p] = 1;     lis.push_back(p);     for (int i = hd[p]; ~i; i = e[i].nt) {       if (used[e[i].v]) continue;       dfs(e[i].v);     }   }   queue<int> Q;   int topsort() {     for (int i = 0; i < (int)lis.size(); i++)       if (!in[lis[i]]) Q.push(lis[i]);     int cnt = 0, u;     for (; !Q.empty();) {       cnt++;       u = Q.front();       Q.pop();       for (int i = hd[u]; ~i; i = e[i].nt) {         if (!e[i].st) continue;         in[e[i].v]--;         if (!in[e[i].v]) Q.push(e[i].v);       }     }     return cnt == (int)lis.size();   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     etot = 0;     memset(hd, -1, sizeof(hd));     for (int i = 0; i < m; i++) {       scanf( %d%d , u + i, v + i);       addedge(u[i], v[i], 1);       addedge(v[i], u[i], 0);       in[v[i]]++;     }     int ans = 0;     for (int i = 1; i < n + 1; i++) {       lis.clear();       if (!used[i]) dfs(i);       if ((int)lis.size() == 0) continue;       ans += (int)lis.size() - topsort();     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 9;   int a[N], n;   double ans(double x) {     double arr[N];     for (int i = 0; i < n; i++) arr[i] = a[i] - x;     double neg = 0;     double pos = 0;     double ans = 0;     for (int i = 0; i < n; i++) {       if (neg > 0) neg = 0;       if (pos < 0) pos = 0;       neg += arr[i];       pos += arr[i];       ans = max(ans, max(-neg, pos));     }     return ans;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     double l = -1e5 - 9, r = 1e5 + 9;     for (int i = 0; i < 100; i++) {       double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;       if (ans(m1) > ans(m2))         l = m1;       else         r = m2;     }     cout << setprecision(12) << fixed << ans(l) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     long long int n, a, b, c, d, e, f, g, h, i, j, x, y, count = 0;     cin >> n >> x >> y;     n = n / 2;     if ((x == n || x == n + 1) && (y == n || y == n + 1))       cout <<  NO ;     else       cout <<  YES ;     return 0;   }