HDU - 4389 X mod f(x) 数位dp

X mod f(x)

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


 

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

 

#include<cstdio>
#include<cstring>
int s[11];int dp[11][82][82][82];

int dfs(int pos,int sum,int divisor,int reminder,bool lim)
{
    if(pos<=0)return sum==divisor&&reminder==0;
    if(!lim&&dp[pos][sum][divisor][reminder]!=-1)return dp[pos][sum][divisor][reminder];
    int num=lim?s[pos]:9;int ans=0;
    for(int i=0;i<=num;i++)
    {
        int sum_x=sum+i;
        ans+=dfs(pos-1,sum_x,divisor,(reminder*10+i)%divisor,lim&&i==num);
    }
    return lim?ans:dp[pos][sum][divisor][reminder]=ans;
}

int solve(int x)
{
    int ans=0;
    int t=0;
    while(x){
        s[++t]=x%10;
        x/=10;
    }
    for(int i=1;i<=81;i++)
        ans+=dfs(t,0,i,0,1);
    return ans;
}

int main()
{
    int t,c=1;
    int n,m;scanf("%d",&t);memset(dp,-1,sizeof(dp));
    while(t--){
        scanf("%d%d",&n,&m);
        printf("Case %d: %d\n",c++,solve(m)-solve(n-1));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32259423/article/details/81268578
今日推荐