malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 7, MAXM = 1e5 + 5, MOD = 1e9 + 7, bit_one = (1 << MAXN) - 1;   int n, ans;   int w[MAXN];   int pre_process[MAXN][(1 << MAXN)][(1 << MAXN)];   int add(int a, int b) {     int c = a + b;     while (c >= MOD) c -= MOD;     return c;   }   int mul(int a, int b) { return 1LL * a * b % MOD; }   struct matrix {     int data[(1 << MAXN)][(1 << MAXN)];     int n, m;     matrix(int a = (1 << MAXN), int b = (1 << MAXN), bool c = 0) {       n = a;       m = b;       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) data[i][j] = (i == j) * c;     }     void operator*=(matrix tmp) {       matrix res(n, tmp.m);       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++)           for (int k = 0; k < tmp.m; k++)             res.data[i][k] = add(res.data[i][k], mul(data[i][j], tmp.data[j][k]));       (*this) = res;     }     void operator^=(int tmp) {       matrix res(n, m, 1);       for (; tmp; tmp /= 2) {         if (tmp & 1) res *= (*this);         (*this) *= (*this);       }       (*this) = res;     }   };   bool cheak(int a, int b, int c, int d) {     for (int i = 0; i <= a; i++)       if (((d >> i) & 1) && ((d >> (i + 1)) & 1) && ((b >> i) & 1) &&           ((c >> i) & 1))         return 0;     return 1;   }   int main() {     for (int i = 0; i < 7; i++) scanf( %d , &w[i]);     for (int i = 0; i < 7; i++)       for (int j = 0; j < (1 << (i + 1)); j++)         for (int k = 0; k < (1 << (i + 1)); k++)           for (int h = 0; h < (1 << i); h++)             pre_process[i][j][k] += cheak(i, j, k, (1 << (i + 1)) | (h << 1 | 1));     matrix M[7];     for (int i = 0; i < 7; i++) M[i].n = M[i].m = (1 << (i + 1));     for (int i = 0; i < 7; i++) {       for (int j = 0; j < (1 << (i + 1)); j++)         for (int k = 0; k < (1 << (i + 1)); k++)           M[i].data[j][k] = pre_process[i][j][k];       M[i] ^= w[i];     }     matrix A[7];     for (int i = 0; i < 7; i++) {       A[i].n = (1 << (i + 1));       A[i].m = 1;     }     A[0].data[1][0] = 1;     M[0] *= A[0];     A[0] = M[0];     for (int i = 1; i < 7; i++) {       for (int j = 0; j < (1 << i); j++) {         int tmp = j << 1 | 1;         A[i].data[tmp][0] = A[i - 1].data[j][0];       }       M[i] *= A[i];       A[i] = M[i];     }     ans = A[6].data[bit_one][0];     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long sum[1000006], add[1000006];   int Q[1000006], A[1000006];   int main() {     int n, m, a;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a);       A[a + 1] = 0;       int head = 1, tail = 0, mx = 0;       for (int j = 1; j <= a; j++) scanf( %d , &A[j]), mx = max(mx, A[j]);       Q[++tail] = 0;       for (int j = 1; j <= a; j++) {         while (tail >= head && A[j] > A[Q[tail]]) tail--;         Q[++tail] = j;         while (head <= tail && j + a - Q[head] > m) head++;         sum[j] += A[Q[head]];       }       int len = a;       head = tail = 1;       Q[1] = a + 1;       for (int j = m; j >= max(m - a + 1, a + 1); j--) {         while (tail >= head && A[len] > A[Q[tail]]) tail--;         Q[++tail] = len--;         while (head <= tail && j - Q[head] < 0) head++;         sum[j] += A[Q[head]];       }       if (a * 2 < m) add[a + 1] += mx, add[m - a + 1] -= mx;     }     for (int i = 1; i <= m; i++)       add[i] += add[i - 1], sum[i] += add[i], printf( %lld  , sum[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, m;       cin >> n >> m;       vector<long long int> graph[n + 1];       for (long long int i = 0; i < m; i++) {         long long int u, v;         cin >> u >> v;         graph[u].push_back(v);         graph[v].push_back(u);       }       long long int dis[n + 1];       memset(dis, 0, sizeof(dis));       long long int s = 1;       bool visited[n + 1];       memset(visited, false, sizeof(visited));       queue<long long int> q;       q.push(s);       visited[s] = true;       dis[s] = 0;       while (!q.empty()) {         long long int v = q.front();         q.pop();         for (auto i : graph[v]) {           if (!visited[i]) {             visited[i] = true;             q.push(i);             dis[i] = dis[v] + 1;           }         }       }       vector<long long int> even, odd;       for (long long int i = 1; i < n + 1; i++) {         if (dis[i] % 2 == 0) {           even.push_back(i);         } else           odd.push_back(i);       }       if (odd.size() < even.size()) {         cout << odd.size() << endl;         for (auto i : odd) cout << i <<    ;       } else {         cout << even.size() << endl;         for (auto i : even) cout << i <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const double eps = 1e-50;   const int maxN = 1100;   const double inf = 1e50;   int n, m;   vector<int> a[maxN];   double d[maxN][maxN];   int b[maxN];   double c[maxN][maxN];   bool have[maxN];   double t[maxN][maxN], s[maxN];   int main() {     for (int i = 0; i < maxN; ++i) {       c[i][0] = 1;       for (int j = 1; j <= i; ++j) {         c[i][j] = c[i - 1][j - 1] + c[i - 1][j];         if (c[i][j] >= inf) {           c[i][j] = inf;         }       }     }     cin >> n >> m;     vector<pair<int, int> > x;     for (int i = 0; i < m; ++i) {       int k;       cin >> k;       a[i].resize(k);       for (int j = 0; j < k; ++j) {         cin >> a[i][j];         x.push_back(make_pair(a[i][j], i));       }     }     sort(x.rbegin(), x.rend());     for (int i = 0; i < n; ++i) {       if (x[i].first != x[n - 1].first) {         ++b[x[i].second];       }     }     int needed = x[n - 1].first;     for (int i = 0; i < m; ++i) {       for (int j = 0; j < a[i].size(); ++j) {         if (a[i][j] == needed) {           have[i] = true;         }       }     }     d[0][0] = 1.0;     t[0][0] = 1.0;     for (int i = 0; i < m; ++i) {       for (int j = 0; j <= n; ++j) {         if (d[i][j] < eps) {           d[i][j] = 0.0;         }         if (t[i][j] > inf) {           t[i][j] = inf;         }         int cnt = b[i];         if (j + cnt <= n) {           d[i + 1][j + cnt] += d[i][j] / c[a[i].size()][cnt];           t[i + 1][j + cnt] += t[i][j];         }         if (have[i] && j + cnt + 1 <= n) {           d[i + 1][j + cnt + 1] += d[i][j] / c[a[i].size()][cnt + 1];           t[i + 1][j + cnt + 1] += t[i][j];         }       }     }     t[0][0] = 1.0;     for (int i = 0; i <= n; ++i) {       s[i] = t[0][i];       if (i > 0) {         s[i] += s[i - 1];       }     }     printf( %.10lf n , d[m][n] / t[m][n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void out() {     cout.flush();     int n;     cin >> n;     exit(0);   }   int tree[1 << 21];   int st = 1 << 20;   void add(int pos, int d) {     pos += st;     for (; pos; pos >>= 1) tree[pos] += d;   }   int findKth(int k) {     int pos = 1;     while (pos < st) {       pos *= 2;       if (tree[pos] < k) {         k -= tree[pos];         pos++;       }     }     return pos - st;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, q;     cin >> n >> m >> q;     vector<int> kols(m, 0);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       kols[x - 1]++;     }     vector<pair<int, int> > ps;     for (int i = 0; i < m; ++i) ps.push_back(make_pair(kols[i], i));     sort(ps.begin(), ps.end());     long long inf = 1000;     inf *= inf;     inf = inf * inf * inf;     inf += 1;     vector<long long> starts(m, 0);     for (int i = 1; i < m; ++i)       starts[i] = min(           inf, starts[i - 1] + (long long)i * (ps[i].first - ps[i - 1].first));     starts.push_back(inf + 1);     vector<pair<long long, int> > qs;     for (int i = 0; i < q; ++i) {       long long k;       cin >> k;       qs.push_back(make_pair(k - n - 1, i));     }     sort(qs.begin(), qs.end());     vector<int> anss(q, -1);     memset(tree, 0, sizeof(tree));     int cur = 0;     for (int i = 0; i < q; ++i) {       while (starts[cur] <= qs[i].first) {         add(ps[cur].second, 1);         ++cur;       }       long long k = qs[i].first - starts[cur - 1];       k %= cur;       anss[qs[i].second] = findKth(k + 1);     }     for (int i = 0; i < q; ++i) cout << anss[i] + 1 <<   n ;     out();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     double d, h, v, e;     scanf( %lf%lf%lf%lf , &d, &h, &v, &e);     if (v / (d * d * 3.1415926 / 4) > e) {       printf( YES n%.10f , h / (v / (d * d * 3.1415926 / 4) - e));     } else       cout <<  NO  << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a, b;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (x == 0) continue;       a.push_back(x);     }     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (x == 0) continue;       b.push_back(x);     }     n = a.size();     if (a.size() == b.size()) {       int s = -1;       {         auto it = find(begin(b), end(b), a[0]);         if (it != b.end()) {           s = it - b.begin();         }       }       if (s == -1) {         cout <<  NO  << endl;         return 0;       }       rotate(b.begin(), b.begin() + s, b.end());       cout << (a == b ?  YES  :  NO ) << endl;     } else {       cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e6 + 7;   int main() {     int n, q;     cin >> n >> q;     vector<int> a(n + 10, 0), b;     vector<pair<int, int>> v;     vector<int> cnt(n + 10, 0), cnt1(n + 10, 0);     set<int> s;     for (int i = 1; i <= n; i++) cin >> a[i], s.insert(a[i]);     for (int i = 0; i < q; i++) {       int l, r;       cin >> l >> r;       cnt[l]++;       cnt1[r]++;       v.push_back({l, r});     }     vector<int> ans;     int ans_mx = 0;     for (int i = 1; i <= n; i++) {       b = a;       vector<int> union_segment, l(n + 10, 0), r(n + 10, 0);       for (int j = 0; j < v.size(); j++) {         if (v[j].second < i || v[j].first > i) {           l[v[j].first]++;           r[v[j].second]++;           union_segment.push_back(j + 1);         }       }       int val = 0;       for (int j = 1; j <= n; j++) {         val += l[j];         b[j] -= val;         val -= r[j];       }       int mi = 1e9, mx = -1e9;       for (int j = 1; j <= n; j++) {         mx = max(mx, b[j]), mi = min(mi, b[j]);       }       if (mx - mi > ans_mx) {         ans_mx = mx - mi;         ans = union_segment;       }     }     cout << ans_mx <<   n ;     cout << ans.size() <<   n ;     for (int i : ans) cout << i <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9 + 7;   int dp[111][1 << 18], tr[111][1 << 18], prime[111], lp, mark[111];   int getbit(int x, int pos) { return (x >> pos) & 1; }   int onbit(int x, int pos) { return (x + (1 << pos)); }   void init() {     int nt[200];     for (int i = (1), _b = (60); i <= _b; i++) nt[i] = 1;     nt[1] = 0;     for (int i = (2), _b = (10); i <= _b; i++) {       if (nt[i] == 1) {         int j = i;         while (j < 60) {           j += i;           nt[j] = 0;         }       }     }     lp = -1;     for (int i = (1), _b = (60); i <= _b; i++) {       if (nt[i]) {         lp++;         prime[lp] = i;       }     }     for (int i = (1), _b = (60); i <= _b; i++) mark[i] = 0;     for (int i = (1), _b = (60); i <= _b; i++) {       int t = 0;       for (int j = (0), _b = (lp); j <= _b; j++) {         if (i % prime[j] == 0) {           t = onbit(t, j);         }       }       mark[i] = t;     }   }   int abS(int x) {     if (x > 0) return x;     return -x;   }   int main() {     int n, a[111];     cin >> n;     for (int i = (1), _b = (n); i <= _b; i++) cin >> a[i];     init();     int maxtt = (1 << 17) - 1;     for (int i = (0), _b = (maxtt); i <= _b; i++) dp[0][i] = 0;     for (int i = (1), _b = (n); i <= _b; i++)       for (int tt = (0), _b = (maxtt); tt <= _b; tt++) dp[i][tt] = oo;     for (int i = (1), _b = (n); i <= _b; i++) {       for (int val = (1), _b = (60); val <= _b; val++) {         for (int tt = (0), _b = (maxtt); tt <= _b; tt++) {           if ((mark[val] & tt) == 0) {             int ttnew = tt + mark[val];             int dpval = dp[i - 1][tt] + abS(val - a[i]);             if (dpval < dp[i][ttnew]) {               dp[i][ttnew] = dpval;               tr[i][ttnew] = val;             }           }         }       }     }     int ret = 0;     for (int tt = (0), _b = (maxtt); tt <= _b; tt++)       if (dp[n][ret] > dp[n][tt]) ret = tt;     int res[111];     for (int i = (n), _b = (1); i >= _b; i--) {       res[i] = tr[i][ret];       ret -= mark[res[i]];     }     for (int i = (1), _b = (n); i <= _b; i++)       printf( %d%c , res[i], i == n ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void func() {     long long n;     cin >> n;     std::vector<pair<long long, long long> > v(n);     for (long long i = 0; i < n; ++i) {       cin >> v[i].first;     }     for (long long i = 0; i < n; ++i) {       cin >> v[i].second;     }     sort(v.begin(), v.end());     multiset<long long> s;     long long i = 0, ans = 0, sum = 0, cur = v[0].first;     while (i < n || !s.empty()) {       while (v[i].first == cur && i < n) {         s.insert(v[i].second);         sum += v[i].second;         ++i;       }       if (!s.empty()) {         long long x = *s.rbegin();         sum -= x;         ans += sum;         s.erase(s.find(x));         cur++;       } else {         cur = v[i].first;       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       func();     }   }
#include <bits/stdc++.h>   using namespace std;   const long long MAX = 2005;   const long long INF = 1e9 + 5;   char grid[MAX][MAX];   long long dp[MAX][MAX];   auto solve(long long n, long long k) -> string {     auto inside = [&](long long row, long long col) {       return (row < n and row >= 0 and col < n and col >= 0);     };     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         dp[row][col] = INF;       }     }     dp[0][0] = (grid[0][0] !=  a ) ? 1 : 0;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if (row == 0 and col == 0) continue;         if ((row != 0) and (col != 0)) {           dp[row][col] =               min(dp[row][col - 1], dp[row - 1][col]) + (grid[row][col] !=  a );         } else {           dp[row][col] = (row == 0) ? dp[row][col - 1] + (grid[row][col] !=  a )                                     : dp[row][col];           dp[row][col] = (col == 0) ? dp[row - 1][col] + (grid[row][col] !=  a )                                     : dp[row][col];         }       }     }     long long pref = 0;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if (dp[row][col] <= k) {           pref = max(pref, row + col + 1);         }       }     }     string answer(pref,  a );     vector<pair<long long, long long>> list;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if ((dp[row][col] <= k) and (pref == row + col + 1)) {           list.push_back(make_pair(row, col));         }       }     }     if (list.empty()) {       answer = grid[0][0];       list.emplace_back(0, 0);     }     while (!list.empty()) {       char best_char =  z  + 1;       vector<pair<long long, long long>> new_list;       for (pair<long long, long long> p : list) {         for (long long d_row : {0, 1}) {           long long d_col = 1 - d_row;           long long r2 = p.first + d_row;           long long c2 = p.second + d_col;           if (inside(r2, c2)) {             if (grid[r2][c2] <= best_char) {               if (grid[r2][c2] < best_char) {                 new_list.clear();               }               best_char = grid[r2][c2];               new_list.push_back(make_pair(r2, c2));             }           }         }       }       new_list.resize(std::unique(new_list.begin(), new_list.end()) -                       new_list.begin());       list = new_list;       if (best_char !=  z  + 1) answer = answer + best_char;     }     return answer;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n, k;     std::cin >> n >> k;     for (long long i = 0; i < n; ++i) {       std::cin >> grid[i];     }     std::cout << solve(n, k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 10;   long long c[2][2][maxn][maxn];   int lowbit(int s) { return s & (-s); }   void modify(int x, int y, int v) {     for (int i = x; i < maxn; i += lowbit(i))       for (int j = y; j < maxn; j += lowbit(j)) c[x & 1][y & 1][i][j] ^= v;     return;   }   long long query(int x, int y) {     long long ret = 0;     for (int i = x; i > 0; i -= lowbit(i))       for (int j = y; j > 0; j -= lowbit(j)) ret ^= c[x & 1][y & 1][i][j];     return ret;   }   int main(void) {     int n, m;     scanf( %d %d , &n, &m);     while (m--) {       long long v;       int op, x0, y0, x1, y1;       scanf( %d %d %d %d %d , &op, &x0, &y0, &x1, &y1);       if (op == 2) {         scanf( %I64d , &v);         modify(x0, y0, v);         modify(x0, y1 + 1, v);         modify(x1 + 1, y0, v);         modify(x1 + 1, y1 + 1, v);       } else         printf( %I64d n , query(x1, y1) ^ query(x0 - 1, y1) ^ query(x1, y0 - 1) ^                               query(x0 - 1, y0 - 1));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     long long n, k, m;     cin >> n >> k >> m;     vector<long long> a(k);     long long second = 0;     for (long long i = 0; i < k; i++) {       cin >> a[i];       second += a[i];     }     sort(a.begin(), a.end());     long long ans = 0;     for (long long p = 0; p <= n; p++) {       long long t = m - second * p;       long long kol = 0;       if (t < 0) break;       kol = p * (k + 1);       for (long long j = 0; j < k; j++) {         long long ost = n - p;         while (ost > 0 && t - a[j] >= 0) {           ost--;           t -= a[j];           kol++;         }       }       ans = max(ans, kol);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const double EXP = 2.7182818284590452;   const double Pi = 3.14159265;   const double EPS = 1e-11;   const int INF = 1000 * 1000 * 1000;   const long long INFL = INF * 1000 * 100;   char arr[2003][2003], c;   bool was[2003][2003];   int n, m;   queue<std::pair<int, int> > stars;   std::pair<int, int> cur;   std::pair<int, int> mask[] = {std::make_pair(-1, -1), std::make_pair(-1, 0),                                 std::make_pair(-1, 1),  std::make_pair(0, -1),                                 std::make_pair(0, 1),   std::make_pair(1, -1),                                 std::make_pair(1, 0),   std::make_pair(1, 1)};   bool IsValid(int r, int c) {     if (r < 1 || r > n) return false;     if (c < 1 || c > m) return false;     return true;   }   bool Check(int r, int c) {     if (arr[r][c - 1] == arr[r - 1][c - 1] &&         arr[r - 1][c - 1] == arr[r - 1][c] && arr[r - 1][c] ==  . )       return true;     if (arr[r - 1][c] == arr[r - 1][c + 1] &&         arr[r - 1][c + 1] == arr[r][c + 1] && arr[r][c + 1] ==  . )       return true;     if (arr[r][c + 1] == arr[r + 1][c + 1] &&         arr[r + 1][c + 1] == arr[r + 1][c] && arr[r + 1][c] ==  . )       return true;     if (arr[r + 1][c] == arr[r + 1][c - 1] &&         arr[r + 1][c - 1] == arr[r][c - 1] && arr[r][c - 1] ==  . )       return true;     return false;   }   void AddNew(int r, int c) {     int rr, cc;     for (int i = (0); i < (8); ++i) {       rr = r + mask[i].first;       cc = c + mask[i].second;       if (IsValid(rr, cc) && arr[rr][cc] ==  * ) {         if (!was[rr][cc] && Check(rr, cc)) {           stars.push(std::make_pair(rr, cc));           was[rr][cc] = true;         }       }     }   }   int main(void) {     cin >> n >> m;     for (int i = (0); i < (n + 2); ++i)       for (int j = (0); j < (m + 2); ++j) arr[i][j] =  * ;     for (int i = (1); i < (n + 1); ++i) {       scanf( %c , &c);       for (int j = (1); j < (m + 1); ++j) scanf( %c , arr[i] + j);     }     for (int i = (1); i < (n + 1); ++i)       for (int j = (1); j < (m + 1); ++j) {         if (Check(i, j)) {           stars.push(std::make_pair(i, j));           was[i][j] = true;         }       }     while (stars.size()) {       cur = stars.front();       stars.pop();       arr[cur.first][cur.second] =  . ;       AddNew(cur.first, cur.second);     }     for (int i = (1); i < (n + 1); ++i) {       for (int j = (1); j < (m + 1); ++j) {         printf( %c , arr[i][j]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int m;   void Add(int &a, int b) {     a += b;     if (a >= m) a -= m;   }   int dp[1010][110][2];   int main() {     int n, k;     while (scanf( %d%d%d , &n, &k, &m) == 3) {       memset(dp, 0, sizeof(dp));       dp[0][0][0] = 1;       long long tmp = 1;       for (int i = 0; i < n; i++) {         for (int j = 0; j < k; j++)           for (int x = 0; x < 2; x++) {             if (dp[i][j][x] == 0) continue;             for (int y = 0; y < 10; y++) {               if (i == n - 1 && y == 0) continue;               int nj = (y * tmp % k + j) % k;               int nx = x;               if (nj == 0 && (y > 0 || j > 0)) nx = 1;               Add(dp[i + 1][nj][nx], dp[i][j][x]);             }           }         tmp = tmp * 10 % k;       }       int ans = 0;       for (int j = 0; j < k; j++) Add(ans, dp[n][j][1]);       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long double n;     int m;     cin >> n >> m;     long sum = n;     long double newsum = 0.0;     for (int i = 0; i < n; i++) {       newsum += pow((long double)i / (long double)n, m);     }     cout << sum - newsum;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 18 | 7;   int a[N], b[N], o[N];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, s;     cin >> n >> s;     --s;     for (int i = 0; i < n; ++i) cin >> a[i], b[i] = a[i];     for (int i = 0; i < n; ++i)       if (!a[i]) a[i] = n;     if (a[s]) a[s] = 0;     priority_queue<pair<int, int> > Q;     for (int i = 0; i < n; ++i) Q.push({a[i], i}), o[a[i]] = 1;     for (int i = 1; i < Q.top().first; ++i) {       if (o[i]) continue;       auto now = Q.top();       Q.pop();       a[now.second] = i;       now.first = i;       Q.push(now);     }     int ans = 0;     for (int i = 0; i < n; ++i) ans += a[i] != b[i];     cout << ans << endl;   }