poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
来源:互联网 发布:java 字符串拷贝 编辑:程序博客网 时间:2024/05/29 16:25
两个题几乎差不多,就是poj2282多求下1-9的数量,求1-9的数量和51nod的1009一样点击打开链接。
数0的情况就有点不一样了,因为前导0不算在内,所以递归时要多加个判断是否是最前面的有效数字.
还有就是0(数字0)单独计算。。递归中不考虑0,最后输出的时候判断下如果L是0的话加个1
poj2282代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 20;int dp[maxn][maxn][maxn], a[maxn];int dfs0(int pos, int num, int lead, int limit){ if(pos == -1) return num; if(!limit && lead && dp[0][pos][num] != -1) return dp[0][pos][num]; int up = limit ? a[pos] : 9; int tmp = 0; for(int i = 0; i <= up; i++) { if(i) tmp += dfs0(pos-1, num, 1, limit && a[pos] == i); else tmp += dfs0(pos-1, num+lead, lead, limit && a[pos] == i); } if(!limit && lead) dp[0][pos][num] = tmp; return tmp;}int dfs(int pos, int val, int num, int limit){ if(pos == -1) return num; if(!limit && dp[val][pos][num] != -1) return dp[val][pos][num]; int up = limit ? a[pos] : 9; int tmp = 0; for(int i = 0; i <= up; i++) tmp += dfs(pos-1, val, num+(i==val), limit && a[pos] == i); if(!limit) dp[val][pos][num] = tmp; return tmp;}int solve(int x, int val){ int pos = 0; while(x) { a[pos++] = x%10; x /= 10; } if(!val) return dfs0(pos-1, 0, 0, 1); else return dfs(pos-1, val, 0, 1);}int main(void){ int a, b; memset(dp, -1, sizeof(dp)); while(cin >> a >> b, a+b) { if(a > b) swap(a, b); for(int i = 0; i <= 9; i++) printf("%d%c", solve(b, i)-solve(a-1, i), i==9 ? '\n' : ' '); } return 0;}
poj3286代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;typedef long long ll;const int maxn = 20;ll dp[maxn][maxn], a[maxn];ll dfs(int pos, int num, int lead, int limit){ if(pos == -1) return num; if(!limit && lead && dp[pos][num] != -1) return dp[pos][num]; int up = limit ? a[pos] : 9; ll tmp = 0; for(int i = 0; i <= up; i++) { if(i) tmp += dfs(pos-1, num, 1, limit && i == a[pos]); else tmp += dfs(pos-1, num+lead, lead, limit && i == a[pos]); } if(!limit && lead) dp[pos][num] = tmp; return tmp;}ll solve(ll x){ int pos = 0; while(x) { a[pos++] = x%10; x /= 10; } return dfs(pos-1, 0, 0, 1);}int main(void){ ll l, r; memset(dp, -1, sizeof(dp)); while(cin >> l >> r, l+r+2) { if(!l) printf("%lld\n", solve(r)+1); else printf("%lld\n", solve(r)-solve(l-1)); } return 0;}
0 0
- poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
- POJ 2282 The Counting Problem & POJ 3286 How many 0's?(按位计算贡献)
- POJ 3286 How many 0's? (数位dp)
- POJ 3286 How many 0's? / 2282 The Counting Problem 排列组合统计数字
- The Counting Problem - POJ 2282 数位dp
- poj 3286 How many 0's? (数位DP+ 统计)
- poj 3286 How many 0's? --- 数位dp
- How many 0's? - POJ 3286 数位dp
- POJ 3286 How many 0's?(数位DP)
- POJ 3286 How many 0's?(数位dp)
- POJ - 3286 - How many 0s? 【数位DP】
- poj 3286 How many 0's? 数位dp
- POJ 2282 The Counting Problem (数位dp)
- poj 2282 The Counting Problem (数位DP)
- Poj 3286 How many 0's? + Bzoj 1833 count 数字计数(数位统计)
- 小白算法练习 hdu 不要62 POJ 2282 the Counting problem 数位dp
- POJ3286:How many 0's?(数位DP)
- 数位dp poj3286 How many 0's?
- 20pin J-link使用SWD模式连接蓝宙K60DN512核心板
- throws和throw以及Finally的用法
- vmware vsphere环境搭建
- 【POJ 2559 Largest Rectangle in a Histogram】+ 栈
- opengl入门一
- poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
- LeetCode修炼之路(Easy系列)
- 500G 视频网盘分享
- perl基本语法概述
- hi3520d烧写开发板遇到的问题及其解决方法
- MXNet官方文档教程(6):神经网络图
- 大败局I,多元化是个陷阱吗?
- node.js入门
- 2016年