BZOJ-3679(数位DP)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b;
int k[20];
ll dp[20][10][2];
ll dfs(int pos,int x,bool lead,bool limit){
    if(pos == -1)return !lead;
    if(!limit && !lead && dp[pos][x])return dp[pos][x];
    int up = limit ? k[pos] : 9;
    ll res = 0;
    for(int i=0;i<=up;i++){
        if(lead){
            if(i == 0){
                res += dfs(pos-1,x,lead,false);
            }
            else{
                res += (x == i ? 2ll : 1ll) * dfs(pos-1,x,false,limit && k[pos] == i);
            }
        }
        else{
            res += (x == i ? 2ll : 1ll) * dfs(pos-1,x,false,limit && k[pos] == i);
        }
    }
    if(!limit && !lead)dp[pos][x] = res;
    return res;
}
ll solve(ll x,int z){
    int pos = 0;
    while(x){
        k[pos++] = x%10;
        x/=10;
    }
    return dfs(pos-1,z,true,true);
}
int main(){
    scanf("%lld%lld",&a,&b);
    for(int i=0;i<=9;i++){
        printf("%lld ",solve(b,i) - solve(a-1,i));
    }
    puts("");
    return 0;
}
posted @ 2019-05-16 17:03  kpole  阅读(171)  评论(0编辑  收藏  举报