hdu4722 Good Numbers (数位DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722


#include <stdio.h>      
#include <string.h>  

_int64 dp[20][10];
int digits[20];

_int64 DFS(int pos,int mod,int limit)
{
	int end,i,nmode;
	_int64 sum=0;
	if(pos==-1)
		return mod==0;
	if(!limit&&dp[pos][mod]!=-1)
		return dp[pos][mod];
	end=limit?digits[pos]:9;
	for(i=0;i<=end;++i)
	{
		nmode=(mod+i)%10;
		sum+=DFS(pos-1,nmode,limit&&i==end);
	}
	if(!limit)
		dp[pos][mod]=sum;
	return sum;
}

_int64 solve(_int64 n)
{
	int pos=0;
	while(n)
	{
		digits[pos++]=n%10;
		n/=10;
	}
	return DFS(pos-1,0,1);
}

int main()
{
	int test,cases=1;
	_int64 n,m,a,b;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%I64d %I64d",&n,&m);
		memset(dp,-1,sizeof(dp));
		a=solve(n-1);
		b=solve(m);
		printf("Case #%d: %I64d\n",cases++,b-a);
	}
}


你可能感兴趣的:(hdu4722 Good Numbers (数位DP))