标签:test == stream print def bool 方案 数位dp string
http://codeforces.com/group/aUVPeyEnI2/contest/227850
E
与一般数位dp不同,保存的是能否满足条件,而非记录方案数
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long ll;
int len, n, ans[1005], dp[1005][1005];
char s[1005];
bool found = false;
bool dfs(int pos, int j) {
if (pos == len) {
if (j == 0) {
for (int i = 0; i < len; i++) {
printf("%d", ans[i]);
}
puts("");
exit(0);
}
return false;
}
if (dp[pos][j] != -1) {
return dp[pos][j];
}
if (s[pos] != ‘?‘) {
ans[pos] = s[pos] - ‘0‘;
dp[pos][j] = dfs(pos + 1, (j * 10 + ans[pos]) % n);
}
else for (int i = pos ? 0 : 1; i <= 9; i++) {
ans[pos] = i;
dp[pos][j] = dfs(pos + 1, (j * 10 + i) % n);
}
if (dp[pos][j] == -1) {
dp[pos][j] = 0;
}
return dp[pos][j];
}
int main() {
scanf("%s %d", s, &n);
len = strlen(s);
memset(dp, -1, sizeof dp);
if (!dfs(0, 0)) {
puts("*");
}
}
标签:test == stream print def bool 方案 数位dp string
原文地址:https://www.cnblogs.com/stolf/p/9575833.html