HDU 4734 (数位DP)题解
2024-08-29 13:45:18
思路:
dp[pos][pre]代表长度为pos的不大于pre的个数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 100+5;
int dp[12][200000],a[20],UP;
int F(int x){
int ret = 0,pos = 0;
while(x){
ret += (x % 10) *(1 << pos);
pos++;
x /= 10;
}
return ret;
}
int dfs(int pos,int sum,bool limit){
if(pos == -1) return sum >= 0;
if(sum < 0) return 0;
if(!limit && dp[pos][sum] != -1) return dp[pos][sum];
int top = limit? a[pos] : 9;
int ret = 0;
for(int i = 0;i <= top;i++){
ret += dfs(pos-1,sum-i*(1<<pos),limit && i == top);
}
if(!limit) dp[pos][sum] = ret;
return ret;
}
int solve(int x){
int pos = 0;
while(x){
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos-1,UP,true);
}
int main(){
int k = 1;
memset(dp,-1,sizeof dp);
int A,B;
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&A,&B);
UP = F(A);
printf("Case #%d: %d\n",k++,solve(B));
}
return 0;
}
最新文章
- 深入理解DOM事件类型系列第四篇——剪贴板事件
- u-boot FIT image介绍_转自“蜗窝科技”
- Webwork 学习之路【01】Webwork与 Struct 的前世今生
- 16.检查是否为BST
- jQuery 写的幻灯左右切换插件
- 启动hadoop报192.168.1.151: Address 192.168.1.151 maps to node1, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
- caches 文件夹删除
- select选项框特效
- 加解密算法二:非对称加解密及RSA算法的实现
- HTML XML XHTML DHTML区别与联系
- linux笔记2.25
- 刚写好的读取多网卡IP地址的函数
- Javascript使用postMessage对iframe跨域通信
- windows程序设计(四)
- linux命令(方可)
- Android APK安装过程学习笔记
- ASP.NET Core 和 ASP.NET Framework 共享 Identity 身份验证
- docker之数据卷管理
- 让IIS支持10万并发
- MySQL -- 全文检索(查询扩展检索)