malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   string s[1100];   int vis[1100];   int num[100];   int main() {     ios::sync_with_stdio(false);     int n, len, k;     cin >> n >> len >> k;     string st;     cin >> st;     for (int i = 0; i < st.size(); i++) {       num[st[i] -  a ]++;     }     int l = 0, r = 25;     for (int i = k + 1; i <= n; i++) {       for (int j = 1; j <= len; j++) {         while (!num[r] && r > l) r--;         s[i] += (char)(r +  a );         num[r]--;       }     }     int now = 0;     int pos = 0;     while (1) {       for (int i = 1; i <= k; i++) {         if (vis[i]) continue;         if (pos >= 1 && i != k && (int)(s[i][pos - 1] -  a ) < now) {           for (int j = pos + 1; j <= len; j++) {             while (!num[r] && r > l) r--;             s[i] += (char)(r +  a );             num[r]--;           }           vis[i] = 1;           continue;         }         while (!num[l] && r > l) l++;         s[i] += (char)(l +  a );         num[l]--;         if (i == k) {           now = l;         }       }       pos++;       if (pos == len) break;     }     sort(s + 1, s + 1 + n);     for (int i = 1; i <= n; i++) cout << s[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int T, n, m, nw, a[110][110], id[110][110], pos[110], p[110];   char ch;   bool cmp(int i, int j) { return a[m][i] < a[m][j]; }   queue<int> q;   void find(int x) {     p[x] = 0;     int y = id[x][pos[x]];     pos[x]--;     if (!p[y])       p[y] = x, p[x] = y;     else if (a[y][x] > a[y][p[y]]) {       p[p[y]] = 0, q.push(p[y]);       p[y] = x, p[x] = y;     }     if (!p[x]) q.push(x);   }   void work() {     for (int i = 1; i <= 2 * n; i++) {       for (int j = 1; j <= n; j++) id[i][j] = (i <= n) * n + j;       m = i;       sort(id[i] + 1, id[i] + n + 1, cmp);       p[i] = 0;     }     for (int i = 1; i <= n; i++) pos[i] = n, q.push(i);     while (!q.empty()) {       int u = q.front();       q.pop();       find(u);     }     for (int i = 1; i <= n; i++) assert(p[i]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         assert(!(a[p[i]][i] < a[p[i]][j] && a[j][p[i]] > a[j][p[j]]));   }   void solve() {     bool falg = 0;     scanf( %d , &n);     memset(a, 0, sizeof(a)), memset(id, 0, sizeof(id));     for (int i = 1; i <= n; i++)       for (int j = n + 1; j <= 2 * n; j++)         scanf( %d , &a[i][j]), a[j][i] = -a[i][j];     puts( B ), fflush(stdout);     scanf(  %c%d , &ch, &nw);     if (ch ==  I ) falg ^= 1;     if (nw > n) falg ^= 1;     if (falg)       for (int i = 1; i <= 2 * n; i++)         for (int j = 1; j <= 2 * n; j++) a[i][j] = -a[i][j];     work();     printf( %d n , p[nw]), fflush(stdout);     while (~scanf( %d , &nw)) {       if (nw == -1) return;       printf( %d n , p[nw]), fflush(stdout);     }   }   int main() {     scanf( %d , &T);     while (T--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 9;   vector<int> getBorder(const string str) {     int n = str.size();     vector<int> border(n, -1);     for (int i = 1, j = -1; i < n; i++) {       while (j >= 0 && str[i] != str[j + 1]) {         j = border[j];       }       if (str[i] == str[j + 1]) j++;       border[i] = j;     }     return border;   }   int matchPattern(string txt, string pat, const vector<int> border) {     int freq = 0;     for (int i = 0, j = -1; i < (int)txt.size(); i++) {       while (j >= 0 && txt[i] != pat[j + 1]) {         j = border[j];       }       if (txt[i] == pat[j + 1]) {         j++;       }       if (j + 1 == (int)pat.size()) {         freq++;         j = border[j];       }     }     return freq;   }   string cut(string s, int p, bool pref = true) {     if (!pref) {       reverse(s.begin(), s.end());     }     while ((int)s.size() >= p) s.pop_back();     if (!pref) {       reverse(s.begin(), s.end());     }     return s;   }   const int ms = 210;   string pref[ms], suf[ms];   long long trans[ms];   int bk = 14;   long long getFreq(int n, string pat, long long k) {     std::vector<long long> f(n + 1, 0);     if (pat ==  0 ) f[0]++;     if (pat ==  1 ) f[1]++;     auto border = getBorder(pat);     for (int i = 2; i <= n; i++) {       long long cur = trans[i - 2];       if (i < bk + 4) {         string curString = cut(suf[i - 2], (int)pat.size(), false) +                            cut(pref[i - 1], (int)pat.size());         cur = matchPattern(curString, pat, border);       }       f[i] = f[i - 1] + f[i - 2] + cur;       if (f[i] > k) return k + 1;       trans[i] = cur;     }     return min(f[n], k + 1);   }   int main() {     pref[0] = suf[0] =  0 ;     pref[1] = suf[1] =  1 ;     for (int i = 2; i < ms; i++) {       pref[i] = cut(pref[i - 2] + pref[i - 1], ms);       suf[i] = cut(suf[i - 2] + suf[i - 1], ms, false);     }     int n, m;     long long k;     cin >> n >> k >> m;     if (n == 0) {       cout <<  0 n ;       return 0;     }     if (n == 1) {       cout <<  1 n ;       return 0;     }     string ans;     k--;     while (k >= 0 && (int)ans.size() < m) {       if (ans.size() > 0 &&           suf[n].substr(suf[n].size() - ans.size(), ans.size()) == ans) {         k--;       }       if (k < 0) break;       ans +=  0 ;       long long got = getFreq(n, ans, k);       if (got <= k) {         ans.back() =  1 ;         k -= got;       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int tag[10];   int main() {     string a, b;     while (cin >> a >> b) {       memset(tag, 0, sizeof(tag));       ;       int len = a.size();       int len1 = b.size();       for (int i = 0; i < len1; i++) {         int num = b[i] -  0 ;         tag[num]++;       }       for (int i = 0; i < len; i++) {         int p = a[i] -  0 ;         int j = 9;         while (j > p) {           if (!tag[j]) j--;           if (tag[j] && j > p) {             a[i] = j +  0 ;             tag[j]--;             break;           }         }       }       for (int i = 0; i < len; i++) cout << a[i];       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   int n, k;   bool d[1001];   long long dp[1001][2][1001];   int cnt[10001];   long long DP(int i, bool f, int j) {     if (i == n) return cnt[j] == k;     if (dp[i][f][j] != -1) return dp[i][f][j];     long long ret = 0;     if (d[i] == 1)       ret = (ret + DP(i + 1, f, j + 1)) % mod;     else if (d[i] == 0 and f)       ret = (ret + DP(i + 1, f, j + 1)) % mod;     ret = (ret + DP(i + 1, f | (d[i] == 1), j)) % mod;     return dp[i][f][j] = ret;   }   int main() {     for (int i = 1; i <= 1000; i++) {       cnt[i] = 1 + cnt[__builtin_popcount(i)];     }     string s;     cin >> s;     n = s.size();     cin >> k;     int i = 0;     for (char c : s) d[i++] = c -  0 ;     memset(dp, -1, sizeof dp);     cout << DP(0, 0, 0) - (k == 1);   }
#include <bits/stdc++.h>   using namespace std;   const long long int INF = 1e18, N = 2e6 + 3, BASE = 130;   int main() {     long long int n, k;     cin >> n >> k;     vector<int> arr(n), up(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     for (int i = n - 2; i >= 0; i--) {       if (arr[i + 1] == 1) {         up[i] = up[i + 1] + 1;       } else {         up[i] = 0;       }     }     long long int ans = 0;     for (int i = 0; i < n; i++) {       int ind = i;       long long int p = 1;       long long int sum = 0;       int cnt = 0;       while (ind < n && cnt <= 20) {         if (p > INF / arr[ind]) {           break;         }         p *= arr[ind];         sum += arr[ind];         cnt++;         if (p % sum == 0 && p / sum == k) {           ans++;         }         if (up[ind]) {           if (p % k == 0) {             long long int num = p / k;             if (sum + 1 <= num && num <= sum + up[ind]) {               ans++;             }           }         }         sum += up[ind];         ind = ind + 1 + up[ind];       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   void PLAY() {     cout << fixed << setprecision(10);     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     vector<string> x(n), y(m), ans;     for (int i = 0; i < n; i++) {       cin >> x[i];     }     for (int i = 0; i < m; i++) {       cin >> y[i];     }     long long q;     cin >> q;     while (q--) {       long long a;       cin >> a;       a--;       cout << x[a % n] + y[a % m] << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:64000000 )   using namespace std;   const double PI = 2 * acos(0.0);   const double eps = 1e-9;   template <class T>   T jog(T a, T b) {     return a + b;   }   template <class T>   T bog(T a, T b) {     return a - b;   }   template <class T>   T gon(T a, T b) {     return a * b;   }   template <class T>   T sq(T x) {     return x * x;   }   template <class T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <class T>   T lcm(T a, T b) {     return (a / gcd(a, b)) * b;   }   template <class T>   T power(T a, T p) {     T res = 1, x = a;     while (p) {       if (p & 1) res = res * x;       x = x * x;       p >>= 1;     }     return res;   }   template <class T>   T cordinatlenth(T a, T b, T c, T d) {     return sqrt(sq(a - c) + sq(b - d));   }   template <class T>   string toString(T n) {     ostringstream oss;     oss << n;     oss.flush();     return oss.str();   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long bigmod(long long a, long long p, long long mod) {     long long res = 1, x = a;     while (p) {       if (p & 1) res = (res * x) % mod;       x = (x * x) % mod;       p >>= 1;     }     return res;   }   int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1};   int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1};   int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1};   int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2};   void input();   const int INF = 2147483647;   const long long LINF = 9223372036854775807ll;   int n, in[1000010];   int main() {     input();     return 0;   }   void input() {     memset(in, 0, sizeof(in));     cin >> n;     for (int i = 0, tmp; i < n; i++) {       scanf( %d , &tmp);       in[tmp]++;     }     long long ans = 0;     for (int i = 0; i < 1000005; i++) {       if (in[i] == 0 || in[i] == 1) continue;       while (in[i] > 1) {         ans++;         in[i + 1]++;         in[i]--;       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int changed;   int answer1, answer2 = 1023;   int main() {     cin >> n;     int i;     for (i = 0; i < n; i++) {       char tp;       cin >> tp;       if (tp ==  | ) {         int number;         cin >> number;         answer1 |= number;         answer2 |= number;         changed |= number;       } else if (tp ==  & ) {         int number;         cin >> number;         answer1 &= number;         answer2 &= number;         number = 1023 - number;         changed |= number;       } else {         int number;         cin >> number;         answer1 ^= number;         answer2 ^= number;       }     }     int answer11 = 0;     int answer22 = 0;     int answer33 = 0;     for (i = 0; i < 10; i++) {       if ((answer1 & (1 << i)) && (answer2 & (1 << i))) {         answer11 += 1 << i;       } else {         if ((answer1 & (1 << i)) && (!(answer2 & (1 << i)))) {           answer33 += 1 << i;           answer22 += 1 << i;         }         if ((!(answer1 & (1 << i))) && (answer2 & (1 << i))) {           answer22 += 1 << i;         }       }     }     cout << 3 << endl;     cout <<  &   << answer22 << endl;     cout <<  |   << answer11 << endl;     cout <<  ^   << answer33 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   string s, t;   char c[N];   int cs[2][N], ct[2][N], q;   int main() {     scanf(  %s , c);     s = c;     scanf(  %s , c);     t = c;     int at = 0;     for (auto &i : s) {       at++;       if (i ==  C  || i ==  B ) {         i =  B ;         cs[1][at] = cs[1][at - 1] + 1;       } else {         cs[1][at] = cs[1][at - 1];         cs[0][at] = cs[0][at - 1] + 1;       }     }     at = 0;     for (auto &i : t) {       at++;       if (i ==  C  || i ==  B ) {         i =  B ;         ct[1][at] = ct[1][at - 1] + 1;       } else {         ct[1][at] = ct[1][at - 1];         ct[0][at] = ct[0][at - 1] + 1;       }     }     scanf( %d , &q);     for (int i = 0, a, b, c, d; i < q; i++) {       scanf( %d%d%d%d , &a, &b, &c, &d);       int difs = cs[1][b] - cs[1][a - 1];       int dift = ct[1][d] - ct[1][c - 1];       bool thereis = 0;       int need = dift - difs;       int trails = min(b - a + 1, cs[0][b]);       int trailt = min(d - c + 1, ct[0][d]);       if (need & 1) {         printf( 0 );         continue;       }       if (need < 0) {         printf( 0 );         continue;       }       if (!need) {         if (trails < trailt || (trails - trailt) % 3) {           printf( 0 );           continue;         }         printf( 1 );         continue;       }       if (need && trails > trailt) {         trails = trailt;         thereis = 1;       }       if ((trails - trailt) % 3 || trails < trailt) {         printf( 0 );         continue;       }       thereis |= difs;       if (!thereis && need) {         printf( 0 );         continue;       }       printf( 1 );     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T maxi(T a, T b) {     return a > b ? a : b;   }   template <typename T>   inline T mini(T a, T b) {     return a < b ? a : b;   }   template <typename T>   inline T absl(T a) {     return a > 0 ? a : -a;   }   template <typename T>   inline T gcd(T a, T b) {     T t;     if (a < b) {       while (a) {         t = a;         a = b % a;         b = t;       }       return b;     } else {       while (b) {         t = b;         b = a % b;         a = t;       }       return a;     }   }   template <typename T>   inline T ceiling(T a, T b) {     if (b < 0) {       a *= -1;       b *= -1;     }     if ((double)a / (double)b < 0) return 0;     {       if (a % b == 0) return (a / b);       return ((a / b) + 1);     }   }   vector<int> adj[500000];   map<pair<int, int>, int> m;   vector<pair<int, int> > v(500000);   vector<bool> visited(500000, false);   int d;   void dfs(int u, int c) {     visited[u] = true;     for (auto it : adj[u]) {       if (!visited[it]) {         m[{u, it}] = c % d;         ++c;         dfs(it, c);       }     }   }   void input() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n - 1; ++i) {       int s, d;       cin >> s >> d;       --s;       --d;       adj[s].push_back(d);       adj[d].push_back(s);       v[i].first = s;       v[i].second = d;     }     int b[n];     for (int i = 0; i < n; ++i) {       b[i] = adj[i].size();     }     sort(b, b + n, greater<int>());     d = b[k];     cout << d << endl;     dfs(0, 0);     for (int i = 0; i < n - 1; ++i) {       cout << maxi(m[v[i]], m[{v[i].second, v[i].first}]) + 1 <<    ;     }   }   int main() {     input();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   struct node {     int l, r, sum;   } hjt[maxn * 40];   int a[maxn], root[maxn], cnt;   int last[maxn];   void insert(int l, int r, int pre, int &now, int p, int v) {     hjt[++cnt] = hjt[pre];     now = cnt;     hjt[now].sum = hjt[pre].sum + v;     if (l == r) return;     int m = (l + r) >> 1;     if (p <= m)       insert(l, m, hjt[pre].l, hjt[now].l, p, v);     else       insert(m + 1, r, hjt[pre].r, hjt[now].r, p, v);   }   int query(int now, int l, int r, int k) {     if (l == r) {       if (hjt[now].sum <= k) return l;       return l - 1;     }     int mid = (l + r) >> 1;     int tmp = hjt[hjt[now].l].sum;     if (tmp > k)       return query(hjt[now].l, l, mid, k);     else       return query(hjt[now].r, mid + 1, r, k - tmp);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = n; i >= 1; i--) {       if (last[a[i]]) {         insert(1, n, root[i + 1], root[i], last[a[i]], -1);         insert(1, n, root[i], root[i], i, 1);       } else         insert(1, n, root[i + 1], root[i], i, 1);       last[a[i]] = i;     }     for (int i = 1; i <= n; i++) {       int ans = 0, l = 1, r = 0;       while (l <= n) {         r = query(root[l], 1, n, i);         l = r + 1;         ans++;       }       cout << ans;       if (i == n)         cout <<   n ;       else         cout <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int num = 0;   int e[21][21];   int flag[21];   int dfs(int x, int sum, int mid) {     if (x > n) {       num = num + 1;     } else {       mid = dfs(x + 1, sum, mid);       for (int y = 1; y <= n; y++) {         if (num >= k) {           break;         }         if (flag[y] == 0 && e[x][y] != 0 && sum + e[x][y] <= mid) {           flag[y] = 1;           mid = dfs(x + 1, sum + e[x][y], mid);           flag[y] = 0;         }       }     }     return mid;   }   int main() {     int u, v, w;     cin >> n >> m >> k;     for (int i = 0; i <= 20; i++) {       for (int j = 0; j <= 20; j++) {         e[i][j] = 0;       }     }     int l = 0;     int h = n * 1000;     int mid = 0;     for (int i = 0; i < m; i++) {       cin >> u >> v >> w;       e[u][v] = w;     }     while (l < h) {       num = 0;       mid = (l + h) / 2;       mid = dfs(1, 0, mid);       if (num < k) {         l = mid + 1;       } else {         h = mid;       }     }     cout << h << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 50005;   const int MAXD = 55;   const int MOD = 1e9 + 7;   template <typename T>   inline void read(T &AKNOI) {     T x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     AKNOI = flag * x;   }   namespace ModCalculator {   inline void Inc(int &x, int y) {     x += y;     if (x >= MOD) x -= MOD;   }   inline void Dec(int &x, int y) {     x -= y;     if (x < 0) x += MOD;   }   inline int Add(int x, int y) {     Inc(x, y);     return x;   }   inline int Sub(int x, int y) {     Dec(x, y);     return x;   }   inline int Mul(int x, int y) { return 1LL * x * y % MOD; }   }  // namespace ModCalculator   using namespace ModCalculator;   char s[MAXN], sx[MAXD], sy[MAXD];   int n, d, dg[MAXD];   int tot, ch[MAXN][10], fail[MAXN], ed[MAXN], q[MAXN];   int dp[MAXD][MAXN];   void Insert(int l, int r) {     int p = 0;     for (int i = l; i <= r; ++i) {       int x = s[i] -  0 ;       if (!ch[p][x]) ch[p][x] = ++tot;       p = ch[p][x];     }     ed[p] = 1;   }   void GetFail() {     int head = 1, tail = 0;     for (int i = 0; i < 10; ++i) {       if (ch[0][i]) {         q[++tail] = ch[0][i];       }     }     while (head <= tail) {       int u = q[head++];       for (int i = 0; i < 10; ++i) {         if (ch[u][i]) {           fail[ch[u][i]] = ch[fail[u]][i];           q[++tail] = ch[u][i];         } else {           ch[u][i] = ch[fail[u]][i];         }       }     }   }   int DP(int i, int j, bool full) {     if (i > d) return 1;     if (!full && dp[i][j] != -1) return dp[i][j];     int ret = 0, r = (full ? dg[i] : 9);     for (int k = 0; k <= r; ++k) {       if (ed[ch[j][k]]) continue;       Inc(ret, DP(i + 1, ch[j][k], full && (k == r)));     }     if (!full) dp[i][j] = ret;     return ret;   }   int Calc(char *ss) {     int ret = 0;     for (int i = 1; i <= d; ++i) {       ret = Add(Mul(ret, 10), (dg[i] = ss[i] -  0 ));     }     return Sub(ret, DP(1, 0, 1));   }   void init() {     scanf( %s , s + 1);     n = strlen(s + 1);     scanf( %s , sx + 1);     scanf( %s , sy + 1);     d = strlen(sx + 1);     int d2 = d / 2;     for (int i = 1; i + d2 - 1 <= n; ++i) {       Insert(i, i + d2 - 1);     }     GetFail();   }   void solve() {     int p = d;     for (; sx[p] ==  0 ; sx[p--] =  9 )       ;     sx[p] -= 1;     memset(dp, -1, sizeof(dp));     printf( %d n , Sub(Calc(sy), Calc(sx)));   }   int main() {     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mdin(long long a, long long m) {     long long r1 = m, r2 = a, t1 = 0, t2 = 1;     while (r2 > 0) {       long long q = r1 / r2;       long long r = r1 % r2;       r1 = r2;       r2 = r;       long long t = (t1 - q * t2) % m;       t1 = t2;       t2 = t;     }     return (t1 + m) % m;   }   long long ex(long long a, long long n, long long m) {     if (n == 0) return 1;     long long ans = ex((a * a) % m, n / 2, m);     if (n % 2) ans = (a * ans) % m;     return ans;   }   void bfs(int r, vector<int> adj[], int n) {     queue<int> q, l;     vector<int> vis(n + 1, 0);     q.push(r);     l.push(1);     while (!q.empty()) {       int nd = q.front();       q.pop();       int lv = l.front();       l.pop();       for (auto it : adj[nd]) {         if (vis[it] == 0) {           vis[it] = 1;           q.push(it);           l.push(lv + 1);         }       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       long long n, k;       cin >> n >> k;       if (k > n - 1) {         cout << 0;         return 0;       }       long long f[n + 1], inf[n + 1];       f[0] = 1;       inf[0] = 1;       for (long long i = 1; i <= n; i++) {         f[i] = (i * f[i - 1]) % 998244353;         inf[i] = mdin(f[i], 998244353);       }       if (k == 0) {         cout << f[n] << endl;       } else {         long long ans = (((f[n] * inf[n - k]) % 998244353) * inf[k]) % 998244353;         k = n - k;         long long ml = 0;         int s = 1;         for (long long i = 0; i <= k - 1; i++) {           long long cm = (((f[k] * inf[i]) % 998244353) * inf[k - i]) % 998244353;           long long t = k - i;           long long et = ex(t, n, 998244353);           ml = (ml + (cm * et * s) % 998244353 + 998244353) % 998244353;           s *= -1;         }         ans = (((ml * ans) % 998244353) * 2) % 998244353;         cout << ans << endl;       }     }     return 0;   }