hdu 3555 Bomb (数位dp入门题)

题目链接 题意:问从1到n的所有数中,有多少个数含有数字串“49” 思路:和上一道不要62很像,但是由于是要统计有49的,但是有49的情况实在太多了,正难则反,用减法定理反过来考虑,先统计出不含49的数的个数,这样就和不要62一样了,然后再用总数减。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月15日 星期二 19时32分24秒
 4File Name :code/hdu/3555.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL n;
34LL digit[30];
35LL dp[30][2];
36LL dfs ( int pos,bool preis4,bool limit)
37{
38    if (pos==0) return 1;
39    if (!limit&&dp[pos][preis4]!=-1) return dp[pos][preis4];
40
41    LL mx = limit?digit[pos]:9;
42    LL res  = 0LL ;
43    for ( LL i = 0 ; i <= mx ; i++)
44    {
45	if (preis4&&i==9) continue;
46	res += dfs(pos-1,i==4,limit&&i==mx);
47    }
48
49    if (!limit) dp[pos][preis4] = res;
50    return res;
51
52}
53LL solve ( LL n)
54{
55    int len = 0 ;
56    ms(digit,0);
57    while (n)
58    {
59	digit[++len] = n ;
60	n /= 10;
61    }
62
63    return dfs(len,false,true);
64}
65int main()
66{
67	#ifndef  ONLINE_JUDGE 
68	freopen("code/in.txt","r",stdin);
69  #endif
70
71	ios::sync_with_stdio(false);
72	int T;
73	cin>>T;
74	while (T--)
75	{
76	    cin>>n;
77	    ms(dp,-1);
78	    LL ans = solve (n);
79	    ans = n - ans + 1;
80	    cout<<ans<<endl;
81	}
82
83  #ifndef ONLINE_JUDGE  
84  fclose(stdin);
85  #endif
86    return 0;
87}