Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define pb push_back
- #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
- #define vi vector<int>
- vi a;
- int N;
- int dp[40][2][2];
- int cnt[40][2];
- int solve(int ind,int bit,int pre){
- if(ind == N-1){
- if(a[ind]==1)dp[ind][bit][pre] = bit;
- else dp[ind][bit][pre] = 0 + bit*(pre==0);
- }
- else if (dp[ind][bit][pre]==-1){
- int tmp = 0;
- if(pre==1){
- if(a[ind]==1){
- tmp = solve(ind+1,0,0)+solve(ind+1,1,1);
- int ad = cnt[ind+1][1];
- tmp += bit*ad;
- }
- else tmp = solve(ind+1,0,1);
- }
- else{
- tmp = solve(ind+1,0,0)+solve(ind+1,1,0);
- int ad = cnt[ind+1][0];
- tmp += ad*bit;
- }
- dp[ind][bit][pre] = tmp;
- }
- return dp[ind][bit][pre];
- }
- int count(int ind,int pre){
- if(ind==N)return 1;
- if(cnt[ind][pre]==-1){
- int res = 0;
- if(a[ind]==1){
- if(pre==1)
- res = count(ind+1,1)+count(ind+1,0);
- else res = 2LL * count(ind+1,0);
- }
- else{
- if(pre==0){
- res = 2LL*count(ind+1,0);
- }
- else res = count(ind+1,1);
- }
- cnt[ind][pre]=res;
- }
- return cnt[ind][pre];
- }
- void init(int n){
- a.clear();
- while(n){
- a.pb(n%2);
- n/=2;
- }
- a.pb(0);
- reverse(a.begin(),a.end());
- N = a.size();
- memset(dp,-1,sizeof(dp));
- memset(cnt,-1,sizeof(cnt));
- count(0,1);
- return;
- }
- int32_t main(){
- fast;
- int q,n;
- cin>>q;
- int t = q;
- while(q--){
- cin>>n;
- init(n);
- cout << "Case "<<t-q<<": "<< solve(0,0,1) << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment