您的位置:首页 > 其它

hdu 3709+hdu 3555(数位dp)

2013-04-07 20:50 246 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709

数位dp...完全看大牛模版理解的。。。。

View Code

#include<iostream>
#include<cstring>
using namespace std;
__int64 dp[20][20];
int digit[20];

__int64 DFS(int pos,int have,bool doing){
if(pos==-1){
return have;
}
if(!doing&&dp[pos][have]!=-1){
return dp[pos][have];
}
__int64 ans=0;
int end=doing?digit[pos]:9;
for(int i=0;i<=end;i++){
int nhave=have;
if(i==1)nhave=have+1;
ans+=DFS(pos-1,nhave,doing&&i==end);
}
if(!doing){
dp[pos][have]=ans;
}
return ans;
}

__int64 solve(__int64 n){
int pos=0;
while(n){
digit[pos++]=n%10;
n/=10;
}
return DFS(pos-1,0,1);
}

int main(){
__int64 n,m;
memset(dp,-1,sizeof(dp));
while(~scanf("%I64d%I64d",&n,&m)){
printf("%I64d\n",solve(m)-solve(n-1));
}
return 0;
}


PS:记忆化搜索真的很强大啊。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: