HDU 3555 Bomb (简单数位DP)
2015-07-22 15:47
429 查看
同上,dp[i][0] 不存在,dp[i][1] 不存在,且上一位为4,dp[i][2] 存在
代码:
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef __int64 LL; LL dp[20][3],l,r; LL bit[20],top; int get_news(int s,int d){ if(s == 0){ if(d == 4) return 1; return 0; } if(s == 1){ if(d == 9) return 2; if(d == 4) return 1; return 0; } return 2; } LL dfs(int i,int s,bool e){ if(i == -1) return s == 2 ? 1 : 0; if(!e&&dp[i][s] != -1) return dp[i][s]; LL res = 0; int d,u = e ? bit[i] : 9; for(d = 0 ; d <= u ; d++){ res += dfs(i-1,get_news(s,d),e&&(d==u)); } return e?res:dp[i][s]=res; } LL solve(LL n){ top = 0; for(;n;n/=10) bit[top++] = n%10; return dfs(top-1,0,1); } int main(){ int cas; scanf("%d",&cas); memset(dp,-1,sizeof(dp)); while(cas--){ scanf("%I64d",&r); printf("%I64d\n",solve(r)); } return 0; }
相关文章推荐
- Extjs4 源码分析系列一 类的创建过程
- AICL(Auto Input Current Limited)
- QListWidget实现图片缩略图形式的列表
- 一般题模板
- python入门
- ibatis iterate 使用 批量插入
- Onblur事件里面写Focus()方法,JS没效果的处理方法
- Debug program crash with dump file.
- 面向对象
- Excel时间处理
- LeetCode -- Course Schedule
- Android工程架构的总结
- Cocos2d打包安卓APK
- 微信卡券开发错误自排查参考文档
- ArcGis ToolBar为灰色
- 进制之间的转换
- 什么是存储保护?
- GRE写作必备句型
- C语言的inline
- 规则引擎需求(捕捉业务规则需求,将需求转换为规则引擎)