hdu 3555 Bomb 数位DP

本文介绍了一种使用动态规划解决特定数字模式(特别是包含49组合)计数问题的方法。通过递归函数实现,文章详细解释了状态转移方程,并提供了完整的C++代码实现。

思路:

dp[i][0]:没有49出现的个数

dp[i][1]:出现只4的个数

dp[i][2]:出现49的个数

代码如下:

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define ll __int64
 5 using namespace std;
 6 int bit[20];
 7 ll dp[20][3];
 8 ll dfs(int pos,int h,bool f)
 9 {
10     if(pos==-1) return h==2;
11     if(!f&&dp[pos][h]!=-1) return dp[pos][h];
12     ll ans=0;
13     int e=f?bit[pos]:9;
14     for(int i=0;i<=e;i++){
15         int ha=h;
16         if(h==1&&i==9) ha=2;
17         if(h==1&&i!=9) ha=0;
18         if(h!=2&&i==4) ha=1;
19         ans+=dfs(pos-1,ha,f&&(i==bit[pos]));
20     }
21     if(!f) dp[pos][h]=ans;
22     return ans;
23 }
24 ll cal(ll n)
25 {
26     int m=0;
27     while(n){
28         bit[m++]=n%10;
29         n/=10;
30     }
31     return dfs(m-1,0,1);
32 }
33 int main()
34 {
35     ll n;
36     int t;
37     memset(dp,-1,sizeof(dp));
38     scanf("%d",&t);
39     while(t--){
40         scanf("%I64d",&n);
41         printf("%I64d\n",cal(n));
42     }
43 }
View Code

 

 

 

转载于:https://www.cnblogs.com/xin-hua/p/3304267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值