博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
O-Bomb(数位dp)
阅读量:7236 次
发布时间:2019-06-29

本文共 2530 字,大约阅读时间需要 8 分钟。

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 15062    Accepted Submission(s): 5437

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.
 
 
//题意 t 组数据,在 1- n 中出现了49的数据个数,n 很大,2^63-1 , 遍历肯定不行。
 
//听说这是数位dp的一道水题,这也是水题吗,我服了
自己想了一阵,只想到了一点点。看了别人的也看了很久,才明白,这篇博客非常详细,我就不赘述了,认真看一定能看懂。
我的与他的dp数组有细微差别,因为我自己理解了写了一遍,稍微注意一下。
 
代码 15ms
 
1 #include 
2 #include
3 4 __int64 dp[25][3]; 5 //dp[i][0] 含49,数的个数 6 //dp[i][1] 不含49,但首位是9的数的个数 7 //dp[i][2] 不含49,数的个数(包含了首位是9的数的个数) 8 void Init() 9 {10 dp[0][2]=1;11 for (int i=1;i<=24;i++)12 {13 dp[i][0]=dp[i-1][0]*10+dp[i-1][1];14 dp[i][1]=dp[i-1][2];15 dp[i][2]=dp[i-1][2]*10-dp[i-1][1];16 }17 }18 19 __int64 solve(__int64 n)20 {21 __int64 a[25],len=0;22 23 while (n)24 {25 a[++len]=n%10;26 n/=10;27 }28 a[len+1]=0;29 30 __int64 ans=0;31 int flag=0;32 33 for (int i=len;i>0;i--)34 {35 ans+=dp[i-1][0]*a[i];36 if (flag) //前面有49就所有情况都是49数了37 ans+=dp[i-1][2]*a[i];38 if (!flag&&a[i]>4) //加上4 9... 的情况39 ans+=dp[i-1][1];40 if (a[i]==9&&a[i+1]==4)//判断是不是 49 41 flag=1;42 }43 return ans;44 }45 46 int main()47 {48 int t;49 __int64 n;50 Init();51 scanf("%d",&t);52 while (t--)53 {54 scanf("%I64d",&n);55 printf("%I64d\n",solve(n+1));//因为函数功能是 (0,n) 区间的49数,题目要求的是[1,n],所以+156 }57 return 0;58 }
View Code

 

 

 

转载于:https://www.cnblogs.com/haoabcd2010/p/5753732.html

你可能感兴趣的文章
List<String> 如何用jstl foreach遍历
查看>>
H2 Web Console to In Memory Database – Spring Boot
查看>>
我的友情链接
查看>>
mysql 运维常用命令收录
查看>>
linux第二周作业
查看>>
SSL及OpenSSL使用
查看>>
mybatis批量插入(Oracle)
查看>>
手工玫瑰花折纸
查看>>
python浓缩(5)数字
查看>>
LAMP源码编译安装
查看>>
Linux(RHEL 5)中Bind服务的安装与配置全过程-续
查看>>
git设置对比工具
查看>>
linux解压 tar命令
查看>>
持久层之DAO工厂类
查看>>
PHP str_pad() 函数 初级系列9
查看>>
亲,您的json键值对用双引号了吗?
查看>>
坚持是件幸福的事情
查看>>
ceph学习笔记之四PG
查看>>
awk用法三
查看>>
Sitemap和Robots.txt SEO优化技巧
查看>>