hdu2089 不要62 (数位dp)

本文介绍了一道来自HDU OJ的题目2089的解决方案,该题要求计算区间内不含特定数字组合的整数数量。通过数位DP的方法进行解答,代码中详细展示了递归函数的实现过程,用于计算满足条件的数的数量。

题意:http://acm.hdu.edu.cn/showproblem.php?pid=2089

就是在l到r之间有多少个数满足  没有包含62相连的  并且不含有4

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
#include<string>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define N 106
#define Lson rood<<1
#define Rson rood<<1|1
LL dp[20][20],d[20];
LL dfs(int now,int up,int fp)
{
    if(now==1) return 1;
    if(!fp&&dp[now][up]!=-1) return dp[now][up];
    LL ans=0;
    int ma=fp?d[now-1]:9;
    for(int i=0;i<=ma;i++)
    {
        if(i==4||(up==6&&i==2)) continue;
        ans+=dfs(now-1,i,fp&&i==ma);
    }
    if(!fp&&dp[now][up]==-1) dp[now][up]=ans;
    return ans;
}
LL calc(LL x)
{
    if(x==0) return 1;
    LL xxx=x;
    int len=0;
    while(xxx)
    {
        d[++len]=xxx%10;
        xxx/=10;
    }
    LL sum=0;
    for(int i=0;i<=d[len];i++)
    {
        if(i!=4)
            sum+=dfs(len,i,i==d[len]);
    }
    return sum;
}
int main()
{
    LL l,r;
    memset(dp,-1,sizeof(dp));
    while(scanf("%lld%lld",&l,&r),l+r)
    {
        printf("%lld\n",calc(r)-calc(l-1));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/a719525932/p/7759971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值