[原创]HDU 3709 Balanced Number [数位DP]【动态规划】

2016-08-25 20:07:46 Tabris_ 阅读数:286


博客爬取于2020-06-14 22:43:40
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52317803


题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3709
------------------------------.

Balanced Number

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4621 Accepted Submission(s): 2169

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 42 + 11 = 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
--------------------------.
题目大意:
就是求[m,n]区间内的平衡数有多少
平衡数的定义为
在一个数中 找一个确定一位数字为"支点" 然后两边的每位数字的值乘上力矩(就是这位到支点的距离)的和相等 这样的数被的定义为平衡数;

解题思路:
还是标准的数位DP
只是要明确的一点是 同一个数 如果为平衡数的话 那么它的支点有且仅有一个 就是说两个数不能被计算两次;
所以在进行DP的时候只要枚举支点即可

还有就是两边的和相等 可以转化为两边的和为0 (只要规定了力矩的方向就能完成)
比如说1234321 支点很明显是4 然后13+22+31+3(-1)+2*(-2)+1*(-3)==0;

上述明白了就可以DP了 鄙某用的是记忆化搜索的形式写的

dp[位数][力矩总和][支点的位数];

dfs(当前位数,支点,力矩总和,数位的限制);

附本题代码
------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MOD = 1000000007;
const int maxn = 200010;

int num[70],len;
LL dp[20][2000][20];

LL dfs(int pos,int o,int pre,int limit)
{
if(pos < 0) return pre==0;
if(pre<0) return 0;
if(dp[pos][pre][o]!=-1&&!limit)
return dp[pos][pre][o];

int endi = 9;
if(limit) endi = num[pos];

LL res = 0;
for(int i=0; i<=endi; i++)
res+=dfs(pos-1,o,pre+i*(pos-o),limit&&(i==endi));

if(!limit) dp[pos][pre][o] = res;
return res;
}

LL solve(LL n)
{
len = 0;

while(n)
{
num[len++] = n%10;
n /= 10;
}

LL res = 0;
for(int i=0;i<len;i++)
res += dfs(len-1,i,0,1);

return res-len+1;
}Balanced Number

int main()
{
memset(dp,-1,sizeof(dp));

int _,p=0;
scanf("%d",&_);
while(_--)
{
LL n,m;
scanf("%lld%lld",&m,&n);
// printf("%lld %lld\n",solve(n),solve(m-1));
printf("%lld\n",solve(n)-solve(m-1));
}
return 0;
}