malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int s[3000005], ps[3000005][5];   int n, ans;   vector<int> temp;   int po(int a, int b) {     if (b == 0) return 1;     return a * po(a, b - 1);   }   void prep() {     int ptr1 = 1, ptr2 = 1, maxi = 0;     ps[1][0] = 1;     for (int i = (2); i <= (3000000); i++) {       while (po(ptr1, 4) < i) ptr1++;       while (po(ptr2 + 1, 2) <= i) ptr2++;       for (int j = (0); j <= (4); j++) ps[i][j] = ps[i - 1][j];       while (ps[ptr2][s[i]] - ps[ptr1 - 1][s[i]] > 0) s[i]++;       ps[i][s[i]]++;     }   }   int f(long long x) {     if (x <= 3000000) return s[x];     int ptr1 = ceil(pow(x, 0.25)), ptr2 = floor(pow(x, 0.5));     int ret = 0;     while (ps[ptr2][ret] - ps[ptr1 - 1][ret] > 0) ret++;     return ret;   }   int main() {     prep();     scanf( %d , &n);     for (int i = (1); i <= (n); i++) {       long long x;       scanf( %lld , &x);       ans ^= f(x);     }     if (ans)       printf( Furlo n );     else       printf( Rublo n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename A, size_t N, typename T>   void Fill(A (&array)[N], const T &val) {     std::fill((T *)array, (T *)(array + N), val);   }   template <class T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   int mi = 1000000007;   map<pair<int, int>, int> mp;   int minima[2000000];   int f(int a, int b) {     if (mp.find(make_pair(a, b)) != mp.end()) {       return mp[make_pair(a, b)];     }     cout <<  ?   << a <<     << b << endl;     int s;     cin >> s;     chmin(mi, s);     return mp[make_pair(a, b)] = s;   }   void SMAWK(vector<int> js, int ib, int ie, int id) {     if (ib > ie) return;     vector<int> js2;     for (int q = 0, i = ib; q < js.size(); ++q) {       while (!js2.empty() && f(i, js2.back()) >= f(i, js[q])) {         js2.pop_back();         i -= id;       }       if (js2.size() != (ie - ib) / id) {         js2.push_back(js[q]);         i += id;       }     }     SMAWK(js2, ib + id, ie, id * 2);     for (int i = ib, q = 0; i <= ie; i += id * 2) {       int jt = (i + id <= ie ? minima[i + id] : js.back());       int fm = 999999999 * 2, fq;       for (; q < js.size(); ++q) {         if (fm > (fq = f(i, js[q]))) {           fm = fq;           minima[i] = js[q];         }         if (js[q] == jt) break;       }     }   }   void rowMinimaTM(int ib, int ie, int jb, int je) {     vector<int> js;     for (int j = jb; j <= je; ++j) js.push_back(j);     SMAWK(js, ib, ie, 1);   }   int main() {     int n, m;     cin >> n >> m;     rowMinimaTM(1, n, 1, m);     cout <<  !   << mi << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 2e5 + 5;   long long int sz[maxn], dp[maxn], ans[maxn];   vector<long long int> g[maxn];   long long int n;   long long int dp1(long long int node, long long int parent) {     sz[node] = 1;     for (auto& itr : g[node])       if (itr != parent) sz[node] += dp1(itr, node);     return sz[node];   }   long long int dp2(long long int node, long long int parent) {     dp[node] = sz[node];     for (auto& itr : g[node])       if (itr != parent) dp[node] += dp2(itr, node);     return dp[node];   }   void dp3(long long int node, long long int parent,            long long int fromParent = 0) {     if (parent == -1)       ans[node] = dp[node];     else       ans[node] = n - 2 * sz[node] + ans[parent];     for (auto& itr : g[node])       if (itr != parent) dp3(itr, node);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (long long int i = 1; i < n; i++) {       long long int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dp1(1, -1);     dp2(1, -1);     dp3(1, -1);     long long int answer = 0;     for (long long int i = 1; i <= n; i++) answer = max(answer, ans[i]);     cout << answer;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[16] = {0};     int index = 0;     for (int i = 1; i <= n; i++) {       string s = to_string(i);       bool lucky = true;       for (int j = 0; j < (s.length()); j++) {         if (s[j] !=  4  && s[j] !=  7 ) {           lucky = false;           break;         }       }       if (lucky) {         a[index] = i;         index++;       }     }     for (int i = 0; i < 16; i++) {       if (a[i] != 0) {         if (n % a[i] == 0) {           cout <<  YES ;           return 0;         }       }     }     cout <<  NO ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     set<long long> ss;     for (int i = 1; i <= 1e6; i++) {       long long need = i * 2;       long long cards = (i * (i + 1)) / 2;       long long lft = cards - i;       need += lft * 3;       if (need > 1e9) break;       ss.insert(-need);     }     while (t--) {       long long n;       cin >> n;       long long cnt = 0;       while (true) {         set<long long>::iterator it = ss.lower_bound(-n);         if (it == ss.end()) break;         cnt++;         n += *it;       }       cout << cnt <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   const int MAXN = 1e5 + 5;   enum { GRASS =  G , WATER =  W , LAVA =  L  };   enum { G = 5, W = 3, L = 1 };   long long a[MAXN];   char s[MAXN];   int n;   bool water;   long long grass, stamina, t;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %lld , &a[i]);     }     scanf(  %s , s);     water = false;     grass = stamina = t = 0LL;     for (int i = 0; i < n; ++i) {       switch (s[i]) {         case GRASS:           grass += 2 * a[i];           stamina += 2 * a[i];           grass = std::min(stamina / 2, grass);           t += G * a[i];           break;         case WATER:           water = true;           stamina += 2 * a[i];           t += W * a[i];           break;         case LAVA:           if (stamina < 2 * a[i]) {             t += ((water) ? W : G) * (a[i] - stamina / 2);             stamina = 0;           } else {             stamina -= 2 * a[i];           }           t += L * a[i];           grass = std::min(stamina / 2, grass);           break;       }     }     t -= (G - L) / 2 * grass;     t -= (W - L) / 2 * (stamina / 2 - grass);     printf( %lld n , t);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int>> g;   vector<int> w;   vector<int> dp;   void dfs(int node, int p) {     dp[node] = w[node];     for (int u : g[node]) {       if (u != p) {         dfs(u, node);         dp[node] += dp[u];       }     }   }   int main() {     cin >> n;     int u, v, t;     g.assign(n + 1, vector<int>());     w.assign(n + 1, 0);     dp.assign(n + 1, 0);     vector<int> ans;     for (int i = 1; i < n; i++) {       cin >> u >> v >> t;       g[u].push_back(v);       g[v].push_back(u);       if (t == 2) {         w[u] = 1;         w[v] = 1;       }     }     dfs(1, 0);     for (int i = 1; i <= n; i++) {       if (dp[i] == 1 && w[i] == 1) ans.push_back(i);     }     cout << ans.size() << endl;     for (auto u : ans) cout << u <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[300010];   long long ans = 0;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n / 2; ++i) {       long long sum = a[i] + a[n - i + 1];       ans += sum * sum;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> a;   vector<int> b;   int n;   void get_input() { cin >> n; }   void solve() {     for (int i = 0; i < n; i++) {       int kom;       cin >> kom;       a.push_back(kom);       b.push_back(kom);     }     sort(b.begin(), b.begin() + n);     int cnt = 0;     for (int i = 0; i < n; i++) {       if (b[i] != a[i]) {         cnt++;       }     }     cout << (cnt > 2 ?  NO  :  YES );   }   void output() {}   int main() {     ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1;     while (t--) {       get_input(), solve(), output();     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   using namespace std;   const double pi =       3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;   int main() {     int n, k;     scanf( %d%d , &n, &k);     int v = 2 * k + 1;     if (n <= k + 1)       printf( 1 n1 n );     else if (n % v == 0) {       printf( %d n , n / v);       int ile = 0, pos = k + 1;       while (ile < n) {         printf( %d  , pos);         pos += v;         ile += v;       }       printf(  n );     } else if (n % v > k) {       int r = n % v;       int pos = 1 + r - (k + 1);       int ile = r;       printf( %d n%d  , n / v + 1, pos);       pos = ile + k + 1;       while (ile < n) {         printf( %d  , pos);         pos += v;         ile += v;       }       printf(  n );     } else {       int res = n * n;       for (int i = (k + 1); i <= (v); ++i)         for (int j = (k + 1); j <= (v); ++j)           if ((n - i - j) >= 0 && (n - i - j) % v == 0)             res = min(res, 2 + (n - i - j) / v);       printf( %d n , res);       for (int i = (k + 1); i <= (v); ++i)         for (int j = (k + 1); j <= (v); ++j)           if ((n - i - j) >= 0 && (n - i - j) % v == 0 &&               2 + (n - i - j) / v == res) {             int pos = 1 + i - (k + 1);             int ile = i;             printf( %d %d  , pos, n - (j - (k + 1)));             n -= j;             pos = ile + k + 1;             while (ile < n) {               printf( %d  , pos);               pos += v;               ile += v;             }             printf(  n );             return 0;           }     }     return 0;   }
#include <bits/stdc++.h>   const int mod = 1000000007;   struct uinon_set {     int father[100003], n, value[100003];     int ans;     void init() {       ans = 0;       for (int i = 1; i <= n; i++) {         father[i] = i;         value[i] = 0;       }       return;     }     int getfather(int i) {       if (father[i] == i) return i;       int tans = getfather(father[i]);       value[i] = (value[i] + value[father[i]]) % mod;       return father[i] = tans;     }     void insert(int root, int i, int xi) {       int temp, tmp = getfather(root);       temp = (value[root] + xi) % mod;       father[tmp] = i;       value[tmp] = temp;       ans = (ans + temp) % mod;       return;     }   } set;   int main() {     int n, t, x, y, i, j;     scanf( %d , &n);     set.n = n;     set.init();     for (i = 1; i <= n; i++) {       scanf( %d , &t);       for (j = 0; j < t; j++) {         scanf( %d%d , &x, &y);         set.insert(x, i, y);       }     }     printf( %d n , (set.ans + mod) % mod);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 5e5 + 5;   bool vis[MAXN];   long long sp[MAXN];   long long f[MAXN];   bool is[MAXN];   long long ar[MAXN];   long long POWER[25];   long long F[25];   void sieve() {     for (long long i = 0; i < MAXN; i++) vis[i] = false, sp[i] = 2;     for (long long i = 3; i < MAXN; i += 2) {       if (!vis[i]) {         sp[i] = i;         for (long long j = i * i; j < MAXN; j += 2 * i) {           vis[j] = true;           sp[j] = i;         }       }     }   }   int main() {     long long n, q;     cin >> n >> q;     for (long long i = 1; i <= n; i++) scanf( %lld , &ar[i]);     sieve();     long long added = 0;     POWER[0] = 1;     for (long long i = 1; i < 15; i++) POWER[i] = POWER[i - 1] << 1;     long long ans = 0;     while (q--) {       long long x;       long long temp = 0;       scanf( %lld , &x);       long long cc = 0;       long long val = ar[x];       while (val != 1) {         long long div = sp[val];         F[cc++] = div;         while (val % div == 0) val /= div;       }       for (long long i = 1; i < POWER[cc]; i++) {         long long p = 1;         long long counter = 0;         for (long long j = 0; j < cc; j++)           if (POWER[j] & i) counter++, p *= F[j];         if (counter & 1)           temp += f[p];         else           temp -= f[p];       }       if (is[x]) {         ans -= added - temp;         if (ar[x] == 1) ans++;         is[x] = false;         for (long long i = 1; i < POWER[cc]; i++) {           long long p = 1;           for (long long j = 0; j < cc; j++)             if (POWER[j] & i) p *= F[j];           f[p]--;         }         added--;       } else {         ans += added - temp;         is[x] = true;         for (long long i = 1; i < POWER[cc]; i++) {           long long p = 1;           for (long long j = 0; j < cc; j++)             if (POWER[j] & i) p *= F[j];           f[p]++;         }         added++;       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     string s, t;     cin >> x >> y >> s >> t;     int arr[y - x + 1];     int mx = -1, res;     for (int i = 0; i < y - x + 1; i++) {       int k = i;       arr[i] = 0;       for (int j = 0; j < x; j++, k++) {         if (t[k] == s[j]) arr[i] += 1;       }       if (mx < arr[i]) {         mx = arr[i];         res = i;       }     }     cout << x - mx << endl;     for (int i = res, j = 0; j < x; i++, j++) {       if (t[i] != s[j]) cout << j + 1 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, m;     cin >> n >> k;     m = n * (n - 1) / 2;     if ((double)k > ((double)n - 1) / 2) {       cout << -1;       return 0;     } else {       cout << k * n << endl;       for (int i = 1; i <= n; ++i) {         for (int j = 0; j < k; ++j) {           cout << i <<     << (i + j) % n + 1 << endl;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const int maxn = 1005;   int main() {     int N;     cin >> N;     cout << 2 * N - 1 <<     << 2 << endl;     cout << 1 <<     << 2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<pair<pair<int, int>, int> > a(2 * n - 1);       for (int i = 0; i < 2 * n - 1; i++) {         cin >> a[i].first.first;         cin >> a[i].first.second;         a[i].second = i + 1;       }       sort(a.begin(), a.end());       long long s = 0, s1 = 0, tt = 0, tt1 = 0;       for (int i = 0; i < 2 * n - 1; i++) {         if (i % 2 == 0) {           s += a[i].first.first;           s1 += a[i].first.second;         }         tt += a[i].first.first;         tt1 += a[i].first.second;       }       tt++;       tt1++;       tt /= 2;       tt1 /= 2;       cout <<  YES  << endl;       if (s >= tt && s1 >= tt1) {         for (int i = 0; i < 2 * n - 1; i++) {           if (i % 2 == 0) cout << a[i].second <<    ;         }         cout << endl;       } else {         for (int i = 0; i < 2 * n - 1; i++) {           if (i % 2 == 1) {             cout << a[i].second <<    ;           }         }         cout << a[2 * n - 2].second << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   char s[1005], ans[1005];   map<long long, bool> dp[1005];   map<long long, char> pre[1005];   inline long long read() {     long long q = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = 0;       ch = getchar();     }     while (isdigit(ch)) q = (q << 3) + (q << 1) + (ch ^ 48), ch = getchar();     return f ? q : -q;   }   inline void print(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) print(x / 10);     putchar(x % 10 + 48);   }   signed main() {     n = read(), k = read(), scanf( %s , s + 1), dp[1][0] = true;     for (long long i = 2; i <= n + 1; ++i)       for (long long j = -k + 1; j < k; ++j)         if (dp[i - 1][j]) {           if (!(s[i - 1] ^  L ))             pre[i][j - 1] =  L , dp[i][j - 1] = true;           else if (!(s[i - 1] ^  W ))             pre[i][j + 1] =  W , dp[i][j + 1] = true;           else if (!(s[i - 1] ^  D ))             pre[i][j] =  D , dp[i][j] = true;           else             pre[i][j + 1] =  W , pre[i][j] =  D , pre[i][j - 1] =  L ,                        dp[i][j + 1] = dp[i][j] = dp[i][j - 1] = true;         }     if (dp[n + 1][k] | dp[n + 1][-k]) {       long long j = dp[n + 1][k] ? k : -k;       for (long long i = n + 1; i > 1; --i) {         char ch = pre[i][j];         ans[i - 1] = ch, ((!(ch ^  W )) ? --j : ((!(ch ^  L )) ? ++j : i));       }       for (long long i = 1; i <= n; ++i) putchar(ans[i]);     } else {       putchar( N ), putchar( O );       return 0;     };     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) {         ans = (ans * a) % 1000000007;       }       a = (a * a) % 1000000007;       b = b / 2;     }     return ans;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       if (x - y == 1) {         cout <<  NO  << endl;       } else         cout <<  YES  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   list<int> l[100005];   list<int> son[100005];   int n, dad[100005], x, y, val[100005], res;   void f(int a) {     for (list<int>::iterator it = l[a].begin(); it != l[a].end(); it++)       if ((*it) != dad[a]) {         dad[*it] = a;         f(*it);         son[a].push_back(*it);       }   }   pair<long long int, long long int> calc(int a) {     pair<long long int, long long int> ret =         pair<long long int, long long int>(0, 0);     pair<long long int, long long int> tmp;     for (list<int>::iterator it = son[a].begin(); it != son[a].end(); it++)       if (*it != dad[a]) {         tmp = calc(*it);         ret = pair<long long int, long long int>(max(tmp.first, ret.first),                                                  max(tmp.second, ret.second));       }     int change = ret.first - ret.second;     if (val[a] + change > 0)       ret.second += abs(val[a] + change);     else       ret.first += abs(val[a] + change);     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       scanf( %d %d , &x, &y);       l[x].push_back(y);       l[y].push_back(x);     }     for (int i = 1; i <= n; i++) scanf( %d , &val[i]);     f(1);     pair<long long int, long long int> res = calc(1);     cout << res.first + res.second << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     }     int n, a, b;     cin >> n;     bool visit[1000005] = {false};     int ans = -1;     while (n--) {       cin >> a >> b;       while (visit[a] || a < ans) {         a += b;       }       ans = max(ans, a);       visit[a] = true;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n;       int diff = 0;       cin >> n;       int a[2 * n];       for (int i = 0; i < 2 * n; i++) {         cin >> a[i];         if (a[i] & 1)           diff++;         else           diff--;       }       if (diff == 0) {         cout << 0 <<   n ;         continue;       }       map<int, int> mp;       mp[0] = 0;       int cnt = 0;       for (int i = n; i < 2 * n; i++) {         if (a[i] & 1)           cnt++;         else           cnt--;         if (mp.find(cnt) == mp.end()) mp[cnt] = i - (n - 1);       }       cnt = 0;       int ans;       if (mp.find(diff) == mp.end())         ans = 2 * n;       else         ans = mp[diff];       for (int i = n - 1; i >= 0; i--) {         if (a[i] & 1)           cnt++;         else           cnt--;         if (mp.find(diff - cnt) != mp.end()) {           ans = min(ans, mp[(diff - cnt)] + n - i);         }       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   inline void amax(T &x, U y) {     if (x < y) x = y;   }   int main() {     int n, p, k, ai;     cin >> n >> k >> p;     vector<int> ods;     vector<int> evns;     for (int i = 0; i < n; i++) {       cin >> ai;       if (ai % 2 == 0)         evns.push_back(ai);       else         ods.push_back(ai);     }     int rem = k - p;     vector<vector<int> > ans;     for (int i = 0; i < p; i++) {       if (evns.size()) {         vector<int> tmp;         tmp.push_back(evns.back());         ans.push_back(tmp);         evns.pop_back();       } else if (ods.size() >= 2) {         vector<int> tmp;         tmp.push_back(ods.back());         ods.pop_back();         tmp.push_back(ods.back());         ods.pop_back();         ans.push_back(tmp);       } else {         cout <<  NO ;         return 0;       }     }     for (int i = 0; i < rem; i++) {       if (ods.size()) {         vector<int> tmp;         tmp.push_back(ods.back());         ans.push_back(tmp);         ods.pop_back();       } else {         cout <<  NO ;         return 0;       }     }     if (ods.size() % 2 == 1) {       cout <<  NO ;       return 0;     }     if (k == 1) {       cout <<  YES  << endl;       int m = ans[0].size() + ods.size() + evns.size();       cout << m <<    ;       for (int j = 0; j < ans[0].size(); j++) cout << ans[0][j] <<    ;       for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ;       for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ;       return 0;     }     cout <<  YES  << endl;     for (int i = 0; i < ans.size(); i++) {       if (ods.size()) {         int m = ans[i].size() + ods.size();         cout << m <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ;         ods.clear();         cout << endl;       } else if (evns.size()) {         int m = ans[i].size() + evns.size();         cout << m <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ;         evns.clear();         cout << endl;       } else {         cout << ans[i].size() <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         cout << endl;       }     }     return 0;   }