malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, i;   int fi = 0, se = 0, th = 0, fo = 0;   int main() {     cin >> n;     cout <<  +------------------------+  << endl;     if (n >= 3) {       n--;       th++;     }     while (n) {       if (n) {         fi++;         n--;       }       if (n) {         se++;         n--;       }       if (n) {         fo++;         n--;       }     }     cout <<  | ;     for (i = 11; i; i--) {       if (fi) {         cout <<  O. ;         fi--;       } else {         cout <<  #. ;       }     }     cout <<  |D|)  << endl;     cout <<  | ;     for (i = 11; i; i--) {       if (se) {         cout <<  O. ;         se--;       } else {         cout <<  #. ;       }     }     cout <<  |.|  << endl;     cout <<  | ;     if (th) {       cout <<  O ;     } else {       cout <<  # ;     }     cout <<  .......................|  << endl;     cout <<  | ;     for (i = 11; i; i--) {       if (fo) {         cout <<  O. ;         fo--;       } else {         cout <<  #. ;       }     }     cout <<  |.|)  << endl;     cout <<  +------------------------+  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, temp, count = 0, found = 0;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> temp;       if (temp == i)         ++count;       else if (temp < i && a[temp] == i)         found = 2;       else if (found != 2)         found = 1;       a[i] = temp;     }     cout << count + found << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct sheet {     long long xt, yt, xb, yb;   };   sheet a[3];   long long flag = 0;   void hor() {     long long yc = a[0].yb;     for (double x = a[0].xb; x <= a[0].xt; x += .5) {       if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) {         continue;       } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }     yc = a[0].yt;     for (double x = a[0].xb; x <= a[0].xt; x += .5) {       if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) {         continue;       } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }   }   void ver() {     long long xc = a[0].xb;     for (double y = a[0].yb; y <= a[0].yt; y += .5) {       if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) {         continue;       } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }     xc = a[0].xt;     for (double y = a[0].yb; y <= a[0].yt; y++) {       if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) {         continue;       } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }   }   int main() {     for (int i = 0; i < 3; i++) {       cin >> a[i].xb >> a[i].yb >> a[i].xt >> a[i].yt;     }     hor();     ver();     cout << (flag ?  YES  :  NO );   }
#include <bits/stdc++.h>   using namespace std;   void ret(string s) {     cout << s;     exit(0);   }   int main() {     int x1, x2, y1, y2, n;     cin >> n >> x1 >> y1 >> x2 >> y2;     if (x1 * x2 == 0 && x1 + x2 == n)       cout << min(n + y1 + y2, 3 * n - y1 - y2);     else if (y1 * y2 == 0 && y1 + y2 == n)       cout << min(n + x1 + x2, 3 * n - x1 - x2);     else       cout << abs(x1 - x2) + abs(y1 - y2);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 34;   const int M = 70;   int n;   int arr[N];   int dp[N][M * M][M];   int find(int pos, int sum, int cnt) {     if (pos > n) {       return (sum * 2 == cnt * (cnt - 1)) ? cnt : M;     }     if (dp[pos][sum][cnt] != -1) {       return dp[pos][sum][cnt];     }     int res = M;     int cur = sum;     int tot = cnt;     for (int i = 1; tot < 65; ++i) {       ++tot;       cur += arr[pos];       if (cur * 2 >= tot * (tot - 1)) {         res = min(res, find(pos + 1, cur, tot));       }     }     return dp[pos][sum][cnt] = res;   }   int deg[M];   void go(int pos, int sum, int cnt) {     if (pos > n) {       return;     }     int cur = sum;     int tot = cnt;     for (int i = 1; tot < 65; ++i) {       ++tot;       cur += arr[pos];       deg[tot] = arr[pos];       if (cur * 2 >= tot * (tot - 1)) {         if (find(pos + 1, cur, tot) == find(pos, sum, cnt)) {           go(pos + 1, cur, tot);           return;         }       }     }   }   int graph[M][M];   pair<int, int> out[M];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> arr[i];     }     sort(arr + 1, arr + 1 + n);     memset(dp, -1, sizeof(dp));     int m = find(1, 0, 0);     go(1, 0, 0);     for (int i = 1; i <= m; ++i) {       out[i] = make_pair(deg[i], i);     }     sort(out + 1, out + 1 + m);     for (int i = 1; i <= m; ++i) {       for (int j = i + 1; j <= i + out[i].first; ++j) {         graph[out[i].second][out[j].second] = 1;       }       for (int j = i + out[i].first + 1; j <= m; ++j) {         graph[out[j].second][out[i].second] = 1;         --out[j].first;       }       out[i].first = 0;       sort(out + i + 1, out + 1 + m);     }     printf( %d n , m);     for (int i = 1; i <= m; ++i) {       for (int j = 1; j <= m; ++j) {         printf( %d , graph[i][j]);       }       printf(  n );     }   }
#include <bits/stdc++.h>   using namespace std;   int tmp, n, tr[100005 * 30][2], cnt;   inline void insert(int x) {     int p = 0, d;     for (int i = 29; i >= 0; --i) {       d = (x >> i) & 1;       if (tr[p][d])         p = tr[p][d];       else {         tr[p][d] = ++cnt;         p = cnt;         memset(tr[p], 0, sizeof(tr[p]));       }     }   }   int dfs(int p, int d) {     if (!tr[p][0] && !tr[p][1]) return 0;     if (!tr[p][0]) return dfs(tr[p][1], d - 1);     if (!tr[p][1]) return dfs(tr[p][0], d - 1);     return (1 << d) + min(dfs(tr[p][0], d - 1), dfs(tr[p][1], d - 1));   }   int main() {     while (~scanf( %d , &n)) {       cnt = 0;       memset(tr[0], 0, sizeof(tr[0]));       for (int i = 1; i <= n; ++i) {         scanf( %d , &tmp);         insert(tmp);       }       int ans = dfs(0, 29);       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     vector<long long> arr(4);     for (int i = 0; i < 4; i++) cin >> arr[i];     int counter = 0;     for (int i = 0; i < 4; i++) counter += arr[i] % 2;     if (counter == 0 || counter == 1 || counter == 4) {       cout <<  Yes n ;       return;     } else if (counter == 3) {       if (arr[0] != 0 && arr[1] != 0 && arr[2] != 0)         cout <<  Yes n ;       else         cout <<  No n ;     } else       cout <<  No n ;     return;   }   int main() {     int t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   int n, m, t;   pair<int, int> a[300005];   int b[300005];   int force(int ll, int rr) {     int r = 0;     for (int i = 1; i <= n; i++)       if (a[i].first >= ll && a[i].second <= rr) r++;     return r;   }   struct indexed_tree_2d {     vector<int> T[1000006];     void set(int first, int second) {       for (int i = first; i < 1000006; i += i & -i) T[i].push_back(second);     }     void end_update() {       for (int i = 1; i < 1000006; i++) sort(T[i].begin(), T[i].end());     }     int get(int first, int second) {       int r = 0;       for (int i = first; i >= 1; i -= i & -i)         r += upper_bound(T[i].begin(), T[i].end(), second) - T[i].begin();       return r;     }     int get(int x1, int y1, int x2, int y2) {       return get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) +              get(x1 - 1, y1 - 1);     }   } tr;   int f(int ll, int rr) {     if (ll > rr) return 0;     return tr.get(ll, ll, rr, rr);   }   int main() {     scanf( %d%d , &n, &t);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i].first, &a[i].second);     }     for (int i = 1; i <= n; i++) tr.set(a[i].first, a[i].second);     tr.end_update();     for (int i = 1; i <= t; i++) {       int answer = n;       scanf( %d , &m);       for (int j = 1; j <= m; j++) {         scanf( %d , &b[j]);       }       sort(b + 1, b + m + 1);       b[m + 1] = 1000006;       for (int j = 1; j <= m + 1; j++) answer -= f(b[j - 1] + 1, b[j] - 1);       printf( %d n , answer);     }   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, pair<int, int> > > v[100005];   vector<int> ans;   int n;   bool has_cycle(int mid) {     int indeg[100005];     int cnt = 0, x;     memset(indeg, 0, sizeof(indeg));     for (int i = 1; i <= n; ++i) {       for (auto g : v[i]) {         if (g.second.first > mid) {           indeg[g.first]++;         }       }     }     queue<int> q;     for (int i = 1; i <= n; ++i) {       if (indeg[i] == 0) {         q.push(i);       }     }     while (!q.empty()) {       x = q.front();       cnt++;       q.pop();       for (auto g : v[x]) {         if (g.second.first > mid) {           indeg[g.first]--;           if (indeg[g.first] == 0) q.push(g.first);         }       }     }     if (cnt == n)       return false;     else       return true;   }   void topological_sort(int res) {     int indeg[100005];     int cnt = 0, x;     memset(indeg, 0, sizeof(indeg));     for (int i = 1; i <= n; ++i) {       for (auto g : v[i]) {         if (g.second.first > res) {           indeg[g.first]++;         }       }     }     queue<int> q;     bool vis[100005];     memset(vis, false, sizeof(vis));     for (int i = 1; i <= n; ++i) {       if (indeg[i] == 0) q.push(i);     }     while (!q.empty()) {       x = q.front();       vis[x] = true;       q.pop();       for (auto g : v[x]) {         if (g.second.first > res) {           indeg[g.first]--;           if (indeg[g.first] == 0) q.push(g.first);         } else {           if (vis[g.first] == true) {             ans.push_back(g.second.second);           }         }       }     }   }   bool check(int mid) {     if (has_cycle(mid))       return false;     else       return true;   }   int binary_search() {     int lo = 0, hi = 1e9;     while (hi - lo > 1) {       int mid = (lo + hi) / 2;       if (check(mid))         hi = mid;       else         lo = mid;     }     if (check(lo)) return lo;     return hi;   }   int main() {     int m, x, y, c, res;     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> x >> y >> c;       v[x].push_back({y, {c, i}});     }     res = binary_search();     topological_sort(res);     cout << res <<     << (int)ans.size() <<   n ;     for (auto g : ans) cout << g <<    ;   }
#include <bits/stdc++.h>   int func(int num) {     if (num > 0)       return 1;     else if (num < 0)       return -1;     else       return num;   }   int main() {     char a[101], b[101];     int i;     scanf( %s , &a);     scanf( %s , &b);     for (i = 0; i < strlen(a); i++)       if (a[i] >=  A  && a[i] <=  Z ) a[i] +=  a  -  A ;     for (i = 0; i < strlen(b); i++)       if (b[i] >=  A  && b[i] <=  Z ) b[i] +=  a  -  A ;     printf( %d n , func(strcmp(a, b)));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int DEBUG = 0;   int main(int argc, char **argv) {     DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;     int t;     cin >> t;     for (int c = 0; c < t; c++) {       char tmp;       int board[8][8];       int kx[2];       int ky[2];       int eger = 0;       for (int i = 0; i < 8; i++) {         for (int j = 0; j < 8; j++) {           cin >> tmp;           if (tmp ==  . ) {             board[i][j] = 1;           } else if (tmp ==  # ) {             board[i][j] = 0;           } else {             kx[eger] = i;             ky[eger] = j;             board[i][j] = 1;             eger++;           }         }       }       bool good = false;       if ((kx[0] - kx[1] + 12) % 4 == 0 && (ky[0] - ky[1] + 12) % 4 == 0) {         int v = (kx[0] + ky[0]) % 4;         for (int i = 0; i < 8; i++) {           for (int j = 0; j < 8; j++) {             if (board[i][j] == 1 && (i + j) % 4 == v) {               good = true;             }           }         }       }       if (good) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int64_t mod = 1000000007;   int main() {     int n;     cin >> n;     int64_t ways = 1;     for (int i = 0; i < 3 * n; ++i) {       ways *= 3;       ways %= mod;     }     int64_t bad = 1;     for (int i = 0; i < n; ++i) {       bad *= 7;       bad %= mod;     }     cout << (mod + (ways - bad) % mod) % mod << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long A[100005 * 10];   long long B[100005 * 10];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long tst = 1;     for (long long tt = 1; tt <= tst; tt++) {       long long n, a, mn = 1e12, m, res = 0;       cin >> m;       for (long long i = 1; i <= m; i++) cin >> a, mn = min(mn, a);       cin >> n;       for (long long i = 0; i < n; i++) cin >> A[i];       ;       sort(A, A + n, greater<long long>());       long long p1 = 0;       while (p1 < n) {         long long x = p1 + mn;         while (p1 < x) res += A[p1], p1++;         p1 += 2;       }       cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N;   pair<pair<int, int>, int> V[100005];   vector<int> r;   bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {     if (a.first.first != b.first.first) return a.first.first > b.first.first;     return a.first.second > b.first.second;   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin >> N;     for (int i = 1; i <= N; i++) cin >> V[i].first.first;     for (int i = 1; i <= N; i++) {       cin >> V[i].first.second;       V[i].second = i;     }     sort(V + 1, V + 1 + N, cmp);     r.push_back(V[1].second);     for (int i = 2; i <= N; i += 2) {       if (V[i].first.second > V[i + 1].first.second)         r.push_back(V[i].second);       else         r.push_back(V[i + 1].second);     }     cout << r.size() <<   n ;     for (auto it : r) cout << it <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using LL = long long;   constexpr int maxn = 100;   int a[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int t;     for (cin >> t; t; t -= 1) {       int n, x, am = 0, ok = 0;       cin >> n >> x;       for (int a; n; n -= 1) {         cin >> a;         am = max(a, am);         if (a == x) ok = 1;       }       if (ok)         cout <<  1 n ;       else         cout << max(2, (x + am - 1) / am) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int caseno = 1;   typedef long long LL;   typedef unsigned long long ULL;   typedef long long int64;   typedef unsigned long long uint64;   const int SIZE = 100 + 10;   int ar[SIZE];   int main() {     std::ios_base::sync_with_stdio(0);     int tcases, I, J, K, N, n, m, cnt = 0, len, f = 0, x, D, t;     cin >> n;     for (I = 0; I < n; I++) {       cin >> ar[I];       f += ar[I];     }     n++;     D = 1;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 2;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 3;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 4;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 5;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     cout << cnt <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const long long LLINF = 9223372036854775807LL;   int digsum(long long x) {     int ans = 0;     do {       ans += x % 10;       x /= 10;     } while (x);     return ans;   }   int main() {     int a, b, c;     cin >> a >> b >> c;     vector<int> v;     for (int sum = 1; sum <= 100; ++sum) {       long long curres = b;       for (int iter = 0; iter < a; ++iter) curres = curres * sum;       curres += c;       if (digsum(curres) == sum && curres > 0 && curres < 1000000000)         v.push_back(curres);     }     sort(v.begin(), v.end());     printf( %d n , int((v).size()));     for (int i = 0; i < int((v).size()); ++i) printf( %d  , v[i]);     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:671088640 )   using namespace std;   const int MOD = 1000000007;   const int INF = 20000000;   const long double EPS = 1e-7;   const int HASH_POW = 29;   const long double PI = acos(-1.0);   mt19937_64 rnd(1);   double workTime() { return double(clock()) / CLOCKS_PER_SEC; }   void my_return(int code) { exit(code); }   int bin_pow(int a, int n) {     int ans = 1;     while (n) {       if (n & 1) ans = ans * 1ll * a % MOD;       n >>= 1;       a = a * 1ll * a % MOD;     }     return ans;   }   int f[100010], rev[100010];   int C(int n, int k) {     if (k < 0 || k > n) return 0;     int res = f[n] * 1ll * rev[k] % MOD;     return res * 1ll * rev[n - k] % MOD;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     f[0] = 1;     for (int i = 1; i <= 100000; ++i) f[i] = f[i - 1] * 1ll * i % MOD;     for (int i = 0; i <= 100000; ++i) rev[i] = bin_pow(f[i], MOD - 2);     int f, w, h;     scanf( %d %d %d , &f, &w, &h);     if (w == 0) {       printf( 1 n );       my_return(0);     } else if (f == 0) {       if (w > h) {         printf( 1 n );         my_return(0);       } else {         printf( 0 n );         my_return(0);       }     } else if (w <= h) {       printf( 0 n );       my_return(0);     }     int total = 0, good = 0;     for (int i = 1; i <= w; ++i) {       if (i > 1) total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD;       total = (total + 2ll * C(w - 1, i - 1) * C(f - 1, i - 1)) % MOD;       total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i)) % MOD;     }     for (int i = 1; i <= w; ++i) {       if (i * (h + 1) > w) break;       int w1 = w - i * h;       if (i > 1) good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD;       good = (good + 2ll * C(w1 - 1, i - 1) * C(f - 1, i - 1)) % MOD;       good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i)) % MOD;     }     printf( %d n , good * 1ll * bin_pow(total, MOD - 2) % MOD);     my_return(0);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int tst;     cin >> tst;     while (tst--) {       int i, j, k, l, m, n;       cin >> n;       string s;       cin >> s;       set<int> ze, on;       for (i = 0; i < n; i++) {         if (s[i] ==  1 )           on.insert(i);         else           ze.insert(i);       }       int ans[n + 10];       memset(ans, 0, sizeof ans);       int st = 1;       j = 0;       int ya = 0, kel, pt;       st = 0;       for (i = 0; i < n; i++) {         if (ans[i]) continue;         if (on.empty() || ze.empty()) break;         st++;         if (s[i] ==  0 ) {           kel = i;           pt = 0;           while (1) {             if (pt == 0) {               auto x = on.lower_bound(kel);               ans[kel] = st;               ze.erase(kel);               if (x == on.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 on.erase(l);                 pt = 1 - pt;                 kel = l;               }             } else {               auto x = ze.lower_bound(kel);               ans[kel] = st;               on.erase(kel);               if (x == ze.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 ze.erase(l);                 pt = 1 - pt;                 kel = l;               }             }           }         } else {           pt = 0;           kel = i;           while (1) {             if (pt == 0) {               auto x = ze.lower_bound(kel);               ans[kel] = st;               on.erase(kel);               if (x == ze.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 ze.erase(l);                 pt = 1 - pt;                 kel = l;               }             } else {               auto x = on.lower_bound(kel);               ans[kel] = st;               ze.erase(kel);               if (x == on.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 on.erase(l);                 pt = 1 - pt;                 kel = l;               }             }           }         }       }       st++;       if (on.size()) {         for (auto x : on) ans[x] = st, st++;       } else if (ze.size()) {         for (auto x : ze) ans[x] = st, st++;       }       set<int> kar;       for (i = 0; i < n; i++) kar.insert(ans[i]);       cout << kar.size() <<   n ;       for (i = 0; i < n; i++) cout << ans[i] <<    ;       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int a[60000], x, mn = 0x3f3f3f3f;   int main() {     int n, m;     cin >> m >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       a[x]++;     }     sort(a + 1, a + m + 1);     cout << a[1];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e4 + 10;   const int inf = 1e9;   int t;   int n;   int b[N], ans[N];   void solve() {     cin >> n;     for (int i = 0; i < n; i++) cin >> b[i];     long long tot = 0;     for (int i = 0; i < n; i++) tot += b[i];     if (tot % (n * (n + 1) / 2) != 0) {       cout <<  NO n ;       return;     }     tot /= n * (n + 1) / 2;     for (int i = 0; i < n; i++) {       long long tmp = b[(i - 1 + n) % n] - b[i] + tot;       if (tmp <= 0 || tmp % n != 0 || tmp / n > inf) {         cout <<  NO n ;         return;       }       ans[i] = tmp / n;     }     cout <<  YES n ;     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long Max = 1e5 + 10;   int T;   long long n, m;   long long number[Max];   int main() {     cin >> T;     while (T--) {       cin >> n >> m;       long long ans = 1;       int k = 1;       for (int i = 1;; i *= 2) {         number[k] = number[k - 1] + i;         if (number[k] > n) {           ans = (ans * (n - number[k - 1] + 1) % m) % m;           break;         } else {           ans = ans * (i + 1) % m;         }         k++;       }       cout << (ans - 1 + m) % m << endl;     }     return 0;   }