• hdu3555(数位DP dfs/递推)


    Bomb

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


    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
     
    Recommend
    zhouzeyong
     
    采用dfs记忆化搜索暴力枚举,dp[len][0]代表长度为len,不含49,首位可以是9的数的个数,dp[len][1]代表长度为len,不含49,首位不是9的数的数的个数,
    这样每次枚举当前位置,
    a:如果当前位置是4,那么下一位不能是9,所以dfs(len,1,false/true);
    b:如果当前位置不是4,那么下一位没有限制,所以dfs(len,0,false/true);
    c:如果包含当前位置的前缀与n这个数相同位置的前缀相等的话,那么下一位的枚举就有限制,所以dfs(len,0/1,true);
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    #define LL  long long
    LL dp[30][2];
    int digit[30];
    
    LL dfs(int len,bool state,bool fp)
    {
        if(!len)
            return 1;
        if(!fp && dp[len][state] != -1)
            return dp[len][state];
        LL ret = 0 ;int  fpmax = fp ? digit[len] : 9;
        for(int i=0;i<=fpmax;i++)
        {
            if(state && i == 9)
                continue;
            ret += dfs(len-1,i == 4,fp && i == fpmax);  
            //第二个参数表明这位是否为4,如果为4,下一位就不能为9,否则没有限制
            //第三个参数表明前缀是否相同,如果相同,下一位就只能枚举到最大值,否则就没有限制
        }
        if(!fp)
            dp[len][state] = ret;
        return ret; //ret代表0到n不含49的个数
    }
    
    LL f(LL n)
    {
        int len = 0;
        while(n)
        {
            digit[++len] = n % 10;
            n /= 10;
        }
        return dfs(len,false,true);
    }
    
    int main()
    {
        //freopen("test.txt","r",stdin);
        LL a,b;
        memset(dp,-1,sizeof(dp));
        int t;
        scanf("%d",&t);
        while(t--)
        {
              cin>>b;
              cout<<(b+1-f(b))<<endl;
        }
    
        return 0;
    }
     
  • 相关阅读:
    OSVERSIONINFOEX structure
    VS系列开发工具发展概述
    VS2008与QT4.6集成
    windows nt service 框架
    Rair
    如何在进程之间共享内核对象
    GOOGLE
    如何获取错误消息说明使用 FormatMessage API
    EnableDebugPriv;
    汇编语言资料
  • 原文地址:https://www.cnblogs.com/xianbin7/p/4718050.html
Copyright © 2020-2023  润新知