[hdu 4734]数位dp例题
2024-08-28 17:26:35
通过这个题目更加深入了解到了数位dp在记忆化搜索的过程中就是实现了没有限制条件的n位数的状态复用。
#include<bits/stdc++.h>
using namespace std; int f2[];
int b[];
int dp[][];
int A; int F(int x)
{
int a[];
int cnt=;
int ret=;
do{
a[cnt]=x%;
x/=;
cnt++;
}while (x);
for (int i=cnt-;i>=;i--)
{
ret=ret*+a[i];
}
return ret;
} int dfs(int pos,int preok,int limit)
{
if (pos==-) return ;
if (preok && dp[pos][limit]!=-) return dp[pos][limit];
int up=preok?:b[pos];
int ans=;
for (int i=;i<=up;i++)
{
if (limit-f2[pos]*i<) continue;
if (i<b[pos]||preok) ans+=dfs(pos-,,limit-f2[pos]*i);
else ans+=dfs(pos-,,limit-f2[pos]*i);
}
if (preok) dp[pos][limit]=ans;
return ans;
} int solve(int n)
{
if (n<) return ;
int cnt=;
int now=n;
do{
b[cnt]=now%;
now/=;
cnt++;
}while (now);
return dfs(cnt-,,F(A));
} int main()
{
memset(dp,-,sizeof(dp));
f2[]=;
for (int i=;i<=;i++) f2[i]=f2[i-]*;
int t;
scanf("%d",&t);
for (int cas=;cas<=t;cas++)
{
int n;
scanf("%d%d",&A,&n);
printf("Case #%d: %d\n",cas,solve(n));
}
return ;
}
最新文章
- WPF入门教程系列十九——ListView示例(一)
- How to install starDIct on suse OS?
- 16-腾讯-intership-面试
- 系统间通信(5)——IO通信模型和JAVA实践 下篇
- Scalding初探之三:Hadoop实战
- bootstrap学习笔记<;十一>;(导航条)
- bzoj2437
- eclipse中如何导入jar包
- log4Net使用的四个步骤
- unexpected problem
- SQL Server 数据库所有者
- ElasticSearch D3
- 初识matplotlib
- DataX通过纯Java代码启动
- RH阴性血妇女怀孕注意事项
- ClassLoader那事儿
- mvc项目 ajax post 返回404错误
- python中的Process
- python学习第9-10天,函数。
- Android 二维码扫描/生成