Guest User

Google Code Jam Round 2 2013 Problem B in O(1)

a guest
Jun 1st, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stdio.h>
  3.  
  4. static inline long long min(long long a, long long b)
  5. { return a < b ? a : b; }
  6.  
  7. static inline int floor_log2(long long x)
  8. { return !x ? -1 : (int) sizeof x * CHAR_BIT - 1 - __builtin_clzll(x); }
  9.  
  10. int main(void)
  11. {
  12.     int T;
  13.     scanf("%d", &T);
  14.    
  15.     for (int iT = 1; iT <= T; ++iT)
  16.     {
  17.         int N;
  18.         long long P;
  19.         scanf("%d%lld", &N, &P);
  20.        
  21.         long long may = (1LL << N) - (1LL << N - floor_log2(P));
  22.         long long must = min((1LL << N - floor_log2((1LL << N) - P)) - 2, (1LL << N) - 1);
  23.         printf("Case #%d: %lld %lld\n", iT, must, may);
  24.     }
  25. }
Add Comment
Please, Sign In to add comment