hdu-2089-digital-dp

don't 62

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


Problem Description
Hangzhou people call those goofy and sticky people 62 (sound: laoer).
The Hangzhou Traffic Administration often expands some taxi license plates. There is good news recently. In the future, the license plate will no longer contain unlucky numbers. In this way, the psychological barriers of individual taxi drivers and passengers can be eliminated, and more Safely serve the public.
Unlucky numbers are all numbers containing 4 or 62. For example:
62315 73418 88914
are all unlucky numbers. However, although 61152 contains 6 and 2, it is not a 62 consecutive number, so it does not belong to the list of unlucky numbers.
Your task is to deduce how many new taxis are actually licensed by the traffic authority this time for each license plate interval number given.
 

 

Input
The input is all integer pairs n and m (0<n≤m<1000000). If an integer pair of 0 is encountered, the input ends.
 

 

Output
For each pair of integers, output a statistic that does not contain unlucky numbers on a single line.
 

 

Sample Input
1 100 0 0
 

 

Sample Output
80
 

 

Author
qianneng
 

 

Source
 
     f[i][j] represents the number of i-digit legal numbers starting with j. This array can be preprocessed first, f[i][j]=SUM{ f[i-1][k] | (j !=4)&&(k!=2 when j==6) }.
   Then use this array to solve the problem of the number of [1,n]. For a number from high to low statistics, assuming 345 can be divided into [000,099]=f[3][0],[100,199]=f[3][ 1],[200,299]=f[3][2],[300,339]=f[2][0]+f[2][1]+f[2][2]+f[2][3] ,[340,345]=f[1][0]+f[1][1]+f[1][2]+f[1][3]+f[1][4]+f[1][ 5], note that sometimes the entire interval is not legal to exit directly, for example, the above [340, 345], all have 4, so there is no need to count, or all have 62 as a prefix. There is also the range of cal() function statistics is [1, n) (because all the other bits except the lowest bit are enumerated to bit[i]-1 and stop, for the convenience of writing, let n+1 directly) remember to + 1 recalculate.
  
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int f[10][10];
 4 int bit[10];
 5 int cal(int N){
 6     int ans=0,len=0;
 7     while(N){
 8         bit[len++]=N%10;
 9         N/=10;
10     }
11     bit[len]=0;
12     for(int i=len-1;i>=0;--i){
13         for(int j=0;j<bit[i];++j)
14             {
15                 if(j==2&&bit[i+1]==6) continue;
16                 ans+=f[i+1][j];
17             }
18             if(bit[i]==4||(bit[i]==2&&bit[i+1]==6))
19             break;
20     }
21     return ans;
22 }
23 int main(){
24     int i,j,k;
25     f[0][0]=1;
26     for(i=1;i<=7;++i){
27         for(j=0;j<=9;++j){
28             if(j==4) continue;
29             for(k=0;k<=9;++k){
30                 if(k==4 || (j==6&&k==2) ) continue;
31                 f[i][j]+=f[i-1][k];
32             }
33         }
34     }
35     int l,r;
36     while(cin>>l>>r&&(l||r)){
37         printf("%d\n",cal(r+1)-cal(l+1-1));
38     }
39     
40     return 0;
41 }

 

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689711&siteId=291194637