malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   bool vis[10000001];   int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }   int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }   int main() {     int n;     scanf( %d , &n);     int sum = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int a;         scanf( %d , &a);         if (i == j) sum += a;       }     }     sum %= 2;     int q;     cin >> q;     for (int i = 0; i < q; i++) {       int t;       scanf( %d , &t);       if (t == 3)         cout << sum;       else {         int y;         scanf( %d , &y);         sum = 1 - sum;       }     }   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > re;   vector<pair<int, int> > e, o;   int n, c[404][404], sink, Find, pre[404], flow;   bool ch[100009], visit[404];   void init() {     for (int i = 2; i * i <= 100000; i++) {       if (!ch[i]) {         for (int j = i * i; j <= 100000; j += i) ch[j] = 1;       }     }   }   void dfs(int cur) {     visit[cur] = 1;     if (cur == sink) Find = 1;     if (Find) return;     for (int i = 0; i <= sink && !Find; i++) {       if (c[cur][i] > 0 && !visit[i]) {         dfs(i);         pre[i] = cur;       }     }   }   void solve() {     init();     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       if (a % 2)         o.push_back(pair<int, int>(a, i + 1));       else         e.push_back(pair<int, int>(a, i + 1));     }     for (int i = 0; i < o.size(); i++) {       c[0][i + 1] = 2;     }     sink = e.size() + o.size() + 1;     for (int i = 0; i < e.size(); i++) {       c[o.size() + i + 1][sink] = 2;     }     for (int i = 0; i < o.size(); i++) {       for (int j = 0; j < e.size(); j++) {         int t = o[i].first + e[j].first;         if (!ch[t]) c[i + 1][o.size() + j + 1] = 1;       }     }     for (;;) {       memset(visit, 0, sizeof(visit));       Find = 0;       dfs(0);       if (!Find) break;       flow++;       int st, en;       en = sink;       while (en) {         st = pre[en];         c[st][en]--;         c[en][st]++;         en = st;       }     }     if (flow != 2 * e.size()) {       puts( Impossible );       return;     }     memset(visit, 0, sizeof(visit));     for (int i = 1; i < sink; i++) {       if (visit[i]) continue;       vector<int> tmp;       int cur = i;       while (!visit[cur]) {         visit[cur] = 1;         if (cur <= o.size())           tmp.push_back(o[cur - 1].second);         else           tmp.push_back(e[cur - o.size() - 1].second);         for (int i = 1; i < sink; i++) {           if (!visit[i] && ((cur <= o.size() && c[i][cur] > 0) ||                             cur > o.size() && c[cur][i] > 0)) {             cur = i;             break;           }         }       }       re.push_back(tmp);     }     printf( %d n , re.size());     for (int i = 0; i < re.size(); i++) {       printf( %d  , re[i].size());       for (int j = 0; j < re[i].size(); j++) printf( %d  , re[i][j]);       puts(  );     }   }   int main(void) {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int max = INT_MIN, min = INT_MAX;     for (int i = 0; i < N; ++i) {       int x;       cin >> x;       if (x > max) max = x;       if (x < min) min = x;     }     cout << max - min + 1 - N << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long a, b[1001], c, cnt1, cnt2, d, n, m;   string s;   int main() {     cin >> a;     if (a == 1) {       cout << 1;       return 0;     }     for (int i = 1; i <= a; i++) {       cin >> b[i];       c += b[i];     }     sort(b + 1, b + a + 1);     reverse(b + 1, b + a + 1);     for (int i = 1; i <= a; i++) {       d = (c - b[i]);       n += c - d;       cnt1++;       if (n > d) {         cout << cnt1;         return 0;       }       c = d;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 20;   const int MAXX = 65536;   int n, m;   long long year;   vector<int> graph[MAXN], rev_graph[MAXN];   vector<int> sol;   long long dp[MAXX];   long long countPermutations(int mask, int pos) {     dp[0] = 1;     for (int i = 1; i < 1 << (n - pos); ++i) {       dp[i] = 0;     }     for (int i = 0; i < 1 << (n - pos); ++i) {       int value = n - 1;       while (value >= 0 && (mask & (1 << value))) --value;       for (int j = 0; j < n - pos; ++j)         if (i & (1 << j)) {           do {             --value;           } while (value >= 0 && (mask & (1 << value)));         }       for (int j = 0; j < n - pos; ++j)         if ((i & (1 << j)) == 0) {           int ok = 1;           for (size_t k = 0; k < graph[j + pos].size() && ok; ++k)             if ((graph[j + pos][k] >= pos &&                  (i & (1 << (graph[j + pos][k] - pos))) == 0) ||                 (graph[j + pos][k] < pos && sol[graph[j + pos][k]] < value)) {               ok = 0;             }           for (size_t k = 0; k < rev_graph[j + pos].size() && ok; ++k)             if (rev_graph[j + pos][k] < pos && sol[rev_graph[j + pos][k]] > value)               ok = 0;           if (ok) dp[i ^ (1 << j)] += dp[i];         }     }     return dp[(1 << (n - pos)) - 1];   }   void findPermutation(int mask, int pos) {     if (pos < n) {       for (int i = 0; i < n; ++i)         if ((mask & (1 << i)) == 0) {           int ok = 1;           for (size_t j = 0; j < graph[pos].size() && ok; ++j)             if (graph[pos][j] < pos && sol[graph[pos][j]] < i) ok = 0;           for (size_t j = 0; j < rev_graph[pos].size() && ok; ++j)             if (rev_graph[pos][j] < pos && sol[rev_graph[pos][j]] > i) ok = 0;           if (!ok) {             continue;           }           mask ^= 1 << i;           sol.push_back(i);           long long cnt = countPermutations(mask, pos + 1);           if (cnt >= year) {             findPermutation(mask, pos + 1);             break;           }           year -= cnt;           mask ^= 1 << i;           sol.pop_back();         }     }   }   int main() {     cin >> n >> year >> m;     year -= 2000;     for (int i = 1; i <= m; ++i) {       int x, y;       cin >> x >> y;       --x, --y;       graph[x].push_back(y);       rev_graph[y].push_back(x);     }     findPermutation(0, 0);     if ((int)sol.size() != n) {       cout <<  The times have changed n ;       return 0;     }     for (int i = 0; i < n - 1; ++i) cout << sol[i] + 1 <<    ;     cout << sol[n - 1] + 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1e5;   const int Inf = 1 << 30;   double a[105];   int main() {     int n, cnt;     double pos, pp, ans, sum;     while (~scanf( %d , &n)) {       sum = 0;       pos = 4.5;       pos *= n;       for (int i = 1; i <= n; i++) {         scanf( %lf , &a[i]);         sum += a[i];       }       cnt = 0;       sort(a + 1, a + 1 + n);       while (sum < pos) {         cnt++;         sum += 5 - a[cnt];       }       printf( %d n , cnt);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> tree[300003];   int colors[300003];   int blueC[300003];   int redC[300003];   int ans = 0;   pair<int, int> dfs(int nd, int pr) {     int blue = (colors[nd] == 2);     int red = (colors[nd] == 1);     for (int i = 0; i < (int)tree[nd].size(); i++) {       if (tree[nd][i] == pr) continue;       pair<int, int> sub = dfs(tree[nd][i], nd);       if ((bool)sub.first ^ (bool)sub.second) {         if (sub.first)           ans += (blueC[0] == sub.first);         else           ans += (redC[0] == sub.second);       }       blue += sub.first;       red += sub.second;     }     return make_pair(blue, red);   }   void solve() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &colors[i]);     int u, v;     for (int i = 1; i < n; i++) {       scanf( %d %d , &u, &v);       tree[u].push_back(v);       tree[v].push_back(u);     }     memset(blueC, 0, sizeof(blueC));     memset(redC, 0, sizeof(redC));     for (int i = 1; i <= n; i++) {       if (colors[i] == 1)         redC[0]++;       else if (colors[i] == 2)         blueC[0]++;     }     dfs(1, 0);     printf( %d , ans);   }   int main() { solve(); }
#include <bits/stdc++.h>   using namespace std;   int n, p[50], sz[50], v[50], id[50], ct, lg[(1 << 24)], bc[(1 << 24)];   unsigned int f[(1 << 24)], av[50];   char s[50][50];   vector<int> vc[50];   int fd(int x) { return p[x] == x ? x : p[x] = fd(p[x]); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) p[i] = i;     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);       for (int j = 1; j <= n; j++)         if (s[i][j] ==  A ) {           int xx = fd(i), yy = fd(j);           p[xx] = yy;         }     }     for (int i = 1; i <= n; i++) {       int xx = fd(i);       sz[xx]++;       vc[xx].push_back(i);     }     for (int i = 1; i <= n; i++)       if (sz[i] > 1 && p[i] == i) {         id[i] = ct++;         for (auto x : vc[i])           for (auto y : vc[i])             if (s[x][y] ==  X ) {               printf( -1 n );               return 0;             }       }     for (int i = 0; i < ct; i++) bc[1 << i] = i;     if (!ct) {       printf( %d , n - 1);       return 0;     }     for (int i = 1; i <= n; i++) {       if (p[i] == i && sz[i] == 1) continue;       for (int j = 1; j <= n; j++) {         if (p[j] == j && sz[j] == 1) continue;         if (s[i][j] ==  X ) {           int xx = fd(i), yy = fd(j);           v[id[xx]] |= (1 << id[yy]);           v[id[yy]] |= (1 << id[xx]);         }       }     }     f[0] = 1;     for (int i = 1; i < (1 << ct); i++) {       int lb = i & -i;       lg[i] = lg[i - lb] + 1;       f[i] = f[i - lb] + f[(i - lb) ^ (i & v[bc[lb]])];     }     for (int i = 0; i < (1 << ct); i++) f[i]--;     for (int i = 0; i < (1 << ct); i++) {       unsigned int r = 1;       for (int j = 1; j <= ct; j++) {         r *= f[i];         if ((ct - lg[i]) & 1)           av[j] -= r;         else           av[j] += r;       }     }     for (int j = 1; j <= ct; j++)       if (av[j]) {         printf( %d , j + n - 1);         return 0;       }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MAX = 1123;   int ans[MAX];   int main() {     int n;     cin >> n;     memset(ans, INF, sizeof(ans));     for (int i = 0; i < 10; i++) {       vector<int> data1, data2;       for (int j = 1; j <= n; j++) {         if (j & (1 << i))           data1.push_back(j);         else           data2.push_back(j);       }       if (data1.size()) {         cout << data1.size() << endl;         for (int j = 0; j < data1.size(); j++) {           cout << data1[j] <<    ;         }         for (int j = 1; j <= n; j++) {           int t;           cin >> t;           if ((j & (1 << i)) == 0) {             ans[j] = min(ans[j], t);           }         }         cout << endl;         fflush(stdout);       }       if (data2.size()) {         cout << data2.size() << endl;         for (int j = 0; j < data2.size(); j++) {           cout << data2[j] <<    ;         }         for (int j = 1; j <= n; j++) {           int t;           cin >> t;           if (j & (1 << i)) ans[j] = min(ans[j], t);         }         cout << endl;         fflush(stdout);       }     }     fflush(stdout);     cout << -1 << endl;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int r, c, i, j, flag = 0;     cin >> r >> c;     char a[r + 2][c + 2];     for (i = 0; i < (r + 2); i++) {       for (j = 0; j < (c + 2); j++) a[i][j] =  . ;     }     for (i = 1; i < (r + 1); i++) {       for (j = 1; j < (c + 1); j++) {         cin >> a[i][j];       }     }     for (i = 1; i < (r + 1); i++) {       for (j = 1; j < (c + 1); j++) {         if (a[i][j] ==  S ) {           if (a[i + 1][j] ==  W  || a[i][j + 1] ==  W  || a[i - 1][j] ==  W  ||               a[i][j - 1] ==  W ) {             flag = 1;             cout <<  No n ;             break;           }         }       }       if (flag == 1) break;     }     if (flag == 0) {       cout <<  Yes n ;       for (i = 1; i < (r + 1); i++) {         for (j = 1; j < (c + 1); j++) {           if (a[i][j] ==  . )             cout <<  D ;           else             cout << a[i][j];         }         cout <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<int> adj[100005];   int din[100005];   queue<int> que;   int Vec[100005], V_size;   int a, b, c, d, cur;   bool succ;   int main() {     int Test;     scanf( %d , &Test);     while (Test--) {       scanf( %d%d%d , &n, &m, &k);       for (int i = 1; i <= n; i += 1) {         adj[i].clear();         din[i] = 0;       }       while (!que.empty()) que.pop();       for (int i = 1; i <= m; i += 1) {         scanf( %d%d , &a, &b);         adj[a].push_back(b), adj[b].push_back(a);         din[a]++, din[b]++;       }       if (k == 1) {         puts( 2 );         puts( 1 );         continue;       }       if (k > ((int)sqrt((double)2 * m) + 2)) {         puts( -1 );         continue;       }       for (int i = 1; i <= n; i += 1) sort(adj[i].begin(), adj[i].end());       for (int i = 1; i <= n; i += 1)         if (din[i] < (k - 1)) que.push(i);       while (!que.empty()) {         cur = que.front();         que.pop();         for (auto& i : adj[cur]) {           if (din[i] <= 0) continue;           din[cur]--, din[i]--;           if (din[i] == (k - 2)) que.push(i);         }         if (din[cur] != 0) {           cout <<  fds ;           return 0;         }       }       for (int i = 1; i <= n; i += 1)         if (din[i] == (k - 1)) que.push(i);       bool nex_loop = false;       while (!que.empty()) {         cur = que.front();         que.pop();         if (din[cur] == (k - 1)) {           V_size = 0;           for (auto& i : adj[cur]) {             if (din[i] <= 0) continue;             Vec[V_size++] = i;           }           succ = true;           for (int i = 0; i <= V_size - 1; i += 1) {             for (int j = i + 1; j <= V_size - 1; j += 1) {               a = Vec[i], b = Vec[j];               auto it = lower_bound(adj[a].begin(), adj[a].end(), b);               if ((it == adj[a].end()) || (*it != b)) {                 succ = false;                 break;               }             }             if (!succ) break;           }           if (succ) {             puts( 2 );             printf( %d  , cur);             for (int i = 0; i <= V_size - 1; i += 1) printf( %d  , Vec[i]);             printf(  n );             nex_loop = true;             break;           }         }         for (auto& i : adj[cur]) {           if (din[i] <= 0) continue;           din[cur]--, din[i]--;           if (din[i] == (k - 1)) que.push(i);         }       }       if (nex_loop) continue;       V_size = 0;       for (int i = 1; i <= n; i += 1) {         if (din[i] <= 0) continue;         Vec[V_size++] = i;       }       if (V_size == 0)         puts( -1 );       else {         printf( 1 %d n , V_size);         for (int i = 0; i <= V_size - 1; i += 1) {           printf( %d  , Vec[i]);         }         printf(  n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     int n, num, ctr;     string s;     while (t--) {       cin >> n >> s;       num = 0;       ctr = 0;       for (char ch : s) {         if (ch ==  ) ) {           num--;           if (num < 0) ctr++, num++;         } else           num++;       }       cout << ctr <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     cin >> t;     while (t--) {       int n, m, i, x = 0, y, z;       cin >> n;       string s[n + 1];       for (int i = 0; i < n; i++) cin >> s[i];       vector<pair<int, int>> v;       if (s[0][1] == s[1][0]) {         if (s[n - 1][n - 2] == s[0][1]) {           v.push_back({n, n - 1});         }         if (s[n - 2][n - 1] == s[0][1]) {           v.push_back({n - 1, n});         }       } else if (s[n - 1][n - 2] == s[n - 2][n - 1]) {         if (s[n - 1][n - 2] == s[0][1]) {           v.push_back({1, 2});         }         if (s[n - 2][n - 1] == s[1][0]) {           v.push_back({2, 1});         }       } else {         if (s[0][1] !=  0 ) {           v.push_back({1, 2});         }         if (s[1][0] !=  0 ) {           v.push_back({2, 1});         }         if (s[n - 2][n - 1] !=  1 ) {           v.push_back({n - 1, n});         }         if (s[n - 1][n - 2] !=  1 ) {           v.push_back({n, n - 1});         }       }       cout << v.size() << endl;       for (int i = 0; i < v.size(); i++) {         cout << v[i].first <<     << v[i].second << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007LL;   const long long MXSZ = 1000000LL;   long long fpow(long long b, long long e) {     long long ret = 1;     while (e > 0) {       if (e & 1) (ret *= b) %= mod;       (b *= b) %= mod;       e >>= 1;     }     return ret;   }   long long fac[MXSZ + 5];   void prec() {     fac[0] = 1;     for (long long i = 1; i <= MXSZ; i++) {       fac[i] = fac[i - 1] * i;       fac[i] %= mod;     }   }   long long inv(long long u) { return fpow(fac[u], mod - 2); }   inline long long comb(long long n, long long k) {     long long ret = fac[n];     (ret *= inv(k)) %= mod;     (ret *= inv(n - k)) %= mod;     return ret;   }   inline long long perm(long long n, long long k) {     long long ret = fac[n];     (ret *= inv(n - k)) %= mod;     return ret;   }   long long n, m, a, b;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     prec();     cin >> n >> m >> a >> b;     long long tot = 0LL;     for (int btwn = 0; btwn < n - 2; btwn++) {       if (m < btwn + 1) break;       long long vleft = n - btwn - 2;       long long k = btwn + 2;       long long curr = comb(m - 1, btwn) * perm(n - 2, btwn);       curr %= mod;       curr *= k;       curr %= mod;       curr *= fpow(n, n - k - 1);       curr %= mod;       curr *= fpow(m, vleft);       curr %= mod;       tot += curr;       tot %= mod;     }     if (m - 1 >= n - 2) {       long long curr = comb(m - 1, n - 2) * perm(n - 2, n - 2);       curr %= mod;       tot += curr;       tot %= mod;     }     cout << tot <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long dist[200001] = {0};   int n, ans[200001], Val[200001], lg, tree[200001][20];   vector<vector<pair<int, long long> > > G(200001);   void Build(int v, int par = 0) {     tree[v][0] = par;     for (int i = 1; i <= lg; i++) tree[v][i] = tree[tree[v][i - 1]][i - 1];     for (int i = 0; i < G[v].size(); i++) {       dist[G[v][i].first] = dist[v] + G[v][i].second;       Build(G[v][i].first, v);     }   }   int FinalDfs(int v) {     for (int i = 0; i < G[v].size(); i++) ans[v] += FinalDfs(G[v][i].first) + 1;     return ans[v];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &Val[i]);     while ((1 << lg) <= n) lg++;     lg--;     for (int i = 2; i <= n; i++) {       long long dis;       int par;       scanf( %d%I64d , &par, &dis);       G[par].push_back(make_pair(i, dis));     }     Build(1);     for (int i = 1; i <= n; i++) {       if (dist[i] <= Val[i]) continue;       int Pos = i;       int Value = Val[i];       for (int j = lg; j >= 0; j--)         if (dist[Pos] - dist[tree[Pos][j]] <= Value && tree[Pos][j] != 0) {           Value -= dist[Pos] - dist[tree[Pos][j]];           Pos = tree[Pos][j];         }       ans[tree[Pos][0]]--;     }     FinalDfs(1);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string A, B;   int used[10][2];   int memo[10][2];   int m;   int dp(int pos, int mayor) {     if (pos == m) return mayor;     if (used[pos][mayor]) return memo[pos][mayor];     used[pos][mayor] = 1;     int &dev = memo[pos][mayor] = 0;     if (B[pos] ==  ? ) {       if (mayor) {         if (pos == 0) {           dev = dp(pos + 1, 1);         } else {           dev = dp(pos + 1, 1);         }       } else {         for (int c = A[pos]; c <=  9 ; ++c) {           if (pos == 0 && c ==  0 ) continue;           dev |= dp(pos + 1, c > A[pos]);         }       }     } else {       if (!mayor && B[pos] < A[pos])         return 0;       else         dev = dp(pos + 1, mayor ? 1 : (B[pos] > A[pos]));     }     return dev;   }   string rec(int pos, int mayor) {     if (pos == m) return   ;     if (B[pos] ==  ? ) {       if (mayor) {         if (pos == 0) {           return string(1,  1 ) + rec(pos + 1, 1);         } else {           return string(1,  0 ) + rec(pos + 1, 1);         }       } else {         for (int c = A[pos]; c <=  9 ; ++c) {           if (pos == 0 && c ==  0 ) continue;           if (dp(pos + 1, c > A[pos]))             return string(1, c) + rec(pos + 1, c > A[pos]);         }       }     } else {       return string(1, B[pos]) + rec(pos + 1, mayor ? 1 : (B[pos] > A[pos]));     }   }   int toi(string &s) {     istringstream in(s);     int x;     in >> x;     return x;   }   string tos(int x) {     ostringstream out;     out << x;     return out.str();   }   string S[100000 + 5];   void fix(string &s) {     for (int i = 0; i < ((int)(s).size()); ++i) {       if (s[i] ==  ? ) {         if (i == 0)           s[i] =  1 ;         else           s[i] =  0 ;       }     }   }   bool f(string &s, string &t) {     if (((int)(s).size()) > ((int)(t).size())) return 0;     for (int i = 0; i < ((int)(s).size()); ++i)       if (s[i] ==  ? ) return 0;     if (((int)(s).size()) < ((int)(t).size())) {       fix(t);       return 1;     }     memset(used, 0, sizeof(used));     m = ((int)(s).size());     A = s, B = t;     if (!dp(0, 0)) return 0;     string C = rec(0, 0);     t = C;     return 1;   }   int main() {     ios_base ::sync_with_stdio(0);     int n;     while (cin >> n) {       S[0] =   ;       bool possible = 1;       for (int i = 0; i < n; ++i) {         string s;         cin >> s;         bool ok = f(S[i], s);         if (!ok) possible = 0;         S[i + 1] = s;       }       if (!possible)         cout <<  NO  <<   n ;       else {         cout <<  YES  <<   n ;         for (int i = 0; i < n; ++i) cout << S[i + 1] <<   n ;       }     }   }