lightoj 1032 - Fast Bit Calculations
来源:互联网 发布:好收益网络贷款 编辑:程序博客网 时间:2024/06/16 22:47
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
如果一个二进制数,它的某一位和它后面一位都是1,那么这一位就称为adjacent bits。
问从0到N所有数的adjacent bits一共有多少个。
这个一眼就能看出来是数位DP,但是第一开始状态少想了一个,所以答案一直不对,最后加上到某位时一共有多少个adjacent bits这个状态,就得出了正确答案。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;long long dp[40][40][2];int a[40];long long dfs(int len,int sum,int s,int fp)//到当前位一共有sum个 adjacent bits{ if(!len) return (long long)sum; if(!fp && dp[len][sum][s] != -1) return dp[len][sum][s]; long long res = 0; int n = fp ? a[len]:1; for(int i=0;i<=n;i++) { if(s == 1 && i == 1) res += dfs(len-1,sum+1,1,fp); else res += dfs(len-1,sum,i == 1,fp&&i==n); } if(!fp) dp[len][sum][s] = res; return res;}long long sum(int x){ int len = 0; while(x) { a[++len] = x % 2; x /= 2; } return dfs(len,0,0,1);}int main(void){ int T,n; memset(dp,-1,sizeof(dp)); scanf("%d",&T); int c = 1; while(T--) { scanf("%d",&n); printf("Case %d: %lld\n",c++,sum(n)); }}
- LightOJ 1032 Fast Bit Calculations
- lightoj 1032 - Fast Bit Calculations
- 【Lightoj 1032 Fast Bit Calculations 】
- 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
- 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)
- light oj 1032 - Fast Bit Calculations
- 1032 - Fast Bit Calculations 数位dp
- 自动生成MVC中的Service层、Dao层及Model层
- nyoj1057最大数(三)
- 局部变量、全局变量、堆、堆栈、静态和全局
- TYVJ 1541 八数码
- HTML
- lightoj 1032 - Fast Bit Calculations
- Maven搭建struts2+spring+hibernate环境(一)
- gdi+绘图应用
- poj 1222 EXTENDED LIGHTS OUT
- chown与chmod的区别
- java poi 读取excel文档最简单方式
- ping 非本地子网时报"ping: sendmsg: Network Is Down"
- python3.0 django mysql连接池说明
- HDU 1698 Just A Hook