提交时间:2026-10-28 14:27:25
运行 ID: 43281
#include <bits/stdc++.h> using namespace std; #define int long long int t,n,x,y,k,cnt,ans; signed main(){ cin >> t; while (t--){ cin >> n >> x; y = x; if (n==1&&x==0){ cout << -1 << endl; continue; } if (x==1&&n%2==0){ cout << n+3 << endl; continue; } k=1,cnt=0,ans=0; while (x>0){ if (x%2==1){ ans += k; cnt ++; } x /= 2; k*=2; } if (cnt < n){ ans+=n-cnt; if ((n-cnt)%2==1){ ans ++; if (y<=1) ans+=2; } } cout << ans << endl; } return 0; }