1. 程式人生 > >HDU 2089 不要62【數位DP入門題】

HDU 2089 不要62【數位DP入門題】

pan eps ava con 所有 數據 strong mon sub

不要62

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 56193 Accepted Submission(s): 21755

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

#include<cstdio>
#include<string
> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include
<list> #include<assert.h> #include<bitset> #include<numeric> #define debug() puts("++++") #define gcd(a,b) __gcd(a,b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a,b,sizeof(a)) #define sz size() #define be begin() #define pu push_up #define pd push_down #define cl clear() #define lowbit(x) -x&x #define all 1,n,1 #define rep(i,x,n) for(int i=(x); i<=(n); i++) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e18; const int maxm = 1e6 + 10; const double PI = acos(-1.0); const double eps = 1e-8; const int dx[] = {-1,1,0,0,1,1,-1,-1}; const int dy[] = {0,0,1,-1,1,-1,1,-1}; int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int mod = 10056; #define inf 0x3f3f3f3f #define ll long long const int maxn = 150; int dp[20][12]; int a[20]; 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 cnt=0; for(int i=0;i<=up;i++) { if(pre==6 && i==2) continue; if(i==4) continue; cnt += dfs(pos-1,i,i==6,limit&&i==a[pos]); } if(!limit) dp[pos][sta]=cnt; return cnt; } int solve(int x) { int pos=0; //記錄有多少個數位 while(x) { a[pos++]=x%10; x/=10; } return dfs(pos-1,-1,0,true); //從最高位開始枚舉,pre位是-1,sta是0,limit是true //dfs(int pos,int pre,int sta,bool limit) } int main() { int t; int n,m; while(~scanf("%d%d",&n,&m),n+m) { ms(dp,-1); printf("%d\n",solve(m)-solve(n-1)); } } /* 【題意】 數位上不能有4也不能有連續的62.給定區間共有多少合法的數。 【類型】 數位DP入門 【分析】 沒有4的話在枚舉的時候判斷一下,不枚舉4就可以保證狀態合法了 所以這個約束沒有記憶化的必要,而對於62的話涉及到兩位, 當前一位是6或者不是6這兩種不同情況我計數是不相同的, 所以要用狀態來記錄不同的方案數。 dp[pos][sta]表示當前第pos位,前一位是否是6的狀態, 這裏sta只需要去0和1兩種狀態就可以了, 不是6的情況可視為同種,不會影響計數。 【時間復雜度&&優化】 【trick】 【數據】 */

HDU 2089 不要62【數位DP入門題】