hdu2089 不要62(数位dp)

不要62

本题大意:不吉利数字为62和4,求给定区间内的不吉利数的个数。

最为基础的数位dp入门题,不过本弱鸡水平太低,对着函数死看看不懂,只能当作模版用了,solve(x)表示x以内的不吉利数字的个数,用右区间减去左区间即可。

#include <iostream>
#include<cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#define init(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
using namespace std;
typedef long long ll;
int a[20];
ll dp[20][2];
ll dfs(int pos, int pre, int state, bool limit)
{
    if(pos==-1) return 1;
    if(!limit && dp[pos][state]!=-1) return dp[pos][state];
    int up=limit?a[pos]:9;
    ll ans=0;

    for(int i=0;i<=up;i++)
    {
        if(i == 4) continue;
        else if(pre == 6 && i == 2) continue;
        ans+=dfs(pos-1, i, i == 6 ? 1 : 0, limit && i==a[pos]);
    }
    if(!limit) dp[pos][state]=ans;
    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos - 1, 0, 0, true);
}
int main()
{
    init();
    int l,r;
    while(cin>>l>>r)
    {
        if(l==0&&r==0)continue;
        memset(dp,-1,sizeof(dp));
        cout<<solve(r)-solve(l-1)<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值