fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define gc getchar_unlocked
  4. #define fo(i,n) for(i=0;i<n;i++)
  5. #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  6. #define ll long long
  7. #define si(x) scanf("%d",&x)
  8. #define sl(x) scanf("%lld",&x)
  9. #define ss(s) scanf("%s",s)
  10. #define pi(x) printf("%d\n",x)
  11. #define pl(x) printf("%lld\n",x)
  12. #define ps(s) printf("%s\n",s)
  13. #define pb push_back
  14. #define mp make_pair
  15. #define F first
  16. #define S second
  17. #define all(x) x.begin(), x.end()
  18. #define clr(x) memset(x, 0, sizeof(x))
  19. #define sortall(x) sort(all(x))
  20. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  21. #define PI 3.1415926535897932384626
  22. typedef pair<int, int> pii;
  23. typedef pair<ll, ll> pl;
  24. typedef vector<int> vi;
  25. typedef vector<ll> vl;
  26. typedef vector<pii> vpii;
  27. typedef vector<pl> vpl;
  28. typedef vector<vi> vvi;
  29. typedef vector<vl> vvl;
  30. int mpow(int base, int exp);
  31. void ipgraph(int m);
  32. void dfs(int u, int par);
  33. const int mod = 1000000007;
  34. const int N = 3e5, M = N;
  35. //=======================
  36.  
  37. vi g[N];
  38. int a[N];
  39. void fac(ll no, vi &a){
  40. a.clear();
  41. while(no){
  42. a.pb(no%2);
  43. no /= 2;
  44. }
  45. while(a.size() < 60) a.pb(0);
  46. }
  47. int main()
  48. {
  49. ios_base::sync_with_stdio(false);
  50. cin.tie(NULL);
  51. int i,n,j;
  52. ll A, B;
  53. cin >> A >> B;
  54. if(A == B){
  55. cout << 1 << endl;
  56. return 0;
  57. }
  58. int r = -1, k = -1, cur = -1;
  59. vi L, R;
  60. fac(A, L);
  61. fac(B, R);
  62. int p = 59;
  63. while(p>=0 and L[p] == R[p]){
  64. if(L[p]) L[p] = R[p] = 0, A -= 1LL<<p, B -= 1LL<<p;
  65. p--;
  66. }
  67. fo(i, 60){
  68. if(L[i] != R[i]) r = i;
  69. if(R[i]) k = cur, cur = i;
  70. }
  71. ll T = 1LL<<r;
  72. //[A, T-1]
  73. ll ans = (T-1) - A + 1;
  74.  
  75. //[T, T+2^(k+1)-1]
  76. ans += 1LL<<(k+1);
  77. //[T+A, 2T-1]
  78. ans += 2*T-1 - (T+A) + 1;
  79. //If overlap
  80. ll x = (1LL<<(k+1)) - 1;
  81. if(A <= x)
  82. ans -= x-A+1;
  83. cout << ans << endl;
  84.  
  85. return 0;
  86. }
  87.  
  88.  
Success #stdin #stdout 0s 23432KB
stdin
65 98
stdout
63