# X mod f(x)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2330    Accepted Submission(s): 919

Problem Description
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 <= 109), 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

sum是数位和，nwmod是取模结果，mod 是枚举的模

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>

typedef long long LL;
const int maxn=81;
int dig[maxn];
int f[10][maxn][maxn][maxn];
//nwmod  数取模后 sum 数位和
LL dfs(int pos,int nwmod,int sum,int mod,int limit)
{
if (pos<0) return sum==mod&&nwmod==0;
if (!limit&&f[pos][nwmod][sum][mod]!=-1)
return f[pos][nwmod][sum][mod];
LL res=0;
int last=limit?dig[pos]:9;
for (int i=0;i<=last;i++)
{
res+=dfs(pos-1,(nwmod*10+i)%mod,sum+i,mod,limit&&(i==last));
}
if (!limit) f[pos][nwmod][sum][mod]=res;
return res;
}

LL solve(LL n){
int len=0;
while (n)
{
dig[len++]=n%10;
n/=10;
}
LL ans=0;
for(int i=1;i<=81;i++)//枚举最后的mod
{
ans+=dfs(len-1,0,0,i,1);
}
return ans;
}
int main()
{
int n;
int t;
int cas=1;
scanf("%d",&t);
int a,b;
memset(f,-1,sizeof f);
while(t--)
{
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
printf("Case %d: %I64d\n",cas++,solve(b)-solve(a-1));
}
return 0;
}

/*
2
1 10
11 20

Sample Output
Case 1: 10
Case 2: 3
*/

#### HDU 4389 - X mod f(x)

2012-08-21 18:21:04

#### hdu 4389

2012-08-22 21:15:52

#### hdu4389-X mod f(x)-多校9-1010题解

2012-08-22 19:59:53

#### 知识补充

2015-02-26 12:27:44

#### HDU4389(数位DP)

2013-07-13 15:15:12

#### hdu 4389 X mod f(x)（超暴力打表）

2016-06-12 11:57:15

#### HDU 4389 X mod f(x)

2014-07-24 11:17:15

#### HDU 4389——X mod f(x)（数位DP）

2014-07-24 10:38:10

#### hdu 4389 X mod f(x) （数位DP）

2014-03-15 21:15:35

#### Hdu 4389 X mod f(x) 数位DP

2017-08-23 00:41:14

## 不良信息举报

hdu 4389 X mod f(x) 数位dp