Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 5097 Accepted Submission(s): 2434 Problem Description
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. 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 pivot at some of its digits. For example, 4139 is a balanced number with pivot 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].
Input
The 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 ≤ 10 18).
Output
For each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input
2 0 9 7604 24324
Sample Output
10 897
Author
GAO, Yuan
Source
思路:枚举支点位置即可。
# include# include # define LL long longint a[20];LL dp[19][19][2000];LL dfs(int pos, int piv, int l, bool limit)//数位,支点位置,当前力矩和,上界限制{ if(pos==-1) return l==0; if(!limit && dp[pos][piv][l] != -1) return dp[pos][piv][l]; if(l<0) return 0; int up = limit?a[pos]:9; LL tmp = 0; for(int i=0; i<=up; ++i) { long long tmp2 = l; tmp2 += (pos-piv)*i; tmp += dfs(pos-1, piv, tmp2, limit&&i==a[pos]); } if(!limit) dp[pos][piv][l] = tmp; return tmp;}LL solve(LL num){ int cnt = 0; LL ans = 0; while(num) { a[cnt++] = num%10; num /= 10; } for(int i=0; i