数位dp

代码:

#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
int a[20];
ll dp[20][2];
//if6:当前为是否为6;limit:上一位是否是上界 
ll dfs(int len,bool if6,bool limit){
    if(len==0) return 1ll;
    if(!limit&&dp[len][if6]) return dp[len][if6];
    ll cnt = 0,up_bound=(limit?a[len]:9);
    for(int i=0;i<=up_bound;i++){
        if(if6&&i==2)
            continue;
        if(i==4)
            continue;
        cnt+=dfs(len-1,i==6,limit&&i==up_bound);
    } 
    if(!limit) dp[len][if6] = cnt;
    return cnt; 
}
ll solve(ll x){
    int pos = 0;
    while(x){
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,false,true);
}
int main(){
    ll n,m;
    while(cin>>n>>m&&(n+m)){
        cout<<solve(m)-solve(n-1)<<endl;
    }    
    return 0;
}

 

posted @ 2020-02-18 21:38  sqsq  阅读(89)  评论(0编辑  收藏  举报