HDU 3555 Bomb

题意:将含有连续的"49"的数称为不吉利数,每给出一个n,[1, n]中不吉利数的个数。n <= 2^63-1

解法:数位DP入门题,类似于HDU 2089,具体解法看那道题的题解吧,HDU 2089 不要62

tag:数位DP, 基础题。

 1 /*
 2  * Author:  Plumrain
 3  * Created Time:  2013-12-14 15:05
 4  * File Name: DP-HDU-3555.cpp
 5  */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 
10 using namespace std;
11 
12 #define CLR(x) memset(x, 0, sizeof(x))
13 typedef long long int64;
14 int64 d[25][3], dit[20];
15 
16 int64 gao(int64 x)
17 {
18     int64 len = 0, ret = 0;
19     bool xxx = 0;
20     CLR (dit);
21     while (x){
22         dit[len++] = x % 10;
23         x /= 10;
24         if (len > 1 && dit[len-1] == 4 && dit[len-2] == 9)
25             xxx = 1;
26     }
27     if (xxx) ++ ret;
28     dit[len] = 0;
29 
30     bool flag = 0;
31     for (int i = len-1; i >= 0; -- i){
32         ret += dit[i] * d[i][2];
33         if (flag) ret += dit[i] * (d[i][0] + d[i][1]);
34         else if (dit[i] > 4) ret += d[i][1];
35 
36         if (dit[i+1] == 4 && dit[i] == 9) flag = 1;
37     }
38     return ret;
39 }
40 
41 int main()
42 {
43     CLR (d);
44     d[0][0] = 1;
45     for (int i = 1; i < 20; ++ i){
46         d[i][0] = 9 * (d[i-1][0] + d[i-1][1]) - d[i-1][1];
47         d[i][1] = d[i-1][0] + d[i-1][1];
48         d[i][2] = 10 * d[i-1][2] + d[i-1][1];
49     }
50     
51     int T;
52     int64 n;
53     scanf ("%d", &T);
54     while (T--){
55         cin >> n;
56         cout << gao(n) << endl;
57     }
58     return 0;
59 }
View Code

 

posted @ 2013-12-14 15:34  Plumrain  阅读(266)  评论(0编辑  收藏  举报