Balanced number
Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3798 Accepted Submission (s): 1772
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
Test instructions: To find out the number of balance in the interval, the so-called balance number, that is, the number of one for the fulcrum, the other side of the number multiplied by the sum of the moment equal, that is, the balance of the number
Idea: First this is a digital DP problem, solve the problem with the enumeration algorithm, enumerate all possible conditions for DFS, finally add, is the result.
AC Code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intbit[ +];__int64 dp[ +][ +][2005];__int64 DFS (intPosintOintLintlimit) { //according to test instructions, to change the conditions of the answer, so when l==0 return 1 if(pos==-1) returnl==0; //note here, l<0 must write, because there is a situation in the left of the pivot <0 after the left ==0, this situation is not consistent, so this step can not be less if(l<0) return 0; __int64&aa=Dp[pos][o][l]; if(!limit&&aa!=-1) returnAA; __int64 ans=0; intEnd=limit?bit[pos]:9; for(intI=0; i<=end;i++) { intnext=l; Next+ = (pos-o) *i; Ans+=dfs (pos-1, o,next,limit&&i==end); } if(!limit) AA=ans; returnans;} __int64 Sol (__int64 N) {intlen=0; while(n) {Bit[len++]=n%Ten; N/=Ten; } __int64 ans=0; for(intI=0; i<len;i++) {ans+=dfs (len-1I0,1); } returnans-(len-1);}intMain () {into; CIN>>N; while(o--) {memset (DP,-1,sizeof(DP)); __int64 x, y; CIN>>x>>y; cout<<sol (y)-sol (x1) <<Endl; } return 0;}
HDU 3709 Balanced Number (digital DP)