hdu 2089 不要62(数位DP)

本文介绍了一种使用数位动态规划(数位DP)的方法,来计算在一定范围内不包含特定数字(如62和4)的整数数量。通过记录当前数字前一位的状态,实现了简洁高效的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求n~m之间,不包含62和4的数的个数。

思路:比较水的数位dp,主要写来练练手。虽然早就知道数位dp是有模板的,但之前一直都没看,都是自己瞎写的,今天看了看,果然写起来飘逸多了~这题很简单,只要记录当前数字前一位是否为6就行了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
int dp[15][2];
int num[15];
int f(int pos,int st,bool limit)
{
    if(pos==-1) return 1;
    if(!limit&&dp[pos][st]!=-1)
        return dp[pos][st];
    int ans=0;
    int last=limit?num[pos]:9;
    for(int i=0;i<=last;++i)
    {
        if(i==4||(i==2&&st)) continue;
        ans+=f(pos-1,i==6,limit&&i==last);
    }
    if(!limit) dp[pos][st]=ans;
    return ans;
}
int cal(int x)
{
    int len=0;
    while(x)
    {
        num[len++]=x%10;
        x/=10;
    }
    return f(len-1,0,true);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    memset(dp,0xff,sizeof(dp));
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        printf("%d\n",cal(m)-cal(n-1));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值