您的位置:首页 > 其它

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