3 solutions
-
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int target, n; cin >> n; vector<int> a(n); int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } target=sum/2; if (sum%2!=0) { cout << 0 << endl; return 0; } vector<int> dp(target + 1, 0); dp[0] = 1; for (int i = 0; i < a.size(); i++) { for (int j = target; j >= a[i]; j--) { dp[j] += dp[j - a[i]]; } } cout<<dp[target]; return 0; }
-
0
#数据读取 n = int(input()) nums = list(map(int, input().split())) # print(nums) 数据正确读入 #子序列之和为原数组总和的一半 我们计算原数组总和的一半并设为target 由于不存在小数,我们可以进行简单的优化 tmp = sum(nums) if tmp%2==1: print(0) #target不是整数,不可能存在有效方案 else: target = tmp//2 #f[0][0] 表示选0个数和为0的方案 初始化为1 f[i][target]表示符合条件的状态数 除f[0][0]外均初始化为0 f = [[0]*(target+1) for _ in range(n+1)] f[0][0] = 1 for i in range(n): for j in range(target+1): if j>=nums[i]: f[i+1][j] = f[i][j] + f[i][j-nums[i]] else: f[i+1][j] = f[i][j] print(f[n][target])
-
0
from collections import Counter n = int(input()) nums = list(map(int,input().split())) sum_num = sum(nums) if sum_num%2==1: print(0) else: upper = (sum(nums)+1)//2 dp = Counter({0:1}) for each in nums: temp_dict = {} for each_key in dp: if each_key+each <=upper: temp_dict[each_key+each] =dp[each_key] dp = dp+ Counter(temp_dict) print(dp[sum_num//2])
- 1
Information
- ID
- 44
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- # Submissions
- 117
- Accepted
- 39
- Uploaded By