HDU 4389 X mod f(x) 数位dp

M - X mod f(x)
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4389
Appoint description:  System Crawler  (2016-05-06)

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

f(x)的值就1-81所以枚举每个f(x)的值然后累加就好惹

ACcode:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[10][81][81][81];
int data[10];
int cnt=1;
int dfs(int len,int sum,int mod,int res,int limit){
    if(!len)return sum==mod&&res==0;
    if(!limit&&dp[len][sum][mod][res]!=-1)return dp[len][sum][mod][res];
    int ed=limit?data[len]:9;
    int ans=0;
    for(int i=0;i<=ed;++i)
        ans+=dfs(len-1,sum+i,mod,(res*10+i)%mod,limit&&i==ed);
    return limit?ans:dp[len][sum][mod][res]=ans;
}
int fun(int a){
    int len=0;
    while(a){
        data[++len]=a%10;
        a/=10;
    }
  int ans=0;
  for(int i=1;i<=81;++i)
    ans+=dfs(len,0,i,0,1);
  return ans;
}
void doit(){
    int a,b;
    scanf("%d%d",&a,&b);
    printf("Case %d: %d\n",cnt++,fun(b)-fun(a-1));
}
int main(){
    int loop;memset(dp,-1,sizeof(dp));
   scanf("%d",&loop);
    while(loop--)doit();}


你可能感兴趣的:(dp,HDU)