提交时间:2026-10-28 13:57:38

运行 ID: 43248

#include<bits/stdc++.h> #define ll long long using namespace std; int T,n,x; ll ans; void sol(){ ll res=1; if(x==1&&n%2==0){cout<<n+3<<'\n';return;} if(n==1&&x==0){cout<<-1<<'\n';return;} if(n<=__builtin_popcount(x)){cout<<x<<'\n';return;} while(x){ if(x%2==1)ans+=res,n--; res*=2; x>>=1; } if(n%2==1)ans=ans+n+1; else ans+=n; cout<<ans<<'\n'; } signed main(){ cin>>T; while(T--){ cin>>n>>x,ans=0; sol(); } return 0; }