Hdu 4389 X mod f(x) 数位DP

来源:互联网 发布:汤姆大叔 javascript 编辑:程序博客网 时间:2024/05/16 15:51

X mod f(x)

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


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
21 1011 20
 

Sample Output
Case 1: 10Case 2: 3
 

Author
WHU
 

Source
2012 Multi-University Training Contest 9


这题和BZOJ 1799 : http://blog.csdn.net/sinat_35406909/article/details/75808465

题意一样。

只不过,这题由于内存限制比较大,可以直接多一维存储搜索过的状态。

一样的穷举数位和进行DP。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=82,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; int dp[11][maxn][maxn][maxn];int num[20];int dfs(int len,int sum,int mod,int nu,bool HaveLimit) {    if (sum>len*9) return 0;    if (len==0)         return sum==0&&nu==0;    if (dp[len][sum][mod][nu]!=-1&&!HaveLimit)         return dp[len][sum][mod][nu];            int p=HaveLimit?num[len]:9;    int ans=0;    for (int i=0;i<=p;i++) {        ans+=dfs(len-1,sum-i,mod,(nu*10+i)%mod,HaveLimit&&i==num[len]);    }    if (!HaveLimit)         dp[len][sum][mod][nu]=ans;    return ans;}int solve(int n) {    int len=0,i;    int k=n;    while (k) {        num[++len]=k%10;        k/=10;    }    int ans=0;    for (i=1;i<=min(9*len,81);i++) {        ans+=dfs(len,i,i,0,1);    }    return ans;}int main() {    int a,b;    int cas,cnt=0;    memset(dp,-1,sizeof(dp));    scanf("%d",&cas);    while (scanf("%d%d",&a,&b)!=EOF) {        cnt++;        int ans=solve(b)-solve(a-1);        printf("Case %d: %d\n",cnt,ans);    }    return 0;}



原创粉丝点击