1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| #include <stdio.h>
int n,m,d,dp[13][13],sum[13],dn[13],dm[13];
// DFS的时候这两个地方根据不同要求写。
int dfs(int da[], int dep, int all)
{
int i,j,ret=0;
if (dep == 0) return 1;
for (i=0;i<da[dep];i++)
{
if (all > 0 || i > 0) {
if (all == 0 || i-da[dep+1]>=d || i-da[dep+1]<=-d)
ret += dp[dep][i];
} else
ret += sum[dep-1];
}
if (all == 0 || da[dep]-da[dep+1]>=d || da[dep]-da[dep+1]<=-d)
ret += dfs(da, dep-1, all+da[dep]);
return ret;
}
int main()
{
int i,j,k,l,T;
scanf("%d", &T);
while (T--)
{
scanf("%d %d %d", &m, &n, &d);
for (i=0;i<13;i++)
for (j=0;j<13;j++) dp[i][j] = 0;
sum[0] = 0; sum[1] = 9;
for (i=0;i<10;i++) dp[1][i] = 1;
for (i=2;i<13;i++) {
sum[i] = sum[i-1];
for (j=0;j<10;j++) {
for (k=0;k<10;k++)
if (j-k>=d || j-k<=-d)
dp[i][j] += dp[i-1][k];
if (j > 0)
sum[i] += dp[i][j];
}
}
// for (i=0;i<=2;i++)
// for (j=0;j<10;j++) printf("%d %d %d\n", i, j, dp[i][j]);
i = 1; k = n;
while (i < 13) {
dn[i] = k % 10; k /= 10;
i++;
}
i = 1; k = m-1;
while (i < 13) {
dm[i] = k % 10; k /= 10;
i++;
}
n = dfs(dn, 11, 0);
if (m == 1)
m = 0;
else
m = dfs(dm, 11, 0);
printf("%d\n", n-m);
}
return 0;
}
|