码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4389 X mod f(x)

时间:2014-07-24 11:30:32      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:mamicode

X mod f(x)


Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)




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


题意:求区间[l,r] 满足x%f[x]==0的数的个数!!

比较经典的数位动规!

纯枚举肯定会超时!!!

所以这里是通过枚举位数和求的!!!


AC代码如下:


#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;

int num[22];
int dp[11][82][82][82];
int mo;

int dfs(int pos,int mo,int sum1,int sum2,bool limit)
{
    int i;
    if(pos==-1) return sum1==mo&&sum2==0;
    if(!limit&&dp[pos][mo][sum1][sum2]!=-1) return dp[pos][mo][sum1][sum2];
    int end = limit ? num[pos] : 9;
    int sum = 0;
    for(i = 0; i <= end; i++)
    {
        sum+=dfs(pos-1,mo,sum1+i,(sum2*10+i)%mo,limit&&i==end);
    }
    return limit ? sum : dp[pos][mo][sum1][sum2] = sum;
}

int solve (int n)
{
    int pos=0;
    int ans=0;
    int i;

    while(n>0)
    {
        num[pos++]=n%10;
        n/=10;
    }
    for(i=1;i<=81;i++)
    {
        ans+=dfs(pos-1,i,0,0,true);
    }
    return ans;
}

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


HDU 4389 X mod f(x)

标签:mamicode

原文地址:http://blog.csdn.net/hanhai768/article/details/38081425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!