malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   int main() {     long long int t, n, ans = 0, count0 = 0, count1 = 0, count2 = 0, num0 = 0,                         num1 = 0, num2 = 0;     t = 1;     while (t--) {       cin >> n;       vector<long long int> A(n, 0);       for (int i = 0; i < n; i++) {         cin >> A[i];       }       sort(A.begin(), A.end());       for (int i = n - 1; i >= 0; i--) {         if (A[i] > 0) {           count1 += (A[i] - 1);           num1++;         } else if (A[i] == 0) {           count0++;           num0++;         } else {           count2 += (-1 - A[i]);           num2++;         }       }       ans += count1;       if (num2 % 2 == 0) {         ans += count2;         ans += count0;       } else {         if (num0 > 0) {           ans += count0;           ans += count2;         } else {           ans += count0;           ans += (count2 + 2);         }       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   double A[1005][1005];   void guass(int n) {     int i, j;     for (i = 0; i < n - 1; i++) {       double f = A[i + 1][i] / A[i][i];       for (j = i; j < n; j++) {         if (j > i + 2) break;         A[i + 1][j] -= f * A[i][j];       }       A[i + 1][n] -= f * A[i][n];     }     for (i = n - 1; i >= 0; i--) {       if (i + 1 < n) A[i][n] -= A[i + 1][n] * A[i][i + 1];       A[i][n] /= A[i][i];     }   }   int main() {     int n, m, x, y, cnt;     while (~scanf( %d%d%d%d , &n, &m, &x, &y)) {       for (; x < n; x++) {         for (int i = 1; i <= m; i++) {           cnt = 2 + (i > 1) + (i < m);           A[i - 1][i - 1] = ((double)(cnt - 1)) / cnt;           if (i > 1) A[i - 1][i - 2] = (-1.0) / cnt;           if (i < m) A[i - 1][i] = (-1.0) / cnt;           A[i - 1][m] = A[i - 1][m] / cnt + 1;         }         guass(m);       }       printf( %.10f n , A[y - 1][m]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   unsigned long long mod = 1e9 + 7;   unsigned long long ncr(long long int n, long long int k) {     unsigned long long res = 1;     if (k > n - k) k = n - k;     for (int i = 0; i < k; ++i) {       res *= (n - i);       res /= (i + 1);       res = res % mod;     }     return res % mod;   }   bool prime(unsigned long long num) {     bool flag = true;     for (unsigned long long i = 2; i * i <= num; i++) {       if (num % i == 0) {         flag = false;         break;       }     }     return flag;   }   long long ipow(long long base, long long exp) {     long long result = 1;     for (;;) {       if (exp & 1) {         result *= base;         result = result % mod;       }       exp >>= 1;       if (!exp) break;       base *= base;       base = base % mod;     }     return result % mod;   }   long long abs(long long a, long long b) {     if ((a - b) > 0)       return (a - b);     else       return (b - a);   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     int mx[n + 1], mn[n + 1];     memset(mn, -1, sizeof mn);     memset(mx, -1, sizeof mx);     for (int i = 0; i < k; i++) {       int k;       cin >> k;       if (mn[k] == -1) mn[k] = i;       mx[k] = i;     }     int ans = 0;     for (int i = 1; i <= n; i++) {       if (mn[i] == -1 && i != 1 && i != n)         ans += 3;       else if (mn[i] == -1 && (i == 1 || i == n))         ans += 2;       else {         if (i != 1) {           if (mn[i - 1] == -1)             ans++;           else {             if (mx[i - 1] < mn[i]) ans++;           }         }         if (i != n) {           if (mn[i + 1] == -1)             ans++;           else {             if (mx[i + 1] < mn[i]) {               ans++;             }           }         }       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   long long bin_pow(long long x, long long e, long long MOD) {     long long res = 1;     while (e) {       if (e & 1) res = res * x % MOD;       e >>= 1;       x = x * x % MOD;     }     return res;   }   long long p, a, b, x, ans;   vector<long long> m[1123456];   long long xd[1123456];   long long xy[1123456];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> a >> b >> p >> x;     for (int i = 1; i < p; i++) {       xd[i] = bin_pow(i, p - 2, p);       xy[i] = bin_pow(a, i, p);     }     for (int i = 0; i < p - 1; i++) {       m[bin_pow(a, i, p)].push_back(i);     }     for (int i = 1; i < p; i++) {       vector<long long> g = m[((b * xd[i]) % p) * xd[xy[i]] % p];       for (auto k : g) {         long long y = x - i;         if (y < 0) continue;         y /= p;         long long t = y / (p - 1);         if (t * (p - 1) + k <= y)           ans += t + 1;         else           ans += t;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q, ans[600005];   bitset<505> a[505][505], b[505][505];   char mp[505][505];   struct ccf {     int x1, y1, x2, y2, id;   };   void solve(int l, int r, vector<ccf> qus) {     if (!qus.size()) return;     int mid = (l + r) >> 1;     for (int i = mid; i >= l; i--) {       for (int j = m; j >= 1; j--) {         a[i][j].reset();         if (i == mid) a[mid][j][j] = (mp[mid][j] ==  . );         if (i != mid && mp[i + 1][j] ==  . ) a[i][j] |= a[i + 1][j];         if (j != m && mp[i][j + 1] ==  . ) a[i][j] |= a[i][j + 1];       }     }     for (int i = mid; i <= r; i++) {       for (int j = 1; j <= m; j++) {         b[i][j].reset();         if (i == mid) b[mid][j][j] = (mp[mid][j] ==  . );         if (i != mid && mp[i - 1][j] ==  . ) b[i][j] |= b[i - 1][j];         if (j != 1 && mp[i][j - 1] ==  . ) b[i][j] |= b[i][j - 1];       }     }     vector<ccf> askl, askr;     int len = qus.size();     for (int i = 0; i < len; i++) {       if (qus[i].x2 < mid)         askl.push_back(qus[i]);       else if (qus[i].x1 > mid)         askr.push_back(qus[i]);       else         ans[qus[i].id] =             (a[qus[i].x1][qus[i].y1] & b[qus[i].x2][qus[i].y2]).any();     }     solve(l, mid - 1, askl);     solve(mid + 1, r, askr);   }   vector<ccf> eve;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) cin >> (mp[i] + 1);     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       ccf qus;       qus.id = i;       scanf( %d%d%d%d , &qus.x1, &qus.y1, &qus.x2, &qus.y2);       eve.push_back(qus);     }     solve(1, n, eve);     for (int i = 1; i <= q; i++)       if (ans[i])         puts( Yes );       else         puts( No );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, n, ans, lcp[5009][5009], dp[5009];   char a[5009];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       scanf( %s , a + 1);       ans = 0;       for (int i = n; i >= 1; i--)         for (int j = n; j >= 1; j--)           lcp[i][j] = (a[i] == a[j]) ? (lcp[i + 1][j + 1] + 1) : 0;       for (int i = 1; i <= n; i++) {         dp[i] = n + 1 - i;         for (int j = 1; j < i; j++) {           int len = lcp[i][j];           if (a[j + len] < a[i + len])             dp[i] = max(dp[i], dp[j] + n + 1 - (i + len));         }         ans = max(ans, dp[i]);       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = int(5e5) + 100;   int n, m;   vector<pair<int, int> > out[maxn];   int fa[maxn];   long long f[maxn], g[maxn], a[maxn];   void read() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) out[i].clear();     for (int i = 1; i < n; ++i) {       int u, v, d;       scanf( %d%d%d , &u, &v, &d);       out[u].push_back(make_pair(v, d));       out[v].push_back(make_pair(u, d));     }   }   void dfs(int cur) {     f[cur] = g[cur] = 0;     for (auto &to : out[cur])       if (to.first != fa[cur]) {         fa[to.first] = cur;         dfs(to.first);         f[cur] += f[to.first];         g[cur] += f[to.first];       }     int cnt = 0;     for (auto &to : out[cur])       if (to.first != fa[cur]) a[++cnt] = to.second + g[to.first] - f[to.first];     sort(a + 1, a + 1 + cnt, greater<long long>());     int fin = min(m - 1, cnt);     for (int i = 1; i <= fin; ++i)       g[cur] += max(0LL, a[i]), f[cur] += max(a[i], 0LL);     if (m <= cnt) f[cur] += max(0LL, a[m]);   }   void solve() {     dfs(1);     printf( %lld n , f[1]);   }   int main() {     int casesum;     scanf( %d , &casesum);     while (casesum--) {       read();       solve();     }     return 0;   }
#include <bits/stdc++.h>   inline int read() {     int f = 1, x = 0;     char ch = getchar();     while (!(ch >=  0  && ch <=  9 )) {       if (ch ==  - ) f *= -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return f * x;   }   int q, n, k, ans, f[200000 + 1], p[200000 + 1], v[3];   char s[200000 + 1];   int main() {     q = read();     while (q--) {       n = read(), k = read();       scanf( %s , s + 1);       for (int i = 1; i <= n; i++) {         if (s[i] ==  R )           f[i] = 2;         else if (s[i] ==  G )           f[i] = 1;         else           f[i] = 0;         p[i] = (f[i] + i) % 3;       }       for (int i = 1; i < k; i++) v[p[i]]++;       ans = 2147483647;       for (int i = k; i <= n; i++) {         v[p[i]]++;         for (int j = 0; j <= 2; j++) ans = std::min(ans, k - v[j]);         v[p[i - k + 1]]--;       }       for (int i = n - k + 2; i <= n; i++) v[p[i]]--;       printf( %d n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   pair<long long, long long> a[100007];   int r[100007];   int c[100007];   int used[100007];   long long ans = -1;   bool cmp1(int x, int y) { return (a[x].first < a[y].first); }   bool cmp2(int x, int y) { return (a[x].second < a[y].second); }   void input() {     cin >> n >> k;     int i;     long long xx1, yy1, xx2, yy2;     for (i = 1; i <= n; i++) {       cin >> xx1 >> yy1 >> xx2 >> yy2;       xx1 *= 2;       yy1 *= 2;       xx2 *= 2;       yy2 *= 2;       a[i].first = (xx1 + xx2) / 2;       a[i].second = (yy1 + yy2) / 2;       r[i] = c[i] = i;     }   }   void solve() {     sort(r + 1, r + n + 1, cmp1);     sort(c + 1, c + n + 1, cmp2);     int i, j;     int mask;     int lim = (2 << (2 * k));     vector<int> v;     for (mask = 0; mask < lim; mask++) {       int u = mask;       v.clear();       for (j = 0; j < k; j++) {         int ost = (u % 4);         u /= 4;         if (ost == 0) {           for (i = 1; i <= n; i++) {             if (used[r[i]] == 1) {               continue;             }             used[r[i]] = 1;             v.push_back(r[i]);             break;           }         } else if (ost == 1) {           for (i = n; i >= 1; i--) {             if (used[r[i]] == 1) {               continue;             }             used[r[i]] = 1;             v.push_back(r[i]);             break;           }         } else if (ost == 2) {           for (i = 1; i <= n; i++) {             if (used[c[i]] == 1) {               continue;             }             used[c[i]] = 1;             v.push_back(c[i]);             break;           }         } else {           for (i = n; i >= 1; i--) {             if (used[c[i]] == 1) {               continue;             }             used[c[i]] = 1;             v.push_back(c[i]);             break;           }         }       }       long long xx1, yy1, xx2, yy2;       xx1 = xx2 = yy1 = yy2 = 0;       for (i = 1; i <= n; i++) {         if (used[r[i]] == 0) {           xx1 = a[r[i]].first;           break;         }       }       for (i = n; i >= 1; i--) {         if (used[r[i]] == 0) {           xx2 = a[r[i]].first;           break;         }       }       for (i = 1; i <= n; i++) {         if (used[c[i]] == 0) {           yy1 = a[c[i]].second;           break;         }       }       for (i = n; i >= 1; i--) {         if (used[c[i]] == 0) {           yy2 = a[c[i]].second;           break;         }       }       if (xx1 == xx2) {         if (k != n) {           xx2++;         }       }       if (yy1 == yy2) {         if (k != n) {           yy2++;         }       }       long long s = (xx2 - xx1 + 1) / 2;       s *= ((yy2 - yy1 + 1) / 2);       if (ans == -1 || ans > s) {         ans = s;       }       for (i = 0; i < k; i++) {         used[v[i]] = 0;       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     input();     solve();     return 0;   }
#include <bits/stdc++.h>   char s[100005];   std::vector<int> v;   int main() {     scanf( %s , s);     int n = strlen(s);     bool pc = 0;     long long now = 0, pre = 0;     bool first = true;     for (int i = 0; i < n; i++) {       if (s[i] <=  9  && s[i] >=  0 ) {         now = now * 10 + s[i] -  0 ;       } else {         if (first) {           first = false;           pre = now;         } else {           if (pc)             pre = pre + now;           else             pre = pre - now;         }         pc = (s[i] ==  + );         now = 0;       }     }     if (pc)       pre = pre + now;     else       pre = pre - now;     if (pre == 0) v.push_back(0);     while (pre) {       v.push_back(pre % 10);       pre /= 10;     }     for (int i = v.size() - 1; i >= 0; i--) {       for (int j = 0; j < v[i] +  0 ; j++) printf( + );       printf( . );       if (i != 0)         printf( > );       else         printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<long long, string> dic;   int main() {     long long n;     scanf( %lld , &n);     for (long long i = 2; (long long)i * i <= n; i++) {       for (long long j = i * i, p = 2; j <= n; j *= i, p++) {         string str = to_string(i) +  ^  + to_string(p);         if (dic.find(j) == dic.end())           dic[j] = str;         else if (dic[j].size() > str.size())           dic[j] = str;       }     }     string ans = to_string(n);     for (auto it = dic.begin(); it != dic.end(); it++) {       long long v = it->first;       string str = it->second;       long long mul = n / v;       long long add = n % v;       string all_str;       if (mul > 1) {         string mul_str = to_string(mul);         if (dic.find(mul) != dic.end() && dic[mul].size() < mul_str.size())           all_str += dic[mul] +  * ;         else           all_str += mul_str +  * ;       }       all_str += str;       if (add > 0) {         all_str +=  + ;         string add_str = to_string(add);         if (dic.find(add) != dic.end() && dic[add].size() < add_str.size())           all_str += dic[add];         else           all_str += add_str;       }       if (all_str.size() < ans.size()) ans = all_str;     }     puts(ans.c_str());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   list<int> adjList[200002];   int depth[200002];   int sz[200002];   class LCA_binary {     int logn, timer;     vector<int> tin, tout;     vector<vector<int>> up;       public:     void innerdfs(int v, int p) {       tin[v] = ++timer;       up[v][0] = p;       for (int i = 1; i <= logn; ++i) up[v][i] = up[up[v][i - 1]][i - 1];       for (int u : adjList[v]) {         if (u != p) innerdfs(u, v);       }       tout[v] = ++timer;     }     bool is_ancestor(int u, int v) {       return tin[u] <= tin[v] && tout[u] >= tout[v];     }     int lca(int u, int v) {       if (is_ancestor(u, v)) return u;       if (is_ancestor(v, u)) return v;       for (int i = logn; i >= 0; --i) {         if (!is_ancestor(up[u][i], v)) u = up[u][i];       }       return up[u][0];     }     void preprocess(int n, int root) {       tin.resize(n + 3);       tout.resize(n + 3);       timer = 0;       logn = ceil(log2(n));       up.assign(n + 3, vector<int>(logn + 1));       innerdfs(root, root);     }   };   set<int> s;   vector<int> univ;   int deep(int i, int p, int d) {     depth[i] = d++;     if (s.count(i)) univ.push_back(i);     sz[i] = 1;     for (int v : adjList[i])       if (v != p) sz[i] += deep(v, i, d);     return sz[i];   }   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < (k << 1); i++) {       int t;       cin >> t;       s.insert(t);     }     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       adjList[a].push_back(b);       adjList[b].push_back(a);     }     LCA_binary f;     f.preprocess(n, 1);     deep(1, 0, 0);     long long ans = 0LL;     for (int i = 0; i < k; i++)       ans += (long long)(depth[univ[i]] + depth[univ[i + k]] -                          (depth[f.lca(univ[i], univ[i + k])] << 1));     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[1000], ans = 0;   int cnt[1000];   int main() {     int n;     scanf( %d , &n);     priority_queue<int> pq;     for (int i = 0; i < n; ++i) {       int x;       scanf( %d , &x);       pq.push(-x);     }     for (int i = 0; i < 1000; ++i) {       arr[i] = 0;       cnt[i] = 0;     }     while (!pq.empty()) {       int u = -pq.top();       pq.pop();       int p = -1, smallest = 100000;       for (int i = 0; i < ans; ++i)         if (arr[i] < smallest && cnt[i] <= u) {           smallest = cnt[i];           p = i;         }       if (p == -1) {         ++cnt[ans];         arr[ans++] = u;       } else {         ++cnt[p];         arr[p] = u;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100010;   int n, m, k;   map<int, vector<pair<int, int> > > a, b;   int s = 0;   void deal(map<int, vector<pair<int, int> > > &a, int n, int m) {     if ((m + 1 - a.size()) % 2) s ^= n;     for (map<int, vector<pair<int, int> > >::iterator it = a.begin();          it != a.end(); ++it) {       sort(it->second.begin(), it->second.end());       vector<pair<int, int> > v;       pair<int, int> p = it->second[0];       for (int i = 1; i < it->second.size(); ++i) {         if (it->second[i].first > p.second) {           v.push_back(p);           p = it->second[i];         } else           p.second = max(p.second, it->second[i].second);       }       v.push_back(p);       it->second = v;       int t = 0;       for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first;       s ^= n - t;     }   }   int search(vector<pair<int, int> > &v, int L, int R, int get) {     int ans = R;     while (L <= R) {       int m = (L + R) >> 1;       int t = m;       for (int i = 0; i < v.size(); ++i) {         if (v[i].first >= m)           t += v[i].second - v[i].first;         else if (v[i].second >= m)           t += v[i].second - m;       }       if (t >= get) {         ans = m;         R = m - 1;       } else         L = m + 1;     }     return ans;   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= k; ++i) {       int x1, y1, x2, y2;       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       if (x1 == x2) {         if (y1 > y2) swap(y1, y2);         a[x1].push_back(pair<int, int>(y1, y2));       } else if (y1 == y2) {         if (x1 > x2) swap(x1, x2);         b[y1].push_back(pair<int, int>(x1, x2));       }     }     deal(a, m, n);     deal(b, n, m);     if (s) {       puts( FIRST );       if (a.size() < n)         for (int i = 1; i < n; ++i) {           if (!a.count(i)) {             a[i];             break;           }         }       for (map<int, vector<pair<int, int> > >::iterator it = a.begin();            it != a.end(); ++it) {         vector<pair<int, int> > v = it->second;         int t = 0;         for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first;         t = m - t;         if (!t) continue;         if ((s ^ t) >= t) continue;         int p = search(v, 0, m, m - (s ^ t));         printf( %d %d %d %d n , it->first, 0, it->first, p);         return 0;       }       if (b.size() < m)         for (int i = 1; i < m; ++i) {           if (!b.count(i)) {             b[i];             break;           }         }       for (map<int, vector<pair<int, int> > >::iterator it = b.begin();            it != b.end(); ++it) {         vector<pair<int, int> > v = it->second;         int t = 0;         for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first;         t = n - t;         if (!t) continue;         if ((s ^ t) >= t) continue;         int p = search(v, 0, n, n - (s ^ t));         printf( %d %d %d %d n , 0, it->first, p, it->first);         return 0;       }     } else {       puts( SECOND );     }     return 0;   }
#include <bits/stdc++.h>   int n, m, i, j;   char s[110], p =  - ;   bool q = true;   int main() {     scanf( %d%d , &n, &m);     gets(s);     for (i = 0; i < n; i++) {       gets(s);       if (s[0] <  0  || s[0] >  9  || s[0] == p)         q = false;       else         p = s[0];       for (j = 1; j < m; j++)         if (s[j] != p) q = false;     }     if (q)       puts( YES );     else       puts( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 10007;   int n, m, d, p[maxn], k[maxn], c[maxn], ans[maxn], vis[maxn], match[maxn];   vector<int> adj[maxn];   bool dfs(int u) {     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (!vis[v]) {         vis[v] = 1;         if (match[v] == -1 || dfs(match[v])) {           match[v] = u;           return true;         }       }     }     return false;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) {       cin >> c[i];     }     cin >> d;     for (int i = 1; i <= d; i++) {       cin >> k[i];       vis[k[i]] = 1;     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         adj[p[i]].push_back(c[i] + 5000);         adj[c[i] + 5000].push_back(p[i]);       }     }     memset(match, -1, sizeof(match));     memset(vis, 0, sizeof(vis));     while (dfs(ans[d])) {       ans[d]++;       memset(vis, 0, sizeof(vis));     }     for (int i = d - 1; i; i--) {       adj[p[k[i + 1]]].push_back(c[k[i + 1]] + 5000);       adj[c[k[i + 1]] + 5000].push_back(p[k[i + 1]]);       ans[i] = ans[i + 1];       memset(vis, 0, sizeof(vis));       while (dfs(ans[i])) {         ans[i]++;         memset(vis, 0, sizeof(vis));       }     }     for (int i = 1; i <= d; i++) printf( %d n , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   map<int, int> mmp;   long long qpow(long long a, long long b, long long m) {     long long ans = 1, aa = a;     while (b) {       if (b & 1) ans = ans * aa % m;       aa = aa * aa % m;       b >>= 1;     }     return ans;   }   int a[200010];   int ccc[200010];   int main() {     int n, i, x = 1;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i];       mmp[a[i]] = i;     }     int pos = 1;     ccc[0] = mmp[a[1]];     int num = 0;     while (pos <= n) {       while (pos <= n) {         ccc[num] = max(mmp[a[pos]], ccc[num]);         if (pos == ccc[num]) break;         pos++;       }       if (pos == n) break;       pos++;       num++;     }     cout << qpow(2, num, mod) << endl;   }