递推四维状态写的我无比纠结 最后无奈弃坑 搞记忆化搜索了 这道题多的一个是要枚举支点o 然后从高位往低位考虑的时候注意力矩是否为正值
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const int MAXN = 18;
const int MAXL = 2000;
int bit[MAXN+10];
LL d[MAXN+10][MAXN+10][MAXL+10];
LL dp(int i, int o, int pre, int limit)
{
if(i <= 0) return pre == 0;
if(pre < 0) return 0;
if(!limit && d[i][o][pre] != -1) return d[i][o][pre];
LL ans = 0;
for(int j = 0; j <= (limit ? bit[i] : 9); j++)
ans += dp(i-1, o, pre + j * (i - o), limit && (j == (limit ? bit[i] : 9)));
if(!limit) d[i][o][pre] = ans;
return ans;
}
LL solve(LL X)
{
int Len = 0; LL ans = 0;
memset(bit, 0, sizeof(bit));
while(X) {
bit[++Len] = X % 10;
X /= 10;
}
for(int i = 1; i <= Len; i++)
ans += dp(Len, i, 0, 1);
return ans - Len + 1;
}
int main()
{
memset(d, -1, sizeof(d));
int T; scanf("%d", &T); while(T--) {
LL L, R;
cin >> L >> R;
LL ans = solve(R) - solve(L-1);
cout << ans << '\n';
}
}