LG2602/BZOJ1833 「ZJOI2010」数字计数 数位DP

问题描述

LG2602

BZOJ1833


题解

数位\(\mathrm{DP}\)板子题。

注意限制位数、前导零。

\([a,b]=[1,b]-[1,a-1]\)


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

#define int long long

template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-'){
        fh=-1;ch=getchar();
    }
    else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    x*=fh;
}

int a,b;
int len;
int opt[21][3][21][3];
int lim[21];
void count(int x){
    len=0;
    while(x){
        lim[++len]=x%10,x/=10;
    }
}

int dp(int siz,bool lit,int base,bool pre,int pos){
    int re=0;
    if(!siz) return base;
    if(opt[siz][lit][base][pre]!=-1) return opt[siz][lit][base][pre];
    for(int i=0;i<=9;i++){
        if(!lit&&i>lim[siz]) break;
        re=re+dp(siz-1,lit||(i<lim[siz]),base+((!pre||i)&&(i==pos)),pre&&(!i),pos);
    }
    return opt[siz][lit][base][pre]=re;
}

int solve(int x,int pos){
    count(x);
    memset(opt,-1,sizeof(opt));
    return dp(len,0,0,1,pos);
}

signed main(){
    read(a);read(b);
    for(int i=0;i<=9;i++){
        printf("%lld ",solve(b,i)-solve(a-1,i));
    }
    return 0;
}

转载于:https://www.cnblogs.com/liubainian/p/11530985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值