Palindromic Numbers LightOJ, palindromiclightoj
Palindromic Numbers LightOJ-1205
Http://blog.csdn.net/harlow_cheng/article/details/77466732
It is rare to say that memory-based search can still trace back... here, the actual function of backtracking is to save the use of other things to represent the status of the previously selected number.
Note:
1. Different tot (total length) must be memorized into different arrays.
2.19 rows
1 # include <cstdio> 2 # include <cstring> 3 typedef long LL; 4 LL ans [30] [30] [2]; 5 LL w [30]; 6 ll t, l, r; 7 LL temp [30]; 8 LL dp (LL tot, LL pos, bool pre0, bool limit) 9 {10 if (pos <1) return 1; 11 if (! Limit & ans [tot] [pos] [pre0]! =-1) 12 return ans [tot] [pos] [pre0]; 13 LL I, res = 0, end = limit? W [pos]: 9; 14 for (I = 0; I <= end; I ++) 15 {16 temp [pos] = I; 17 if (I = 0 & pre0) 18 res + = dp (tot-1, pos-1,); 19 // res + = dp (tot, pos-1, 1, 0); then 20 else if (pos> tot/2) // 5 --> 5, 4, 3 6 --> 6, 5, 4 if the first half can be filled with 21 res + = dp (tot, pos-1, 0, limit & I = w [pos]); 22 else if (temp [pos] = temp [tot-pos + 1]) // if the last half is the same as the first half, 23 res + = dp (tot, pos-1, 0, limit & I = w [pos]); 24} 25 if (! Limit) ans [tot] [pos] [pre0] = res; 26 return res; 27} 28 LL get (LL x) 29 {30 if (x <0) return 0; 31 LL len = 0; 32 for (; x> 0; x/= 10) w [++ len] = x % 10; 33 return dp (len, len, 1, 1); 34} 35 int main () 36 {37 LL iii, ttt; 38 memset (ans,-1, sizeof (ans); 39 scanf ("% lld ", & T); 40 for (iii = 1; iii <= T; iii ++) 41 {42 scanf ("% lld", & l, & r ); 43 if (l> r) ttt = l, l = r, r = ttt; 44 printf ("Case % lld: % lld \ n", iii, get (r) -get L-1); 45} 46 return 0; 47}