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.
Examples:
Number Binary Adjacent Bits
12 1100 1
15 1111 3
27 11011 2
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer N (0 ≤ N < 231).
Output
For each test case, print the case number and the summation of all adjacent bits from 0 to N.
Sample Input
Output for Sample Input
7
0
6
15
20
21
22
2147483647
Case 1: 0
Case 2: 2
Case 3: 12
Case 4: 13
Case 5: 13
Case 6: 14
Case 7: 16106127360
没什么技巧
不能瞎套板子
#include<iostream>
#include<cmath>
#include<memory.h>
#include<algorithm>
#include<cstdio>
using namespace std;
long long n, bte[50], dp[50][50][3][2];
long long dfs(int chang, int zhuangtai, int biaoji, int zhege, int yigong)
{
if (biaoji&&dp[chang][yigong][zhuangtai][yigong] != -1)return dp[chang][yigong][zhuangtai][yigong];
if (chang == 0)
{
return yigong;
}
int bianjie = biaoji ? 1 : bte[chang];
long long sum = 0;
for (int a = 0; a <= bianjie; a++)
{
if (zhuangtai == 1 || zhuangtai == 2)
{
if (a == 1)
{
if (a == bianjie&&bianjie == bte[chang] && biaoji == 0)sum += dfs(chang - 1, 2, 0, 1, yigong + 1);
else sum += dfs(chang - 1, 2, 1, 1, yigong + 1);
}
else
{
if (a == bianjie&&bianjie == bte[chang] && biaoji == 0)sum += dfs(chang - 1, 0, 0, 0, yigong);
else sum += dfs(chang - 1, 0, 1, 0, yigong);
}
}
else
{
if (a == 1)
{
if (a == bianjie&&bianjie == bte[chang] && biaoji == 0)sum += dfs(chang - 1, 1, 0, 1, yigong);
else sum += dfs(chang - 1, 1, 1, 1, yigong);
}
else
{
if (a == bianjie&&bianjie == bte[chang] && biaoji == 0)sum += dfs(chang - 1, 0, 0, 0, yigong);
else sum += dfs(chang - 1, 0, 1, 0, yigong);
}
}
}
if (biaoji)dp[chang][yigong][zhuangtai][yigong] = sum;
return sum;
}
int main()
{
int T;
cin >> T;
int u = 0;
while (T--)
{
bte[0] = 0;
cin >> n;
for (int a = 0; a <= 49; a++)
{
for (int b = 0; b <= 49; b++)
{
for (int c = 0; c < 3; c++)
{
dp[a][b][c][0] = dp[a][b][c][1] = -1;
}
}
}
long long i = n;
while (i)
{
bte[++bte[0]] = i & 1;
i >>= 1;
}
long long sum = dfs(bte[0], 0, 0, 0, 0);
printf("Case %d: %lld\n", ++u, sum);
}
return 0;
}