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

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 <= 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, b]中符合条件的个数和[1, a-1]中符合条件的个数。

d[l][i][j][k]表示前l位和为i模j的结果为k的数的个数,那么就有方程

d[l+1][i+x][j][(k*10+x)%j] += d[l][i][j][k]

预处理出d[l][i][j][k],然后再逐位统计即可

 

#include 
#include 
#include 
using namespace std;

int bit[10];
int dp[10][82][82][82];
//d[l][i][j][k]表示前l位和为i模j的结果为k的数的个数
void set()
{
    int i,j,k,l,x;
    for(i = 1; i<=81; i++)
        dp[0][0][i][0] = 1;
    for(l = 0; l<9; l++)
        for(i = 0; i<=l*9; i++)
            for(j = 1; j<=81; j++)
                for(k = 0; k


 

 

你可能感兴趣的:(DP)