A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true" or "false" respectively). And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is 1 and its next bit is also 1 then we can say that the number has a 1 adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.
解析:简答数位DP。
dp[i][0]是第i+1位为0,0~(1<<i)-1内所有的adjacent bit的个数;
dp[i][1]是第i+1位为1,0~(1<<i)-1内所有的adjacent bit的个数。
[code]:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL dp[35][2];
int n,a[35],b[35],top;
void getBit(int n){
top = 0;
while(n){
a[top++] = n&1;
n>>=1;
}
for(int i = 0;i < top;i++){
if(i) b[i] = b[i-1]+(a[i]<<i);
else b[i] = a[i];
}
}
LL dfs(int i,int s,bool e){
if(i == -1) return 0;
if(!e&&dp[i][s]!=-1) return dp[i][s];
LL res = 0;
int d,u = e?a[i]:1;
for(d = 0;d <= u;d++){
res += dfs(i-1,d,e&&(d==u));
}
if(u&&s){
int tmp = e?b[i-1]+1:(1<<i);
res += tmp;
}
return e?res:dp[i][s]=res;
}
LL sol(int n){
getBit(n);
return dfs(top-1,0,1);
}
int main(){
int i,j,cas,T;
memset(dp,-1,sizeof(dp));
scanf("%d",&cas);
for(T = 1;T <= cas;T++){
scanf("%d",&n);
printf("Case %d: %lld\n",T,sol(n));
}
return 0;
}