No 62 HDU-2089 digital dp Number of numbers other than 62

Hangzhou people call those who are dumb and sticky as 62 (laoer).
The Hangzhou Transportation Administration often expands some taxi license plates. Recently, there is good news that the license plates will no longer contain unlucky numbers. In this way, the psychological barriers of individual taxi drivers and passengers can be eliminated. Serve the public safely.
The unlucky numbers are all numbers containing 4 or 62. For example:
62315 73418 88914
are all unlucky numbers. However, although 61152 contains 6 and 2, it is not 62 consecutive numbers, so it is not among the inauspicious numbers.
Your task is to infer how many new taxis the Traffic Management Bureau will actually have license plates for each time a license plate number is given.
Input
is a pair of integers n and m (0<n≤m<1000000). If an integer pair that is 0 is encountered, the input ends.
Output
For each integer pair, output a statistical number that does not contain an unlucky number, which occupies a row.
Sample Input
1 100
0 0
Sample Output
80
digital dp template, the current position and cannot have 4, if the number 6 appears, it will be recorded in this position, and 2 cannot appear on the next digit.

#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;
}

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/112909555