Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
108949 | 彭士宝 | 求和比较 | C++ | Wrong Answer | 1 MS | 276 KB | 1091 | 2025-01-23 17:18:39 |
#include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; int total_sum = N * (N + 1) / 2; // 计算总和 // 如果 (total_sum + M) 或 (total_sum - M) 不是偶数,则无解 if ((total_sum + M) % 2 != 0 || (total_sum - M) % 2 != 0) { cout << 0 << endl; return 0; } int sum1 = (total_sum + M) / 2; int sum2 = (total_sum - M) / 2; vector<int> dp(total_sum + 1, 0); // dp[j] 表示和为j的子数组的数量 dp[0] = 1; // 初始化:和为0的子数组有1种(空集) // 动态规划计算所有可能的子数组和 for (int i = 1; i <= N; ++i) { for (int j = total_sum; j >= i; --j) { dp[j] += dp[j - i]; } } // 统计满足条件的方案数 int count = 0; if (sum1 <= total_sum) count += dp[sum1]; if (sum2 <= total_sum) count += dp[sum2]; // 如果sum1 == sum2,需要减去重复计算的方案 if (sum1 == sum2) count /= 2; cout << count << endl; return 0; }
------Input------
5 3
------Answer-----
3
------Your output-----
6