hdu 3652 B-number 数位DP

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.

InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).OutputPrint each answer in a single line.Sample Input

13
100
200
1000

Sample Output

1
1
2
2

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <string>
11 using namespace std;
12 typedef long long LL;
13 int dp[15][15], n, m, num[10];
14 void init() {
15     dp[0][0] = 1;
16     for (int i = 1 ; i <= 7 ; i++) {
17         for (int j = 0 ; j < 10 ; j++) {
18             for (int k = 0 ; k < 10 ; k++) {
19                 if (j == 4 || (j == 6 && k == 2)) continue;
20                 dp[i][j] += dp[i - 1][k];
21             }
22         }
23     }
24 }
25 int getlen(int x) {
26     int ret = 0;
27     while(x) ret++, x /= 10;
28     return ret;
29 }
30 void getnum(int x, int len) {
31     memset(num, 0, sizeof(num));
32     for (int i = 1 ; i <= len ; i++) {
33         num[i] = x % 10;
34         x /= 10;
35     }
36 }
37 int getans(int x) {
38     int ans = 0, len = getlen(x);
39     getnum(x,len);
40     for (int i = len ; i >= 1 ; i--) {
41         for (int j = 0 ; j < num[i] ; j++) {
42             if (j == 4 || (j == 2 && num[i + 1] == 6) ) continue;
43             ans += dp[i][j];
44         }
45         if (num[i] == 4 || (num[i] == 2 && num[i + 1] == 6)) break;
46     }
47     return ans;
48 }
49 int main() {
50     init();
51     while(scanf("%d%d", &n, &m) != EOF) {
52         if (n == 0 && m == 0) break;
53         printf("%d\n", getans(m + 1) - getans(n));
54     }
55     return 0;
56 }

猜你喜欢

转载自www.cnblogs.com/qldabiaoge/p/9345744.html