HDU4389:X mod f(x)(数位DP)

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 int bit[15], dp[10][85][85][85], n, m;
 6 int dfs(int pos, int mod, int d, int sum, int limit) {
 7     if (pos == 0 ) return (d == sum && mod == 0) ;
 8     if (!limit && dp[pos][mod][d][sum] != -1 ) return dp[pos][mod][d][sum];
 9     int num = limit ? bit[pos] : 9, ans = 0;
10     for (int i = 0 ; i <= num ; i++) {
11         int tmod = (mod * 10 + i) % d;
12         ans += dfs(pos - 1, tmod, d, sum + i, limit && i == num);
13     }
14     if (!limit) dp[pos][mod][d][sum] = ans;
15     return ans;
16 }
17 int solve(int x) {
18     int len = 0;
19     while(x) {
20         bit[++len] = x % 10;
21         x /= 10;
22     }
23     int ans = 0;
24     for (int i = 1 ; i <= 81 ; i++)
25         ans += dfs(len, 0, i, 0, 1);
26     return ans;
27 }
28 int main() {
29     int t, cas = 1;
30     scanf("%d", &t);
31     memset(dp, -1, sizeof(dp));
32     while(t--) {
33         scanf("%d%d", &n, &m);
34         printf("Case %d: %d\n", cas++, solve(m) - solve(n - 1));
35     }
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/qldabiaoge/p/9345746.html
今日推荐