You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

HDU 3709 Balanced Number

题目传送门

一、题目描述

题目描述:求区间[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......

三、实现代码

#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 : 这句话很牛Xpos > 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;
}