Advertisement
aropan

B. Lottery

Mar 25th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. long long L, R, X, K;
  6.  
  7. int main()
  8. {
  9.     cin >> L >> R >> X >> K;
  10.     long long ans = 0;
  11.     for (long long i = 1LL << 62; i; i >>= 1)
  12.     {
  13.         if ((L & i) == (R & i)) continue;
  14.         long long c = (R & ((1LL << 62) - i));
  15.         if ((L & i) == (X & i))
  16.             if ((c - L) >= K)
  17.             {  
  18.                 R = c - 1;
  19.             }
  20.             else
  21.             {
  22.                 K -= (c - L);
  23.                 L = c;
  24.             }
  25.         else
  26.             if ((R - c + 1) >= K)
  27.             {  
  28.                 L = c;
  29.             }
  30.             else
  31.             {
  32.                 K -= (R - c + 1);
  33.                 R = c - 1;
  34.             }
  35.     }
  36.     cout << L << endl;
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement