您的位置:首页 > 其它

HDU - 3555 Bomb

2016-07-28 08:52 337 查看

1.题面

http://acm.hdu.edu.cn/showproblem.php?pid=3555

2.题意

求在[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: