Bomb HDU-3555 how many 49, digital DP, winter vacation training

Counter-Strike found a time bomb in the dust. But this time the terrorists improved the time bomb. The numbering sequence of time bombs is counted from 1 to N. If the current numbering sequence includes (substring!!) "49", the explosive power will increase by one point.
Asked how powerful it was when it exploded.

Input description
Input T represents group T (T <= 1e4)
Each group input one n (1 <= n<= 2^ 63-1)
Output description
The power of output explosion
Sample input
1
1
sample output
0 number in
interval How many 49, digital dp boards appeared in between

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 2e6+5;
const int INF = 0x3f3f3f3f;
ll dp[50][15];
ll a[30];
// void init(){
    
    
//     dp[0][0]=1;
//     dp[0][1]=dp[0][2]=0;
//     for(int i=1;i<25;i++){
    
    
//         dp[i][0]=10*dp[i-1][0]-dp[i-1][1];//长度为i不含有49的个数
//         dp[i][1]=dp[i-1][0];//最高位为9但不含49的个数
//         dp[i][2]=10*dp[i-1][2]+dp[i-1][1];//含有49的个数
//     }
// }
ll dfs(ll pos,ll pre,bool limit){
    
    
    if(pos==-1) return 1;
    if(!limit&&dp[pos][pre]!=-1)
        return dp[pos][pre];
    ll up;
    if(limit)
        up=a[pos];
    else up=9;
    ll ans=0;
    for(int i=0;i<=up;i++){
    
    
        if(pre==4&&i==9)
            continue;
        else
            ans+=dfs(pos-1,i,limit&&i==a[pos]);
    }
    if(!limit)  
        dp[pos][pre]=ans;
    return ans;
}
void solve(){
    
    
    int t;  
    cin>>t;
    while(t--){
    
     
        ll x,y;
        cin>>x;
        y=x;
        memset(dp,-1,sizeof(dp));
        ll len=0;
        while(x){
    
    
            a[len++]=x%10;
            x/=10;
        }
        ll sum=dfs(len-1,0,1);
        ll res=y-sum+1;
        cout<<res<<endl;
    }
}
int main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/113000816