不要62(HDU-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

————————————————————————————————————————————————————

思路:數位DP入門題。

該題實質是求數位上不能有4也不能有連續的62,在枚舉的時候判斷是否有4,對於62的話,涉及到兩位,當前一位是6或者不是6的兩種不同情況計數是不同的,因此要用狀態來記錄不同的方案數。

用 dp[pos][sta] 表示第 pos 位,前一位是否是 6 的狀態,這裏的 sta 只需要 0、1 兩種狀態即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 201
#define MOD 10007
#define E 1e-6
typedef long long LL;
using namespace std;
int a[20];
int dp[20][2];
int dfs(int pos,int pre,int sta,bool limit)
{
    if(pos==-1)
        return 1;
    if(!limit && dp[pos][sta]!=-1)
        return dp[pos][sta];

    int up=limit?a[pos]:9;
    int temp=0;
    for(int i=0;i<=up;i++)
    {
        if(pre==6&&i==2)
            continue;
        if(i==4)
            continue;
        temp+=dfs(pos-1,i,i==6,limit&&i==a[pos]);
    }

    if(!limit)
        dp[pos][sta]=temp;
    return temp;
}
int solve(int x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,-1,0,true);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
    {
        memset(dp,-1,sizeof(dp));
        printf("%d\n",solve(m)-solve(n-1));
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章