malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int const N = 100000 + 10;   int ans[N], sum, a[N], c[N];   int n, m, block, root, id[N], size[N];   int ne[N << 1], to[N << 1], first[N], tot;   int num[N], tim[N];   void add(int u, int v) {     ne[tot] = first[u];     to[tot] = v;     first[u] = tot++;   }   struct Node {     int l, r, id, k;     bool operator<(const Node& e) const {       return l / block < e.l / block || l / block == e.l / block && r < e.r;     }   } q[N];   int dfs(int u, int f) {     id[u] = ++tot;     size[u] = 1;     for (int i = first[u]; ~i; i = ne[i]) {       int v = to[i];       if (v == f) continue;       size[u] += dfs(v, u);     }     return size[u];   }   void Init() {     root = 1;     memset(first, -1, sizeof(first));     scanf( %d%d , &n, &m);     block = (int)ceil(pow(n, 0.5));     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     for (int i = 1; i <= n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       add(u, v);       add(v, u);     }     tot = 0;     dfs(root, -1);     for (int i = 1; i <= n; i++) a[id[i]] = c[i];     for (int i = 1; i <= m; i++) {       int v;       scanf( %d%d , &v, &q[i].k);       q[i].l = id[v];       q[i].r = id[v] + size[v] - 1;       q[i].id = i;     }     sort(q + 1, q + 1 + m);   }   void add(int pos) {     num[a[pos]]++;     tim[num[a[pos]]]++;   }   void del(int pos) {     tim[num[a[pos]]]--;     num[a[pos]]--;   }   int main() {     Init();     int L = 1, R = 0;     for (int i = 1; i <= m; i++) {       while (L < q[i].l) del(L++);       while (L > q[i].l) add(--L);       while (R < q[i].r) add(++R);       while (R > q[i].r) del(R--);       ans[q[i].id] = tim[q[i].k];     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   int main() {     int l, r, a, count;     scanf( %d%d%d , &l, &r, &a);     count = 0;     while (count < a) {       if (l > (r + a)) {         r = r + a;         goto x;       }       if (r > (l + a)) {         l = l + a;         goto x;       }       if (l < r) {         l++;         count++;       }       if (l == r) {         if ((a - count) % 2 == 0) {           l = l + (a - count) / 2;           r = r + (a - count) / 2;           goto x;         } else {           l = l + (a - count - 1) / 2;           r = r + (a - count - 1) / 2;           goto x;         }       }       if (r < l) {         r++;         count++;       }     }   x:     if (l <= r)       printf( %d , 2 * l);     else       printf( %d , 2 * r);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5, M = 35;   int n, m, x;   int g[M][M], tot, v[1000][2];   int main() {     int T;     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           scanf( %d , &g[i][j]);         }       }       int ans = 0;       memset(v, 0, sizeof(v));       tot = (n + m - 1) / 2;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           int k = i + j - 1;           if (k <= tot) {             v[k][g[i][j]]++;           } else if ((m + n - k) <= tot) {             v[(m + n - k)][g[i][j]]++;           }         }       }       for (int i = 1; i <= tot; i++) {         ans += min(v[i][0], v[i][1]);       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, h, g;     vector<int> home, guest;     cin >> n;     while (n--) {       cin >> h >> g;       home.push_back(h);       guest.push_back(g);     }     n = home.size();     h = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (home[i] == guest[j]) h++;       }     }     cout << h <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> bit(n);     for (int i = 0; i < n; i++) {       cin >> bit[i];     }     bit.push_back(bit[n - 1]);     int bit_before = bit[0];     int bit_start = bit[0];     int start = 0;     int cur_max = 0;     for (int i = 1; i < n + 1; i++) {       if (bit_before == bit[i]) {         int end = i - 1;         cur_max = max(cur_max, (end - start) / 2);         if (bit_start != bit[end]) {           for (int j = start + 1; j <= (end + start) / 2; j++) bit[j] = bit_start;           for (int j = (end + start) / 2 + 1; j < end; j++) bit[j] = bit[end];         } else {           for (int j = start; j <= end; j++) bit[j] = bit[end];         }         start = i;         bit_start = bit[i];       }       bit_before = bit[i];     }     cout << cur_max << endl;     for (int i = 0; i < n - 1; i++) cout << bit[i] <<    ;     cout << bit[n - 1] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   int n, m, c[2005][2005];   char s[2005][2005];   bool vis[2005][2005];   bool judge(int x, int y) {     return x >= 0 && x < n && y >= 0 && y < m && s[x][y] ==  . ;   }   queue<pair<int, int> > q;   void bfs(int x, int y) {     vis[x][y] = 1;     q.push(make_pair(x, y));     while (!q.empty()) {       pair<int, int> p = q.front();       q.pop();       for (int k = 0; k < 4; k++) {         int tx = p.first + d[k][0];         int ty = p.second + d[k][1];         if (judge(tx, ty) && !vis[tx][ty]) {           vis[tx][ty] = 1;           if (k == 0) {             s[p.first][p.second] =  < ;             s[tx][ty] =  > ;           } else if (k == 1) {             s[p.first][p.second] =  > ;             s[tx][ty] =  < ;           }           if (k == 2) {             s[p.first][p.second] =  ^ ;             s[tx][ty] =  v ;           }           if (k == 3) {             s[p.first][p.second] =  v ;             s[tx][ty] =  ^ ;           }           for (int i = 0; i < 4; i++) {             int ttx = tx + d[i][0];             int tty = ty + d[i][1];             if (judge(ttx, tty) && !vis[ttx][tty]) {               c[ttx][tty]--;               if (c[ttx][tty] == 1) {                 q.push(make_pair(ttx, tty));                 vis[ttx][tty] = 1;               }             }           }           break;         }       }     }   }   bool has_ans() {     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (s[i][j] ==  . ) return 0;     return 1;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         if (s[i][j] ==  * ) continue;         for (int k = 0; k < 4; k++)           if (judge(i + d[k][0], j + d[k][1])) c[i][j]++;       }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (c[i][j] == 1 && !vis[i][j]) bfs(i, j);     if (has_ans())       for (int i = 0; i < n; i++) printf( %s n , s[i]);     else       printf( Not unique n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   bool isPrime(int x) {     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   int main() {     cin >> n;     for (int i = (1); i <= (1000); i++)       if (!isPrime(n * i + 1)) {         cout << i;         return 0;       }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, k;     cin >> n >> k;     string s;     cin >> s;     int g, t;     for (i = 0; i < n; i++) {       if (s[i] ==  G ) {         g = i;       } else if (s[i] ==  T )         t = i;     }     if (g == t) {       cout <<  YES ;       return 0;     } else if (g < t) {       i = g;       while (i < t) {         if (s[i] ==  # ) break;         i = i + k;       }       if (i != t)         cout <<  NO ;       else         cout <<  YES ;     } else {       i = g;       while (i > t) {         if (s[i] ==  # ) break;         i = i - k;       }       if (i != t)         cout <<  NO ;       else         cout <<  YES ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string str, ch[103];     int n;     cin >> str >> n;     for (int i = 1; i <= n; i++) cin >> ch[i];     sort(ch + 1, ch + 1 + n);     for (int i = 1; i <= n; i++) {       int len = str.length();       string t = ch[i].substr(0, len);       if (t == str) {         cout << ch[i] << endl;         return 0;       }     }     cout << str << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     long long c, f, l;   };   bool operator<(node a, node b) { return a.f < b.f; }   long long n, dx, fx, bn, tot, i, tf, fn, first, xn, sum, xx;   node a[100004], b[100004];   bool can;   int main() {     cin >> n >> dx >> fx;     bn = -1;     tot = 0;     for (i = 0; i < n; i++) {       cin >> a[i].c >> a[i].f >> a[i].l;       tot += a[i].c;       if (a[i].l >= dx) {         bn++;         b[bn] = a[i];       }     }     bn++;     sort(b, b + bn);     can = false;     tf = 0;     fn = 0;     first = -1;     for (i = 0; i < bn; i++) {       if (tf + b[i].f <= fx) {         tf += b[i].f;         if (b[i].c != 0 && first == -1) {           first = i;           can = true;         }         fn++;       }     }     if (can == false) {       first = -1;       for (i = 0; i < bn; i++) {         if (b[i].c != 0) {           first = i;           break;         }       }       if (first != -1) {         if (b[first].f <= fx) {           tf = b[first].f;           xx = 0;           for (i = 0; i < bn; i++) {             if (tf + b[i].f > fx) break;             tf += b[i].f;             xx++;           }           if (tot + xx + 1 > fn) {             can = true;             fn = xx + 1;           }         }       }     }     if (can == true) {       if (tot + fn > n) {         cout << n <<    ;         xn = n - tot;         if (xn - 1 < first) {           sum = 0;           for (i = 0; i <= xn - 2; i++) sum += b[i].f;           sum += b[first].f;         } else {           sum = 0;           for (i = 0; i < xn; i++) sum += b[i].f;         }         cout << sum << endl;       } else {         cout << tot + fn <<    ;         if (fn - 1 < first) {           sum = 0;           for (i = 0; i < fn - 1; i++) sum += b[i].f;           sum += b[first].f;         } else {           sum = 0;           for (i = 0; i < fn; i++) sum += b[i].f;         }         cout << sum << endl;       }     } else {       cout << fn <<    ;       sum = 0;       for (i = 0; i < fn; i++) sum += b[i].f;       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const long long MODSQ = 1LL * MOD * MOD;   int W[8];   int dp[1 << 7][1 << 7][8][2];   struct matrix {     int sz;     int data[1 << 7][1 << 7];   };   matrix multiply(matrix& a, matrix& b) {     matrix c;     c.sz = a.sz;     for (int i = 0; i < c.sz; i++)       for (int j = 0; j < c.sz; j++) {         long long sum = 0;         for (int k = 0; k < c.sz; k++) {           sum += 1LL * a.data[i][k] * b.data[k][j];           if (sum >= MODSQ) sum -= MODSQ;         }         c.data[i][j] = sum % MOD;       }     return c;   }   matrix pow(matrix b, int e) {     if (e == 1) return b;     matrix ret = pow(multiply(b, b), e / 2);     if (e & 1) return multiply(ret, b);     return ret;   }   matrix transition_matrix(int sz) {     matrix ret;     ret.sz = 1 << sz;     for (int i = 0; i < (1 << sz); i++)       for (int j = 0; j < (1 << sz); j++) ret.data[i][j] = dp[i][j][sz][1];     return ret;   }   int main() {     for (int i = 0; i < (1 << 7); i++)       for (int j = 0; j < (1 << 7); j++) {         dp[i][j][0][0] = 0;         dp[i][j][0][1] = 1;         for (int k = 0; k < 7; k++) {           dp[i][j][k + 1][0] = dp[i][j][k][0] + dp[i][j][k][1];           dp[i][j][k + 1][1] = dp[i][j][k][0];           if (!(((i >> k) & 1) && ((j >> k) & 1)))             dp[i][j][k + 1][1] += dp[i][j][k][1];         }       }     int last = 0;     for (int i = 1; i <= 7; i++) {       scanf( %d , W + i);       if (W[i]) last = i;     }     matrix pre, base, tmp;     int psz = -1;     for (int i = 1; i <= last; i++)       if (W[i]) {         base.sz = 1 << i;         if (psz == -1) {           for (int j = 0; j < (1 << i); j++) base.data[j][0] = 0;           base.data[(1 << i) - 1][0] = 1;         } else {           for (int j = 0; j < (1 << i); j++) {             if ((j >> psz) == (1 << (i - psz)) - 1)               base.data[j][0] = pre.data[j & ((1 << psz) - 1)][0];             else               base.data[j][0] = 0;           }         }         tmp = pow(transition_matrix(i), W[i]);         pre = multiply(tmp, base);         psz = i;       }     printf( %d n , pre.data[(1 << last) - 1][0]);     return 0;   }
#include <bits/stdc++.h>   int main() {     long long n;     while (scanf( %lld , &n) != EOF) {       printf( %lld n , n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 +                            n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 -                            n / 70 - n / 105 + n / 210);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 303;   const int MOD = 1e9 + 7;   long long ncr[MAX][MAX];   map<int, int> cnt;   int tot[MAX];   long long dp[MAX][MAX];   vector<int> fin;   int get(int a) {     int ans = 1;     for (int i = 2; (i * i) <= a; i++) {       int k = 0;       while ((a % i) == 0) {         a /= i;         k ^= 1;       }       if (k) {         ans *= i;       }     }     ans *= a;     return ans;   }   int main() {     for (int i = 0; i < MAX; i++) {       ncr[i][0] = 1;       for (int j = 1; j <= i; j++) {         ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % MOD;       }     }     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int a;       scanf( %d , &a);       cnt[get(a)]++;     }     for (auto x : cnt) {       fin.push_back(x.second);     }     sort(fin.begin(), fin.end());     reverse(fin.begin(), fin.end());     int m = fin.size();     for (int i = 1; i <= m; i++) {       tot[i] = tot[i - 1] + fin[i - 1];     }     dp[1][fin[0] - 1] = 1;     for (int i = 2; i <= m; i++) {       int slots = tot[i - 1] + 1;       for (int j = 0; j <= n; j++) {         if (!dp[i - 1][j]) {           continue;         }         for (int k = 1; k <= min(slots, fin[i - 1]); k++) {           for (int l = 0; l <= min(j, k); l++) {             dp[i][fin[i - 1] - k + j - l] +=                 (((((dp[i - 1][j] * ncr[j][l]) % MOD) *                    ncr[fin[i - 1] - 1][k - 1]) %                   MOD) *                  ncr[slots - j][k - l]) %                 MOD;             dp[i][fin[i - 1] - k + j - l] %= MOD;           }         }       }     }     long long ans = dp[m][0];     for (int i = 0; i < m; i++) {       for (int j = 1; j <= fin[i]; j++) {         ans = (ans * j) % MOD;       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7, MAX = 1e5 + 5;   long long powN(long long a, long long p) {     if (p == 0) return 1;     long long z = powN(a, p / 2);     z = (z * z) % MOD;     if (p % 2) z = (z * a) % MOD;     return z;   }   vector<bool> is_prime(MAX + 1, true);   void Sieve() {     is_prime[0] = is_prime[1] = false;     int i, j;     for (i = 2; i * i <= MAX; i++) {       if (is_prime[i]) {         for (j = i * i; j <= MAX; j += i) is_prime[j] = false;       }     }   }   int main() {     string s, t;     cin >> s >> t;     int n = s.length();     string s1 = s;     if (s == t)       cout <<  No such string  << endl;     else {       bool ans = false;       bool dec = false;       for (int i = 0; i < n; i++) {         if (dec) {           if (t[i] ==  a ) {             s[i] = t[i];             continue;           }           s[i] = (t[i] - 1);           ans = true;           cout << s << endl;           break;         } else if (t[i] - s[i] > 1) {           ans = true;           s[i] = t[i] - 1;           cout << s << endl;           break;         } else if (t[i] - s[i] == 1) {           s[i]++;           dec = true;         } else           continue;       }       if (!ans) {         dec = false;         for (int i = 0; i < n; i++) {           if (dec) {             if (s1[i] ==  z ) continue;             s1[i]++;             ans = true;             cout << s1 << endl;             break;           } else if (t[i] - s1[i] > 1) {             ans = true;             s1[i] = t[i] - 1;             cout << s1 << endl;             break;           } else if (t[i] - s1[i] == 1) {             dec = true;           } else             continue;         }       }       if (!ans) cout <<  No such string  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const long long INFLL = 1e18 + 7;   const long double EPS = 1e-9;   const int MAXN = 1e5 + 17;   mt19937 myRand(1337);   mt19937_64 myRand64(1337);   signed main(void) {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int k;     cin >> k;     int len = k / (int)1e6 + (k % (int)1e6 != 0) + 2;     int r = k + len;     vector<int> ans = {-1};     int m = r / (int)1e6;     r = r - (r / (int)1e6) * 1e6;     ans.push_back(r);     for (int i = 0; i < m; ++i) {       ans.push_back((int)1e6);     }     for (int i = 0; i < len - (int)ans.size(); ++i) ans.push_back(0);     cout << (int)ans.size() << endl;     for (int i : ans) cout << i <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int n, p, cnt, r[1010];   pair<int, int> c[1010];   bool check[1010];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> c[i].second >> c[i].first;       if (c[i].second > c[i].first) swap(c[i].second, c[i].first);     }     sort(c, c + n);     while (1) {       int i = 0;       for (i = 0; i < n; i++)         if (!check[i]) break;       if (i == n) break;       r[cnt] = c[i].first;       for (i = 0; i < n; i++)         if (c[i].first >= r[cnt] && c[i].second <= r[cnt]) check[i] = true;       cnt++;     }     cout << cnt << endl;     for (int i = 0; i < cnt; i++) cout << r[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, u, v, vk = 0, vv = 0;   long long res = 0, se = 0;   int visited[1000000], selfE[1000000];   vector<vector<int> > G(1000000, vector<int>());   void dfs(int i) {     if (visited[i]) return;     visited[i] = 1;     vk++;     for (int u : G[i]) {       dfs(u);     }   }   void solve() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d , &u, &v);       if (u == v) {         selfE[v - 1] += 1;         se++;       } else {         G[u - 1].push_back(v - 1);         G[v - 1].push_back(u - 1);       }     }     for (int i = 0; i < n; i++) {       long long l = G[i].size();       res += l * (l - 1) / 2;     }     res += se * (se - 1) / 2;     res += se * (m - se);     memset(visited, 0, sizeof visited);     for (int i = 0; i < n; i++) {       if (G[i].size()) {         dfs(i);         break;       }     }     for (int i = 0; i < n; i++)       if (G[i].size() || selfE[i]) vv++;     if (vk == vv)       cout << res << endl;     else       cout << 0 << endl;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   double p[1001];   double getp(unsigned long long a, unsigned long long b) {     unsigned long long st = 1, ed = 1;     double ret;     while (st < a) st = st * 10;     while (ed < b) ed = ed * 10;     if (ed > b) ed = ed / 10;     if (st > a) st = st / 10;     if (ed + ed - 1 < a) return 0;     if ((ed + ed - 1) <= b) {       ret = st * (pow(10, log10(ed / st) + 1) - 1) / 9;     } else       ret = st * (pow(10, log10(ed / st)) - 1) / 9 + (b - ed + 1);     if (st + st - 1 >= a) ret = ret - (a - st);     if (st + st - 1 < a) ret = ret - (st);     return (double)ret / (b - a + 1);   }   double memo[1001][1001];   double dp(int i, int j) {     if (j < 0 || i < 0) return 0;     if (memo[i][j] != -1) return memo[i][j];     if (i == 0 && j == 0) {       memo[i][j] = 1;       return 1;     }     double ret = p[i - 1] * dp(i - 1, j - 1) + (1 - p[i - 1]) * dp(i - 1, j);     memo[i][j] = ret;     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= n; j++) {         memo[i][j] = -1;       }     }     unsigned long long a, b;     int i = 0;     int t = n;     while (t--) {       cin >> a >> b;       p[i++] = getp(a, b);     }     for (int j = 0; j <= n; j++) {       if (memo[n][j] == -1) dp(n, j);     }     double k;     cin >> k;     k = ceil(k * n / 100);     double sum = 0;     for (int j = 0; j <= n; j++) {       if (j >= k) sum = sum + memo[n][j];     }     printf( %.15f , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, a[5] = {0};     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       a[x]++;     }     int res = a[4];     res += a[2] / 2;     a[2] = a[2] % 2;     if (a[2] == 1) {       res++;       a[1] -= 2;     }     res += a[3];     if (a[1] > a[3]) {       a[1] -= a[3];       res += (a[1] + 3) / 4;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void init() {     ios::sync_with_stdio(false);     cin.tie(0);   }   const int N = 100005;   vector<int> adj[N];   bool reach[N][2];   int prv[N][2];   void dfs(int u, int k) {     reach[u][k] = true;     for (int v : adj[u]) {       if (!reach[v][k ^ 1]) {         prv[v][k ^ 1] = u;         dfs(v, k ^ 1);       }     }   }   int vis[N];   bool detectCycle(int u) {     vis[u] = 1;     for (int v : adj[u]) {       if (vis[v] == 1) return true;       if (vis[v] == 0 && detectCycle(v)) return true;     }     vis[u] = 2;     return false;   }   int main() {     init();     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       while (x--) {         int j;         cin >> j;         adj[i].push_back(j);       }     }     int s;     cin >> s;     dfs(s, 0);     bool win = false;     for (int i = 1; i <= n; i++) {       if (reach[i][1] && (int)adj[i].size() == 0) {         win = true;         cout <<  Win n ;         int p = 1;         vector<int> res;         while (prv[i][p] != 0) {           res.push_back(i);           i = prv[i][p];           p ^= 1;         }         res.push_back(s);         reverse(res.begin(), res.end());         for (int r : res) cout << r <<    ;         cout <<   n ;         break;       }     }     if (!win) {       if (detectCycle(s)) {         cout <<  Draw n ;       } else {         cout <<  Lose n ;       }     }     return 0;   }