URAL 1057 数位DP(递归)
来源:互联网 发布:图片透视软件 编辑:程序博客网 时间:2024/04/30 22:59
题目:给出一个区间,问这个区间内的数,转换成B进制后,刚好有B个1的的数有多少个。
递归
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int k, b, l, r;int dp[66][66][66];int a[66];int dfs(int len, int cnt, bool lim) {if(!len) return cnt == k;if(!lim && ~dp[len][cnt][b]) return dp[len][cnt][b];int m = lim ? a[len-1] : b-1;int i, ret = 0;for(i = 0; i <= m && i <= 1; i++)ret += dfs(len-1, cnt+(i==1),lim&&i==m);if(!lim) dp[len][cnt][b] = ret;return ret;}int gao(int n) {int len = 0;while(n) { a[len++] = n%b; n /= b;}return dfs(len, 0, 1);}int main() {memset(dp, -1, sizeof(dp));while(cin >> l >> r >> k >> b) {cout << gao(r) - gao(l-1) << endl;}return 0;}
非递归
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int l, r, k, b;int dp[35][35];void init() {int i, j;memset(dp, 0, sizeof(dp));dp[0][0] = 1;for(i = 1; i <= 34; i++)for(j = 0; j <= i; j++) { dp[i][j] += dp[i-1][j]; if(j) dp[i][j] += dp[i-1][j-1];}}int gao(int n) {int a[35], len = 0;while(n) {a[len++] = n%b; n/=b;}int i, j;int cnt = 0, ans = 0;bool flag = 0;for(i = len-1; i >= 0; i--) {for(j = 0; j < a[i]; j++) {if(j > 1) break;if(cnt+j> k) continue;ans += dp[i][k-cnt-j];}if(a[i] == 1) cnt++;if(a[i] > 1) { flag = 1; break; }}if(cnt == k && !flag) ans++;return ans;}int main() {while(cin >> l >> r >> k >> b) {init();cout << gao(r) - gao(l-1) << endl;}return 0;}
- URAL 1057 数位DP(递归)
- URAL 1057 数位DP
- URAL 1057 数位DP
- URAL 1057 数位DP
- URAL 1057 (数位dp)
- ural 1057(数位dp)
- [数位dp] ural 1057 Amount of Degrees
- ural 1057 Amount of Degrees(数位DP)
- ural 1057 Amount of degrees 【数位dp】
- 【URAL】1057 Amount of Degrees 数位DP
- URAL 1057 Amount of Degrees (数位dp)
- 数位DP-URAL-1057-Amount of Degrees
- 【URAL 1057】 Amount of Degrees 【数位DP】
- 【数位DP】URAL 1057 Amount of Degrees
- URAL 1057 Amount of Degrees(数位dp)
- URAL 1057 Amount of Degrees 数位DP *
- URAL - 1057 Amount of Degrees--数位dp
- URAL 1057 Amount of Degrees 数位dp
- Android NDK开发(1)----- Java与C互相调用实例详解
- 进程和程序间的区别
- Java中自定义枚举(Enum)项的值及int和Enum的互相转换
- Maganto错误-Cannot initialize the indexer process的解决方法
- 从Android模拟器sdcard中导出文件
- URAL 1057 数位DP(递归)
- warning c4273: inconsistent dll linkage 的解决方法
- Linq中Take、TakeWhile、Skip、SkipWhile的比较
- 编程之美2.21 只考加法的面试题
- C++基础一波流——读《C++编程思想》
- 关于GSL库在vs2005中的移植
- http://bbs.chinaunix.net
- hdu 4352 数位DP
- 如何编译Windows版本的GSL