Hdu3709 digital dp (self-balancing number), hdu3709 digital dp number
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 3709
Problem DescriptionA balanced number is a non-negative integer that can be balanced if a partition is placed at some digit. more specifically, imagine each digit as a box with weight indicated by the digit. when a distance is placed at some digit of the number, the distance from a digit to the distance is the offset between it and the distance. then the torques of left part and right part can be calculated. it is balanced if they are the same. A balanced number must be balanced with the specified at some of its digits. for example, 4139 is a balanced number with fixed at 3. the torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. it's your job
To calculate the number of balanced numbers in a given range [x, y].
InputThe input contains multiple test cases. the first line is the total number of cases T (0 <T ≤ 30 ). for each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 1018 ).
OutputFor each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input
20 97604 24324
Sample Output
10897
/** Hdu 3709 digital dp (self-balancing number) Question: calculate the number of self-balancing numbers within a given range, the so-called balance, for example: 4139 is a balanced number with specified fixed at 3. the torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. solution: enumeration pivot. Dp [I] [j] [k] I indicates the number of processed digits, j indicates the pivot point, and k indicates the torque and. However, we need to exclude the numbers all 0 */# include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> using namespace std; typedef long LL; LL dp [20] [20] [2000]; int bit [25]; LL dfs (int pos, int level, int presum, int flag) {if (pos =-1) return presum = 0; if (presum <0) return 0; if (! Flag & dp [pos] [level] [presum]! =-1) return dp [pos] [level] [presum]; int end = flag? Bit [pos]: 9; LL ans = 0; for (int I = 0; I <= end; I ++) {ans + = dfs (pos-1, level, presum + (pos-level) * I, flag & (I = end);} if (! Flag) dp [pos] [level] [presum] = ans; return ans;} LL solve (LL n) {int len = 0; while (n) {bit [len ++] = n % 10; n/= 10;} LL ans = 0; for (int I = 0; I <len; I ++) {ans + = dfs (len-1, I,);} return ans-(len-1); // remove all 0 cases} int main () {int T; LL l, r; memset (dp,-1, sizeof (dp); scanf ("% d", & T); while (T --) {scanf ("% I64d % I64d", & l, & r); printf ("% I64d \ n", solve (r)-solve L-1 ));} return 0 ;}