解题思路:
dp[i][j][k] 表示 i 为 支点为j, 力矩为k, 其实数位DP也是有模版的。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#define ll long long
using namespace std;
ll dp[20][20][2000];
int bit[20];
ll dfs(int pos, int center, int sum, bool flag)
{
if (pos == -1) return sum == 0;
if (sum < 0) return 0;
if (!flag && dp[pos][center][sum] != -1)
return dp[pos][center][sum];
int end = flag ? bit[pos] : 9;
ll ans = 0;
for (int i = 0; i <= end; i++)
ans += dfs(pos - 1, center, sum + i * (pos - center), flag && i == end);
if(!flag)
return dp[pos][center][sum] = ans;
return ans;
}
ll solve(ll n)
{
int m = 0;
while (n)
{
bit[m++] = n % 10;
n /= 10;
}
ll ans = 0;
for (int i = 0; i < m; i++)
ans += dfs(m - 1, i, 0, 1);
ans -= (m - 1);
return ans;
}
int main()
{
int T;
memset(dp, -1, sizeof(dp));
cin >> T;
while (T--)
{
ll x, y;
cin >> x >> y;
cout << solve(y) - solve(x - 1) << endl;
}
return 0;
}