hdu3555Bomb以及对数位dp的讲解
2015-04-15 20:52
274 查看
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 9295 Accepted Submission(s): 3282
[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]
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.
[align=left]Author[/align]
fatboy_cw@WHU
[align=left]Source[/align]
2010 ACM-ICPC Multi-University Training Contest(12)——Host
by WHU
所谓数位dp就是dp+组合数学
首先是预处理出dp:
dp[i][0]:i位数,不包含49的有几个
dp[i][1]:i位数,不包含49但是以9开头的有几个
dp[i][2]:i位数,包含49的有几个
(i位数包含0开头的情况)
组合数学部分:
算1到x有几个数自己含有49时,
为了方便描述,用一个串s储存x,其中0为起始位置,串长为len,
s[i]到s[len-1]组成一个数字y,算出数字恰有len-i位,且不大于y时,且自己含有49的有多少个
枚举所有i,累加即可
注意:
当49已经在之前出现时,要特殊处理
若x本身含有49,记得算上x本身
y可以是0开头,因为若每次都这样,是不会算多的,相当于每次都把下一次的i算了一些情况
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; typedef long long ll; ll dp[20][3]; ll work(string s) { int len=s.length(); ll ans=0; bool flag=0; for(int i=0;i<len;i++) { if(flag) ans+=(dp[len-i-1][0]+dp[len-i-1][2])*(s[i]-'0'); else { ans+=dp[len-i-1][2]*(s[i]-'0'); if(s[i]>'4') ans+=dp[len-i-1][1]; } if(!flag&&i>0&&s[i-1]=='4'&&s[i]=='9') flag=1; } if(flag) ans++; return ans; } int main() { dp[0][0]=1; for(int i=1;i<19;i++) { dp[i][0]=dp[i-1][0]*10-dp[i-1][1]; dp[i][1]=dp[i-1][0]; dp[i][2]=dp[i-1][2]*10+dp[i-1][1]; } int T; cin>>T; while(T--) { string s; cin>>s; cout<<work(s)<<endl; } }
相关文章推荐
- Android 编程下 DP、SP 以及屏幕像素密度
- 推荐:CLR 完全介绍-一篇讲解CLR内存回收机制以及常见的调试技巧的文章
- 实例讲解iframe之间以及父窗体的函数调用传值
- java验证码识别带源码以及讲解视频
- 【Android游戏开发二十七】讲解游戏开发与项目下的hdpi 、mdpi与ldpi资源文件夹以及游戏高清版本的设置
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- Java内存分配的栈、堆以以及常量池详细的讲解
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- ssh整合以及springMVC讲解
- px,in,mm,pt,dp,dip,sp 之间的换算公式以及区别
- React Native控件之ToolbarAndroid工具栏控件讲解以及使用
- Idea基本讲解、常用配置以及快捷键设置
- 个人对于Dubbo以及基本深入讲解
- 安卓中显示单位px和dip以及sp讲解
- 深入理解dp,px,以及density
- Gradle详情以及代码讲解
- 常见浏览器兼容问题、盒模型2种模式以及css hack知识讲解
- 有关脱壳以及脱壳实例讲解
- Android异步加载网络数据以及数据缓存的讲解、实例
- NOIP2012Day2T1同余方程解题报告以及扩展欧几里得讲解