您的位置:首页 > 编程语言 > C语言/C++

HDU 3555 Bomb (数位DP)

2015-01-19 10:55 330 查看
跟前面数位DP基本一样的,只不过这里是要求包含49,那么可以利用容斥,求出不包含49的,然后再用总数减去就行了。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=100000000;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
LL dp[21][11], c[21];
LL dfs(int cnt, int pre, int maxd, int zero)
{
if(cnt==-1) return 1;
if(maxd&&zero&&dp[cnt][pre]!=-1) return dp[cnt][pre];
int i, r;
LL ans=0;
r=maxd==0?c[cnt]:9;
for(i=0;i<=r;i++){
if(!zero||!(pre==4&&i==9)){
ans+=dfs(cnt-1,i,maxd||i<r,zero||i);
}
}
if(maxd&&zero) dp[cnt][pre]=ans;
return ans;
}
LL Cal(LL x)
{
int i, cnt=0;
while(x){
c[cnt++]=x%10;
x/=10;
}
return dfs(cnt-1,-1,0,0);
}
int main()
{
int t;
LL n;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
while(t--){
scanf("%I64d",&n);
printf("%I64d\n",n+1-Cal(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息