题目链接:https://cn.vjudge.net/problem/HDU-4389
Here is a function f(x):
   int f ( int x ) {
       if ( x == 0 ) return 0;
       return f ( x / 10 ) + x % 10;
   }


   Now, you want to know, in a given interval [A, B] (1 <= A <= B <= 10 9), how many integer x that mod f(x) equal to 0.

Input

   The first line has an integer T (1 <= T <= 50), indicate the number of test cases. 
   Each test case has two integers A, B. 

Output

   For each test case, output only one line containing the case number and an integer indicated the number of x. 

Sample Input

2
1 10
11 20

Sample Output

Case 1: 10
Case 2: 3

题意:

f(x)为x的各位数之和。

求[A,B]范围内x%f(x)==0的x的个数

 

用dp[pos][sum][x][mod]来表示 第pos位之前各位数字之和为sum的数 除以x(也就是题意中的f(x))的余数为mod

那么终止条件就是当pos枚举到-1时 判断枚举的这个数是否满足数字之和为sum并且他的余数为0 

转移就是

dp[pos][sum][x][mod]=dp[pos-1][sum+i][x][(mod*10+i)%x]

然后我们对于每一个需要solve的数 枚举各位数加起来的81种情况即可

#include<cstdio>
#include<cstring>
using namespace std;
int dp[10][82][82][82];
int a[10];
int dfs(int pos,int sum,int x,int mod,bool limit){
	if(pos==-1) return x==sum&&mod==0;
	if(!limit&&dp[pos][sum][x][mod]!=-1) return dp[pos][sum][x][mod];
	int up=limit?a[pos]:9;
	int ans=0;
	for(int i=0;i<=up;i++){
		ans+=dfs(pos-1,sum+i,x,(mod*10+i)%x,limit&&i==up);
	}
	if(!limit) dp[pos][sum][x][mod]=ans;
	return ans;
}
int solve(int x){
	int pos=0;
	while(x){
		a[pos++]=x%10;
		x/=10;
	}
	int ans=0;
	for(int i=1;i<=81;i++)
	ans+=dfs(pos-1,0,i,0,true);
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	memset(dp,-1,sizeof(dp));
	for(int i=1;i<=t;i++){
		int A,B;
		scanf("%d%d",&A,&B);
		printf("Case %d: %d\n",i,solve(B)-solve(A-1));		
	}
	return 0;
}