用户输出
1
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#14413 | #1086. jwp的幸运集合 | Accepted | 100 | 114 ms | 804 K | C++ / 1.8 K | lenijwp | 2019-07-08 16:01:17 |
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
void half_enum_left(int n);
int half_enum_right(int n);
const int MAX = 40;
int a[MAX];
map<int, int> mp0;
map<int, int> mp1;
int main() {
// freopen("test.txt", "r", stdin);
int n;
while (scanf("%d", &n) != EOF) {
// 初始化
// 输入
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// 折半枚举
half_enum_left(n);
int res = half_enum_right(n);
// 输出
printf("%d\n", res);
}
return 0;
}
// 折半枚举:左半部分
void half_enum_left(int n) {
// 枚举
int m = n / 2;
int bound = (1 << m);
for (int status = 0; status < bound; status++) {
// 计算状态
int sum = 0;
int cnt = 0;
for (int i = 0; i < m; i++) {
if ((status >> i) & 1) {
cnt++;
sum += a[i];
}
}
// 记录状态
if (cnt % 2 == 1) {
mp1[sum]++;
} else {
mp0[sum]++;
}
}
}
// 折半枚举:右半部分
int half_enum_right(int n) {
// 枚举
int res = 0;
int m = n / 2;
int bound = (1 << (n - m));
for (int status = 0; status < bound; status++) {
// 计算状态
int sum = 0;
int cnt = 0;
for (int i = 0; i < n - m; i++) {
if ((status >> i) & 1) {
cnt++;
sum += a[m + i];
}
}
// 记录状态
if (cnt % 2 != 0) {
res += mp1[-sum];
} else {
res += mp0[-sum];
}
}
// 特判空子集
res--;
// 返回
return res;
}
31
749 349 69 -880 282 -926 -831 225 988 552 -193 634 369 476 204 487 387 -803 -370 -975 -17 -485 3
<40 bytes omitted>
用户输出
260459
系统信息
Exited with return code 0
31
362 -14 -181 -465 -400 -232 26 963 70 728 -728 -222 -510 854 139 386 555 994 103 279 213 76 953
<36 bytes omitted>
用户输出
81861
系统信息
Exited with return code 0
用户输出
16383
系统信息
Exited with return code 0