不要62 (数位dp)

杭州人称那些傻乎乎粘嗒嗒的人为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

数位dp讲解:不要62

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#define MAX 0x3f3f3f3f
using namespace std;
typedef long long LL;
LL dp[20][20];
int a[20];
LL slove(int n)
{
    int len=0;
    while(n)
    {
        a[++len]=n%10;
        n/=10;
    }
    a[len+1]=0;
    LL ans=0;
    for(int i=len;i>=1;i--)
    {
        for(int j=0;j<a[i];j++)
        {
            if(j==4||a[i+1]==6&&j==2)
               continue;
            else
                ans+=dp[i][j];

        }
        if(a[i]==4)
            break;
        if(a[i+1]==6&&a[i]==2)
           break;

    }
    return ans;
}
int main()
{
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=1;i<=7;i++)
    {
        for(int j=0;j<=9;j++)
        {
            if(j==4)
                dp[i][j]=0;
            else if(j==6)
            {
                for(int k=0;k<=9;k++)
                {
                    if(k!=2)
                        dp[i][j]+=dp[i-1][k];

                }
            }
            else
            {
                for(int k=0;k<=9;k++)
                    dp[i][j]+=dp[i-1][k];
            }
        }
    }
    int n,m;
    while(cin >> m >> n&&(n||m))
    {
        LL ans1=slove(m);
        LL ans2=slove(n+1);
        cout << ans2-ans1<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/M_Y_Y_/article/details/81487949
今日推荐