lightoj - 1032 Fast Bit Calculations (数位dp)总结
2015-06-18 10:32
274 查看
1032 - Fast Bit Calculations
PDF (English)
Statistics
Forum
Time Limit: 2 second(s)
Memory Limit: 32 MB
A bit is a binary digit, taking a logical value of either 1or
0 (also referred to as "true" or "false"respectively). And every decimal number has a binary representation which isactually a series of bits. If a bit of a number is
1 and its next bit isalso 1 then we can say that the number has a
1 adjacent bit. Andyou 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
Each case contains an integer N (0 ≤ N < 231).
0 to N.
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
思路:dp[cur][s][last]表示到当前位置,前面有多少个11,前一位是last的,答案是多少
总结了一下:这个题跟上一个求多少个零一样,刚开始还没想明白,为什么要保存前面有多少个零,其实这个跟刚开始的求含有49的有多少个还是有区别的,因为前面的一个49算一次,而这个要求所有的11有多少个,那么前面含有11的,那么后面的肯定要再加上,因为后面有多少个不一样的数,就对应前面那个11多少次
一下解决了两个题(一类题),好开心
PDF (English)
Statistics
Forum
Time Limit: 2 second(s)
Memory Limit: 32 MB
A bit is a binary digit, taking a logical value of either 1or
0 (also referred to as "true" or "false"respectively). And every decimal number has a binary representation which isactually a series of bits. If a bit of a number is
1 and its next bit isalso 1 then we can say that the number has a
1 adjacent bit. Andyou 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 summationof all adjacent bits from0 to N.
Sample Input
Output for Sample Input
70
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
思路:dp[cur][s][last]表示到当前位置,前面有多少个11,前一位是last的,答案是多少
总结了一下:这个题跟上一个求多少个零一样,刚开始还没想明白,为什么要保存前面有多少个零,其实这个跟刚开始的求含有49的有多少个还是有区别的,因为前面的一个49算一次,而这个要求所有的11有多少个,那么前面含有11的,那么后面的肯定要再加上,因为后面有多少个不一样的数,就对应前面那个11多少次
一下解决了两个题(一类题),好开心
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=40; typedef long long LL; int N; int dig[maxn]; LL dp[maxn][maxn][2]; LL dfs(int cur,int s,int e,int last) { if(cur<0)return s; if(!e&&dp[cur][s][last]!=-1)return dp[cur][s][last]; LL ans=0; int end=(e?dig[cur]:1); for(int i=0;i<=end;i++) { if(last&&i)ans+=dfs(cur-1,s+1,e&&i==end,i); else ans+=dfs(cur-1,s,e&&i==end,i); } if(!e)dp[cur][s][last]=ans; return ans; } LL solve(int n) { int len=0; memset(dp,-1,sizeof(dp)); while(n) { dig[len++]=n%2; n/=2; } return dfs(len-1,0,1,0); } int main() { int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d",&N); printf("Case %d: %lld\n",cas++,solve(N)); } return 0; }
相关文章推荐
- 和《超能陆战队》中“大白”硬扯上的关系的职场哲学
- MonkyTalk学习-1-前言,环境安装,环境配置。
- 数1游戏
- Texas Instruments matrix-gui-2.0 hacking -- generate.php
- android下载
- 十六进制图片转二进制
- mysql limit用法
- 如何正确的使用SharedPreferences?
- Unity压缩图片
- C++实现设计模式 - STATE(状态)—对象行为模式(第二十个)
- 数据结构学习之链表(单向、单循环以及双向)(递归实现)
- 找水王2
- webservice和soap原理
- Eclipse 下的TomcatPlugin
- JavaScript回调函数
- JS 面向对象之神奇的prototype(深入)
- JavaScript回调函数
- oracle 体系结构详解
- 阅读《构建之法》第13-17章
- CAS实战の遇到的问题