用户输出
3
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#127 | #1017. JM的完美集合 | Accepted | 100 | 878 ms | 4460 K | C++ 11 / 1.4 K | JM233333 | 2019-06-08 21:02:19 |
#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> mp;
int main() {
// freopen("test.txt", "r", stdin);
int n;
while (scanf("%d", &n) != EOF) {
// 初始化
mp.clear();
// 输入
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;
for (int i = 0; i < m; i++) {
if ((status >> i) & 1) {
sum += a[i];
}
}
// 记录状态
if (mp.find(sum) != mp.end()) {
mp[sum]++;
} else {
mp[sum] = 1;
}
}
}
// 折半枚举:右半部分
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;
for (int i = 0; i < n - m; i++) {
if ((status >> i) & 1) {
sum += a[m + i];
}
}
// 记录状态
if (mp.find(-sum) != mp.end()) {
res += mp[-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>
用户输出
520729
系统信息
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>
用户输出
163619
系统信息
Exited with return code 0
用户输出
32767
系统信息
Exited with return code 0
34
-480 -519 50 980 136 511 821 -785 471 -781 -452 33 -828 -95 61 939 -372 -942 163 622 447 -48 -46
<51 bytes omitted>
用户输出
4162792
系统信息
Exited with return code 0
34
48519807 -53389835 49483403 17212025 -35476527 29754885 -22197599 -15471042 20851481 -49606949 -
<219 bytes omitted>
用户输出
47
系统信息
Exited with return code 0
31
58533164 -58898543 15918910 -44326927 -34936787 -22301908 -31391841 6479531 21841516 -39666628 5
<196 bytes omitted>
用户输出
9
系统信息
Exited with return code 0
35
7230510 -11706249 -3304187 10122861 9546298 -4442935 -33084582 -27010272 -43066828 -38618779 126
<227 bytes omitted>
用户输出
148
系统信息
Exited with return code 0
34
-41418109 33342962 -39042254 28956950 -426371 -52708040 27657595 13857069 -46409548 -4181307 -43
<221 bytes omitted>
用户输出
40
系统信息
Exited with return code 0
30
12891648 42473494 8187791 31646680 15117362 21049429 -5219011 -3836405 51563712 -23453243 493559
<181 bytes omitted>
用户输出
1
系统信息
Exited with return code 0
32
14064413 50585082 19765257 2999136 24894051 -42290511 20154916 -23633481 -46140190 -22836277 -14
<204 bytes omitted>
用户输出
21
系统信息
Exited with return code 0
34
-36585710 20358839 52054688 6497673 -20688047 16346134 -31405363 -32737212 -53397594 -44921061 -
<222 bytes omitted>
用户输出
57
系统信息
Exited with return code 0
35
16603843 -45892403 -45886820 -44441674 40785156 19628683 47358422 -19887459 38501762 -57319125 -
<233 bytes omitted>
用户输出
105
系统信息
Exited with return code 0
30
20140884 51573053 -52691015 -23126027 42246059 55981176 42361277 25020920 58763999 41810806 -376
<185 bytes omitted>
用户输出
3
系统信息
Exited with return code 0