HDU 4389 X mod f(x)
X mod f(x)
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 438964-bit integer IO format: %I64d Java class name: Main
Here is a function f(x):
1 int f(int x) { 2 if(x == 0) return 0; 3 return f(x/10) + x%10; 4 }
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.
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
Source
解题:数位dp,我们可以枚举数字的各位和进行dp,如果数字和是预定的那么多,并且这个数字模其等于0,那么这个数字正是我们要找的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 int b[20],dp[20][90][90],ans[60];//长度,余数,位和 4 int dfs(int len,int mod,int left,int sum,bool flag) { 5 if(!len) return left == 0 && sum == mod; 6 if(flag && dp[len][left][sum] != -1) return dp[len][left][sum]; 7 int u = flag?9:b[len],ret = 0; 8 for(int i = 0; i <= u; ++i) 9 ret += dfs(len-1,mod,(left*10 + i)%mod,sum + i,flag||i < u); 10 if(flag) dp[len][left][sum] = ret; 11 return ret; 12 } 13 int solve(int x,int cur) { 14 int len = 0; 15 while(x) { 16 b[++len] = x%10; 17 x /= 10; 18 } 19 return dfs(len,cur,0,0,false); 20 } 21 int main() { 22 int kase,cs,x[60],y[60]; 23 scanf("%d",&kase); 24 for(cs = 0; cs < kase; ++cs) 25 scanf("%d%d",x + cs,y + cs); 26 for(int i = 1; i <= 81; ++i) { 27 memset(dp,-1,sizeof dp); 28 for(int j = 0; j < kase; ++j) 29 ans[j] += solve(y[j],i) - solve(x[j]-1,i); 30 } 31 for(cs = 0; cs < kase; ++cs) 32 printf("Case %d: %d\n", cs + 1, ans[cs]); 33 return 0; 34 }
夜空中最亮的星,照亮我前行