malteklaes/cpp-code-code_search_net-style huggingface dataset:
/***************************************************************    File name: CF1517F.cpp    Author: ljfcnyali    Create time: 2021年04月27日 星期二 10时19分32秒   ***************************************************************/   #include<bits/stdc++.h>      using namespace std;      #define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i )    #define mem(a) memset ( (a), 0, sizeof ( a ) )    #define str(a) strlen ( a )    #define pii pair<int, int>   #define int long long   typedef long long LL;      const int maxn = 1010;   const int Mod = 998244353;      int n, Begin[maxn], Next[maxn], To[maxn], e, ans[maxn], ret, f[maxn][maxn][2], g[maxn][2], Max[maxn];      inline void add(int u, int v) { To[++ e] = v; Next[e] = Begin[u]; Begin[u] = e; }       inline int power(int a, int b) { int r = 1; while ( b ) { if ( b & 1 ) r = r * a % Mod; a = a * a % Mod; b >>= 1; } return r; }      inline void DFS(int u, int fa, int x)   {    f[u][0][0] = f[u][x][1] = 1; Max[u] = 0;    for ( int i = Begin[u]; i; i = Next[i] )     {     int v = To[i]; if ( v == fa ) continue ;      DFS(v, u, x);      REP(k, 0, Max[u]) REP(j, 0, Max[v])     {      g[max(k, j + 1)][0] = (g[max(k, j + 1)][0] + f[u][k][0] * f[v][j][0]) % Mod;      if ( j < x - k ) g[x - k][1] = (g[x - k][1] + f[u][x - k][1] * f[v][j][0]) % Mod;      else g[j + 1][0] = (g[j + 1][0] + f[u][x - k][1] * f[v][j][0]) % Mod;     }        REP(k, 0, Max[u]) REP(j, x - Max[v], x)     {      if ( k <= j - 1 ) g[j - 1][1] = (g[j - 1][1] + f[u][k][0] * f[v][j][1]) % Mod;      else g[k][0] = (g[k][0] + f[u][k][0] * f[v][j][1]) % Mod;      g[max(x - k, j - 1)][1] = (g[max(x - k, j - 1)][1] + f[u][x - k][1] * f[v][j][1]) % Mod;     }     REP(k, 0, x) REP(o, 0, 1) { f[u][k][o] = g[k][o]; g[k][o] = 0; }      Max[u] = max(Max[u], Max[v] + 1);    }   }      inline int Solve(int x)   {    mem(f); DFS(1, 0, x);    int sum = 0; REP(i, 0, x) sum = (sum + f[1][i][1]) % Mod; return sum;   }      signed main()   {   #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);   #endif    scanf( %lld , &n);    REP(i, 1, n - 1) { int u, v; scanf( %lld%lld , &u, &v); add(u, v); add(v, u); }    REP(i, 0, n) ans[i] = Solve(i);      REP(i, 1, n) ret = (ret + (ans[i] - ans[i - 1]) * (i - 1)) % Mod;    ret = (ret + n - 1) * power((Mod + 1) / 2, n) % Mod;    printf( %lld n , (ret + Mod) % Mod);       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     cout.tie();     int n, q, i;     cin >> n;     int arr[106] = {};     for (i = 1; i <= n; i++) {       cin >> arr[i];     }     cin >> q;     int x, y;     while (q--) {       cin >> x >> y;       arr[x - 1] += (y - 1);       arr[x + 1] += (arr[x] - y);       arr[x] = 0;     }     for (i = 1; i <= n; i++) {       cout << arr[i] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct bus {     double t, v;     double cost, time;   };   bus bus[100000];   int main() {     int n, a, d;     scanf( %d%d%d , &n, &a, &d);     double fastest = 0.0;     double x, y;     for (int i = 0; i < n; i++) {       scanf( %lf%lf , &bus[i].t, &bus[i].v);       x = (d - bus[i].v * bus[i].v / (2.0 * a)) / bus[i].v;       y = bus[i].t + (bus[i].v / a) + x;       if (x < 0) {         y = bus[i].t + sqrt(2.0 * d / (double)a);       }       if (fastest <= y) fastest = y;       printf( %.10f n , fastest);     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100050;   vector<int> E[N];   queue<int> q[2];   int c, in[N], col[N], sol;   int main() {     int n, m, u, v, i;     scanf( %i %i , &n, &m);     for (i = 0; i < n; i++) scanf( %i , &col[i]);     while (m--) {       scanf( %i %i , &u, &v);       E[v].push_back(u);       in[u]++;     }     for (i = 0; i < n; i++)       if (!in[i]) q[col[i]].push(i);     while (!q[0].empty() || !q[1].empty()) {       if (q[c].empty()) {         if (!c) sol++;         c ^= 1;       }       u = q[c].front();       q[c].pop();       for (i = 0; i < E[u].size(); i++) {         v = E[u][i];         in[v]--;         if (!in[v]) q[col[v]].push(v);       }     }     printf( %i n , sol);     return 0;   }
#include <bits/stdc++.h>   const int mod = 1e9 + 7;   using namespace std;   bool sortcol(const vector<int>& v1, const vector<int>& v2) {     return v1[1] < v2[1];   }   void fastio() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool compare(const pair<long long int, long long int>& a,                const pair<long long int, long long int>& b) {     if (a.first > b.first) return true;     if (a.first == b.first) return a.second < b.second;     return false;   }   long long int power(long long int x, long long int y) {     long long int res = 1;     x = x % mod;     while (y > 0) {       if (y & 1) res = (res * x) % mod;       y = y >> 1;       x = (x * x) % mod;     }     return res;   }   vector<int> vin(int n) {     vector<int> v;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       v.push_back(t);     }     return v;   }   vector<int> ch;   void solve() {     long long int n, k;     cin >> n >> k;     long long int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     long long int l = a[0] - k, r = a[0] + k;     for (int i = 0; i < n; i++) {       long long int x = a[i] - k;       long long int y = a[i] + k;       l = max(l, x);       r = min(r, y);     }     if (l > r) {       cout << -1 <<   n ;     } else       cout << r <<   n ;   }   int main() {     int tc;     cin >> tc;     while (tc--) solve();   }
#include <bits/stdc++.h>   using namespace std;   long long qpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b % 2) ans = ans * a;       a = a * a;       b = b / 2;     }     return ans;   }   vector<int> pos;   stack<long long> num;   stack<char> ch;   long long cal(string s) {     while (!num.empty()) num.pop();     while (!ch.empty()) ch.pop();     for (int i = 0; i < s.length(); i++) {       char c = s[i];       if (c >=  0  && c <=  9 )         num.push(c -  0 );       else if (c ==  ( ) {         ch.push(c);       } else if (c ==  ) ) {         while (ch.top() !=  ( ) {           char x = ch.top();           ch.pop();           long long a = num.top();           num.pop();           long long b = num.top();           num.pop();           if (x ==  * )             num.push(a * b);           else             num.push(a + b);         }         ch.pop();       } else {         if (c ==  + ) {           while (!ch.empty() && ch.top() ==  * ) {             char x = ch.top();             ch.pop();             long long a = num.top();             num.pop();             long long b = num.top();             num.pop();             if (x ==  * )               num.push(a * b);             else               num.push(a + b);           }           ch.push(c);         } else           ch.push(c);       }     }     while (!ch.empty()) {       char x = ch.top();       ch.pop();       long long a = num.top();       num.pop();       long long b = num.top();       num.pop();       if (x ==  * )         num.push(a * b);       else         num.push(a + b);     }     return num.top();   }   int main() {     ios::sync_with_stdio(false);     string str;     while (cin >> str) {       int n = str.size();       pos.clear();       pos.push_back(-1);       for (int i = 1; i < n; i += 2) {         if (str[i] ==  * ) {           pos.push_back(i);         }       }       pos.push_back(n);       int len = pos.size();       long long ans = 0;       for (int i = 0; i < len - 1; i++) {         for (int j = i + 1; j < len; j++) {           string s = str;           s.insert(pos[i] + 1, 1,  ( );           s.insert(pos[j] + 1, 1,  ) );           ans = max(ans, cal(s));         }       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 7;   bitset<N> a[60];   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int n, m;     cin >> m >> n;     for (int i = 0, t; i < m; i++) {       cin >> t;       for (int j = 0, x; j < t; j++) cin >> x, a[i].set(x);       for (int j = 0; j < i; j++) {         if ((a[i] & (~a[j])) == a[i]) return cout <<  impossible  << endl, 0;       }     }     cout <<  possible n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> build(int n) {     map<int, bool> s;     for (int i = 2; i * i <= n; i++) {       if (s[i] == false) {         for (int j = 2 * i; j <= n; j = j + i) s[j] = true;       }     }     vector<int> t;     for (int i = 2; i <= n; i++) {       if (s[i] == false) t.push_back(i);     }     return t;   }   int main() {     int n;     cin >> n;     vector<int> a = build(n);     int b[n - 1];     int s = 1;     for (int i = 0; i < a.size(); i++) {       for (int j = a[i]; j <= n; j = j + a[i]) b[j - 2] = s;       s++;     }     for (int i = 0; i < n - 1; i++) cout << b[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long sum, i, j;     cin >> sum;     vector<long> length(3);     for (i = 0; i < 3; i++) cin >> length[i];     vector<vector<long> > dp(2, vector<long>(sum + 1, INT_MIN));     dp[3 % 2][0] = 0;     for (i = 2; i >= 0; i--) {       for (j = 0; j <= sum; j++) {         dp[i % 2][j] = dp[(i + 1) % 2][j];         if (j - length[i] >= 0 && dp[i % 2][j - length[i]] != INT_MIN)           dp[i % 2][j] = max(dp[i % 2][j], 1 + dp[i % 2][j - length[i]]);       }     }     cout << dp[0][sum];   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long long add(long long x, long long y) {     x += y;     if (x >= mod) x -= mod;     return x;   }   long long mul(long long x, long long y) { return (x * y) % mod; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     long long x, s;     cin >> s >> x;     long long ret = 0;     if (s == x) ret = -2;     long long ans = 1;     for (int i = 0; i <= 40; i++) {       if (x & (1LL << (i))) {         ans *= 2;         if (s < (1LL << (i))) {           cout << 0 << endl;           exit(0);         };         s -= (1LL << (i));       }     }     for (int i = 0; i <= 40; i++) {       if (!(x & (1LL << (i)))) {         if (s & (1LL << (i + 1))) {           s -= (1LL << (i + 1));         }       }     }     if (s != 0) {       cout << 0 << endl;       exit(0);     };     cout << ans + ret << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, ans = 0;   vector<int> a, b;   bool cmp(const int &a, const int &b) { return a > b; }   int main(int argc, char *argv[]) {     scanf( %d%d , &n, &x);     a.resize(n);     b.resize(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     for (int i = 0; i < n; ++i) {       scanf( %d , &b[i]);     }     sort(a.begin(), a.end(), cmp);     sort(b.begin(), b.end(), cmp);     for (int i = 0, j = n - 1; i < n && j >= 0; ++i) {       for (; j >= 0 && a[i] + b[j] < x; --j)         ;       if (j >= 0) {         ++ans;       }       --j;     }     printf( 1 %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 30;   int n, m, T;   string s[N], t[N];   int main() {     ios::sync_with_stdio(false), cin.tie(0);     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < m; i++) cin >> t[i];     cin >> T;     while (T--) {       int x;       cin >> x;       x--;       cout << s[x % n] << t[x % m] <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   struct Hungary {     vector<int> g[1005];     int from[1005], mx[1005], ans;     bool use[1005];     int n, sume;     int nume[1005];     bool match(int x, int exp) {       for (int i = int(0); i < int(g[x].size()); ++i) {         if (g[x][i] == exp) continue;         if (!use[g[x][i]]) {           use[g[x][i]] = true;           if (from[g[x][i]] == -1 || match(from[g[x][i]], exp)) {             from[g[x][i]] = x;             mx[x] = g[x][i];             return true;           }         }       }       return false;     }     void matching(int exp) {       ans = 0;       memset(from, -1, sizeof(from));       memset(mx, 0, sizeof(mx));       for (int i = int(1); i < int(n + 1); ++i) {         if (i == exp) continue;         memset(use, 0, sizeof(use));         if (match(i, exp)) ans++;       }     }     void init(int nn) {       n = nn;       for (int i = int(1); i < int(n + 1); ++i) g[i].clear();       sume = 0;       memset(nume, 0, sizeof(nume));     }     void AddEdge(int u, int v) {       g[u].push_back(v);       sume++;       nume[u]++;       if (u != v) nume[v]++;     }   } H;   bool mp[1005][1005];   int main() {     int n, m;     while (scanf( %d%d , &n, &m) == 2) {       memset(mp, 0, sizeof(mp));       H.init(n);       for (int i = int(0); i < int(m); ++i) {         int u, v;         scanf( %d%d , &u, &v);         mp[u][v] = true;         H.AddEdge(u, v);       }       int ans = n * n * n;       for (int i = int(1); i < int(n + 1); ++i) {         int pans = 0;         if (!mp[i][i]) pans++;         for (int j = int(1); j < int(n + 1); ++j) {           if (j == i) continue;           if (!mp[i][j]) pans++;           if (!mp[j][i]) pans++;         }         H.matching(i);         pans += (H.sume - H.nume[i] - H.ans);         pans += (n - 1 - H.ans);         ans = min(ans, pans);       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 1e6 + 10;   int main() {     int n;     cin >> n;     map<int, int> mc;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       mc[x] = i;     }     pair<int, int> ans = {INF, INF};     for (auto &p : mc) {       pair<int, int> pp = p;       swap(pp.first, pp.second);       ans = min(ans, pp);     }     cout << ans.second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long const INF = 1e18;   long long s[300100], a[300100];   int n, k;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     s[n + 1] = 0;     for (int i = n; i >= 1; i--) s[i] = s[i + 1] + a[i];     s[0] = s[1];     sort(s + 2, s + n + 1);     long long res = 0;     for (int i = n; i >= n - k + 2; i--) res = res + s[i];     res += s[1];     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double tux, pur, foo, bar, baz, quz = 1;   int main() {     cin >> tux;     for (int i = 1; i <= tux; i++) {       cin >> pur;       foo += pur;       bar++;       if (foo * quz > baz * bar) {         baz = foo;         quz = bar;       }     }     printf( %.6lf , baz / quz);     return 0;   }
#include <bits/stdc++.h>   int main(int argc, char **argv) {     long count = 0;     std::cin >> count;     long sum = 0;     long tmp;     long size;     do {       sum += (tmp = count);       size = (count / 2) + 1;       int offset = (count % 2) ? 2 : 1;       int start = (count % 2) ? 3 : 2;       for (long i = start; i < size; i += offset) {         if (!(count % i)) {           tmp = i;           break;         }       }       count /= tmp;     } while (count != 1);     std::cout << sum + 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, men = 0, pluss = 0, nol = 0;   long long a, ans = 0;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a;       if (a < 0) {         ans += (-1 - a);         men++;       } else if (a > 0) {         ans += (a - 1);         pluss++;       } else {         nol++;       }     }     if (nol > 0) {       cout << ans + nol << endl;     } else {       if (men % 2 == 0) {         cout << ans << endl;       } else {         cout << ans + 2 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e >> f;     if (d == 0)       cout <<  Hermione  << endl;     else if (c == 0)       cout <<  Ron  << endl;     else if (b == 0)       cout <<  Hermione  << endl;     else if (a == 0)       cout <<  Ron  << endl;     else if (f == 0)       cout <<  Hermione  << endl;     else if (e == 0)       cout <<  Ron  << endl;     else       cout << (b * d * f > a * c * e ?  Ron  :  Hermione ) << endl;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ul = unsigned long long;   using ld = long double;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ll n;     cin >> n;     vector<ll> a(n);     vector<pair<ll, ll>> b;     for (int i = 0; i < (n); i++) {       cin >> a[i];       a[i] -= i;       while (1) {         if (b.size() > 1 &&             (b[b.size() - 2].first - b.back().first) * b.back().second +                     b[b.size() - 2].first <=                 a[i]) {           b[b.size() - 2].second += b.back().second;           a[i] -= (b[b.size() - 2].first - b.back().first) * b.back().second;           b.pop_back();         } else if (!b.empty() && b.back().first <= a[i]) {           ll d = (a[i] - b.back().first) / (b.back().second + 1);           b.back().first += d;           a[i] -= d * b.back().second;           ll q = a[i] - b.back().first;           if (q > 0) {             pair<ll, ll> pp{b.back().first, b.back().second - q + 1};             b.back().first++;             b.back().second = q;             if (b.size() > 1 && b[b.size() - 2].first == b.back().first) {               b[b.size() - 2].second += b.back().second;               b.pop_back();             }             b.push_back(pp);           } else             b.back().second++;           break;         } else {           b.push_back({a[i], 1});           break;         }       }     }     ll cx = 0;     for (auto &pp : b)       for (int i = 0; i < (pp.second); i++) cout << cx++ + pp.first <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e3 + 5;   string s, p;   int nxt[MAX];   vector<int> cost;   int memo[MAX][MAX];   int roll(int idx, int val) {     if (idx >= (int)cost.size()) {       return 0;     }     int &ans = memo[idx][val];     if (~ans) {       return ans;     }     ans = roll(idx + 1, val);     if (val >= (int)cost[idx]) {       ans = max(ans, roll(nxt[idx], val - cost[idx]) + 1);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> s >> p;     vector<pair<int, int> > ranges;     for (int i = int(0); i < int(s.size()); i++) {       int idx = 0;       int match = -1;       if (s[i] == p[0]) {         for (int j = int(i); j < int(s.size()); j++) {           if (p[idx] == s[j]) {             idx++;           }           if (idx == (int)p.size()) {             match = j;             break;           }         }       }       if (match != -1) {         ranges.emplace_back(i, match);       }     }     for (int i = int(0); i < int(ranges.size()); i++) {       auto it = lower_bound(ranges.begin(), ranges.end(),                             make_pair(ranges[i].second + 1, 0));       nxt[i] = it - ranges.begin();       cost.push_back(           max(0, ranges[i].second - ranges[i].first - (int)p.size() + 1));     }     memset(memo, -1, sizeof memo);     for (int i = int(0); i < int(s.size() + 1); i++) {       cout << min(roll(0, i), ((int)s.size() - i) / (int)p.size()) <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 9;   vector<int> g[N];   int a[N], cnt[N], b[N << 1], c[N << 1];   int *vis = b + N, *val = c + N;   set<int> S, now;   int main() {     int n;     scanf( %d , &n);     for (int i = (1); i < (n + 1); ++i)       scanf( %d , &a[i]), cnt[a[i]]++, g[a[i]].push_back(i);     int maxn = 0, p = 0;     for (int i = (1); i < (n + 1); ++i) {       if (maxn < cnt[i]) {         maxn = cnt[i];         p = i;       }     }     if (maxn == n) {       puts( 0 );       return 0;     }     int ans = 0;     for (auto v : g[p]) S.insert(v);     vector<int> temp;     int key = 0;     for (int i = (1); i < (n + 1); ++i) {       if (p == i) continue;       now.clear();       temp.clear();       for (auto v : g[i]) {         auto it = S.lower_bound(v);         if (it != S.end()) {           now.insert(*it);           S.erase(it);         }         it = S.lower_bound(v);         if (it != S.end()) {           now.insert(*it);           S.erase(it);         }         it = S.lower_bound(v);         if (it != S.begin()) {           --it;           now.insert(*it);           S.erase(it);         }         it = S.lower_bound(v);         if (it != S.begin()) {           --it;           now.insert(*it);           S.erase(it);         }       }       for (auto v : now) temp.push_back(v);       for (auto v : g[i]) now.insert(v);       int pos = -1, sum = 0;       for (auto v : now) {         if (v > pos) ++key;         if (vis[sum] != key) {           vis[sum] = key;           val[sum] = v;         }         if (a[v] == p) ++sum;         if (a[v] == i) --sum;         auto it = S.lower_bound(v);         if (it == S.end())           pos = n + 1;         else           pos = *it;         if (vis[sum] == key) {           int l, r;           int vv = val[sum];           auto it = now.lower_bound(vv);           if (it == now.begin())             l = 0;           else             l = *prev(it);           it = now.lower_bound(v);           ++it;           if (it == now.end())             r = n + 1;           else             r = *it;           ans = max(r - l - 1, ans);         }       }       for (auto v : temp) S.insert(v);     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = int(5e3);   const int MOD = int(1e9 + 7);   const int oo = INT_MAX;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, d, h;     while (cin >> n >> d >> h) {       if (2 * h < d) {         cout << -1 <<   n ;         continue;       }       vector<pair<int, int> > edges;       for (int i = 2; i <= h + 1; i++) {         if (i > n) continue;         edges.push_back({i - 1, i});       }       for (int i = h + 2; i <= d + 1; i++) {         if (i > n) continue;         if (i == h + 2)           edges.push_back({1, i});         else           edges.push_back({i - 1, i});       }       int id = d + 2, node = -1;       if (h < d)         node = 1;       else if (h == d && h != 1)         node = 2;       if (node != -1) {         while (id <= n) {           edges.push_back({node, id});           id++;         }       }       if (int(edges.size()) == n - 1)         for (auto& e : edges) cout << e.first <<     << e.second <<   n ;       else         cout << -1 <<   n ;     }     return 0;   }