HDU - 3555 Bomb
2016-07-28 08:52
337 查看
1.题面
http://acm.hdu.edu.cn/showproblem.php?pid=35552.题意
求在[a,b]区间中,有多少数字是含有49这个串的。3.思路
数位dp,注意到没有限制的时候,后面所能表示的数是一样的我一开始使用了一种奇怪的写法,不过也过了
4.代码
通过的代码/*****************************************************************
> File Name: Cpp_Acm.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年07月27日 星期三 19时33分33秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;
template<class T>void PrintArray(T* first,T* last,char delim=' '){
for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}
const int debug = 1;
const int size = 10 + 30;
const int INF = INT_MAX>>1;
typedef long long ll;
int num[size];
int len;
ll dp[30][20][2];
ll dfs(int pos,int pre,int statue,int limit){
if (pos==-1)
return statue;
if (limit!=1&&dp[pos][pre][statue]!=-1)
return dp[pos][pre][statue];
int ub = limit?num[pos]:9;
ll ret = 0;
for (int i=0;i<=ub;i++){
ret += dfs(pos-1,i,statue||(pre==4&&i==9),limit&&i==ub);
}
if (limit==0)
dp[pos][pre][statue] = ret;
return ret;
}
ll solve(ll n){
len = 0;
while (n){
num[len++] = n%10;
n /= 10;
}
return dfs(len-1,-1,0,1);
}
int main(){
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j;
int T;
cin >> T;
memset(dp,-1,sizeof(dp));
while (T--){
ll a;
cin >> a;
cout << solve(a) << endl;
}
return 0;
}
奇怪的写法
/*****************************************************************
> File Name: Cpp_Acm.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年07月27日 星期三 19时33分33秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;
template<class T>void PrintArray(T* first,T* last,char delim=' '){
for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}
const int debug = 1;
const int size = 10 + 30;
const int INF = INT_MAX>>1;
typedef long long ll;
int num[size];
int len;
ll dp[30][20][2][2];
ll dfs(int pos,int pre,int statue,int limit){
if (pos==-1)
return statue;
if (dp[pos][pre][statue][limit]!=-1)
return dp[pos][pre][statue][limit];
int ub = limit?num[pos]:9;
ll ret = 0;
for (int i=0;i<=ub;i++){
ret += dfs(pos-1,i,statue||(pre==4&&i==9),limit&&i==ub);
}
if (dp[pos][pre][statue][limit]==-1)
dp[pos][pre][statue][limit] = ret;
return ret;
}
ll solve(ll n){
memset(dp,-1,sizeof(dp));
len = 0;
while (n){
num[len++] = n%10;
n /= 10;
}
return dfs(len-1,0,0,1);
}
int main(){
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j;
int T;
cin >> T;
while (T--){
ll a;
cin >> a;
cout << solve(a) << endl;
}
return 0;
}
相关文章推荐
- 如何提高php性能1
- Java开发必装的IntelliJ IDEA插件
- SDUT1197约瑟夫问题
- Longest Increasing Subsequence
- 进程控制( 一)
- 【NYOJ 95】众数问题 &&STL map 学习
- SwipeRefreshLayout与ViewPager滑动事件冲突源码分析及解决办法
- GC是如何判断一个对象为"垃圾"的?被GC判断为"垃圾"的对象一定会被回收吗?
- shiro-springmvc-mybatis登录认证 权限控制
- iOS开发从入门到精通--UIGesture手势扩展,平移,滑动,长按
- 立即执行函数表达式(IIFE)
- Zabbix-3.0.x使用OneAlert发送告警
- 串行 Flash 通用驱动库 SFUD (Serial Flash Universal Driver)
- [Cloud Computing]Mechanisms: Virtual Disk
- Unity初识(1)
- 行内元素为何不能设置margin-top、margin-bottom;padding-top、padding-bottom值
- 多个inline元素、block元素、inline-block元素在父容器中的换行情况
- [20160727][文件夹重命名]
- 我的文章:JSP
- [Codeforces 451A] Game With Sticks (博弈)