HDU 4389 x Mod f (x)

Source: Internet
Author: User

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


Question: The number of [L, R] ranges that meet the requirements of X % F [x] = 0 !!

Relatively classic digital motion regulation!

Pure enumeration will definitely time out !!!

So here we use the enumerated bits and find them !!!


The AC code is as follows:


#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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.