HDU3555 Bomb 数位DP

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[25][2];//dp[pos][last is 4]
ll n; 
vector<int>s;
ll dfs(int pos,int is4,int sp){
    if(pos==-1) return 1;
    if(!sp&&dp[pos][is4]!=-1) return dp[pos][is4];
    ll ans=0;
    int maxn=sp?s[pos]:9;
    for(int i=0;i<=maxn;i++){
        if(is4&&i==9) continue;
        ans+=dfs(pos-1,i==4,sp&&i==maxn);
    }
    if(!sp) dp[pos][is4]=ans;
    return ans;
}
ll cal(ll num){
    s.clear();
    while(num){
        s.push_back(num%10);
        num/=10;
    }
    return dfs(s.size()-1,0,1);
}
int main(){
    int t;cin>>t;
    memset(dp,-1,sizeof(dp));
    while(t--){
        cin>>n;
        cout<<n+1-cal(n)<<endl;
    }
}

 

posted on 2020-08-08 18:07  学无止境的小程序员  阅读(71)  评论(0编辑  收藏  举报

导航