Guest User

Untitled

a guest
Oct 29th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6. #define pb push_back
  7. #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  8. #define vi vector<int>
  9.  
  10. vi a;
  11. int N;
  12. int dp[40][2][2];
  13. int cnt[40][2];
  14.  
  15. int solve(int ind,int bit,int pre){
  16.     if(ind == N-1){
  17.         if(a[ind]==1)dp[ind][bit][pre] = bit;
  18.         else dp[ind][bit][pre] = 0 + bit*(pre==0);
  19.     }
  20.     else if (dp[ind][bit][pre]==-1){
  21.         int tmp = 0;
  22.         if(pre==1){
  23.             if(a[ind]==1){
  24.                 tmp = solve(ind+1,0,0)+solve(ind+1,1,1);
  25.                 int ad = cnt[ind+1][1];
  26.                 tmp += bit*ad;
  27.             }
  28.             else tmp = solve(ind+1,0,1);
  29.         }
  30.         else{
  31.             tmp = solve(ind+1,0,0)+solve(ind+1,1,0);
  32.             int ad = cnt[ind+1][0];
  33.             tmp += ad*bit;
  34.         }
  35.         dp[ind][bit][pre] = tmp;
  36.     }
  37.     return dp[ind][bit][pre];
  38. }
  39.  
  40. int count(int ind,int pre){
  41.     if(ind==N)return 1;
  42.     if(cnt[ind][pre]==-1){
  43.         int res = 0;
  44.         if(a[ind]==1){
  45.             if(pre==1)
  46.                 res = count(ind+1,1)+count(ind+1,0);
  47.             else res = 2LL * count(ind+1,0);
  48.         }
  49.         else{
  50.             if(pre==0){
  51.                 res = 2LL*count(ind+1,0);
  52.             }
  53.             else res = count(ind+1,1);
  54.         }
  55.         cnt[ind][pre]=res;
  56.     }
  57.     return cnt[ind][pre];
  58. }
  59.  
  60. void init(int n){
  61.     a.clear();
  62.     while(n){
  63.         a.pb(n%2);
  64.         n/=2;
  65.     }
  66.     a.pb(0);
  67.     reverse(a.begin(),a.end());
  68.     N = a.size();
  69.     memset(dp,-1,sizeof(dp));
  70.     memset(cnt,-1,sizeof(cnt));
  71.     count(0,1);
  72.     return;
  73. }
  74.  
  75. int32_t main(){
  76.     fast;  
  77.     int q,n;
  78.     cin>>q;
  79.     int t = q;
  80.         while(q--){
  81.         cin>>n;
  82.         init(n);
  83.         cout << "Case "<<t-q<<": "<< solve(0,0,1) << endl;
  84.     }
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment