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 | #include <iostream> #include <string> using namespace std; const int P = 998244353, N = 1e4 + 10, M = 20; int n, m; string s; int dp[1 << M]; int solve() { dp[0] = 1; for (int i = 0; i < n; ++i) { for (int j = (1 << (m - 1)) - 1; j >= 0; --j) { int k = (j << 1) | (s[i] - '0'); if (j != 0 || s[i] == '1') dp[k] = (dp[k] + dp[j]) % P; } } int ans = 0; for (int i = 0; i < (1 << m); ++i) { ans = (ans + 1ll * i * dp[i]) % P; } return ans; } int solve2() { int ans = 0; for (int i = 0; i < (1 << n); ++i) { int cnt = 0; int num = 0; for (int j = 0; j < n; ++j) { if (i & (1 << j)) { num = num * 2 + (s[j] - '0'); cnt++; } } if (cnt <= m) (ans += num) %= P; } return ans; } int main() { cin >> n >> m; cin >> s; if (n <= 20) { cout << solve2() << endl; } cout << solve() << endl; return 0; } |
注:假设输入的 s 是包含 n 个字符的 01 串。
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
solve()
所实现的算法的时间复杂度是 O(n×\(2^m\))。( )2、输入 11 2 10000000001
时,程序输出两个数 32 和 23。( )
3、在 n≤10 时,solve()
的返回值始终小于 4^10。( )
4、当n=10 且m=10 时,有多少种输入使得两行的结果完全一致?( )
5、当 n≤6 时,solve()
的最大可能返回值为( )?
5、若 n=8,m=8,solve
和 solve2
的返回值的最大可能的差值为( )