Advertisement
shamiul93

1032 - Fast Bit Calculations

Jun 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. /**
  2. @author - Rumman BUET CSE'15
  3. Problem -1032 - Fast Bit Calculations
  4.  
  5. Concept - Digit DP
  6. Idea - I had solved a problem "How many Zeroes". That problem taught me the basic of Digit DP. It is easier than that one.
  7.  
  8. **/
  9.  
  10.  
  11. // LightOJ always needs this format for sure..so I made a copy of it...
  12. #include <bits/stdc++.h>
  13. #include<vector>
  14. #define ll                                      long long int
  15. #define fi                                      freopen("in.txt", "r", stdin)
  16. #define fo                                      freopen("out.txt", "w", stdout)
  17. #define m0(a) memset(a , 0 , sizeof(a))
  18. #define m1(a) memset(a , -1 , sizeof(a))
  19. #define inf LLONG_MAX
  20. #define min3(a,b,c) min(a,min(b,c))
  21. #define max3(a,b,c) max(a,max(b,c))
  22. #define ones(mask)  __builtin_popcount(mask) /// __builtin_popcount : it's a built in function of GCC. Finds out the numbers of 1 in binary representation.
  23. #define mx 150000
  24.  
  25. using namespace std;
  26.  
  27. vector<ll>  v;
  28. ll len ;
  29.  
  30. ll dp[40][2][2][70] ;
  31.  
  32. ll solve(ll pos , ll prevSmall , ll PrevBit , ll adjacent) /// prevBit means the prevous 1 or 0..
  33. {
  34.     if(pos >= len)
  35.     {
  36.         return  adjacent ;
  37.     }
  38.     if(dp[pos][prevSmall][PrevBit][adjacent] != -1)
  39.     {
  40.         return dp[pos][prevSmall][PrevBit][adjacent] ;
  41.     }
  42.  
  43.     ll bestPossible ;
  44.  
  45.     bestPossible = (prevSmall) ? 1:v[pos] ;
  46.  
  47.     ll ret = 0 ;
  48.  
  49.     for(ll i = 0 ; i <= bestPossible ; i++)
  50.     {
  51.         ret += solve(pos+1 , prevSmall | (i < v[pos]) , i , (i == PrevBit && i == 1) + adjacent) ;
  52.     }
  53.  
  54.     return dp[pos][prevSmall][PrevBit][adjacent] = ret ;
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60.     ll T , t = 0 ;
  61.     scanf("%lld",&T) ;
  62.  
  63.     while(T--)
  64.     {
  65.         t++ ;
  66.  
  67.         m1(dp) ;
  68.         v.clear() ;
  69.  
  70.         ll N ;
  71.         scanf("%lld",&N) ;
  72.  
  73.         while(N)
  74.         {
  75.             ll d ;
  76.             d = N % 2 ;
  77.             v.push_back(d) ;
  78.             N = N / 2 ;
  79.         }
  80.         len = v.size() ;
  81.         reverse(v.begin() , v.end()) ;
  82.  
  83.  
  84.         printf("Case %lld: ",t) ;
  85.         printf("%lld\n",solve(0 , 0 , 0 , 0)) ;
  86.     }
  87.     return 0 ;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement