http://acm.hdu.edu.cn/showproblem.php?pid=4389
简直有毒……
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int read()
{
char c;int sign = 1, n = 0; c = getchar();
while(c > '9' || c < '0'){ if(c == '-')sign = -1; c = getchar(); }
while(c <= '9' && c >= '0'){ n = n*10 + c-'0'; c = getchar();}
return n * sign;
}
int T, bit[20];
int f[11][82][82][82]; // pos std mod sum
int dfs(int pos, int std, int mod, int sum, bool flag)
{
if (! pos) return (std == sum && (! mod));
if (! flag && ~f[pos][std][mod][sum]) return f[pos][std][mod][sum];
int ans = 0;
int d = flag ? bit[pos] : 9;
for (int next = 0; next <= d; ++ next) {
int next_mod = (mod * 10 + next) % std;
int next_sum = sum + next;
ans += dfs(pos - 1, std, next_mod, next_sum, flag && d == next);
}
if (! flag) f[pos][std][mod][sum] = ans;
return ans;
}
int solve(int n)
{
int ans = 0, len = 0;
for (; n; ) {
bit[++ len] = n % 10;
n /= 10;
}
for (int i = 1; i <= 81; ++ i)
ans += dfs(len, i, 0, 0, 1);
return ans;
}
int main()
{
cin >> T;
for (int i = 1; i <= T; ++ i) {
int L = read(), R = read();
int res = solve(R) - solve(L - 1);
printf("Case %d: %d\n", i, res);
}
return 0;
}