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

HDU 4389 数位dp

时间:2017-03-22 00:24:23      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:sizeof   scanf   others   output   sub   ant   bug   lib   tom   

X mod f(x)

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


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
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define dazhi 2147483647
12 #define bug() printf("!!!!!!!")
13 #define M 100005
14 using namespace  std;
15 int bit[10];
16 int dp[10][82][82][82];
17 int  n;
18 int  functi(int pos,int mod,int xx ,int sum,bool flag)
19 {
20     if(pos==0) return (xx==sum&&mod%sum==0);
21     if(flag&&dp[pos][mod][xx][sum]!=-1) return dp[pos][mod][xx][sum];
22     ll x=flag ? 9 : bit[pos];
23     ll ans=0;
24     for(ll i=0;i<=x;i++)
25     ans+=functi(pos-1,(mod*10+i)%xx,xx,sum+i,flag||i<x);
26     if(flag)
27         dp[pos][mod][xx][sum]=ans;
28     return ans;
29 }
30 int  fun(int x)
31 {
32     int len=0;
33     while(x)
34     {
35         bit[++len]=x%10;
36         x/=10;
37     }
38     ll re=0;
39     for(int i=1;i<=81;i++)
40         re+=functi(len,0,i,0,0);
41     return re;
42 }
43 int main()
44 {
45    int t;
46    int  l,r;
47    while(scanf("%d",&t)!=EOF)
48    {
49        memset(dp,-1,sizeof(dp));
50        for(int i=1;i<=t;i++)
51        {
52            scanf("%d %d",&l,&r);
53            int exm=fun(r);
54            printf("Case %d: %d\n",i,exm-fun(l-1));
55        }
56    }
57     return 0;
58 }

 

HDU 4389 数位dp

标签:sizeof   scanf   others   output   sub   ant   bug   lib   tom   

原文地址:http://www.cnblogs.com/hsd-/p/6597263.html

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