lightoj 1032 - Fast Bit Calculations 【数位dp】
来源:互联网 发布:青山软件 编辑:程序博客网 时间:2024/06/09 17:49
题目链接:lightoj 1032 - Fast Bit Calculations
定义二进制中相邻的两个
题意:定义
事实证明我又一次把简单问题复杂化。。。TMD,一开始直接设
SB思路(已AC):用
那么
设
SB思路
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e7 + 1;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void getmax(int &a, int b) {a = max(a, b); }void getmin(int &a, int b) {a = min(a, b); }void add(LL &x, LL y) { x += y; x %= MOD; }struct Node { LL num, sum;};Node dp[100][2][2][100];void init() { for(int i = 0; i < 100; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { for(int p = 0; p < 100; p++) dp[i][j][k][p].num = dp[i][j][k][p].sum = -1; } } }}int bit[100]; int len;bool get(int pre, int now, bool flag) { if(pre == 1 && now == 1) return true; if(pre == 1 && now == 0) return false; if(pre == 0 && now == 1) return true; if(pre == 0 && now == 0) return flag;}Node DFS(int pos, int pre, int cnt, bool flag, bool yes){ if(pos == -1) { Node ans; ans.num = cnt > 0; ans.sum = 0; return ans; } if(!yes && dp[pos][pre][flag][cnt].num != -1) return dp[pos][pre][flag][cnt]; int End = yes ? bit[pos] : 1; Node ans, temp; ans.num = ans.sum = 0; for(int i = 0; i <= End; i++) { if(i == 1 && pre == 1) { temp = DFS(pos-1, i, cnt+1, get(pre, i, flag), yes&&i==End); ans.num += temp.num; ans.sum += temp.num + temp.sum; } else { temp = DFS(pos-1, i, cnt, get(pre, i, flag), yes&&i==End); ans.num += temp.num; ans.sum += temp.num * i * flag + temp.sum; //if(pos == 3) cout << i << ' ' << ans.sum << endl; } } if(!yes) dp[pos][pre][flag][cnt] = ans; //cout << pos << ' ' << pre << ' ' << cnt << ' ' << yes << ' ' << zero << ' ' << ans.num << ' ' << ans.sum << endl; return ans;}LL Count(int n){ len = 0; while(n) { bit[len++] = n & 1; n >>= 1; } return DFS(len-1, 0, 0, 1, 1).sum - DFS(len-1, 0, 0, 1, 1).num;}int main(){ int t, kcase = 1; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); init(); printf("Case %d: %lld\n", kcase++, Count(n)); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e7 + 1;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void getmax(int &a, int b) {a = max(a, b); }void getmin(int &a, int b) {a = min(a, b); }void add(LL &x, LL y) { x += y; x %= MOD; }LL dp[100][2][100];int bit[100];LL DFS(int pos, int pre, int cnt, bool yes){ if(pos == -1) return cnt; if(!yes && dp[pos][pre][cnt] != -1) return dp[pos][pre][cnt]; int End = yes ? bit[pos] : 1; LL ans = 0; for(int i = 0; i <= End; i++) { if(pre == 1 && i == 1) { ans += DFS(pos-1, i, cnt+1, yes&&i==End); } else { ans += DFS(pos-1, i, cnt, yes&&i==End); } } if(!yes) dp[pos][pre][cnt] = ans; return ans;}LL Count(int n){ int len = 0; while(n) { bit[len++] = n & 1; n >>= 1; } return DFS(len-1, 0, 0, 1);}int main(){ int t, kcase = 1; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); CLR(dp, -1); printf("Case %d: %lld\n", kcase++, Count(n)); } return 0;}
0 0
- 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)
- 1032 - Fast Bit Calculations 数位dp
- LightOJ 1032 Fast Bit Calculations
- lightoj 1032 - Fast Bit Calculations
- 【Lightoj 1032 Fast Bit Calculations 】
- [light oj 1032]Fast Bit Calculations[数位DP]
- 什么是RESTFUL
- LMS项目总结
- 远程VisualSVN Server+本地TortoiseSVN
- ajax异步加载html片段
- struts2与springMVC的区别
- lightoj 1032 - Fast Bit Calculations 【数位dp】
- Linux安装JDK详细步骤
- 装饰模式实现穿衣搭配 C++
- 程序打包成exe文件
- 如何理解Java中的装箱与拆箱
- Java网络编程(二)总结
- HDU 5040 图论bfs
- 编写天气Demo,接触OKhttp框架,框架没那么难
- 关于sqlmap执行sql语句 二,关于sqlmap执行语句的前后台相关参数的传递