01背包的一道变形
题目链接
代码中已表明注释
ACcode
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int M = 1e4 + 9;
ll dp[M];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll n;cin >> n;
ll m = n * (n + 1) / 2;
if (m % 2) {//必须是偶数才能分为两个和相同的集合
cout << 0;
return 0;
}
m /= 2;//背包容量
dp[0] = 1;
for (int i = 1;i <= n;i++) {//每个物品
for (int j = m;j >= i;j--) {
dp[j] += dp[j - i];//累加方案
}
}
cout << dp[m] / 2;
return 0;
}