Balanced number
Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3988 Accepted Submission (s): 1869
Problem Descriptiona Balanced number is a non-negative integer the can be balanced if a pivot was placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot was placed at some digit of the number, the distance from a digit to the pivot is the offset between it and th e pivot. Then the torques of left part and right part can calculated. It's balanced if they is the same. A balanced number must is balanced with the pivot at some of its digits. For example, 4139 are a balanced number with pivot fixed at 3. The torqueses is 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 was the total number of cases T (0 < t≤30). For each case, there is 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] with a line.
Sample Input20 97604 24324
Sample Output10897
Authorgao, Yuan test instructions is the equilibrium number in the range. The number is balanced like a moment in physics, and the number has a pivot point, and the number on either side of the point multiplied by the distance from the pivot point is equal to the sum of the numbers. For example 24380:3 points to the left and 2 * 2 + 4 * 1 = 8, the right is 8 * 1 + 0 * 2 = 8. So this number is balanced. A digital DP that enumerates the pivot points.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#definell Long Longintdig[ -];ll dp[ -][ -][ -];ll DFS (intPosintPivotintSumintLim) {if(pos = =-1)returnsum = =0; if(Sum <0)return 0; if(!lim && dp[pos][pivot][sum]! =-1)returnDp[pos][pivot][sum]; intEnd = Lim? Dig[pos]:9; LL ret=0; for(inti =0; I <= End; i++) {ret+ = DFS (pos-1, Pivot, sum + i * (Pos-pivot), (i = = End) &&Lim); } if(!lim) Dp[pos][pivot][sum] =ret; returnret;} ll func (ll num) {intn =0; while(num) {dig[n+ +] = num%Ten; Num/=Ten; } LL ret=0; for(inti =0; I < n; i++) {ret+ = DFS (n-1I0,1); } returnRET-N;}intMain () {intT; ll N, M; Memset (DP,-1,sizeof(DP)); scanf ("%d", &t); while(t--) {scanf ("%i64d%i64d", &n, &m); printf ("%i64d\n", Func (M)-Func (N-1)); }}
HDU 3709 Balanced Number