数位dp板子

code:

#include<iostream>
#include<cstring>
using namespace std;
const int N=16;
int d[N],n;
int dp[N];

int dfs(int pos,bool limit){
    if(pos==0) return 1;
    if(!limit&&~dp[pos]) return dp[pos];
    int up=limit?d[pos]:9;
    int ret=0;
    for(int i=0;i<=up;i++)
        ret+=dfs(pos-1,limit&&(i==d[pos]));
    if(!limit) dp[pos]=ret;
    return ret;
}

int calc(int x){
    memset(dp,0xff,sizeof(dp));
    memset(d,0,sizeof(d));
    int len=0;
    while(x){
        d[++len]=x%10;
        x/=10;
    }
    return dfs(len,1);
}
int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int x;
    cin>>x;
    //0-x有多少个数
    cout<<calc(x)<<endl;
    return 0;
}
posted @ 2025-08-20 19:05  xdhking  阅读(6)  评论(0)    收藏  举报