不要62(HDU-2089)
生活随笔
收集整理的這篇文章主要介紹了
不要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; }?
總結
以上是生活随笔為你收集整理的不要62(HDU-2089)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网线主管(信息学奥赛一本通-T1242)
- 下一篇: Summarize to the Pow