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;
}
全部评论

相关推荐

Tom哥981:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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