Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2640 Accepted Submission(s): 1196
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 ≤ 1018).
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
2010 Asia Chengdu Regional Contest
Recommend
zhengfeng
题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩 之和相等,即为平衡数
题解:枚举支点。一开始以为同一个数以不同的数位为支点会可能都是平衡数,这样就会算重复。然后仔细想想不可能
如果一开始以第一个为支点,左边为suml,支点右边为sumr,然后支点左移,suml是递减的,sumr是递增的。所以
不会重复。(0,00,000除外)
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #define ll long long #define N 1505 using namespace std; ll l,r; ll dp[20][20][N];///dp[i][j][k] i:长度 j:支点 k:和 int num[30]; ll dfs(int i,int z,int sum,bool e) { if(i<=0)return sum==0; if(sum<0)return 0; if(!e&&dp[i][z][sum]!=-1)return dp[i][z][sum]; ll res=0; int u=e?num[i]:9; for(int d=0; d<=u; d++) { int s=sum+(i-z)*d; res+=dfs(i-1,z,s,e&&d==u); } return e?res:dp[i][z][sum]=res; } ll solve(ll n) { int len=1; while(n) { num[len++]=n%10; n/=10; } ll ans=0; for(int i=1; i<=len-1; i++) {///枚举支点 ans+=dfs(len-1,i,0,1); } return ans-len+1; } int main() { memset(dp,-1,sizeof dp); int t; cin>>t; while(t--) { scanf("%lld%lld",&l,&r); printf("%lld\n",solve(r)-solve(l-1)); } return 0; }