枚举支点,每次从后往前累加力矩和,其中后面为负数。当最后和为0时将状态存在dp数组里。
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = 21;
const int INF = 1e8;
LL dp[N][N][2005];
int bit[N];
LL dfs(int pos, int central, int pre, bool limit)
{
if(pos == 0) return pre == 0;
if(pre < 0) return 0;
if(!limit && dp[pos][central][pre] != -1) return dp[pos][central][pre];
int endd = limit ? bit[pos] : 9;
LL ans = 0;
for(int i = 0; i <= endd; i ++)
{
ans += dfs(pos - 1, central, pre + i * (pos - central), limit && (i == endd));
}
if(!limit) dp[pos][central][pre] = ans;
return ans;
}
LL solve(LL n)
{
int len = 0;
LL ans0 = 0;
while(n)
{
bit[++ len] = n % 10;
n /= 10;
}
for(int i = 1; i <= len; i ++)
ans0 += dfs(len, i, 0, true);
return ans0 - (len - 1);
}
int main()
{
// freopen("in.txt", "r", stdin);
int t;
LL l, r;
scanf("%d", &t);
while(t --)
{
scanf("%lld%lld", &l, &r);
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(r) - solve(l - 1));
}
return 0;
}