Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
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].
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
————————————————————集训21.2的分割线————————————————————
前言:乍一看和SPOJ的BALNUM一样的题,结果只是题名很像。
思路:一开始踌躇了很久,不知道力矩怎么办,记录左力矩的值、右力矩的值?不行……到后来看了题解,真是的。我只需要记录支点的坐标就行了。为什么不分左右?因为支点左边力臂是正的,右边是负的,加起来是零不就表示平衡了= =b。
说来倒也是,本来思路就该是“记录左右力矩之间的差值即可”,关于支点,仅需在Solve函数里面枚举,将结果相加,毕竟只需要枚举18次。
代码如下:
前言:乍一看和SPOJ的BALNUM一样的题,结果只是题名很像。
思路:一开始踌躇了很久,不知道力矩怎么办,记录左力矩的值、右力矩的值?不行……到后来看了题解,真是的。我只需要记录支点的坐标就行了。为什么不分左右?因为支点左边力臂是正的,右边是负的,加起来是零不就表示平衡了= =b。
说来倒也是,本来思路就该是“记录左右力矩之间的差值即可”,关于支点,仅需在Solve函数里面枚举,将结果相加,毕竟只需要枚举18次。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
/****************************************/
int num[20];
LL dp[20][1550][20];
LL dfs(int len, int L, int o, bool bnd)
{
LL &f = dp[len][L][o];
if(L < 0) return 0;
if(len == 0) {
if(L == 0) {
//printf("who is %lld\n", m);
return 1;
}
return 0;
}
if(!bnd && (~f)) return f;
int lim = bnd ? num[len] : 9;
LL ret = 0;
for(int k = lim; k >= 0; k--) {
ret += dfs(len-1, L + (len-o)*k, o, bnd&&k==lim);
}
if(!bnd) f = ret;
return ret;
}
LL Solve(LL x)
{
int cnt = 0;
while(x) {
num[++cnt] = x % 10;
x /= 10;
}
LL ans = 0;
for(int i = 1; i <= cnt; i++) {
ans += dfs(cnt, 0, i, true);
}
return ans-cnt+1;
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
memset(dp, -1, sizeof(dp));
int T;
scanf("%d", &T);
LL l, r;
/*
LL rrr = 0;
for(int i = 0; i <= 18; i++)
rrr += 9*i;
printf("rrr is %lld\n", rrr);//计算上限
*/
while(T--) {
scanf("%I64d%I64d", &l, &r);
printf("%I64d\n", Solve(r) - Solve(l-1));
}
return 0;
}