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

运行 ID: 43247

#include<bits/stdc++.h> using namespace std; #define int long long int t,n,x; void jc(int x){ if(x==0&&n==1){ cout<<-1<<endl; return; } if(x==0&&n%2){ cout<<n-2+5<<endl; return; } int ans=0,tx=x; while(tx){ if(tx&1)ans++; tx>>=1; } if(ans>n){ cout<<x<<endl; return; } if(ans==1){ if(n%2){ cout<<x+n-1<<endl; return; } if(x==1)cout<<3+n<<endl; else cout<<x+n<<endl; return; } else{ if((n-ans)%2){cout<<x+n-ans+1<<endl;} else cout<<x+n-ans<<endl; return; } cout<<-1<<endl; } signed main(){ cin>>t; while(t--){ cin>>n>>x; jc(x); } return 0; } /* 思路: 1:分奇偶 2:二进制分解 */