您的位置:首页 > 其它

hdu2089 不要62 数位DP模板题

2014-08-08 21:57 357 查看
算是学习数位dp的入门题。。。

具体看代码中注释
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define maxn 30
using namespace std;
int limit[40];//存储数字的各位值
int dp[40][10];
int dfs(int now,int prev,bool flag)
{
    if(now<=0) return 1;
    if(!flag&&dp[now][prev]!=-1) return dp[now][prev]; //记忆化搜索
    int bound=flag?limit[now]:9,ret=0;       //如果此时为边界,则bound最大值为limit[now]
    for(int k=0;k<=bound;k++)
    {
        if(k==4) continue;
        if(k==2&&prev==1) continue;
        ret+=dfs(now-1,k==6,flag&&(k==bound));
    }
    if(!flag) dp[now][prev]=ret;
    return ret;
}
int count(int x)
{
    int len=0;
    while(x)
    {
        limit[++len]=x%10;//将x的各位用limit数组保存
        x/=10;
    }
    return dfs(len,0,true);
}
int main()
{
    int n,m;
    memset(dp,-1,sizeof(dp));
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        cout<<count(m)-count(n-1)<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: