不要62 HDU - 2089 数位dp 除62以外的数字个数 寒假集训

杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
1 100
0 0
Sample Output
80
数位dp的模板,当前位置和不能有4,如果出现数字6就在这个位置状态记录下来,并且在他的后一位上面不能出现2

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 2e6+5;
const int INF = 0x3f3f3f3f;
ll dp[50][15];//dp[pos][pre]记录了遍历第pos为位时,前一位为pre时的状态数
ll a[30],b[30];
// void init(){
    
    
//     dp[0][0]=1;
//     dp[0][1]=dp[0][2]=0;
//     for(int i=1;i<25;i++){
    
    
//         dp[i][0]=10*dp[i-1][0]-dp[i-1][1];//长度为i不含有49的个数
//         dp[i][1]=dp[i-1][0];//最高位为9但不含49的个数
//         dp[i][2]=10*dp[i-1][2]+dp[i-1][1];//含有49的个数
//     }
// }
ll dfs(ll pos,ll pre,bool limit){
    
    //pos表示当前遍历的是第几位,pre表示前一位是几,limit为限制标志 
    if(pos==-1) return 1;//找到一个没有49的数
    if(!limit&&dp[pos][pre]!=-1)
        return dp[pos][pre];
    ll up;
    if(limit)
        up=a[pos];//确定当前数的最大值
    else up=9;//无限制
    ll ans=0;
    for(int i=0;i<=up;i++){
    
    
        if((pre==6&&i==2)||i==4)
            continue;
        else
            ans+=dfs(pos-1,i,limit&&i==a[pos]);//向下搜素并且
    }
    if(!limit)  
        dp[pos][pre]=ans;//无限制更新
    return ans;
}

ll init(ll n)
{
    
    
    ll len = 0;
    while(n) {
    
    
        a[len++] = n % 10;
        n /= 10;
    }
    return dfs(len - 1, 0, 1);
}

void solve(){
    
    
    ll x,y;
    memset(dp,-1,sizeof(dp));
    while(scanf("%lld %lld",&x,&y)!=EOF){
    
    
        if(x==0&&y==0) break;
        cout<<init(y)-init(x-1)<<endl;
    }
}
int main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/112909555
今日推荐