Created
December 8, 2025 05:18
-
-
Save hikariyo/513450152dc935b15388c7df8da55670 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| #define pb push_back | |
| const int P = 1e9+7, N = 1510, K = 1210; | |
| int dp[N][K], tr[N][10], fail[N], mark[N], idx; | |
| vector<int> G[N]; | |
| void insert(string& s) { | |
| int u = 0; | |
| for (char c: s) { | |
| int p = c - '0'; | |
| if (!tr[u][p]) tr[u][p] = ++idx; | |
| u = tr[u][p]; | |
| } | |
| mark[u] = 1; | |
| } | |
| void dfs(int u, int m) { | |
| mark[u] |= m; | |
| for (int to: G[u]) dfs(to, m | mark[u]); | |
| } | |
| void build() { | |
| queue<int> q; | |
| for (int i = 0; i < 10; i++) if (tr[0][i]) q.push(tr[0][i]); | |
| while (q.size()) { | |
| int fa = q.front(); q.pop(); | |
| for (int i = 0; i < 10; i++) { | |
| int& u = tr[fa][i]; | |
| if (u) fail[u] = tr[fail[fa]][i], q.push(u); | |
| else u = tr[fail[fa]][i]; | |
| } | |
| } | |
| for (int i = 1; i <= idx; i++) G[fail[i]].pb(i); | |
| dfs(0, 0); | |
| } | |
| signed main() { | |
| ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); | |
| string n; | |
| int m; | |
| cin >> n >> m; | |
| for (int i = 1; i <= m; i++) { | |
| string s; | |
| cin >> s; | |
| insert(s); | |
| } | |
| build(); | |
| // build dp | |
| for (int k = 0; k < K; k++) { | |
| for (int i = 0; i <= idx; i++) { | |
| if (mark[i]) continue; | |
| if (k == 0) { | |
| dp[i][k] = 1; | |
| } | |
| else { | |
| for (int j = 0; j < 10; j++) { | |
| dp[i][k] = (dp[tr[i][j]][k-1] + dp[i][k]) % P; | |
| } | |
| } | |
| } | |
| } | |
| int u = 0, len = n.size(); | |
| int ans = 0; | |
| n = ' ' + n; | |
| for (int i = 1; i <= len; i++) { | |
| int p = n[i] - '0'; | |
| for (int j = (i == 1); j < p; j++) { | |
| ans = (ans + dp[tr[u][j]][len-i]) % P; | |
| } | |
| u = tr[u][p]; | |
| if (mark[u]) break; | |
| } | |
| if (!mark[u]) ans = (ans + 1) % P; | |
| for (int i = 1; i <= len-1; i++) { | |
| for (int j = 1; j < 10; j++) { | |
| ans = (ans + dp[tr[0][j]][i-1]) % P; | |
| } | |
| } | |
| cout << ans << '\n'; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment