###### ramay7

https://ramay7.github.io/

###### HDU 3709 Balanced Number（数位dp）

HDU 3709 Balanced Number

BZOJ 1799 self同类分布类似，我们枚举支点位置即可。因为对于一个Balanced Number它的支点只会有1个。仔细想一下就好了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;

int digit[20];
ll dp[20][20][4000];

ll dfs(int pos, int pivot, int sum, int limit)
{
if (sum < 0 || sum > pivot * (pivot + 1) / 2 * 9) return 0;
if (pos == -1) return sum == 0;
if (!limit && dp[pos][pivot][sum] != -1) return dp[pos][pivot][sum];
int last = limit ? digit[pos] : 9;
ll ret = 0;
for (int i = 0; i <= last; ++i) {
int next_sum = sum + i * (pos - pivot);
ret += dfs(pos - 1, pivot, next_sum, limit && i == last);
}
if (!limit) dp[pos][pivot][sum] = ret;
return ret;
}

ll solve(ll x)
{
if (x < 0) return 0;
else if (x == 0) return 1;
memset(digit, 0, sizeof(digit));
int len = 0;
while (x) {
digit[len++] = x % 10;
x /= 10;
}
ll ret = 0;
for (int i = 0; i < len; ++i) {
ret += dfs(len - 1, i, 0, 1);
}
return ret - (len - 1); // 把0算了len次
}

int main()
{
int T;
ll L, R;
scanf("%d", &T);
while (T--) {
memset(dp, -1, sizeof(dp));
scanf("%lld%lld", &L, &R);
printf("%lld\n", solve(R) - solve(L - 1));
}
return 0;
}

#### 数位DP学习小结

2016-08-06 21:03:07

#### HDU - 3709 数位dp

2017-01-16 09:41:40

#### HDU 3709 Balanced Number （数位DP）

2015-09-02 08:54:47

#### 数位dp hdu3709 Balanced Number

2015-09-09 01:49:25

#### HDU3709 Balanced Number 数位DP

2015-10-20 21:39:11

#### hdu3709 Balanced Number（数位dp）

2016-08-23 15:35:50

#### 1

2013-09-23 22:11:21

#### hdu 3709 力矩

2017-07-24 22:09:54

#### Balanced Number

2017-03-09 11:17:39

#### hdu3709Balanced Number【数位dp】

2016-03-11 22:36:01

## 不良信息举报

HDU 3709 Balanced Number（数位dp）