1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 3e6 + 10; const int mod = 998244353; typedef pair<ll, ll> pii; ll dp[10005][2048]; ll dp2[10005][2048]; int a[10005], st; ll base[10005]; pii dfs(int pos, int state, bool limit, bool lead) { if (pos == -1) { if ((state & st) == st) return {1,0}; else return {0,0}; } if (!limit && !lead && dp[pos][state] != -1) return {dp[pos][state], dp2[pos][state]}; int End = limit ? a[pos] : 9; ll ans1 = 0, ans2 = 0; for (int i = 0; i <= End; i++) { if (lead) { if (i == 0) { pii tmp = dfs(pos-1, state, limit && i == End, 1); ans1 = (ans1 + tmp.first) % mod; ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod; } else { pii tmp = dfs(pos-1, state | (1<<i), limit && i == End, 0); ans1 = (ans1 + tmp.first) % mod; ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod; } } else { pii tmp = dfs(pos-1, state | (1 << i), limit && i == End, 0); ans1 = (ans1 + tmp.first) % mod; ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod; } } if (!limit && !lead) dp[pos][state] = ans1, dp2[pos][state] = ans2; return {ans1, ans2}; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); base[0] = 1; for (int i = 1; i <= 10000; i++) base[i] = base[i-1] * 10 % mod; string s; cin >> s; int len = s.size(); int m; cin >> m; for (int i = 1; i <= m; i++) { int x; cin >> x; st |= 1 << x; }
for (int i = 0; i < s.size(); i++) a[i] = s[len-1-i]-'0'; memset(dp, -1, sizeof dp); memset(dp2, -1, sizeof dp2); cout << dfs(len-1, 0, true, true).second << endl; }
|