malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n;   int x[1000005], y[1000005];   int s[1000005], _sum[1000006], _sumx[1000006], _sumy[1000006],       *sum = _sum + 1, *sumx = _sumx + 1, *sumy = _sumy + 1;   long long _tot[1000006], *tot = _tot + 1;   const int MOD = 1e9 + 7;   inline int area(int x, int y, int xx, int yy) {     int ret = ((long long)x * yy - (long long)xx * y) % MOD;     if (ret < 0) ret += MOD;     return ret;   }   inline long long areaL(int x, int y, int xx, int yy) {     return (long long)x * yy - (long long)xx * y;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < (int)(n); ++i) scanf( %d%d , x + i, y + i);     int ans = 0, mul = -1;     for (int times = 0; times < (int)(2); ++times) {       for (int i = 0; i < (int)(n + 5); ++i) {         x[n + i] = x[i];         y[n + i] = y[i];       }       int N = 2 * n + 4;       for (int i = 0; i < (int)(N); ++i)         s[i] = area(x[i], y[i], x[i + 1], y[i + 1]);       tot[-1] = sum[-1] = sumx[-1] = sumy[-1] = 0;       for (int i = 0; i < (int)(N); ++i)         tot[i] = tot[i - 1] + areaL(x[i], y[i], x[i + 1], y[i + 1]);       for (int i = 0; i < (int)(N); ++i)         sum[i] = (sum[i - 1] - (long long)i * s[i]) % MOD;       for (int i = 0; i < (int)(N); ++i) sumx[i] = (sumx[i - 1] + x[i]) % MOD;       for (int i = 0; i < (int)(N); ++i) sumy[i] = (sumy[i - 1] + y[i]) % MOD;       for (int i = 0, j = 1; i < n; ++i) {         if (j == i) ++j;         while (j < i + n - 2 &&                abs(tot[j] - tot[i - 1] + areaL(x[j + 1], y[j + 1], x[i], y[i])) +                        times <=                    abs(tot[n + i - 1] - tot[j] +                        areaL(x[i], y[i], x[j + 1], y[j + 1]))) {           ++j;         }         if (j == i + 1) continue;         ans = (ans + mul * (sum[j - 1] - sum[i - 1] +                             (tot[j - 1] - tot[i - 1]) % MOD * j - s[i])) %               MOD;         ans = (ans + mul * areaL(sumx[j] - sumx[i + 1], sumy[j] - sumy[i + 1],                                  x[i], y[i])) %               MOD;       }       if (times == 0) {         reverse(x + 1, x + n);         reverse(y + 1, y + n);         mul = 1;       }     }     int ss = 0;     for (int i = 0; i < (int)(n); ++i)       ss = (ss + area(x[i], y[i], x[i + 1], y[i + 1])) % MOD;     ans = ((long long)ss * ((long long)n * (n - 3) / 2 % MOD) - ans) % MOD;     if (ans < 0) ans += MOD;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int l, c;   } a[100005];   int b, c, d, e, f[205], g, h, i, j, k, l, m, n, s[100005];   inline bool cmp(node a, node b) { return a.l < b.l; }   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i].l);     for (i = 1; i <= n; i++) scanf( %d , &a[i].c);     sort(a + 1, a + 1 + n, cmp);     for (i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].c;     h = s[n];     for (i = 1; i <= n; i = j + 1) {       for (j = i; a[j + 1].l == a[i].l; j++)         ;       k = j - i + 1;       g = s[n] - s[j];       for (l = 1, d = i - k; l <= 200 && d > 0; l++) {         if (d > f[l]) {           d = d - f[l];           g = g + f[l] * l;         } else {           g = g + d * l;           d = 0;         }       }       h = min(h, g);       for (l = i; l <= j; l++) f[a[l].c]++;     }     printf( %d n , h);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int u, int v) { return (v != 0) ? gcd(v, u % v) : u; }   struct disjoint_sets {     int parent[100000 + 10];     int ccOfParent[100000 + 10];     int cc;     disjoint_sets(int V) {       cc = V;       for (int i = 1; i <= V; i++) parent[i] = i, ccOfParent[i] = 1;     }     int getParent(int a) {       if (parent[a] == a) return a;       return parent[a] = getParent(parent[a]);     }     void link(int a, int b) {       ccOfParent[getParent(b)] += ccOfParent[getParent(a)];       ccOfParent[getParent(a)] = ccOfParent[getParent(b)];       parent[getParent(a)] = getParent(b);     }     bool unionSets(int a, int b) {       int p1 = getParent(a);       int p2 = getParent(b);       if (p1 == p2) return 0;       link(a, b);       cc--;       return 1;     }   };   vector<int> adj[100000 + 10];   int V, E;   vector<pair<int, int> > vec;   bool vis[100000 + 10] = {0};   double solve() {     disjoint_sets s(V);     long long total = 0;     for (int i = 0; i < V; i++) {       int a = vec[i].second;       long long cost = vec[i].first;       for (int j = 0; j < adj[a].size(); j++) {         int b = adj[a][j];         if (!vis[b]) continue;         int cc1 = s.ccOfParent[s.getParent(a)];         int cc2 = s.ccOfParent[s.getParent(b)];         if (s.unionSets(a, b)) total += 2 * cost * cc1 * cc2;       }       vis[a] = 1;     }     return (total / ((double)V * (V - 1)));   }   int main() {     ios_base::sync_with_stdio(false);     cin >> V >> E;     vec.resize(V);     for (int i = 1; i <= V; i++) cin >> vec[i - 1].first, vec[i - 1].second = i;     int a, b;     for (int j = 0; j < E; j++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     sort(vec.rbegin(), vec.rend());     double res = solve();     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }   long long n;   long long x[1010];   long long y[1010];   int main() {     cin >> n;     for (int i = 0; i < n / 3; i++) {       printf( %d %d n , 0, i * 2 + 1);     }     for (int i = 0; i < n - n / 3; i++) {       printf( %d %d n , 3, i);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2010;   struct Point {     long long x, y;     int id;     Point(int _x = 0, int _y = 0, int _id = 0) : x(_x), y(_y), id(_id) {}     void read(int i) {       id = i;       scanf( %lld%lld , &x, &y);     }   } ps[maxn];   Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }   long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }   Point base;   bool cmp(Point a, Point b) { return cross(a - base, b - base) > 0; }   char s[maxn];   int vis[maxn], ans[maxn];   Point t[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       ps[i].read(i);     }     Point start = ps[1];     for (int i = 2; i <= n; i++) {       if (ps[i].y < start.y) start = ps[i];     }     ans[1] = start.id;     vis[start.id] = 1;     base = start;     scanf( %s , s + 1);     for (int i = 2; i < n; i++) {       int cnt = 0;       for (int j = 1; j <= n; j++)         if (!vis[j]) t[cnt++] = ps[j];       sort(t, t + cnt, cmp);       if (s[i - 1] ==  L ) {         ans[i] = t[0].id;         vis[t[0].id] = 1;         base = t[0];       } else {         ans[i] = t[cnt - 1].id;         vis[t[cnt - 1].id] = 1;         base = t[cnt - 1];       }     }     for (int i = 1; i <= n; i++)       if (!vis[i]) ans[n] = i;     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long a, b, f = 0;     long long min = 500, max = 0;     string s =   ;     long long suma = 0, sumb = 0;     for (int i = 0; i < n; i++) {       cin >> a >> b;       if (abs((suma + a) - sumb) <= 500) {         s +=  A ;         suma += a;       } else if (abs(suma - (sumb + b)) <= 500) {         s +=  G ;         sumb += b;       } else {         cout <<  -1 n ;         f = 1;         break;       }     }     if (f == 0) cout << s << endl;   }
#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   int main() {     int n, m, cnt, i, j;     while (~scanf( %d%d , &n, &m)) {       if ((n % 2 == 0 && m * 2 <= n * n) || (n % 2 && m * 2 <= n * n + 1)) {         printf( YES n );         cnt = 0;         for (i = 1; i <= n; i++) {           for (j = 1; j <= n; j++) {             if (i % 2 == j % 2 && cnt < m) {               printf( L );               cnt++;             } else               printf( S );           }           printf(  n );         }       } else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int C = 26;   void add(int &x, int y) {     x += y;     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;   }   int fix(int x) {     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;     return x;   }   int pw(int a, int b) {     int ret = 1;     while (b) {       if (b & 1) ret = 1ll * ret * a % MOD;       b >>= 1;       a = 1ll * a * a % MOD;     }     return ret;   }   const int MAXN = 1e6 + 10;   bool pal(string s) {     string t = s;     reverse(t.begin(), t.end());     return s == t;   }   void solve() {     string s;     cin >> s;     int n = s.size();     int ans = 10;     int m = (n + 1) / 2;     for (int i = m; i + 1 < n; i++) {       if (s[i] != s[i + 1]) {         ans = 3 + n % 2;         break;       }     }     for (int i = 0; i < n / 2; i++) {       string t = s.substr(0, i + 1);       if (!pal(t)) {         ans = 2;         break;       }     }     for (int i = 1; i < n; i++) {       string t = s.substr(i) + s.substr(0, i);       if (t != s && pal(t)) {         ans = 1;         break;       }     }     if (ans < 10) {       cout << ans <<   n ;     } else {       cout <<  Impossible n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int te = 1;     for (int w = 1; w <= te; w++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using llong = long long;   inline char Flip(char x) { return (x ==  0 ) ?  1  :  0 ; }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     vector<int> ans;     while (t--) {       int n;       cin >> n;       n *= 2;       vector<int> a(n);       for (int& i : a) cin >> i;       vector<int> b;       int i = n - 1;       while (i >= 0) {         int j = max_element(a.begin(), a.begin() + i + 1) - a.begin();         b.push_back(i - j + 1);         i = j - 1;       }       bitset<4001> dp;       dp[0] = 1;       for (int len : b) dp = dp | (dp << len);       if (dp[n / 2])         cout <<  YES n ;       else         cout <<  NO n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m, u, v, flag, num, vis[100100];   vector<int> edge[100100];   queue<int> sk[100100];   struct G {     int a, b, c;   } ans[100100];   void dfs(int u, int father) {     vis[u] = true;     for (int i = 0; i < edge[u].size(); ++i) {       int v = edge[u][i];       if (vis[v] == false)         dfs(v, u);       else if (v != father && vis[v] == true)         sk[v].push(u);     }     while (sk[u].size() >= 2) {       int x = sk[u].front();       sk[u].pop();       int y = sk[u].front();       sk[u].pop();       ans[++num] = {x, u, y};     }     if (sk[u].size() == 1) {       if (father == 0) flag = 1;       int x = sk[u].front();       sk[u].pop();       ans[++num] = {x, u, father};     } else       sk[father].push(u);   }   int main() {     cin >> n >> m;     if (m % 2 == 1) {       cout <<  No solution  << endl;       return 0;     }     for (int i = 1; i <= m; ++i) {       cin >> u >> v;       edge[u].push_back(v);       edge[v].push_back(u);     }     dfs(1, 0);     if (flag == 1)       cout <<  No solution  << endl;     else {       for (int i = 1; i <= num; ++i) {         cout << ans[i].a <<     << ans[i].b <<     << ans[i].c << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, k, i, start;     bool b;     while (scanf( %d%d , &n, &k) != EOF) {       if (n < k || k == 1 && n > 1) {         printf( -1 n );         continue;       }       start = k;       for (i = b = 0; i < n; i++) {         if (b && n - i == k - 2) {           start = 2;           break;         }         if (!b && n - i == k - 1) {           start = 1;           break;         }         printf(i& 1 ? b = 1,  b  :  a );       }       for (i = start; i < k; i++) printf( %c , i +  a );       printf(  n );     }     return 0;   }
#include<bits/stdc++.h>   #define pb push_back   using namespace std;   typedef long long ll;   const int maxn=1e6+100;   const int M=1000000007;   typedef array<int,41> A;   struct hasher {       size_t operator()(const A& p) const {           size_t result = 0;           for (int i=1;i<=40;i++) result = result * 19260817 + p[i];           return result;       }   };   bool vis[50];   int f[50],n,mx,F[50][50],G[50][50],p[50],nf[50],inv[50];   int ways[50][50][50];   unordered_map<A,int,hasher> dp[41][41][41];   void add(int &x,int y){x+=y;if (x>=M)x-=M;}   int C(int x,int y){    if (y<0||y>x) return 0;    return 1ll*f[x]*nf[y]%M*nf[x-y]%M;   }   void init(){    inv[1]=1; for (int i=2;i<=n;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;    f[0]=nf[0]=1; for (int i=1;i<=n;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;    F[0][0]=f[n-1];    for (int i=0;i<=n-1;i++)    for (int j=1;j+i<=n-1;j++){           F[i][j]=0;           for (int x=0;x<=i;x++)      for (int y=0;y<=j;y++){       add(F[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M);       if (!y) add(G[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M);      }    }    for (int i=0;i<=n-1;i++)    for (int j=0;j+i<=n-1;j++){     int x=1,y=1;     for (int k=1;k<=n;k++){      x=1ll*x*F[i][j]%M;      y=1ll*y*G[i][j]%M;      ways[i][j][k]=(x-y+M)%M;     }    }   }   int solve(int lasttot,int tot,int block,A a){       if (dp[lasttot][tot][block].find(a)!=dp[lasttot][tot][block].end()){     return dp[lasttot][tot][block][a];       }       int ret=0,sum=n;       for (int i=1;i<=mx;i++) sum-=a[i]*i;       if (sum==n) return dp[lasttot][tot][block][a]=nf[block];    A tmp=a;       for (int i=1;i<=mx;i++) {     if (a[i]){      tmp[i]--;               add(ret,1ll*ways[sum-tot-lasttot][lasttot][i]*a[i]%M*solve(lasttot,tot+i,block+1,tmp)%M);               if (block) add(ret,1ll*ways[sum-tot][tot][i]*a[i]%M*nf[block]%M*solve(tot,i,1,tmp)%M);      tmp[i]++;     }       }       return dp[lasttot][tot][block][a]=ret;   }   int main(){    scanf( %d ,&n);    init();    for (int i=1;i<=n;i++) scanf( %d ,&p[i]);    A a;    for (int i=0;i<=40;i++) a[i]=0;    for (int i=1;i<=n;i++) if (!vis[i]){     int w=0,tmp=i;     while (!vis[tmp]){      w++;      vis[tmp]=1;      tmp=p[tmp];     }     a[w]++;     mx=max(mx,w);    }    printf( %d n ,solve(0,0,0,a));    return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int NN = 1000100;   int link[NN], fail[NN], len[NN], dif[NN], nxt[NN][26];   int ed, sz, cur;   char s[NN], S[NN], T[NN];   int tp[NN], dp[NN], pre[NN], pr[NN];   void clear(int u, int L = 0) { len[u] = L; }   void init() {     sz = 2;     ed = 1;     cur = 0;     s[cur] = -1;     clear(0);     clear(1, -1);     fail[0] = 1;   }   inline int get_fail(int u) {     while (s[cur - len[u] - 1] != s[cur]) u = fail[u];     return u;   }   int insert(char c) {     s[++cur] = c -=  a ;     ed = get_fail(ed);     if (!nxt[ed][c]) {       int p1 = sz++;       clear(p1, len[ed] + 2);       fail[p1] = nxt[get_fail(fail[ed])][c];       nxt[ed][c] = p1;       dif[p1] = len[p1] - len[fail[p1]];       if (dif[p1] == dif[fail[p1]])         link[p1] = link[fail[p1]];       else         link[p1] = fail[p1];     }     ed = nxt[ed][c];     return ed;   }   int main() {     scanf( %s%s , S, T);     int n = strlen(S);     init();     memset(tp, 0x3f, sizeof tp);     for (int i = 1, now; i <= n * 2; i++) {       if (i & 1)         now = insert(S[i - 1 >> 1]);       else         now = insert(T[i - 1 >> 1]);       dp[i] = 0x3f3f3f3f;       if ((~i & 1) && S[i - 1 >> 1] == T[i - 1 >> 1]) {         dp[i] = dp[i - 2];         pre[i] = i - 2;       }       for (int u = now; u; u = link[u]) {         pr[u] = i - len[link[u]] - dif[u];         tp[u] = dp[pr[u]];         if (link[u] ^ fail[u]) {           if (tp[u] > tp[fail[u]]) {             tp[u] = tp[fail[u]];             pr[u] = pr[fail[u]];           }         }         if (~i & 1) {           if (dp[i] > tp[u] + 1) {             dp[i] = tp[u] + 1;             pre[i] = pr[u];           }         }       }     }     if (dp[n * 2] == 0x3f3f3f3f) return puts( -1 );     vector<pair<int, int>> ans;     int now = n * 2;     while (now) {       int pre = ::pre[now];       if (now - pre > 2) ans.emplace_back((pre >> 1) + 1, now >> 1);       now = pre;     }     cout << ans.size() << endl;     for (auto &a : ans) printf( %d %d n , a.first, a.second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Fenwick {     vector<long long> BIT;     long long N;     Fenwick(long long n) {       N = n + 2;       BIT.resize(N + 10);     }     void reset() {       for (long long i = 0; i <= N; i++) {         BIT[i] = 0;       }     }     void add(long long r, long long value) {       for (long long i = r; i <= N; i = i | (i + 1)) {         BIT[i] += value;       }     }     long long qry(long long r) {       long long ans = 0;       for (long long i = r; i >= 0; i = (i & (i + 1)) - 1) {         ans += BIT[i];       }       return ans;     }   };   const long long N = (long long)1e6;   void solve() {     long long n, m;     cin >> n >> m;     vector<array<long long, 3>> x(n), y(m);     for (auto &i : x) {       cin >> i[0] >> i[1] >> i[2];     }     for (auto &i : y) {       cin >> i[0] >> i[1] >> i[2];     }     m += 1;     y.push_back({N, 0, N});     long long ans = 0;     Fenwick sg(N + 10);     sg.add(N, 1);     sg.add(0 * 1LL, 1);     sort(y.begin(), y.end());     vector<vector<long long>> add(N + 10), remove(N + 10);     for (auto &i : x) {       if (i[1] == 0) {         add[0].push_back(i[0]);         remove[i[2]].push_back(i[0]);       } else {         add[i[1]].push_back(i[0]);         remove[N - 1].push_back(i[0]);       }     }     long long cur = 0;     42;     for (long long i = 0; i <= N; i++) {       for (auto &j : add[i]) {         sg.add(j, 1);       }       while (cur < m and y[cur][0] == i) {         long long u = max(y[cur][1], y[cur][2]);         long long v = min(y[cur][1], y[cur][2]);         if (v == 0) {           ans += (sg.qry(u) - 1);         } else {           ans += (sg.qry(u) - sg.qry(v - 1) - 1);         }         cur += 1;       }       for (auto &j : remove[i]) {         sg.add(j, -1);       }     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();   }
#include <bits/stdc++.h>   using namespace std;   const long long LINF = 1e18;   const long long INF = 1e9;   const long long MOD = 1000000007;   const long long MAX = 100005;   #pragma GCC optimize( O3 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( -ffloat-store )   #pragma GCC optimize( -fno-defer-pop )   long long strtoll(string second) {     stringstream str(second);     long long number = 0;     str >> number;     return number;   }   string lltostr(long long n) {     stringstream ss;     ss << n;     return ss.str();   }   bool prime[MAX];   void sieve() {     memset(prime, true, sizeof(prime));     prime[0] = prime[1] = false;     for (long long p = 2; p * p <= MAX; p++) {       if (prime[p] == true) {         for (long long i = p * 2; i <= MAX; i += p) prime[i] = false;       }     }   }   long long power(long long a, long long b, long long m) {     if (b == 0) return 1;     if (b == 1) return a % m;     long long t = power(a, b / 2, m);     t = (t * t) % m;     if (b & 1) t = (t * a) % m;     return t;   }   long long modInverse(long long a, long long m) { return power(a, m - 2, m); }   long long __gcd(long long a, long long b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   bool cmp(const pair<long long, long long> &l,            const pair<long long, long long> &r) {     return l.first < r.first;   }   void solve() {     long long n, m;     cin >> n >> m;     long long a[n + 1], c[n + 1];     for (long long i = 0; i < n; i++) cin >> a[i + 1];     for (long long i = 0; i < n; i++) cin >> c[i + 1];     map<long long, long long> mm;     set<pair<long long, long long> > second;     for (long long i = 1; i <= n; i++) second.insert({c[i], i});     for (long long i = 1; i <= n; i++) mm[i] = a[i];     while (m--) {       long long ans = 0;       long long type, q;       cin >> type >> q;       if (mm[type] >= q) {         ans += c[type] * q;         mm[type] -= q;       } else {         ans += mm[type] * c[type];         q -= mm[type];         mm[type] = 0;         while (q && !second.empty()) {           pair<long long, long long> p = *second.begin();           long long x = p.first;           long long y = p.second;           if (mm[y] >= q) {             mm[y] -= q;             ans += (c[y] * q);             q = 0;           } else {             ans += mm[y] * c[y];             q -= mm[y];             mm[y] = 0;             second.erase(second.begin());           }         }         if (q != 0) ans = 0;       }       cout << ans <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     ;     long long t;     t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     map<int, vector<int> > map1;     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       map1[i].push_back(a);       map1[a].push_back(i);     }     int counter = 0;     map<int, bool> check;     for (int i = 1; i <= n; i++) {       if (!check[i]) {         counter++;         queue<int> que;         que.push(i);         while (que.size()) {           int front = que.front();           que.pop();           if (!check[front])             for (int j = 0; j < map1[front].size(); j++) {               que.push(map1[front][j]);             }           check[front] = true;         }       }     }     cout << counter;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 10;   const long long inf = 1e18;   const long long mod = 1e9 + 7;   long long n, q;   long long a[maxn];   long long cost(long long g) {     long long ans = 0;     for (long long i = 1; i <= n; i++) {       if (a[i] < g)         ans += g - a[i];       else         ans += min(a[i] % g, g - a[i] % g);     }     return ans;   }   long long ans = inf;   void DO(long long x) {     if (x <= 1) return;     for (long long i = 2; i <= sqrt(x); i++)       if (x % i == 0) {         ans = min(ans, cost(i));         while (x % i == 0) x /= i;       }     if (x > 1) ans = min(ans, cost(x));   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 1; i <= n; i++) cin >> a[i];     random_shuffle(a + 1, a + n + 1);     for (long long i = 1; i <= 10; i++) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1);     for (long long i = n / 3; i <= n / 3 + 15; i++)       DO(a[i] - 1), DO(a[i]), DO(a[i] + 1);     for (long long i = 2 * n / 3; i <= 2 * n / 3 + 15; i++)       DO(a[i] - 1), DO(a[i]), DO(a[i] + 1);     for (long long i = n; i >= n - 10; i--) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1);     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   long long h, n;   int main() {     cin >> h >> n;     long long ans = 0;     long long l = 1, r = (1LL << h);     int dir = 0;     for (int i = h - 1; i >= 0; i--) {       ans++;       long long mid = (l + r) / 2;       if (dir == 0) {         if (mid < n) {           ans += (1LL << (i + 1)) - 1;           dir = 0;           l = mid + 1;         } else {           r = mid;           dir = 1;         }       } else {         if (n <= mid) {           ans += (1LL << (i + 1)) - 1;           dir = 1;           r = mid;         } else {           l = mid + 1;           dir = 0;         }       }     }     cout << ans << endl;   }