A strange thing
Positive and negative can dp !:
normal digital dp we are descending, in such a way to ensure that a given number is less than ->
ll n;
int num[N],l;
ll dp[];
ll dfs(int p,int lim){
if(p==0) return 1;
if(!lim&&~dp[]) return dp[];
int Uplim=lim?num[p]:9;
ll res=0;
for(int i=0;i<=Uplim;i++) {
res+=dfs(p-1,lim&&(i==Uplim));
}
if(!lim) dp[]=res;
return res;
}
int main(){
scanf("%lld",&n);
while(n) num[++l]=n%10,n/=10;
printf("%lld\n",dfs(l,1));
}
However, from low to high it should be like this:
ll n;
int num[N],l;
ll dp[];
ll dfs(int p,int fl){
if(p==l+1) return fl;
if(~dp[]) return dp[];
ll res=0;
for(int i=0;i<10;i++) {
res+=dfs(p+1,i<num[p]||(i==num[p]&&fl));
}
return dp[]=res;
}
int main(){
scanf("%lld",&n);
while(n) num[++l]=n%10,n/=10;
printf("%lld\n",dfs(1,1));
}
In some special cases this can eliminate some of the aftereffect and cumbersome steps