HDU3555 Bomb(数位dp)
2016-01-26 18:52
363 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output an integer indicating the final points of the power.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
和HDU2089一样的思想,我发现网上的题解大多是
dp[i][0]表示不含有49
dp[i][1]表示不含有49,且最高位为9
dp[i][2]表示含有49
这样的方法做的,改天试一下吧。
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output an integer indicating the final points of the power.
[align=left]Sample Input[/align]
3 1 50 500
[align=left]Sample Output[/align]
0 1 15 HintFrom 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.
和HDU2089一样的思想,我发现网上的题解大多是
dp[i][0]表示不含有49
dp[i][1]表示不含有49,且最高位为9
dp[i][2]表示含有49
这样的方法做的,改天试一下吧。
#include<iostream> #include<cstdio> using namespace std; long long dp[20][10]; void Init() { dp[0][0]=1; for(int i=1;i<=20;i++) for(int j=0;j<=9;j++)//枚举第i位 for(int k=0;k<=9;k++)//枚举第i-1位 if(!(j==4&&k==9)) dp[i][j]+=dp[i-1][k]; } long long solve(long long n) { long long ans=0; int digit[20];//表示n从右到左第i位的数字 int len=0; while(n>0) { digit[++len]=n%10; n/=10; } digit[len+1]=0; for(int i=len;i>=1;i--)//枚举第i位 { for(int j=0;j<digit[i];j++)//枚举第i位的数字 if(!(digit[i+1]==4&&j==9)) ans+=dp[i][j]; if((digit[i+1]==4&&digit[i]==9)) break; } return ans; } int main(void) { int t; long long n; Init(); scanf("%d",&t); while(t--) { scanf("%lld",&n); printf("%lld\n",n+1-solve(n+1)); } return 0; }
相关文章推荐
- uva 484 - The Department of Redundancy Department
- android View事件传递
- Python中有效的字符串合并方法
- AFNetworking 支持cookies的解决方案
- 怎么解决tomcat占用8080端口问题图文教程
- 使用SQLiteOpenHelper对数据库进行版本管理
- ubuntu下JAVA开发环境搭建及实例
- C3P0官方对于MySQL8小时问题的解决方案
- poj-1195-Mobile phones(二维树状数组)
- vs2008 winform工程打包记录
- ROS_OpenCV_socket客户端发送kinect图像及接受图像
- MR-1.MapReduce概述
- 利用机器学习进行恶意代码分类
- R语言概述
- 【Linux调试经验】编译时sscanf参数警告不可忽略
- Java:使用synchronized和Lock对象获取对象锁
- 关于highChart图形中,环形图像,文字居中的处理
- 用xerces-c来进行xml schema校验
- 对openCV中image.copyTo()的一点理解
- java的参数是值传递而非引用传递