HDU2089[不要62]--数位DP

本文介绍了一种使用数位动态规划(数位DP)解决特定类型数学问题的方法,并通过HDU2089题目进行实例讲解。文章详细展示了如何通过递归函数实现数位DP算法,以及如何在给定范围内计算满足条件的数字数量。

【链接】
hdu2089

【解题报告】
简单的数位DP

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=25;
int n,m,ans,len,f[maxn][10],a[maxn];
inline int Read()
{
    int res=0; char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=getchar();
    return res;
}
int Dfs(int x,int las,bool pd)
{
    if (x>len) return 1;
    if (!pd&&f[x][las]) return f[x][las];
    int MAX=9; if (pd) MAX=a[x]; f[x][las]=0;
    for (int i=0; i<=MAX; i++)
     if ((las!=6&&i!=4)||(las==6&&i!=2&&i!=4)) f[x][las]+=Dfs(x+1,i,pd&(i==a[x]));
    return f[x][las];
}
void Work()
{
    memset(f,0,sizeof(f));
    a[0]=len=0;int x=n-1;
    while (x) a[++len]=x%10,x/=10;
    for (int i=1; i<=len/2; i++) swap(a[i],a[len-i+1]);
    ans=Dfs(1,0,1);
    memset(f,0,sizeof(f));
    a[0]=len=0; x=m;
    while (x) a[++len]=x%10,x/=10;
    for (int i=1; i<=len/2; i++) swap(a[i],a[len-i+1]);
    printf("%d\n",Dfs(1,0,1)-ans);
    n=Read(); m=Read();
}
int main()
{
    freopen("2089.in","r",stdin);
    freopen("2089.out","w",stdout);
    n=Read(); m=Read();
    while (n||m) Work();
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值