LightOJ - 1032 Fast Bit Calculations(数位DP/ 递推)
2017-08-21 13:39
423 查看
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:
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
7
0
6
15
20
21
22
2147483647
Sample Output
Case 1: 0
Case 2: 2
Case 3: 12
Case 4: 13
Case 5: 13
Case 6: 14
Case 7: 16106127360
题意:
定义 相邻位数=一个数的二进制状态下 1的后面也是1那么相邻位数+1;
比如 1111 的num=3 ;
1011 num=1;
11011 num=2;
现在给一个n 求 所有小于等于n的数 num之和;
思路:
1.数位dp。。套板子。。不知道这个状态是怎么定义的呃
2.递推
定义f[i] 表示小于 2^i-1 所有num之和
那么f[i]=f[i-1]+f[i-1]+2^(i-2);
第一个f[i-1] 就是累加的前面的
第二个f[i-1] 表示 最高位取1 f[i-1]所有的情况都会加进来,但是还少了一部分 就是 2^(i-2) 这个表示最高位取1的时候会增加的个数。因为最高位增加一个1 只会对次高位也是1的情况的答案+1 ,其他的还是原答案。
那么对于一个数 11011 我们怎么算呢
先算[0,1111]这个区间 就是f[i] ,然后还剩[10000,11011] ,我们还需要加上011种方案 因为如果首位和次位都是1 那么需要加上首位的影响 才能去掉首位的1 重复上述步骤接着计算。
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
7
0
6
15
20
21
22
2147483647
Sample Output
Case 1: 0
Case 2: 2
Case 3: 12
Case 4: 13
Case 5: 13
Case 6: 14
Case 7: 16106127360
题意:
定义 相邻位数=一个数的二进制状态下 1的后面也是1那么相邻位数+1;
比如 1111 的num=3 ;
1011 num=1;
11011 num=2;
现在给一个n 求 所有小于等于n的数 num之和;
思路:
1.数位dp。。套板子。。不知道这个状态是怎么定义的呃
2.递推
定义f[i] 表示小于 2^i-1 所有num之和
那么f[i]=f[i-1]+f[i-1]+2^(i-2);
第一个f[i-1] 就是累加的前面的
第二个f[i-1] 表示 最高位取1 f[i-1]所有的情况都会加进来,但是还少了一部分 就是 2^(i-2) 这个表示最高位取1的时候会增加的个数。因为最高位增加一个1 只会对次高位也是1的情况的答案+1 ,其他的还是原答案。
那么对于一个数 11011 我们怎么算呢
先算[0,1111]这个区间 就是f[i] ,然后还剩[10000,11011] ,我们还需要加上011种方案 因为如果首位和次位都是1 那么需要加上首位的影响 才能去掉首位的1 重复上述步骤接着计算。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; long long f[40]; int n; long long dfs(int x){ long long ans=0; for(int i=31;i>=0;i--){ if(x & (1<<i)){ x = x-(1<<i); ans+=f[i]+max(0,x-(1<<(i-1))+1); } } return ans; } int main(){ // freopen("in.txt","r",stdin); f[1]=0; f[2]=1; for(int i=3;i<=31;i++) { f[i]=f[i-1]*2+(1 << (i-2)); } int T,cas=0; scanf("%d",&T); while(T--){ scanf("%d",&n); printf("Case %d: %lld\n",++cas,dfs(n)); } return 0; }
相关文章推荐
- LightOJ1032 Fast Bit Calculations(数位DP)
- LightOJ 1032 Fast Bit Calculations(数位DP)
- lightoj 1032 Fast Bit Calculations(数位dp)
- Lightoj1032——Fast Bit Calculations(数位dp)
- LightOJ - 1032 Fast Bit Calculations(数位DP)
- lightoj 1032 Fast Bit Calculations 数位dp
- LightOJ - 1032 Fast Bit Calculations (数位DP)
- LightOJ 1032 Fast Bit Calculations(数位DP)
- LightOJ 1032 - Fast Bit Calculations (数位dp)
- lightoj 1032 - Fast Bit Calculations (数位DP)
- lightoj - 1032 Fast Bit Calculations (数位dp)总结
- [暑假集训--数位dp]LightOj1032 Fast Bit Calculations
- LightOJ 1032 - Fast Bit Calculations (数位dp)
- lightoj 1032 - Fast Bit Calculations 【数位dp】
- lightOJ1032(数位dp)
- LightOJ 1032 Fast Bit Calculations(数位统计)
- lightoj 1032 数位DP
- Light oj 1032 - Fast Bit Calculations(数位dp)
- Light OJ 1032 - Fast Bit Calculations(数位DP)
- 1032 - Fast Bit Calculations 数位dp