您的位置:首页 > 其它

HDU 2089 不要62 (简单数位DP)

2015-07-22 15:42 393 查看
dp[i][0] 不存在

dp[i][1]不存在,上一位为6

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 2;
if(d == 6) return 1;
return 0;
}
if(s == 1){
if(d == 2 || d == 4) return 2;
if(d == 6) 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(){
memset(dp,-1,sizeof(dp));
while(scanf("%I64d%I64d",&l,&r),l+r){
printf("%I64d\n",(r - l + 1)-solve(r)+solve(l-1));
}

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