luogu P4127 [AHOI2009]同类分布 数位dp

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define rint register int
using namespace std;
typedef long long ll;
int mod,a[20];
ll l,r,dp[19][163][163];
inline ll dfs(int pos,int sum,int now,int limit)
{
	//剩下的都是最大的也不行 就直接推出 
	if(sum+9*pos<mod)
		return 0;
	if(!limit&&dp[pos][sum][now]!=-1)
		return dp[pos][sum][now];
	if(!pos)
		return (sum==mod)&&(now==0);
	rint up=limit?a[pos]:9;
	register ll res=0;
	//不能大于最大 
	for(rint i=0; i<=up&&i+sum<=mod; i++)
		res+=dfs(pos-1,sum+i,(now*10+i)%mod,limit&&i==a[pos]);
	if(!limit)
		dp[pos][sum][now]=res;
	return res;
}
inline ll solve(ll x)
{
	rint cnt=0;
	while(x)
	{
		a[++cnt]=x%10;
		x/=10;
	}
	register ll res=0;
	//枚举各位数字的和
	for(mod=1; mod<=cnt*9; mod++)
	{
		memset(dp,-1,sizeof dp);
		res+=dfs(cnt,0,0,1);
	}
	return res;
}
int main()
{
	scanf("%lld%lld",&l,&r);
	printf("%lld",solve(r)-solve(l-1));
	return 0;
}
posted @ 2020-05-11 20:04  晴屿  阅读(91)  评论(0编辑  收藏  举报