版本比较
12.7总结
正文
增加 10 行删除 10 行
5 行未改动6789101112131420 行未改动35363738394041424344454647484950515253545556575859606162636429 行未改动9495969798991001011024 行未改动1071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381395 行未改动- 所有数位中有偶数个数字 $k$- 求有多少个满足条件的数,答案对 $998244353$ 取模## 思路使用动态规划。设 $dp[i]$ 表示长度为 $i$ 且不包含 $k$ 的数的个数,$dp1[i]$ 表示长度为 $i$ 且包含奇数个 $k$ 的数的个数。但根据你的代码,实际上:- $dp[i]$:长度为 $i$ 且包含偶数个 $k$ 的数的个数- $dp1[i]$:长度为 $i$ 且包含奇数个 $k$ 的数的个数## 解题步骤20 行未改动signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);// 预处理dp[1] = 8;dp1[1] = 1;for (int i = 2; i <= 100000; i++) {dp[i] = (dp[i - 1] * 9 + dp1[i - 1]) % MOD;dp1[i] = (dp1[i - 1] * 9 + dp[i - 1]) % MOD;}int t;cin >> t;while (t--) {int n, k;cin >> n >> k;if (n == 1) {cout << 9 << "\n";} else {cout << dp[n] << "\n";}}return 0;}```# P2196 [NOIP 1996 提高组] 挖地雷29 行未改动for (int j = i + 1; j <= n; j++) {cin >> g[i][j];}}for (int i = 1; i <= n; i++) {dp[i] = a[i];pre[i] = 0;for (int j = 1; j < i; j++) {4 行未改动}}}}stack<int> st;int pos = 0, ans = 0;for (int i = 1; i <= n; i++) {if (dp[i] > ans) {ans = dp[i];pos = i;}}while (pos) {st.push(pos);pos = pre[pos];}while (!st.empty()) {cout << st.top();st.pop();if (!st.empty()) {cout << ' ';}}cout << endl << ans << endl;return 0;}```#### 时间复杂度20+400+400+20+20+20 = 8805 行未改动6789101112131420 行未改动35363738394041424344454647484950515253545556575859606162636429 行未改动9495969798991001011024 行未改动1071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381395 行未改动- 所有数位中有偶数个数字 $k$- 求有多少个满足条件的数,答案对 $998244353$ 取模## 思路使用动态规:- $dp[i]$:长度为 $i$ 且包含偶数个 $k$ 的数的个数- $dp1[i]$:长度为 $i$ 且包含奇数个 $k$ 的数的个数## 解题步骤20 行未改动signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);// 预处理dp[1] = 8;dp1[1] = 1;for (int i = 2; i <= 100000; i++) {dp[i] = (dp[i - 1] * 9 + dp1[i - 1]) % MOD;dp1[i] = (dp1[i - 1] * 9 + dp[i - 1]) % MOD;}int t;cin >> t;while (t--) {int n, k;cin >> n >> k;if (n == 1) {cout << 9 << "\n";} else {cout << dp[n] << "\n";}}return 0;}```# P2196 [NOIP 1996 提高组] 挖地雷29 行未改动for (int j = i + 1; j <= n; j++) {cin >> g[i][j];}}for (int i = 1; i <= n; i++) {dp[i] = a[i];pre[i] = 0;for (int j = 1; j < i; j++) {4 行未改动}}}}stack<int> st;int pos = 0, ans = 0;for (int i = 1; i <= n; i++) {if (dp[i] > ans) {ans = dp[i];pos = i;}}while (pos) {st.push(pos);pos = pre[pos];}while (!st.empty()) {cout << st.top();st.pop();if (!st.empty()) {cout << ' ';}}cout << endl << ans << endl;return 0;}```#### 时间复杂度20+400+400+20+20+20 = 880