瞎猜的規律:除0以外 每個數只有一箇中心或者沒有,中心是本題定義的中心
dp[i][mid][sum] 代表給第i位填數, 中心點定義爲mid, 比i高的位與中心的距離爲sum
距離定義左正右負
坑點數組要開大和0要特殊考慮
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
LL dp[30][30][3000], x[100], l;
LL dfs(LL i, bool e, LL pre, LL mid)
{
if(i == -1)
{
if(pre == 0) return 1;
else return 0;
}
if(e && dp[i][mid][pre] != -1) return dp[i][mid][pre];
LL Max = e? 9 : x[i], ans = 0;
for(int j = 0; j <= Max; j++)
ans += dfs(i - 1, !(!e && j == x[i]), pre + j * (i - mid), mid);
if(e) dp[i][mid][pre] = ans;
return ans;
}
LL cal(LL n)
{
l = 0;
while(n)
{
x[l++] = n % 10;
n /= 10;
}
LL ans = 1;
for(int i = 0; i < l; i++)
ans += dfs(l - 1, 0, 0, i);
ans -= l;
return ans;
}
int main()
{
LL a, b, t;
memset(dp, -1, sizeof(dp));
scanf("%I64d", &t);
while(t--)
{
scanf("%I64d%I64d", &a, &b);
LL sum1 = cal(b), sum2 = 0;
if(a - 1 >= 0) sum2 = cal(a - 1);
printf("%I64d\n", sum1 - sum2);
}
return 0;
}