fork(7) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+7;
  7.  
  8. void solve() {
  9. int n,x;
  10. cin >> n >> x;
  11. if(x==0){
  12. if(n==1) cout << -1 << '\n';
  13. else if(n%2==0) cout << n << '\n';
  14. else cout << n+3 << '\n';
  15. }
  16. else if(x==1){
  17. if(n%2==1) cout << n << '\n';
  18. else cout << n+3 << '\n';
  19. }
  20. else{
  21. int k = x;
  22. int cnt = 0;
  23. while(k>0){
  24. if(k&1) cnt++;
  25. k/=2;
  26. }
  27. if(n<=cnt) cout << x << '\n';
  28. else{
  29. int p = n-cnt;
  30. if(p%2==0) cout << x+p << '\n';
  31. else cout << x+p+1 << '\n';
  32. }
  33. }
  34. }
  35. int main(){
  36. ios::sync_with_stdio(false);
  37. cin.tie(nullptr);
  38.  
  39. int t;
  40. cin >> t;
  41. while (t--) solve();
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5320KB
stdin
8
2 1
3 6
1 0
2 0
5 0
2 27
15 43
12345678 9101112
stdout
5
8
-1
2
8
27
55
21446778