Idea
可以模仿我们人类分类讨论的时候做的事情, 并且加以记忆化, 这样就可以完成.
Code
#include <bits/stdc++.h>
using namespace std;
#define MAXN 16
#define F(i, a, b) for(int i=(a); i<=(b);i++)
#define Fd(i, a, b) for(int i=(a);i>=(b);i--)
#define int long long
int f[15][MAXN][3][3];
int a, b, num[15];
int tot;
int dp(int x, int sum, int d, int lim, int zero){
// printf("%d %d %d %d %d\n", x, sum, d, lim, zero);
if(x==0) {return sum;}
if(f[x][sum][lim][zero]!=-1) return f[x][sum][lim][zero];
int ans = 0;
F(i, 0, (lim==1 ? num[x] : 9)){
if(zero && i==0) ans += dp(x-1, sum, d, 0, 1);
else ans += dp(x-1, sum+(i==d), d, lim&&(i==num[x]),0);
// printf("The ans is %d\n", ans);
}
return f[x][sum][lim][zero]=ans;
}
int sol(int d,int x) {
F(i, 0, 14) F(j, 0, MAXN-1) F(k, 0, 2) F(l, 0, 2) f[i][j][k][l] = -1;
tot=0;
while(x){ num[++tot]=x%10,x/=10;}
return dp(tot,0,d,1,1);
}
signed main(){
cin>>a>>b;
F(i,0,9) cout<<sol(i,b)-sol(i,a-1)<<" ";
return 0;
}
标签:计数,int,lim,sum,ans,ZJOI2010,zero,num,P2602
From: https://www.cnblogs.com/augpath/p/16913913.html