hdu 3555 Bomb(不要49,数位DP)

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 15102    Accepted Submission(s): 5452


Problem Description
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
 
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.
 
Output
For each test case, output an integer indicating the final points of the power.
 
Sample Input
3 1 50 500
 
Sample Output
0 1 15
Hint
From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
 
Author
fatboy_cw@WHU
 
Source
 
/*
这个是自己的代码反着求的,正如“不要49”,跑了65ms
*/
#include<iostream> #include<stdio.h> #include<vector> #include<algorithm> #include<string.h> #include<cstdio> #define N 22 using namespace std; long long t,n; int g[N];//用来存放数字的位数 long long dp[N][N];//dp[i][j][k]表示当前剩余位数是i上一位数字是j且当前位上是否为4的状态的个数 long long dfs(int len,bool s,bool f)//s表示是不是当前位上是不是4 { if(len<1) return 1; if(!f&&dp[len][s]!=-1) return dp[len][s]; int fmax=f?g[len]:9; long long cur=0; //cout<<"fmax="<<fmax<<endl; for(int i=0;i<=fmax;i++) { if(s&&i==9) continue;//不要有49 cur+=dfs(len-1,i==4,f&&(i==fmax)); //cout<<"cur="<<cur<<endl; } //cout<<cur<<endl; if(!f) dp[len][s]=cur; return cur; } long long solve(long long x) { int len=1; while(x!=0) { g[len++]=x%10; x/=10; } //for(int i=1;i<=len;i++) // cout<<g[i]; //cout<<endl; memset(dp,-1,sizeof dp); return dfs(len-1,false,true); } int main() { //freopen("in.txt","r",stdin); scanf("%lld",&t); while(t--) { scanf("%I64d",&n); printf("%I64d ",n-solve(n)+1); } return 0; }
/*
一开始真的按照“不要49”这么来求的。写博客的时候看看别人博客吸收精华,下面是正着求得,z[i]数组真是神来之笔
本来我也行正着求但是不知道怎么表示找到49之后应该加什么,一个z[i]完美解决了找到49之后应该加多少
*/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstdio>
#define N 30
using namespace std;
long long t,n;
int g[N];//用来存放数字的位数
long long dp[N][2];//dp[i][j][k]表示当前剩余位数是i上一位数字是j且当前位上是否为4的状态的个数
long long z[N]={1};
long long dfs(int len,bool s,bool f)//s表示是不是当前位上是不是4
{
    if(len==0)
        return 0;
    if(!f&&dp[len][s]>=0)
        return dp[len][s];
    int fmax=f?g[len]:9;
    long long  cur=0;
    //cout<<"fmax="<<fmax<<endl;
    for(int i=0;i<=fmax;i++)
    {
        if(s&&i==9)
        {
            cur+=f?n%z[len-1]+1:z[len-1];//当前位找到了,剩下的不用搜了,直接加上就行了,这里加的是剩下的所有位
        }    
        else
            cur+=dfs(len-1,i==4,f&&g[len]==i);
        //cout<<"cur="<<cur<<endl;
    }
    //cout<<cur<<endl;
    return f?cur:dp[len][s]=cur;
}
long long solve(long long x)
{
    int len=0;
    while(x)
    {
        g[++len]=x%10;
        x/=10;
    }
    //for(int i=1;i<=len;i++)
    //    cout<<g[i];
    //cout<<endl;
    g[len+1]=0;
    return dfs(len,false,true);
}
int main()
{
    //freopen("in.txt","r",stdin);
    for (int i=1;i<N;i++)
    {
        z[i]=z[i-1]*10;
    }
    scanf("%lld",&t);
    memset(dp,-1,sizeof dp);
    while(t--)
    {
        scanf("%lld",&n);
        printf("%lld
",solve(n));
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5755966.html