malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int fail[1000006];   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     fail[-1] = fail[0] = -1;     for (int i = 1; i < n; ++i) {       int ptr = i - 1;       while (s[i] != s[fail[ptr] + 1] && ptr > 0) ptr = fail[ptr];       fail[i] = fail[ptr] + (s[i] == s[fail[ptr] + 1]);     }     for (int i = 0; i < n; ++i) {       int le = i - fail[i];       int cc = (i + 1) / le;       if ((i + 1) % le == 0) {         if (cc / k - cc % k >= 0)           cout << 1;         else           cout << 0;       } else {         if (cc / k - cc % k > 0)           cout << 1;         else           cout << 0;       }     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long min(long long x, long long y) { return (x < y) ? x : y; }   long long max(long long x, long long y) { return (x > y) ? x : y; }   const int N = 100009;   int x, y, n, m, i, u, w, z, sum, best, j, k, t, mx;   int a[N];   string s, ans;   int main() {     ios_base::sync_with_stdio(false);     cin >> s;     n = s.size();     while (i < n) {       if (s[i] ==  d  && s[i + 1] ==  o  && s[i + 2] ==  t  && i != 0 &&           i != n - 3) {         ans.push_back( . );         i += 2;       } else if (s[i] ==  a  && s[i + 1] ==  t  && !x && i != 0 && i != n - 2) {         x = 1;         i++;         ans.push_back( @ );       } else         ans.push_back(s[i]);       i++;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> V[1005];   int dis[1005], rdis[1005], G[1005][1005];   void spfa(int s, int e) {     queue<int> Q;     memset(dis, 0x3f, sizeof(dis));     memset(rdis, 0x3f, sizeof(rdis));     Q.push(s);     dis[s] = 0;     while (!Q.empty()) {       int f = Q.front();       for (int i = (0); i < ((int)(V[f]).size()); i++) {         int nex = V[f][i];         if (dis[nex] > dis[f] + 1) {           dis[nex] = dis[f] + 1;           Q.push(nex);         }       }       Q.pop();     }     Q.push(e);     rdis[e] = 0;     while (!Q.empty()) {       int f = Q.front();       for (int i = (0); i < ((int)(V[f]).size()); i++) {         int nex = V[f][i];         if (rdis[nex] > rdis[f] + 1) {           rdis[nex] = rdis[f] + 1;           Q.push(nex);         }       }       Q.pop();     }   }   int main() {     int n, m, s, e;     scanf( %d%d%d%d , &n, &m, &s, &e);     while (m--) {       int a, b;       scanf( %d%d , &a, &b);       V[a].push_back(b);       V[b].push_back(a);       G[a][b] = G[b][a] = 1;     }     int ans = 0;     spfa(s, e);     for (int i = (1); i < (n + 1); i++)       for (int j = (i + 1); j < (n + 1); j++)         if (dis[i] + 1 + rdis[j] >= dis[e] && dis[j] + 1 + rdis[i] >= dis[e] &&             !G[i][j])           ans++;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   typedef long long ll;   using namespace std;      int k;      int addk(int a,int b)   {    int base,aw,bw,ret=0;    for(base=1;base<=max(a,b);base*=k)    {     aw=a/base%k;     bw=b/base%k;     ret+=(aw+bw)%k*base;    }     return ret;   }      int subk(int a,int b)   {    int base,aw,bw,ret=0;    for(base=1;base<=max(a,b);base*=k)    {     aw=a/base%k;     bw=b/base%k;     ret+=(aw-bw+k)%k*base;    }     return ret;   }      int main()   {    int T,n,y,i,r;    scanf( %d ,&T);    while(T--)    {     scanf( %d%d ,&n,&k);     for(i=0;i<n;i++)     {      if(i==0)       y=0;      else if(i&1)       y=subk(i-1,i);      else       y=subk(i,i-1);      printf( %d n ,y);      fflush(stdout);      scanf( %d ,&r);      if(r==1)       break;     }    }   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   const int maxn = 1e5 + 10;   stack<int> max_seen;   int ans[maxn];   vector<int> to[maxn];   void calc(int a) {     int s = max(a - k, 0), t = min(n, a + k + 1);     if (max_seen.empty())       ans[a] += t - s;     else       ans[a] += t - max(max_seen.top(), s);     max_seen.push(t);     for (int i = 0; i < to[a].size(); i++) {       ans[to[a][i]] = ans[a];       calc(to[a][i]);     }     max_seen.pop();   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (x > 0) to[x - 1].push_back(i);     }     for (int i = 0; i < n; i++)       if (ans[i] == 0) calc(i);     for (int i = 0; i < n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     long ans = 0;     for (int i = 0; i < n; i++) {       if ((s[i] -  0 ) % 2 == 0) ans += (i + 1);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 35;   int n, m;   int a[MAXN][MAXN];   int part[MAXN][MAXN], partn = 0;   long long ans = 0;   int x[MAXN][2];   int res[MAXN][MAXN];   int c[MAXN];   int u[MAXN];   void read() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       char tmp[35];       scanf( %s , tmp + 1);       for (int j = 1; j <= m; j++) {         if (tmp[j] ==  . )           a[i][j] = 0;         else if (tmp[j] >=  a )           a[i][j] = tmp[j] -  a  + 1;         else           a[i][j] = tmp[j] -  A  + 26 + 1;       }     }   }   void prepare() {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] != 0 && !part[i][j]) {           partn++;           part[i][j] = part[i][j + 1] = part[i + 1][j] = part[i + 1][j + 1] =               partn;         }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] != 0) {           int k = a[i][j];           if (x[k][0] == 0)             x[k][0] = part[i][j];           else             x[k][1] = part[i][j];         }     for (int i = 1; i <= 28; i++) {       if (x[i][0] > x[i][1]) x[i][0] ^= x[i][1] ^= x[i][0] ^= x[i][1];     }   }   bool chk(int p) {     bool e[10][10];     memset(e, 0, sizeof(e));     for (int i = 1; i <= 28; i++)       if (x[i][1] <= p) {         int f = c[x[i][0]], g = c[x[i][1]];         if (f > g) f ^= g ^= f ^= g;         if (e[f][g]) return false;         e[f][g] = true;       }     return true;   }   void dfs(int p) {     if (p > partn) {       if (ans == 0) {         for (int i = 1; i <= n; i++)           for (int j = 1; j <= m; j++)             if (a[i][j] != 0) res[i][j] = c[part[i][j]];       }       ans += 5040;       return;     }     for (int i = 0; i <= 6; i++)       if (u[i] <= 1) {         c[p] = i;         if (!chk(p)) continue;         u[i]++;         dfs(p + 1);         u[i]--;         if (u[i] == 0) return;       }   }   void work() { dfs(1); }   void print() {     printf( %I64d n , ans);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (a[i][j] == 0)           printf( . );         else           printf( %d , res[i][j]);       }       printf(  n );     }   }   int main() {     read();     prepare();     work();     print();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     long long c = n, maxi = INT_MIN, mini = INT_MIN, t = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (maxi != a[i]) {         maxi = a[i];         mini = a[i];         t = 1;       } else {         t++;         c += t - 1;       }     }     cout << c << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ar[305][305];   vector<pair<int, int> > poss[90005];   int dp[305][305];   vector<int> col[305];   int vis[305];   int n, m, p;   void pr() {}   void solve() {     memset(dp, 127, sizeof(dp));     ;     scanf( %d %d %d , &n, &m, &p);     for (int i = 0; i < (int)(n); i++) {       for (int j = 0; j < (int)(m); j++) {         scanf( %d , &ar[i][j]);         poss[ar[i][j]].push_back(make_pair(i, j));         if (ar[i][j] == 1) {           dp[i][j] = i + j;         }       }     }     for (int val = 2; val <= p; val++) {       memset(vis, 0, sizeof(vis));       ;       for (int i = 0; i < (int)(m); i++) {         col[i].clear();       }       for (int i = 0; i < poss[val].size(); i++) {         pair<int, int> a = poss[val][i];         col[a.second].push_back(a.first);       }       for (int i = 0; i < poss[val - 1].size(); i++) {         pair<int, int> a = poss[val - 1][i];         vis[a.first] = 1;       }       for (int i = 0; i < n; i++) {         if (!vis[i]) continue;         int best = 1e9;         for (int j = 0; j < m; j++) {           best++;           if (ar[i][j] == val - 1) best = min(best, dp[i][j]);           for (int k = 0; k < col[j].size(); k++) {             dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i));           }         }         best = 1e9;         for (int j = m; j >= 0; j--) {           best++;           if (ar[i][j] == val - 1) best = min(best, dp[i][j]);           for (int k = 0; k < col[j].size(); k++) {             dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i));           }         }       }     }     printf( %d , dp[poss[p][0].first][poss[p][0].second]);   }   int main() {     int TC = 1;     for (int ZZ = 1; ZZ <= TC; ZZ++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int cnt[1001] = {0}, a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];       cnt[a[i]] = i;     }     int vis[1001] = {0};     vector<int> res;     for (int i = 0; i < n; i++) {       if (cnt[a[i]] == i) res.push_back(a[i]);     }     cout << res.size() << endl;     for (int i = 0; i < res.size(); ++i) cout << res[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     char ar[n + 2];     char br[n + 2];     scanf( %s%s , ar + 1, br + 1);     vector<int> v[2];     for (int i = 1; i <= n; i++) {       if (ar[i] != br[i]) {         if (ar[i] ==  a )           v[0].push_back(i);         else           v[1].push_back(i);       }     }     if ((v[0].size() + v[1].size()) % 2 == 1)       cout << -1 << endl;     else {       int res = v[0].size() + v[1].size();       res /= 2;       if (v[0].size() % 2) res += 1;       cout << res << endl;       for (int i = 1; i < v[0].size(); i += 2) {         cout << v[0][i - 1] <<     << v[0][i] << endl;       }       for (int i = 1; i < v[1].size(); i += 2) {         cout << v[1][i - 1] <<     << v[1][i] << endl;       }       if (v[0].size() % 2) {         cout << v[0][v[0].size() - 1] <<     << v[0][v[0].size() - 1] << endl;         cout << v[0][v[0].size() - 1] <<     << v[1][v[1].size() - 1] << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int k;   int n;   int planks[1005];   int g = 0;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> k;     for (int t = 0; t < k; t++) {       cin >> n;       for (int i = 0; i < n; i++) cin >> planks[i];       sort(planks, planks + n, [](int a, int b) { return a > b; });       g = 0;       for (int i = 0; i < n; i++)         if (planks[i] >= i + 1) g = max(g, i + 1);       cout << g <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   long long A[200005];   long long fast_pow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b >>= 1;     }     return res;   }   int main() {     long long div = 1, num = 1;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> A[i];       num = (num * A[i]) % 1000000007;     }     sort(A, A + n);     int idx = 0;     bool chk = true;     while (idx < n) {       long long p = A[idx];       long long cnt = 0;       while (idx < n && p == A[idx]) {         idx++;         cnt++;       }       div = (div * (cnt + 1LL)) % (2LL * (1000000007 - 1));       chk = (chk && (cnt % 2 == 0));     }     div = (div % ((1000000007 - 1) * 2LL)) / 2;     long long ans = fast_pow(num, div);     if (chk) {       idx = 0;       while (idx < n) {         long long p = A[idx];         long long cnt = 0;         while (idx < n && p == A[idx]) {           idx++;           cnt++;         }         ans = (ans * fast_pow(p, cnt / 2)) % 1000000007;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     if ((n == 1 && k > 0) || (k == 0 && n > 1) || k < n / 2)       cout << -1;     else if (k == 0)       cout << 1;     else {       int x = k - (n - 2) / 2;       cout << x <<     << x * 2 <<    ;       for (int i = 1; i <= n - 2; ++i) cout << x * 2 + i <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2001000, mod = 998244353;   int n, a[N], b[N], f[N], ans, sum, inv[N];   void Init() {     f[0] = inv[1] = 1;     f[1] = n;     for (int i = 2; i < N; ++i) {       inv[i] = mod - mod / i * (long long)inv[mod % i] % mod;       f[i] = (long long)f[i - 1] * (n - 1 + i) % mod * inv[i] % mod;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     Init();     for (int i = 1; i <= n; ++i) {       cin >> a[i];       ++b[a[i]];     }     for (int i = 0, j = 0; i < n; ++i) {       j += b[i];       if (j >= i + 2) {         cout << (ans + 1) % mod <<   n ;         return 0;       }       ans = (ans + f[i - j + 1]) % mod;     }     memset(b, 0, sizeof b);     ans = 0;     for (int i = 1; i <= n; ++i) sum += a[i] / n, b[a[i] % n]++;     for (int i = 0; i < n; ++i) {       ans = (ans + f[sum]) % mod;       sum -= b[i] - 1;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct mat {     long double x[51][51];   } mi, ma;   int n, p;   long double pa, push_back, aa[51][51], bb[51][51], dp[51][51], ta, tb, ans;   mat tim(mat a, mat b) {     mat re;     for (int c = 0; c < 51; c++)       for (int d = 0; d < 51; d++) {         re.x[c][d] = 0;         for (int e = 0; e < 51; e++) re.x[c][d] += a.x[c][e] * b.x[e][d];       }     return re;   }   mat po(int p) {     if (p == 0) return mi;     if (p & 1) return tim(ma, po(p - 1));     mat tt = po(p / 2);     return tim(tt, tt);   }   int main() {     scanf( %d%d , &n, &p);     pa = p;     pa /= 1000000007 - 7;     push_back = 1 - pa;     for (int a = 0; a <= 50; a++) aa[a][0] = bb[a][0] = 1;     aa[1][1] = pa;     aa[1][2] = push_back;     bb[1][2] = 1;     for (int a = 2; a <= 50; a++) {       aa[a][1] = pa;       aa[a][2] = pa * pa + push_back;       bb[a][2] = 1;       for (int b = 3; b <= 50; b++) {         aa[a][b] = aa[a][b - 1] * aa[a - 1][b - 1];         bb[a][b] = bb[a][b - 1] * aa[a - 1][b - 1];       }     }     for (int a = 1; a <= 50; a++) dp[1][a] = a;     for (int a = 2; a <= 50; a++)       for (int b = 1; b <= 50; b++) {         ta = tb = 0;         if (b == 1) {           for (int c = 2; c <= 50; c++) {             ta += dp[a - 1][c] * bb[a - 1][c] * (1 - aa[a - 2][c]);             tb += bb[a - 1][c] * (1 - aa[a - 2][c]);           }         } else {           for (int c = 1; c < b; c++) {             ta += dp[a - 1][c] * aa[a - 1][c] * (1 - aa[a - 2][c]);             tb += aa[a - 1][c] * (1 - aa[a - 2][c]);           }         }         dp[a][b] = b + ta / tb;       }     if (n <= 50) {       for (int a = 1; a <= 50; a++)         ans += aa[n][a] * (1 - aa[n - 1][a]) * dp[n][a];     } else {       for (int a = 0; a < 51; a++) mi.x[a][a] = 1;       ma.x[0][0] = 1;       for (int a = 1; a <= 50; a++) {         ma.x[a][0] = a;         if (a == 1) {           ta = 0;           for (int b = 2; b <= 50; b++) {             ma.x[a][b] += bb[50][b] * (1 - aa[50][b]);             ta += bb[50][b] * (1 - aa[50][b]);           }           for (int b = 2; b <= 50; b++) ma.x[a][b] /= ta;         } else {           ta = 0;           for (int b = 1; b < a; b++) {             ma.x[a][b] += aa[50][b] * (1 - aa[50][b]);             ta += aa[50][b] * (1 - aa[50][b]);           }           for (int b = 1; b < a; b++) ma.x[a][b] /= ta;         }       }       ma = po(n - 50);       for (int a = 1; a <= 50; a++) {         ta = ma.x[a][0];         for (int b = 1; b <= 50; b++) ta += ma.x[a][b] * dp[50][b];         ans += aa[50][a] * (1 - aa[50][a]) * ta;       }     }     printf( %.20lf n , (double)ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int sum[51], ind[51];   int main() {     for (int i = 1; i <= 50; ++i) sum[i] = i * (i + 1) / 2;     for (int i = 1; i <= 50; ++i) ind[i] = i;     int n, temp, reqind;     cin >> n;     for (int i = 1; i <= 50; ++i) {       if (n <= sum[i]) {         temp = sum[i];         reqind = i;         break;       }     }     int diff = temp - n;     if (diff == 0) {       cout << reqind << endl;       for (int k = 1; k <= reqind; ++k) cout << ind[k] <<    ;       cout << endl;       return 0;     }     cout << reqind - 1 << endl;     for (int i = 1; i <= reqind; ++i) {       if (i != diff) cout << i <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       long long s, f, t;       cin >> s >> f >> t;       if (s == f) {         cout << t << endl;       } else if (s < f) {         long long t1 = s - 1;         while (t1 < t) {           t1 += (m - s) + (m - 1) + (s - 1);         }         t1 += (f - s);         cout << t1 << endl;       } else if (s > f) {         long long t1 = (m - 1) + (m - s);         while (t1 < t) {           t1 += (m - 1) + (m - s) + (s - 1);         }         t1 += (s - f);         cout << t1 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 405;   const int inf = 0x3f3f3f3f;   int n, m;   int a[maxn];   int f[maxn][maxn][maxn];   template <typename T>   inline void minimize(T &target, const T &value) {     if (target > value) {       target = value;     }   }   void divide(int a[], int f[], int g[], int l, int r, int pl, int pr) {     if (l > r) {       return;     }     int m = (l + r) / 2;     int ptr = l;     for (int i = pl; i < min(m, pr + 1); ++i) {       int tmp = max(f[i], a[m] - a[i]);       if (tmp < g[m]) {         g[m] = tmp;         ptr = i;       }     }     divide(a, f, g, l, m - 1, pl, ptr);     divide(a, f, g, m + 1, r, ptr, pr);   }   void compute(int a[], int n, int f[maxn][maxn]) {     f[0][0] = 0;     for (int i = 1; i < n; ++i) {       divide(a, f[i - 1], f[i], 0, n - 1, 0, n - 1);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);     }     memset(f, inf, sizeof(f));     compute(a, n, f[0]);     for (int i = 0; i < n; ++i) {       compute(a + i, n - i, f[i]);     }     long long ans = 0;     while (m--) {       int s, f, c, r;       scanf( %d%d%d%d , &s, &f, &c, &r), s--, f--;       r++;       r = min(r, f - s);       r = max(r, 1);       ans = max(ans, (long long)c * ::f[s][r][f - s]);     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> pairs[2 * 100000 + 10];   vector<int> adj[100000 + 10];   bool done[100000 + 10];   int result[100000 + 10];   void check(int N) {     bool adj[N + 2][N + 2];     memset(adj, false, sizeof(adj));     for (int i = 1; i <= 2 * N; i++) {       scanf( %d %d , &pairs[i].first, &pairs[i].second);       adj[pairs[i].first][pairs[i].second] = true;       adj[pairs[i].second][pairs[i].first] = true;     }     int A[N];     for (int i = 0; i < N; i++) A[i] = i + 1;     do {       bool found = true;       for (int i = 0; i < N; i++)         if (!(adj[A[i]][A[(i + 1) % N]] && adj[A[i]][A[(i + 2) % N]]))           found = false;       if (found) {         for (int i = 0; i < N; i++) printf( %d  , A[i]);         printf(  n );         return;       }     } while (next_permutation(A, A + N));     printf( -1 n );   }   bool contains(int a, int b) {     for (int i = 0; i < adj[a].size(); i++)       if (adj[a][i] == b) return true;     return false;   }   int main() {     int N;     scanf( %d , &N);     if (N == 5 || N == 6) {       check(N);       return 0;     }     for (int i = 1; i <= 2 * N; i++) {       scanf( %d %d , &pairs[i].first, &pairs[i].second);       adj[pairs[i].first].push_back(pairs[i].second);       adj[pairs[i].second].push_back(pairs[i].first);     }     int prev = 1;     done[1] = true;     result[0] = 1;     int candidate[4 + 1], counts[4 + 1], num;     for (int i = 1; i <= N; i++)       if (adj[i].size() != 4) {         printf( -1 n );         return 0;       }     for (int i = 1; i < N; i++) {       int num = 0;       for (int j = 0; j < adj[prev].size(); j++) candidate[num++] = adj[prev][j];       memset(counts, 0, sizeof(counts));       for (int j = 0; j < num; j++)         for (int k = 0; k < adj[candidate[j]].size(); k++)           for (int l = 0; l < num; l++)             if (adj[candidate[j]][k] == candidate[l]) counts[l]++;       for (int j = 0; j < num; j++)         if (!done[candidate[j]] && counts[j] >= 2) {           prev = candidate[j];           done[prev] = true;           result[i] = prev;           break;         }     }     for (int i = 1; i <= N; i++)       if (!done[i]) {         printf( -1 n );         return 0;       }     bool found = true;     for (int i = 0; i < N; i++)       if (!(contains(result[i], result[(i + 1) % N]) &&             contains(result[i], result[(i + 2) % N])))         found = false;     if (!found) {       printf( -1 n );       return 0;     }     for (int i = 0; i < N; i++) printf( %d  , result[i]);     printf(  n );     return 0;   }