题解:AT_abc465_e [ABC465E] Digit Circus
stripe_python · · 题解
数位 DP 板子。
考虑记搜实现,记录数码和模
粗略分析一下状态数是
string s;
bool vis[505][4][1 << 10];
mint f[505][4][1 << 10];
mint dfs(int x, bool lim, int d, int mask, bool lead0) {
if (!lim && !lead0 && vis[x][d][mask]) return f[x][d][mask];
if (x == (int)s.size()) {
if (lead0) return 0;
int c = (d == 0) + (mask >> 3 & 1) + (popcount(mask) == 3);
return c == 1 ? 1 : 0;
}
int up = lim ? s[x] - '0' : 9;
mint res = 0;
for (int i = 0; i <= up; i++) {
bool nl = lead0 && (i == 0);
int ms = nl ? 0 : (mask | (1 << i));
res += dfs(x + 1, lim && (i == up), (d + i) % 3, ms, nl);
}
if (!lim && !lead0) vis[x][d][mask] = 1, f[x][d][mask] = res;
return res;
}
void _main() {
cin >> s;
cout << dfs(0, 1, 0, 0, 1);
}