Light OJ 1032 - Fast Bit Calculations(数位DP)
2015-10-26 19:25
302 查看
题目大意:
一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少?
分解成2进制数字,然后数位DP就行了。
========================================================================
一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少?
分解成2进制数字,然后数位DP就行了。
========================================================================
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; const int INF = 1e9+7; const int MAXN = 255; int bit[40];///dp[位数][首位是否是1] LL dp[40][60][2]; LL DFS(int pos,int num,bool Is1,int Lim)///num代表这个数字里面成对的 { if(pos == -1) return num; if(dp[pos][num][Is1] != -1 && !Lim) return dp[pos][num][Is1]; int end = Lim?bit[pos]:1; LL ans = 0, k = 0; for(int i=0; i<=end; i++) { if(i == 1 && Is1) k = 1; ans += DFS(pos-1, num+k, i==1, Lim && i==end); } if(!Lim) dp[pos][num][Is1] = ans; return ans; } LL solve(int n) { int len = 0; while(n) { bit[len++] = n%2; n /= 2; } return DFS(len-1, 0, false, true); } int main() { int T, cas = 1, n; memset(dp, -1, sizeof(dp)); scanf("%d", &T); while(T --) { scanf("%d", &n); printf("Case %d: %lld\n",cas++, solve(n)); } return 0; }
相关文章推荐
- BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA
- Codeforces Round #327 (Div. 2)——B. Rebranding
- iphone数据存储之-- Core Data的使用(一)
- codeforces#327-C.Median Smoothing-规律题
- 有关802.11协议
- C++,JAVA之间相互调用学习
- #在蓝懿学习iOS的日子#Day5
- 记使用linux命令行编译c语言步骤
- 局部变量和成员变量的区别
- bfprt算法----找出数组中最小的k个数(Java)
- http状态码详解
- python property
- shell条件判断小知识
- poj 3080(后缀数组)
- document.createElement()的用法
- XVWA黑客靶场发布
- 数据结构_不带头结点的链表逆制
- DownFile
- C# 关键字
- C++:类的组合