hdu3555——Bomb

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.

数位dp入门题,挺神奇的,不过还没学到要点
代码里面最后注释的那个判断不知道为什么不能放进else里面,wa了 求路过的大佬解答一下

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int dig[25];
//dp[i][0]表示i位数不含49的个数
//dp[i][1]表示i位数不含49且最高位为9的个数
//dp[i][2]表示i位数含有49的个数
long long dp[25][3];
long long n;
void init(){
    //虽然不知道这里为什么要1
    dp[0][0]=1;
    for(int i=1;i<=20;i++){
        //i位数不含49的个数就等于i-1位数不含49的个数*10(因为第i位可以放0-9)
        //再减去i-1位数不含49但最高位为9的情况,(这种情况第i位加个4即可)
        dp[i][0]=dp[i-1][0]*10-dp[i-1][1];
        //i位数不含49且最高位为9的个数就等于i-1位不含49的在第i位加一个9
        dp[i][1]=dp[i-1][0];
        //i位数含49的个数就等于i-1位数含49的个数*10(因为第i位可以放0-9)
        //再加上i-1位数不含49但最高位为9的情况,(这种情况第i位加个4即可)
        //与第一种情况类似
        dp[i][2]=dp[i-1][2]*10+dp[i-1][1];
    }
}
long long solve(long long num){
    memset(dig,0,sizeof(dig));
    int k=0;
    while(num){
        dig[++k]=num%10;
        num/=10;
    }
    //dig[k+1]=0;
    long long ans=0;
    //标志是否第i位和第i+1是否是49
    //也就是dig[i]==9 dig[i+1]==4
    bool flag=false;
    //每一位的dp加起来
    for(int i=k;i>=1;i--){
        //后面i-1位已经包含49,第i位可以有dig[i]种选择
        ans+=dp[i-1][2]*dig[i];
        if(flag){
            //第i位和第i+1是49,那么i-1位那些不含49的也算
            ans+=dp[i-1][0]*dig[i];
        }
        else{
            //第i位大于4,那么就可以选4,后面选第i-1位是9的
            if(dig[i]>4){
                ans+=dp[i-1][1];
            }

        }
        //这个判断如果放在上面else里就会WA ???
        //因为这个标志
        //第i位和前面第i+1位可以凑成49,标记
        if(dig[i]==9 && dig[i+1]==4){
            flag=true;
        }
    }
    return ans;
}
int main(void){
    int t;
    long long n;
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&n);
        printf("%lld\n",solve(n+1));
    }
    return 0;
}
全部评论

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
01-12 09:24
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务