0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00

P4127 [AHOI2009]同类分布

https://www.luogu.com.cn/record/86021113
This commit is contained in:
Baoshuo Ren 2022-09-06 20:48:57 +08:00
parent 1ee825c548
commit 5ecac15e1e
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

55
Luogu/P4127/P4127.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <bitset>
#include <cstring>
using std::cin;
using std::cout;
const char endl = '\n';
long long a, b, f[20][200][200], ans;
int c[20], p;
long long dfs(int len, int s, int x, bool limit) {
if (s + 9 * len < p) return 0;
if (!limit && f[len][s][x] != -1) return f[len][s][x];
if (!len) return s == p && x == 0;
int n = limit ? c[len] : 9;
long long res = 0;
for (int i = 0; i <= n && i + s <= p; i++)
res += dfs(len - 1, s + i, (x * 10 + i) % p, limit && (i == c[len]));
if (!limit) f[len][s][x] = res;
return res;
}
long long calc(long long x) {
int cnt = 0;
while (x) {
c[++cnt] = x % 10;
x /= 10;
}
long long res = 0;
for (p = 1; p <= cnt * 9; p++) {
memset(f, 0xff, sizeof(f));
res += dfs(cnt, 0, 0, 1);
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a >> b;
cout << calc(b) - calc(a - 1) << endl;
return 0;
}