DPS Digit Sum
题意
求 \(1 \to n\) 中有多少个数是 \(d\) 的倍数。
\(n \le 10 ^ {10000}\)。
Sol
数位 dp,设 \(f_{i, j, 1 / 0}\) 表示第 \(i\) 位,膜 \(d\) 等于 \(j\),是否贴住上限。
转移是 \(trivial\) 的。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <string>
#include <cstring>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE
/* #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) */
/* char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; */
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 1e4 + 5, M = 105, mod = 1e9 + 7;
char strbuf[N];
string s;
int f[N][M][2];
void Mod(int &x) {
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
void dfs(int x, int k, bool flg, int n, int d) {
if (~f[x][k][flg]) return;
if (!x) {
if (!k) f[x][k][flg] = 1;
else f[x][k][flg] = 0;
return;
}
f[x][k][flg] = 0;
int ed = 9;
if (flg) ed = s[x] - '0';
for (int i = 0; i <= ed; i++) {
int _k = (k + i) % d;
dfs(x - 1, _k, flg && i == ed, n, d);
f[x][k][flg] += f[x - 1][_k][flg && i == ed], Mod(f[x][k][flg]);
}
}
signed main() {
memset(f, -1, sizeof(f));
scanf("%s", strbuf);
s = strbuf;
int n = s.size(), d = read();
reverse(s.begin(), s.end());
s = "0" + s;
dfs(n, 0, 1, n, d);
write((f[n][0][1] - 1 + mod) % mod), puts("");
return 0;
}