类型:数位DP
题意:定义一个Good Number 为 一个数所有位数相加的和%10==0.问[A,B]之间有多少Good Number.
方法:
正常“暴力”的定义状态:(i,d,相关量)
定义dp[i][d][mod] 为 d开头的i位数中,%10==mod的数的个数
dp[i][d][mod] = sum(dp[i-1][0~9][(mod-d+10)%10]
出口:dp[1][d][mod] = (d==mod);
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std; long long dp[][][];
int num[]; long long dfs(int i, int d, int mod, bool isQuery) {
if (i == ) return d==mod;
if (!isQuery && ~dp[i][d][mod]) return dp[i][d][mod];
long long ans = ;
int end = isQuery?num[i-]:;
int nextMod = (mod-d+)%;
for (int j = ; j <= end; j++) {
ans += dfs(i-, j, nextMod, isQuery && j == end);
}
if (!isQuery) dp[i][d][mod] = ans;
return ans;
} long long cal(long long x) {
if (x == ) return ;
if (x == -) return ;
int len = ;
while (x) {
num[++len] = x%;
x/=;
}
return dfs(len+, , , true);
} int main() {
int t;
cin>>t;
int cas = ;
memset(dp, -, sizeof(dp));
while (t--) {
long long a,b ;
cin>>a>>b;
cout<<"Case #"<<cas++<<": "<<cal(b)-cal(a-)<<endl;
}
return ;
}