-
Notifications
You must be signed in to change notification settings - Fork 1
/
1032 - Fast Bit Calculations.cpp
61 lines (61 loc) · 1.28 KB
/
1032 - Fast Bit Calculations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int arr[32];
ll dp[32][2][32][2];
ll fun(int pos, int isSmall, ll tot, int prev)
{
if(pos==32) return tot;
ll &ret = dp[pos][isSmall][tot][prev];
if(ret!=-1) return ret;
ret=0;
if(isSmall){
if(prev){
ret+=fun(pos+1, isSmall, tot+1, 1);
ret+=fun(pos+1, isSmall, tot, 0);
}
else{
ret+=fun(pos+1, isSmall, tot, 1);
ret+=fun(pos+1, isSmall, tot, 0);
}
}
else{
if(arr[pos]){
if(prev){
ret+=fun(pos+1, isSmall, tot+1, 1);
ret+=fun(pos+1, 1, tot, 0);
}
else{
ret+=fun(pos+1, isSmall, tot, 1);
ret+=fun(pos+1, 1, tot, 0);
}
}
else{
ret+=fun(pos+1, isSmall, tot, 0);
}
}
return ret;
}
int main()
{
int tc,cs=1,n;
scanf("%d",&tc);
while(tc--){
scanf("%d",&n);
for(int i=31; i>=0; i--){
arr[i] = n%2;
n/=2;
}
memset(dp,-1,sizeof dp);
printf("Case %d: %lld\n", cs++, fun(0, 0, 0, 0));
}
return 0;
}
/**
7 0 6
15
20
21
22
2147483647
*/