Files
python/TangDou/AcWing/DP/ShuWei/HDU3709.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

87 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## $HDU$ $3709$ $Balanced$ $Number$
[题目传送门](http://acm.hdu.edu.cn/showproblem.php?pid=3709)
### 一、题目描述
题目描述:求区间$[l,r]$里面满足**平衡数**的数的个数
**平衡数**:可以通过找一个平衡数位,该数位左边的数位乘以偏移距离的和等于右边的数位乘以偏移距离的和。
举个栗子:$4139$,平衡数位为$3$,$4*2+1=9$,因此该数是平衡数。
### 二、解题思路
定义状态$dp[pos][k][sum]$表示枚举到$pos$位置,当前平衡数位位置为$k$,前面数位的数乘以偏移距离的和。
最后判断一下是否等于$0$就行了。
我们可以枚举平衡数位的位置,同时在枚举时维护和就行了。
另外,这个题是做到的第一个前导$0$有影响的题
如果数为$0$,是可以的(既符合题意,又确实存在这个数)
如果数为$00$,是不可以的(符合题意,是平衡数,但是这个数跟$0$不是一个吗)
所以这个数有几位,数位最大是$len$,就会多统计了$len-1$个,在计算的时候要除掉
$00,000,0000......$
### 三、实现代码
```c++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 20;
/*
x,y <= 10^{18},用10进制拆分到数组a,20位足够
本题对于内存空间的限制比较极限,如果无脑使用了32,就会使得f的容量超限
32*32*10010=10250240 byte = 10010 kb = 9.775390625 mb
内存上限是:65535kb=64mb
因为C++本身运行还需要一次的内存,64MB是很极限的,所以在多维数组创建时,一定要注意维度的上界
*/
LL a[N];
/*
pos:走到哪个数位,长度最长是N=20
k:以哪个数位为平衡数位,可以是1到20
sum:前面数位的数乘以偏移距离的和,最大偏移量是20,数位最大是9
可以视为
10*20+10*19+....+10*1=10*(1+2+3+...+20)=10*21*10=2100
*/
LL f[N][N][2110];
LL dfs(int pos, int k, int sum, bool lead, bool limit) {
if (pos == 0) return sum == 0;
if (sum < 0) return 0;
if (!lead && !limit && ~f[pos][k][sum]) return f[pos][k][sum];
int up = limit ? a[pos] : 9;
LL ans = 0;
for (int i = 0; i <= up; i++)
// sum + (pos - k) * i : 这句话很牛X,pos > k 是加,pos < k是减
ans += dfs(pos - 1, k, sum + (pos - k) * i, lead && i == 0, limit && i == a[pos]);
if (!limit && !lead) return f[pos][k][sum] = ans;
return ans;
}
LL calc(LL x) {
if (x == -1) return 0; //如果输入的是l=0,则返回0
int al = 0;
while (x) a[++al] = x % 10, x /= 10;
LL ans = 0;
for (int i = al; i; i--) //枚举每一个数位,模拟此位置k为平衡数位
ans += dfs(al, i, 0, true, true);
//0,00,000,0000,....
return ans - al + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//平衡数是数的本身性质,与给定的区间无关,可以将memset放在循环外初始化
memset(f, -1, sizeof f);
int T;
cin >> T;
LL l, r;
while (T--) {
cin >> l >> r;
printf("%lld\n", calc(r) - calc(l - 1));
}
return 0;
}
```