hdu 3555 Bomb (数位dp入门题)

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

/* ***********************************************
Author :111qqz
Created Time :2016年03月15日 星期二 19时32分24秒
File Name :code/hdu/3555.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6LL n;
 7LL digit[30];
 8LL dp[30][2];
 9LL dfs ( int pos,bool preis4,bool limit)
10{
11    if (pos==0) return 1;
12    if (!limit&&dp[pos][preis4]!=-1) return dp[pos][preis4];
1    LL mx = limit?digit[pos]:9;
2    LL res  = 0LL ;
3    for ( LL i = 0 ; i <= mx ; i++)
4    {
5	if (preis4&&i==9) continue;
6	res += dfs(pos-1,i==4,limit&&i==mx);
7    }
    if (!limit) dp[pos][preis4] = res;
    return res;
 1}
 2LL solve ( LL n)
 3{
 4    int len = 0 ;
 5    ms(digit,0);
 6    while (n)
 7    {
 8	digit[++len] = n ;
 9	n /= 10;
10    }
1    return dfs(len,false,true);
2}
3int main()
4{
5	#ifndef  ONLINE_JUDGE 
6	freopen("code/in.txt","r",stdin);
7  #endif
 1	ios::sync_with_stdio(false);
 2	int T;
 3	cin>>T;
 4	while (T--)
 5	{
 6	    cin>>n;
 7	    ms(dp,-1);
 8	    LL ans = solve (n);
 9	    ans = n - ans + 1;
10	    cout<<ans<<endl;
11	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}