您的位置:首页 > 其它

HDU 2089 不要62(数位DP啊)

2015-09-26 00:39 302 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089

Problem Description

杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。

杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。

不吉利的数字为所有含有4或62的号码。例如:

62315 73418 88914

都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。

你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。



Input

输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。



Output

对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。



Sample Input

1 100
0 0




Sample Output

80




Author

qianneng

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
LL dp[25][3];
//dp[i][j]:长度为i,状态为j
int digit[25];
//status: 0:不含62和4, 1:不含62和4但末尾是6, 2 :含62或4
LL DFS(int pos, int status, int limit)
{
    if(pos <= 0)
        return status!=2;
    if(!limit && dp[pos][status]!=-1)
        return dp[pos][status];
    LL ans = 0;
    int End = limit?digit[pos]:9;  // 确定这一位的上限是多少
    for(int i = 0; i <= End; i++)
    {
        int nstatus = status;
        if(i == 4)
            nstatus = 2;
        else if(status==0 && i==6)
            nstatus = 1;
        else if(status==1 && i!=2 && i!=6)
            nstatus = 0;
        else if(status==1 && i==2)
            nstatus = 2;
        ans+=DFS(pos-1, nstatus, limit && i==End);
    }
    if(!limit)
        dp[pos][status] = ans;
    return ans;
}

int cal(LL x)
{
    int cnt = 0;
    while(x)
    {
        digit[++cnt] = x%10;
        x/=10;
    }
    digit[cnt+1] = 0;
    return cnt;
}

int main()
{
    LL n, m;
    memset(dp,-1,sizeof(dp));
    while(~scanf("%I64d%I64d",&n,&m))
    {
        if(n==0 && m==0)
            break;
        int lenn = cal(n-1);
        LL ansn = DFS(lenn, 0, 1);

        int lenm = cal(m);
        LL ansm = DFS(lenm, 0, 1);
        //printf("%I64d %I64d\n",ansn,ansm);
        printf("%I64d\n",ansm-ansn);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: