malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int INF = 123456789;   template <class T>   void scanint(T& x) {     register int c = getchar();     x = 0;     int m = 0;     if (c ==  - ) {       m = 1;     }     for (; (c < 48 || c > 57); c = getchar())       ;     for (; c > 47 && c < 58; c = getchar()) {       x = (x << 1) + (x << 3) + c - 48;     }     if (m == 1) {       x *= -1;     }   }   template <typename T>   void cinall(T& t) {     cin >> t;   }   template <typename T, typename... Args>   void cinall(T& t, Args&... args) {     cin >> t;     cinall(args...);   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cinall(n);     ;     vector<int> v(n);     vector<int> xv(n + 1);     long long sol = 0;     for (int i = 0; i < n; ++i) {       cin >> v[i];       sol ^= v[i];       xv[i + 1] = xv[i] ^ i;     }     for (int i = 2; i <= n; ++i) {       sol ^= xv[((n / i) % 2) * i] ^ xv[(n % i) + 1];     }     cout << sol;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, q, a[300005], ans[300005];   vector<pair<int, int> > queries[300005];   int tree[1200006];   void upd(int l, int r, int pos, int x) {     if (l == r) {       tree[pos]++;       return;     }     int mid = l + r >> 1;     if (x <= mid)       upd(l, mid, pos << 1, x);     else       upd(mid + 1, r, pos << 1 | 1, x);     tree[pos]++;   }   int query(int l, int r, int pos, int x) {     if (l == r) return r;     int mid = l + r >> 1;     if (tree[pos << 1] >= x)       return query(l, mid, pos << 1, x);     else       return query(mid + 1, r, pos << 1 | 1, x - tree[pos << 1]);   }   int query1(int l, int r, int pos, int ql, int qr) {     if (l >= ql && r <= qr) return tree[pos];     if (l > qr || r < ql) return 0;     int mid = l + r >> 1;     return query1(l, mid, pos << 1, ql, qr) +            query1(mid + 1, r, pos << 1 | 1, ql, qr);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i < q; i++) {       int x, y;       cin >> x >> y;       queries[n - y].push_back({x, i});     }     for (int i = 1; i <= n; i++) {       if (a[i] > i)         upd(0, n, 1, 0);       else if (a[i] == i)         upd(0, n, 1, i);       else {         int x = query(0, n, 1, a[i]);         upd(0, n, 1, x);       }       for (auto p : queries[i]) {         int x = query1(0, n, 1, 0, p.first);         ans[p.second] = i - x;       }     }     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tests;     cin >> tests;     for (int test = 0; test < (int)(tests); test++) {       int n;       cin >> n;       map<int, int> m;       for (int i = 0; i < (int)(n); i++) {         int x;         cin >> x;         m[-x]++;       }       vector<int> v;       for (auto i : m) {         v.push_back(i.second);       }       int g, s, b;       int j = 0;       int k = 0;       g = s = b = 0;       vector<int> ans(3);       g += v[0];       s -= v[0];       for (; j < n; j++) {         s += v[j];         b -= v[j];         if (s > g) {           break;         }       }       for (; k < n; k++) {         b += v[k];         if (b > g) {           break;         }       }       k++;       while (g + s + b <= n / 2 && k < v.size()) {         ans[0] = g;         ans[1] = s;         ans[2] = b;         b += v[k];         k++;       }       cout << ans[0] <<     << ans[1] <<     << ans[2] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const long long inf64 = 1e18;   const int N = 1e6 + 5;   int n, m;   vector<int> g[N], gr[N];   int ptr = 1;   int del[N];   int mark[N];   void solve() {     while (ptr <= n && del[ptr]) ptr++;     if (ptr > n) return;     int v = ptr++;     for (int to : g[v]) del[to] = 1;     solve();     int any = 0;     for (int to : gr[v]) {       if (mark[to]) {         any = 1;         break;       }     }     if (!any) {       mark[v] = 1;     }   }   int main() {     scanf( %d %d , &n, &m);     for (int u, v, i = 0; i < m; i++) {       scanf( %d %d , &u, &v);       g[u].push_back(v);       gr[v].push_back(u);     }     solve();     printf( %d n , count(mark + 1, mark + n + 1, 1));     for (int i = 1; i <= n; i++) {       if (mark[i]) {         printf( %d  , i);       }     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int pre[N][32], suf[N][32], mx[N][32];   int n, a[N];   long long ans;   void init() {     for (int i = 0; i < n; i++) mx[i][0] = i;     for (int i = 1; (1 << i) < N; i++) {       for (int j = 0; j < n; j++) {         mx[j][i] = (a[mx[j][i - 1]] > a[mx[j + (1 << (i - 1))][i - 1]]                         ? mx[j][i - 1]                         : mx[j + (1 << (i - 1))][i - 1]);       }     }   }   int query(int l, int r) {     int d = (int)(log((double)r - l + 1) / log(2.0));     return a[mx[l][d]] > a[mx[r - (1 << d) + 1][d]] ? mx[l][d]                                                     : mx[r - (1 << d) + 1][d];   }   void dfs(int l, int r) {     if (l > r) return;     int x = query(l, r);     int L = l, R = r;     int fl = 0, fr = 0;     for (int i = 0; i < 32; i++) {       if (!((a[x] >> i) & 1) && pre[x][i] != -1 && pre[x][i] >= l) {         fl = 1;         L = max(L, pre[x][i]);       }       if (!((a[x] >> i) & 1) && suf[x][i] != -1 && suf[x][i] <= r) {         fr = 1;         R = min(R, suf[x][i]);       }     }     L -= !fl;     R += !fr;     ans -= 1LL * (x - L) * (R - x);     dfs(l, x - 1);     dfs(x + 1, r);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       for (int j = 0; j < 32; j++) {         if (!((a[i] >> j) & 1)) {           if (i)             pre[i][j] = pre[i - 1][j];           else             pre[i][j] = -1;         } else           pre[i][j] = i;       }     }     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j < 32; j++) {         if (!((a[i] >> j) & 1)) {           if (i < n - 1)             suf[i][j] = suf[i + 1][j];           else             suf[i][j] = -1;         } else           suf[i][j] = i;       }     }     init();     ans = 1LL * n * (n + 1) / 2;     dfs(0, n - 1);     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int n, m, k, dp[2020][2020];   long long int getDp(long long int x, long long int y) {     if (x == n) return y == k;     if (~dp[x][y]) return dp[x][y];     long long int &ret = dp[x][y] = 0;     ret = getDp(x + 1, y);     ret = (ret + (getDp(x + 1, y + 1) * (m - 1)) % 998244353);     ret %= 998244353;     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     memset(dp, -1, sizeof(dp));     cout << ((getDp(1, 0) * m) % 998244353) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 1 << 19;   struct node {     long long left, right, cnt, tot, length;   };   struct segtree {     node t[MX * 2];     node merge(node& l, node& r) {       node ans;       ans.left = l.left + r.left + l.length * r.cnt;       ans.right = l.right + r.right + r.length * l.cnt;       ans.cnt = l.cnt + r.cnt;       ans.length = l.length + r.length;       ans.tot = l.tot + r.tot + l.right * r.cnt + r.left * l.cnt;       return ans;     }     void update(int wi, int su) {       wi += MX;       t[wi].cnt = su;       while (wi > 1) {         wi /= 2;         t[wi] = merge(t[wi * 2], t[wi * 2 + 1]);       }     }     long long read(int s, int e) {       s += MX, e += MX;       node l = {0}, r = {0};       while (s <= e) {         if (s & 1)           l = merge(l, t[s]), s = s / 2 + 1;         else           s /= 2;         if (e & 1)           e /= 2;         else           r = merge(t[e], r), e = e / 2 - 1;       }       return merge(l, r).tot;     }   } t;   struct query {     long long o, s, e;   } Q[MX];   map<long long, long long> point;   long long D[MX], tmp[MX];   int main() {     int N, T;     scanf( %d , &N);     for (int i = 1; i <= N; i++)       scanf( %lld , D + i), tmp[i] = D[i], point[D[i]] = 0;     scanf( %d , &T);     for (int i = 0; i < T; i++) scanf( %lld%lld%lld , &Q[i].o, &Q[i].s, &Q[i].e);     for (int i = 0; i < T; i++) {       if (Q[i].o == 2) continue;       tmp[Q[i].s] += Q[i].e;       point[tmp[Q[i].s]] = 0;     }     int wi = 0;     for (auto it = point.begin(), it2 = point.begin(); it != point.end();          ++it, wi++) {       it->second = wi * 2;       ++it2;       if (it2 != point.end())         t.t[MX + wi * 2 + 1].length = it2->first - it->first;     }     for (int i = MX - 1; i >= 1; i--) {       t.t[i].length = t.t[i * 2].length + t.t[i * 2 + 1].length;     }     for (int i = 1; i <= N; i++) {       t.update(point[D[i]], 1);     }     for (int i = 0; i < T; i++) {       if (Q[i].o == 1) {         t.update(point[D[Q[i].s]], 0);         D[Q[i].s] += Q[i].e;         t.update(point[D[Q[i].s]], 1);       } else {         auto l = point.lower_bound(Q[i].s), r = point.upper_bound(Q[i].e);         if (r == l)           printf( 0  n );         else           --r, printf( %lld n , t.read(l->second, r->second));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, mod = 998244353;   int n, p[N], c[N];   void add(int& x, int y) {     x += y;     if (x >= mod) {       x -= mod;     }   }   int mul(int x, int y) { return (long long)x * y % mod; }   int qpow(int v, int p) {     int result = 1;     for (; p; p >>= 1, v = mul(v, v)) {       if (p & 1) {         result = mul(result, v);       }     }     return result;   }   void modify(int p) {     for (; p <= n; p += p & -p) {       ++c[p];     }   }   int query(int p) {     int result = 0;     for (; p; p -= p & -p) {       result += c[p];     }     return result;   }   int query(int l, int r) { return query(r) - query(l - 1); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &p[i]);     }     int answer = 0, t = 0, emp = 0;     for (int i = 1; i <= n; ++i) {       if (!~p[i]) {         ++t;       }     }     emp = t;     add(answer, mul(mul(t, t - 1), qpow(4, mod - 2)));     for (int i = 1; i <= n; ++i) {       if (~p[i]) {         add(answer, query(p[i], n));         modify(p[i]);       }     }     t = 0;     for (int i = 1; i <= n; ++i) {       if (!~p[i]) {         ++t;       } else {         int pos = mul(n - p[i] - query(p[i] + 1, n), qpow(emp, mod - 2));         add(answer, mul(pos, t));       }     }     t = 0;     for (int i = n; i; --i) {       if (!~p[i]) {         ++t;       } else {         int pos = mul(p[i] - 1 - query(1, p[i] - 1), qpow(emp, mod - 2));         add(answer, mul(pos, t));       }     }     printf( %d n , answer);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   int n, q;   long long int m;   vector<long long int> mods;   long long int w[N];   long long int phi(long long int x) {     long long int ans = x;     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) {         ans -= ans / i;         while (x % i == 0) {           x /= i;         }       }     }     if (x > 1) {       ans -= ans / x;     }     return ans;   }   long long int magic(long long int x, long long int mod) {     if (x < 50) {       return x;     }     return 32 + (x - 32) % mod;   }   long long int fexp(long long int base, long long int e, long long int mod) {     long long int ans = 1;     while (e > 0) {       if (e & 1) {         ans = magic(ans * base, mod);       }       base = magic(base * base, mod);       e >>= 1;     }     return ans;   }   long long int go(int l, int r, int on) {     if (r == l) {       return magic(w[l], mods[on]);     }     if (mods[on] == 1) {       return magic(w[l], mods[on]);     }     long long int e = go(l + 1, r, on + 1);     return fexp(w[l], e, mods[on]);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     mods.push_back(m);     while (mods.back() != 1) {       mods.push_back(phi(mods.back()));     }     for (int i = 0; i < n; i++) {       cin >> w[i];     }     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       l--;       r--;       cout << go(l, r, 0) % mods[0] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s[1005];   long long int M[1005][1005], T[1005][1005];   queue<pair<long long int, long long int> > q;   long long int xr[] = {0, 0, -1, 1};   long long int xc[] = {-1, 1, 0, 0};   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     long long int p, t, n, m, i, j, k, x, y, z, a, b, c;     cin >> n >> m >> t;     for (i = 0; i < n; ++i) cin >> s[i];     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         a = 0;         for (k = 0; k < 4; ++k) {           x = i + xr[k];           y = j + xc[k];           if (x < 0 || x >= n || y < 0 || y >= m) continue;           a += (s[x][y] == s[i][j]);         }         if (a) {           q.push(make_pair(i, j));           M[i][j] = 1;         } else           T[i][j] = LLONG_MAX;       }     }     while (!q.empty()) {       auto it = q.front();       q.pop();       i = it.first;       j = it.second;       a = b = c = 0;       for (k = 0; k < 4; ++k) {         x = i + xr[k];         y = j + xc[k];         if (x < 0 || x >= n || y < 0 || y >= m || M[x][y]) continue;         if (s[x][y] != s[i][j]) {           q.push(make_pair(x, y));           M[x][y] = 1;           T[x][y] = T[i][j] + 1;         }       }     }     while (t--) {       cin >> i >> j >> p;       i -= 1;       j -= 1;       if (T[i][j] > p)         cout << s[i][j] << endl;       else         cout << (((p - T[i][j]) & 1) ^ (s[i][j] -  0 )) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k, x, y, a, b, d[2010][2010], v[2010][2010];   bool solve(int x, int y, int k) {     int l = max(1, x - k), r = min(x + k, n), t;     for (int i = l; i < r + 1; i++) {       t = k - abs(i - x);       if (y - t > 0 && !v[i][y - t]) return a = i, b = y - t, 1;       if (y + t <= m && !v[i][y + t]) return a = i, b = y + t, 1;     }     return 0;   }   int main() {     cin >> n >> m >> k;     for (int t = 0; t < k; t++) {       cin >> x >> y;       for (int i = -2; i < 3; i++)         for (int j = -2; j < 3; j++) {           if (x + i < 1 || x + i > n || y + j < 1 || y + j > m) continue;           d[x][y] = max(d[x][y], d[x + i][y + j] - abs(i) - abs(j));         }       while (!solve(x, y, d[x][y])) d[x][y]++;       cout << a <<     << b << endl;       v[a][b] = 1;     }   }
#include <bits/stdc++.h>   using namespace std;   vector<int> g[100100];   int a[100100], b[100100], s[100100];   int n, sa, sb;   double ans;   void dfs(int v, int p) {     s[v] = 1;     for (int i = 0; i < g[v].size(); i++) {       int u = g[v][i];       if (u == p) continue;       dfs(u, v);       s[v] += s[u];       a[v] += a[u];       ans += 1.0 * b[v] * s[u] * a[u];     }     ans += 1.0 * b[v] * (n - s[v]) * (sa - a[v]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= n; i++) scanf( %d%d , a + i, b + i);     for (int i = 1; i <= n; i++) {       sa += a[i];       sb += b[i];     }     dfs(1, 0);     printf( %.12lf n , ans / sa / sb);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e9 + 7;   const int P = 1e6 + 9;   long long n, m;   long long bin_pow(int a, int x) {     if (x == 1) return a % N;     if (x % 2 == 0) return (bin_pow(a, x / 2) * bin_pow(a, x / 2)) % N;     if (x % 2 != 0) return (bin_pow(a, x - 1) * (a % N)) % N;   }   int main() {     cin >> n >> m;     cout << bin_pow(bin_pow(2, m) - 1, n);   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   const int MAX = 1e3 + 10;   vector<int> g[MAX];   int qt[MAX];   int dfs(int i, int p = -1) {     int ret = 1;     for (int j : g[i])       if (j != p) ret += dfs(j, i);     return qt[i] = ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       x--;       for (int i = 0; i < n; i++) g[i].clear();       int deg = 0;       for (int i = 0; i < n - 1; i++) {         int a, b;         cin >> a >> b;         a--, b--;         if (a == x or b == x) deg++;         g[a].push_back(b);         g[b].push_back(a);       }       if (deg <= 1) {         cout <<  Ayush  <<   n ;         continue;       }       dfs(x);       vector<int> v;       for (int i : g[x]) v.push_back(qt[i]);       int par = 0;       for (int i : v) par += (i % 2 == 0);       if (par % 2 != deg % 2)         cout <<  Ayush  <<   n ;       else         cout <<  Ashish  <<   n ;     }     exit(0);   }
#include <bits/stdc++.h>   using namespace std;   int a[100000];   int m[100001];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     m[n] = 1e9 + 1;     for (int i = n - 1; i >= 0; i--) {       m[i] = min(m[i + 1], a[i]);     }     for (int i = 0; i < n; i++) {       cout << (lower_bound(m + i + 1, m + n, a[i]) - m - i - 2) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int x, s = 0, t = 0, m, ans = 0, hp = 0;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     if (a[0] == 1)       t++;     else       s++;     for (int i = 1; i < n; i++) {       if (a[i] != a[i - 1] && hp == 0) {         hp = 1;       }       if (a[i] != a[i - 1] && hp == 1) {         m = min(s, t);         if (ans < m) ans = m;         if (a[i] == 1)           t = 0;         else           s = 0;       }       if (a[i] == 1) {         t++;       } else if (a[i] == 2) {         s++;       }     }     m = min(s, t);     if (ans < m) ans = m;     cout << ans * 2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a[11][11] = {0}, n;     cin >> n;     for (int i = 0; i < n; i++) {       a[0][i] = 1;       a[i][0] = 1;     }     for (int i = 1; i < n; i++) {       for (int j = 1; j < n; j++) {         a[i][j] = a[i - 1][j] + a[i][j - 1];       }     }     cout << a[n - 1][n - 1] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void boost() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const long long maxn = 2e6 + 123;   const long long inf = 1e9 + 123;   const long long linf = 1e18 + 123;   const long long mod = 1e9 + 7;   const double eps = 1e-9;   const double pi = acos(-1);   int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1};   int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1};   long long a[maxn], b[maxn];   vector<pair<long long, long long> > v[maxn];   int main() {     boost();     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];       b[i] += b[i - 1];     }     bool WAS = 0;     if (a[n] > b[n]) {       swap(a, b);       WAS = 1;     }     for (int i = 1; i <= n; i++) {       int j = lower_bound(b + 1, b + 1 + n, a[i]) - b;       if (a[i] != b[j]) {         j--;       }       v[a[i] - b[j]].push_back({i, j});     }     if (v[0].size() >= 1) {       if (WAS == 0) {         cout << v[0][0].first <<   n ;         for (int i = 1; i <= v[0][0].first; i++) {           cout << i <<    ;         }         cout <<   n ;         cout << v[0][0].second <<   n ;         for (int i = 1; i <= v[0][0].second; i++) {           cout << i <<    ;         }         cout <<   n ;       } else {         cout << v[0][0].second <<   n ;         for (int i = 1; i <= v[0][0].second; i++) {           cout << i <<    ;         }         cout <<   n  << v[0][0].first <<   n ;         for (int i = 1; i <= v[0][0].first; i++) {           cout << i <<    ;         }         cout <<   n ;       }       return 0;     }     for (int i = 1; i < n; i++) {       if (v[i].size() >= 2) {         if (WAS == 0) {           cout << v[i][1].first - v[i][0].first <<   n ;           for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) {             cout << j <<    ;           }           cout <<   n  << v[i][1].second - v[i][0].second <<   n ;           for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) {             cout << j <<    ;           }           return 0;         } else {           cout << v[i][1].second - v[i][0].second <<   n ;           for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) {             cout << j <<    ;           }           cout <<   n  << v[i][1].first - v[i][0].first <<   n ;           for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) {             cout << j <<    ;           }           return 0;         }       }     }     exit(0);   }
#include <bits/stdc++.h>   using namespace std;   void mejor(int player, long long int k, int *arr, int n) {     if (player == 0) {       if (player + k >= n - 1)         cout << n << endl;       else {         for (int i = 0; i < k; i++) {           if (arr[player] < arr[player + 1 + i]) {             mejor(player + 1 + i, k, arr, n);             return;           }         }         cout << arr[player] << endl;       }     } else {       if (player + k > n - 1)         cout << n << endl;       else {         for (int i = 0; i < k - 1; i++) {           if (arr[player] < arr[player + 1 + i]) {             mejor(player + 1 + i, k, arr, n);             return;           }         }         cout << arr[player] << endl;       }     }     return;   }   int main() {     int n;     long long int k;     cin >> n >> k;     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     if (k >= n - 1)       cout << n << endl;     else       mejor(0, k, arr, n);     return 0;   }
#include <bits/stdc++.h>   int main() {     int len, i, l = 0, r = 0, u = 0, d = 0, dif1, dif2;     scanf( %d , &len);     char str[101];     scanf(  %s , str);     for (i = 0; i < len; i++) {       if (str[i] ==  L )         l++;       else if (str[i] ==  R )         r++;       else if (str[i] ==  U )         u++;       else if (str[i] ==  D )         d++;     }     dif1 = l - r;     if (dif1 < 0) dif1 = -dif1;     dif2 = u - d;     if (dif2 < 0) dif2 = -dif2;     printf( %d n , (len - (dif1 + dif2)));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const int MOD = (int)1e9 + 7;   const int N = (int)1e6 + 7;   const int M = (int)-1;   int a[N], cnt[100];   int main() {     int T;     cin >> T;     while (T--) {       long long n;       int m;       cin >> n >> m;       for (int i = (0); i < (m); ++i) cin >> a[i];       long long sum = 0;       for (int i = (0); i < (m); ++i) sum += a[i];       if (sum < n) {         cout << -1 << endl;         continue;       }       sort(a, a + m);       memset(cnt, 0, sizeof(cnt));       for (int i = 0, j = 0; i < m; i++) {         while (a[i] > (1ll << j)) j++;         cnt[j]++;       }       long long res = 0;       int ans = 0;       for (int i = (0); i < (63); ++i) {         if (n >> i & 1) {           if (!cnt[i] && res < (1ll << i)) {             int j = i + 1;             cnt[i] = 2;             while (!cnt[j]) {               cnt[j]++;               j++;               ans++;             }             cnt[j]--;             ans++;           }           if (cnt[i])             cnt[i]--;           else             res -= 1ll << i;         }         res += (1ll << i) * cnt[i];       }       cout << ans << endl;     }     return 0;   }