HDU 4722 (数位DP 水~)

Good Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3814    Accepted Submission(s): 1213


Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
 

Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
 

Output
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
 

Sample Input
   
   
   
   
2 1 10 1 20
 

Sample Output
   
   
   
   
Case #1: 0 Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
 


求出区间里面每个数位和模10余0的个数.

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define maxn 22
typedef long long ull;

ull a, b;
int bit[maxn], l;
ull dp[maxn][11];

ull dfs (int pos, int m1, bool f2) {
    //当前的位数 之前模10余数 是不是可以取到9
    if (pos == 0) {
        return m1 == 0;
    }
    if (f2 && dp[pos][m1] != -1) {
        return dp[pos][m1];
    }
    ull Max = (f2 ? 9 : bit[pos]);
    ull ans = 0;
    for (int i = 0; i <= Max; i++) {
        ans += dfs (pos-1, (m1+i)%10, f2 || (i<Max));
    }
    if (f2)
        dp[pos][m1] = ans;
    return ans;
}

ull f (ull num) {
    l = 0;
    while (num) {
        bit[++l] = num%10;
        num /= 10;
    }
    return dfs (l, 0, 0);
}

int main () {
    //freopen ("in.txt", "r", stdin);
    int t, kase = 0;
    memset (dp, -1, sizeof dp);
    cin >> t;
    while (t--) {
        cin >> a >> b;
        printf ("Case #%d: %lld\n", ++kase, f (b)-f (a-1));
    }
    return 0;
}


你可能感兴趣的:(HDU 4722 (数位DP 水~))