fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define all(x) x.begin(), x.end()
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace __gnu_pbds;
  9. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  11. typedef long long ll;
  12.  
  13. #define inf 1e9
  14. #define MOD 1000000007
  15. #define vint vector<int>
  16. #define vll vector<ll>
  17. #define no cout << "NO" << endl;
  18. ll logg(ll x ) {
  19. ll ans = 0 ;
  20. while ( x ) {
  21. ans++ ;
  22. x/=2 ;
  23. }
  24. return x ;
  25. }
  26.  
  27.  
  28. void solve() {
  29.  
  30. ll n , m ; cin >> n >> m ;
  31. ll ans = 0 , sofar = 0 , plus = 0 , cumulative ;
  32. set <ll > st ;
  33. for (int bit = 0 ; bit <=63 ; bit ++ ) {
  34. ll x = (1 << bit) ;
  35. if ((x&n))st.insert(x) ;
  36. }
  37. vector < bool > v(64 , 0 ) ;
  38. ll sofar2 = 0;
  39. for (ll bit = 0 ; bit <=63 ; bit ++ ) {
  40. ll x = (1ll<<bit );
  41. if ((n&x) ) {
  42. //cout << x <<" "<< n <<" hhh" << endl;
  43. //cout << bit <<"bittt" << endl ;
  44. if (sofar2+1<=m) {
  45. for (int j = 0; j< bit ; j++) v[j]=1 ;
  46.  
  47. }
  48. sofar2+= x ;
  49. }
  50.  
  51. }
  52. ///for (auto e : v )cout << e <<" ";
  53. //cout << endl ;
  54. for (ll bit = 0 ; bit <= 63 ; bit ++ ) {
  55. ll x = (1ll << bit );
  56.  
  57. if ( (n&x) ) {
  58. sofar+= x ;
  59. continue ;
  60. }
  61. if (v[bit]) {
  62. plus+= x;
  63. continue ;
  64. }
  65. ll distance = x - sofar ;
  66.  
  67. if (distance > m ) break ;
  68. plus += x;
  69. }
  70. cout << n + plus << endl;
  71.  
  72. }
  73.  
  74. int main() {
  75. ios::sync_with_stdio(false);
  76. cin.tie(nullptr);
  77.  
  78. #ifndef ONLINE_JUDGE
  79. freopen("input.txt", "r", stdin);
  80. freopen("output.txt", "w", stdout);
  81. #endif
  82.  
  83. int t = 1;
  84. cin >> t;
  85. while (t--) {
  86. solve();
  87. }
  88.  
  89. return 0;
  90. }
  91.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
281474976710655