HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

时间:2023-03-09 19:41:58
HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

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.

题目大意:求a~b中,位上所有数字加起来能整除10的数有多少个。

思路:位数DP。dp[i][j][k]表示第i位小于j,除以10余k的数的个数,j=10只是为了i+1的j=1准备的……第一次写这个写得有点挫啊……

PS:有数学方法但是我不会,我只知道接近于对于0~n接近于n/10……

代码(109MS):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ; LL dp[MAXN][MAXN][MAXN];
//i位,字头小于j,和为k的数一共有多少个
LL a, b;
int T; void init() {
dp[][][] = ;
for(int i = ; i <= ; ++i) {
for(int k = ; k <= ; ++k) dp[i][][k] = dp[i - ][][k];
for(int j = ; j <= ; ++j) {
for(int k = ; k <= ; ++k)
dp[i][j][k] = dp[i][][(k + - (j - )) % ] + dp[i][j - ][k];
}
}
} int t[MAXN]; LL calculate(LL n) {
int cnt = ;
while(n) t[++cnt] = n % , n /= ;
int sum = ;
LL ret = ;
for(int i = cnt; i > ; --i) {
ret += dp[i][t[i]][( - sum) % ];
sum = (sum + t[i]) % ;
}
return ret;
} int main() {
ios::sync_with_stdio(false);
cin>>T;
init();
for(int t = ; t <= T; ++t) {
cin>>a>>b;
cout<<"Case #"<<t<<": "<<calculate(b + ) - calculate(a)<<endl;
}
}