天天看点

P2602 [ZJOI2010]数字计数 [数位DP, 记忆化搜索]

传送门

不知道为什么没有a , 对拍过都是对的

#include<bits/stdc++.h>
#define N 15
#define LL long long
using namespace std;
LL f[N][N][2][2]; // 位数,次数,limit,前导0
LL l,r,a[N],ans[N];
LL dfs(int u,int d,int l,int z,int s){
	if(u<1) return s;
	LL &res = f[u][s][l][z];
	if(res != -1) return res;
	int lim = l ? a[u] : 9; LL ans=0;
	for(int i=0;i<=lim;i++)
		ans += dfs(u-1,d,(l&&(i==lim)),(z&&(i==0)),s+((!z||i)&&(i==d))); 
	return res = ans;
}
void Solve(LL x,LL k){
	memset(a,0,sizeof(a));
	LL tmp=x; int Len=0;
	while(tmp) a[++Len]=tmp%10,tmp/=10;
	for(int i=0;i<=9;i++){
		memset(f,-1,sizeof(f));
		ans[i] += dfs(Len,i,1,1,0) * k;
	}
}
int main(){
	scanf("%d%d",&l,&r); Solve(r,1); Solve(l-1,-1);
	for(int i=0;i<=9;i++) printf("%lld ",ans[i]); return 0;
}