BZOJ 1799 - [AHOI2009]self 同类分布 - 枚举 数位DP

时间:2022-12-03 04:26:00

Description

找出$[L, R]$ 区间内有多少数, 各位数字和 能整除原数

Solution

枚举每个可能的数字和, 进行数位DP即可 , 水爆

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std; int tot = ;
int a[], mod;
ll sum[][][]; ll dfs(int pos, int ad, int r, bool lim, bool lead) {
if(ad > mod)
return ;
if(!pos) return ad == mod && r == ;
if(!lim && !lead && sum[pos][ad][r] != -)
return sum[pos][ad][r];
int up = lim ? a[pos] : ;
ll tmp = ;
for(int i = ; i <= up; ++i) {
tmp += dfs(pos - , ad + i, (r * + i) % mod, lim && a[pos] == i, lead && i == );
}
if(!lim && !lead)
sum[pos][ad][r] = tmp;
return tmp;
} ll work(ll x) {
int len = ;
while(x) a[++len] = x % , x /= ;
return dfs(len, , , true, true);
} int main()
{
ll l, r;
ll ans = ;
scanf("%lld%lld", &l, &r);
for(int i = ; i <= tot; ++i) {
mod = i;
memset(sum, -, sizeof(sum));
ans += work(r) - work(l - );
}
printf("%lld\n", ans);
}