Digital dp (Balanced Numbers)

Meaning of the questions: a number, if the number of odd-even number of times appear to meet the even number appears odd,

It is in line with the number of injection such as: 12313 satisfied, because 13 there was an even number. 2 there was an odd number

Ideas, for this question, is the state of compressed dp;

For a number, the 0 to 9 each, are only three kinds of state quantities, either never appear or odd occurrences

Or is even, the band 3 may be represented by three binary state which is shown, then the time to pos-1

Determining whether the respective states 0-9 to qualify

 1 #include<cstdio>
 2 #include<string.h>
 3 using namespace std;
 4 typedef long long ll;
 5 ll dp[20][60000];
 6 ll a[20];
 7 int check(int s)
 8 {
 9     for(int i=0;i<=9;++i){
10         int k=s%3;
11         s/=3;
 12 is          IF (K == 0 ) Continue ;
 13 is          the else  IF ((I & . 1 ) && K == . 1 )   return  0 ;   // if odd, if the number of occurrences is also surprising exit; 
14          the else  IF ! ((I & . 1 ) && K == 2 ) return  0 ;   // if an even number. . . . . . . . . . . . . . 
15      }
 16      return  . 1 ;
 . 17  }
 18 is  // ternary codes; 
. 19  int Change ( int D, intS)
 20 is  {
 21 is      int TEMP [ 12 is ];
 22 is      for ( int I = 0 ; I <= . 9 ; I ++ ) {
 23 is          TEMP [I] = S% . 3 ;
 24          S / = . 3 ;
 25      }
 26 is      S = 0 ;
 27      IF (TEMP [D] == 0 ) TEMP [D] = . 1 ; // code representing its state 
28      the else TEMP [D] = . 3 -temp [D];    // code representing its state 
29      for (int i=9;i>=0;i--)
30         s=s*3+temp[i];
31     return s;
32 }
33 
34 ll dfs(ll pos,ll sum,bool flag,bool limit)
35 {
36     //flag是判断前导0用的
37     if(pos==-1) return check(sum);
38     if(!limit&&dp[pos][sum]!=-1) return dp[pos][sum];
39     int up=limit? a[pos]:9;
 40      LL ANS = 0 ;
 41 is      for ( int I = 0 ; I <= up; I ++ )
 42 is                         // If before always 0, i == 0, the value of the sum is still equal to 0 
43 is          ANS + = DFS (POS - . 1 , (In Flag == 0 && I == 0 )? 0 : Change (I, SUM), In Flag || I> 0 , I limit && == A [POS]);
 44 is      IF (limit) DP [POS] [! SUM] = ANS;                // if i has become greater than 0, then this statement no leading zero 
45      return ANS;
 46 is  }
 47  LL Solve (LL X)
 48  {
 49      int pos=0;
50     while(x){
51         a[pos++]=x%10;
52         x/=10;
53     }
54     return dfs(pos-1,0,0,true);
55 }
56 int main()
57 {
58     int T;
59     scanf("%d",&T);
60     memset(dp,-1,sizeof(dp));
61     while(T--){
62         ll l,r;
63         scanf("%lld%lld",&l,&r);
64         printf("%lld\n",solve(r)-solve(l-1));
65     }
66     return 0;
67 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11881433.html