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)


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
 

 

Author
WHU
 

 

Source
dp进阶之路写法;
或者分块打表;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=2147493647;
int f[11][83][83][83],bit[11];
int dp(int pos,int sum,int m,int p,int flag)
{
    if(pos==0)return (sum==p&&m==0);
    if(flag&&f[pos][sum][m][p]!=-1)return f[pos][sum][m][p];
    int x=flag?9:bit[pos];
    int ans=0;
    for(int i=0;i<=x;i++)
    {
        ans+=dp(pos-1,sum+i,(m*10+i)%p,p,flag||i<x);
    }
    if(flag)f[pos][sum][m][p]=ans;
    return ans;
}
int getans(int x,int p)
{
    int len=0;
    while(x)
    {
        bit[++len]=x%10;
        x/=10;
    }
    return dp(len,0,0,p,0);
}
int main()
{
    int T,cas=1;
    memset(f,-1,sizeof(f));
    scanf("%d",&T);
    while(T--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        int ans=0;
        for(int i=1;i<=81;i++)
            ans+=getans(r,i)-getans(l-1,i);
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}

 

 
posted @ 2017-03-20 19:24  jhz033  阅读(159)  评论(0编辑  收藏  举报