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;
}
全部评论

相关推荐

目前感觉简历还有很多问题,希望各位能不吝赐教以及非常感谢这位老哥——@黑皮白袜臭脚体育生&nbsp;的项目,学完一遍感觉受益颇丰
小菜鸡只想转正:校友,我的建议是冗余的最好去掉,突出重点,比如985,211双一流的提示,专业技能调整到个人项目之后的位置。专业技能感觉写的太细了?占用篇幅最好腾出一点给项目经历,如果没写手机号和邮箱,记得加上。
点赞 评论 收藏
分享
01-07 15:50
四川大学 Java
看日出看日落:好好背八股,做算法。我身边跟你bg差不多的基本都大厂暑期
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务