Digital DP, the leading 0 needs to be recorded.
Digital DP in need to pay attention to statistics 0,00,000 ... These numbers.
The notation of digital DP can be divided into two categories. Since we usually do DP in a memory-based search, we have a memory array.
One is that the meaning of the memory array is not universal, and for different case, the value of the array is different. The other is generic, different case, and the value of the array is unchanged.
The implementation of the first case is relatively simple, and only the entire parameters of the recursive process are recorded in the dimensions of the array.
Due to the fact that all parameters are recorded, the array dimension is high, so the space efficiency is low.
Because of the different case to recalculate the memory array, it is inefficient to judge the time of the multi.
The template is as follows:
Long LongDfsintDigitBOOLLessBOOLLeading_zero, ...) { if(Digit <0) { return ...; } if(memoize[digit][less][leading_zero][...]! =-1) { returnmemoize[digit][less][leading_zero][...]; } intLimit = less?9: F[digit]; Long LongRET =0; for(inti =0; I <= limit; i++) {ret+ = DFS (digit-1, less | | I < F[digit], Leading_zero && i==0, ...); } returnmemoize[digit][less][leading_zero][...] =ret;}
View Code
For the second case, it is necessary to conditionally judge certain parameters, and the memory array Memoize[digit] is recorded in the case where the lowest digit bit can be arbitrarily valued, the answer we need.
Thus, this kind of memory array is naturally not constrained by the upper bounds.
But it is complex to implement, especially if a variable that requires conditional judgment (in recursive parameters, but not in the memory array) is too large.
Especially for that kind of multiple numbers, each number has an upper bound, while the DP situation, should not use this method, but should choose the first method.
The template is as follows:
Long LongDfsintDigitBOOLLessBOOLLeading_zero, ...) { if(Digit <0) { return ...; } if(Less &&!leading_zero && memoize[digit][...]! =-1) { returnmemoize[digit][...]; } intLimit = less?9: F[digit]; Long LongRET =0; for(inti =0; I <= limit; i++) {ret+ = DFS (digit-1, less | | I < F[digit], Leading_zero && i = =0, ...); } if(Less &&!)Leading_zero) {memoize[digit][...]=ret; } returnret;}
View Code
The answer is as follows:
#include <cstdio>#include<cstring>using namespacestd;Const intMax_digit = -;Long LongN;intF[max_digit];Long Longmemoize[max_digit][ -* -*9];intpivot;intTo_digits (Long Longa) { intRET =0; while(A >0) {F[ret+ +] = a%Ten; A/=Ten; } returnret;}Long LongDfsintDigitBOOLLessBOOLLeading_zero,intzero_num) { if(Digit <0) { returnZero_num; } if(Less &&!leading_zero && memoize[digit][zero_num]! =-1) { returnMemoize[digit][zero_num]; } intLimit = less?9: F[digit]; Long LongRET =0; for(inti =0; I <= limit; i++) { intDelta =!leading_zero && i = =0?1:0; RET+ = DFS (digit-1, less | | I < F[digit], Leading_zero && i = =0, Zero_num +Delta); } if(Less &&!)Leading_zero) {Memoize[digit][zero_num]=ret; } returnret;}Long LongWorkLong LongN) { if(N <0) { return 0; } if(n = =0) { return 1; } intLen =to_digits (n); returnDFS (Len-1,false,true,0) +1;}intMain () {intT; scanf ("%d", &t); memset (Memoize,-1,sizeof(memoize)); for(inti =1; I <= t; i++) { Long LongA, B; scanf ("%lld%lld", &a, &b); printf ("Case %d:%lld\n", I, work (b)-Work (A-1)); } return 0;}
View Code
Light OJ 1140