Chain: http://acm.hdu.edu.cn/showproblem.php?pid=4389
x mod f (x)Time
limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2330 Accepted Submission (s): 919
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 had an integer t (1 <= t <=), indicate the number of test cases.
Each test case has both 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
Digital DP Learning Link: http://blog.csdn.net/cyendra/article/details/38087573
Test instructions: Equal to the number of questions within the interval to meet X (∑XI) ==0. ∑xi is the digit and of the number x.
Procedure: Because the maximum number of 9 digits, you can enumerate ∑xi, up to 81. Then there's the digital DP.
Sum is the number of digits and, nwmod is the modulo result, MoD is the modulo of the enumeration
When the digits and sum==mod and, Nwmod finally ==0, the count is set.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> typedef long LONG ll;const int maxn=81;int dig[maxn];int f[ 10][maxn][maxn][maxn];//nwmod number modulo sum digits and ll dfs (int pos,int nwmod,int sum,int mod,int limit) {if (pos<0) return sum==m Od&&nwmod==0;if (!limit&&f[pos][nwmod][sum][mod]!=-1) return f[pos][nwmod][sum][mod]; LL res=0;int last=limit?dig[pos]:9;for (int i=0;i<=last;i++) {Res+=dfs (pos-1, (nwmod*10+i)%mod,sum+i,mod,limit && (I==last));} if (!limit) F[pos][nwmod][sum][mod]=res;return Res;} ll solve (ll n) {int len=0; while (n) {dig[len++]=n%10;n/=10;} LL ans=0;for (int i=1;i<=81;i++)//Enumerate the last Mod{ans+=dfs (len-1,0,0,i,1);} Return ans;} int main () {int n;int t;int cas=1;scanf ("%d", &t), int a,b;memset (f,-1,sizeof f), while (t--) {scanf ("%d%d", &a, &B); if (a>b) swap (A, b); printf ("Case%d:%i64d\n", Cas++,solve (b)-solve (A-1));} return 0; }/*21 1011 Sample outputcase 1:10case 2:3 * *
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4389 X mod f (x) digital DP