用户输出
1
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#22897 | #1086. jwp的幸运集合 | Accepted | 100 | 155 ms | 1764 K | C++ / 2.0 K | 计试91-李禹陪 | 2020-02-15 15:03:45 |
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false); //关cin同步;加速
int n;
cin >> n;
int c1[20];
int *sum1 = new int[1 << (n / 2)], *cnt1 = new int[1 << (n / 2)];
int result = 0;
for (int i = 0; i < n / 2; i++) cin >> c1[i];
map<int, int> a1, a2;
for (int i = 0; i < (1 << (n / 2)); i++) { //折半枚举二进制的位数,n个数
sum1[i] = 0, cnt1[i] = 0;
for (int j = 0; j < n / 2; j++) {
if (i & (1 << j))
sum1[i] += c1[n / 2 - 1 - j], cnt1[i]++;
}
}
for (int i = 0; i < (1 << (n / 2)); i++) {
if (a1.find(sum1[i]) == a1.end() && cnt1[i] % 2 == 1)
a1[sum1[i]] = 0;
if (a2.find(sum1[i]) == a2.end() && cnt1[i] % 2 == 0)
a2[sum1[i]] = 0;
if (a1.find(sum1[i]) != a1.end() && cnt1[i] % 2 == 1)
a1[sum1[i]]++; //分元素个数的奇偶
if (a2.find(sum1[i]) != a2.end() && cnt1[i] % 2 == 0)
a2[sum1[i]]++;
}
//下面解决下一半
delete[] sum1, delete[] cnt1;
int c2[20];
for (int i = 0; i < n / 2; i++) cin >> c2[i];
if (n % 2 == 1)
cin >> c2[n / 2];
int *sum2 = new int[1 << (n / 2)], *cnt2 = new int[1 << (n / 2)];
for (int i = 0; i < (1 << (n / 2)); i++) {
sum2[i] = 0, cnt2[i] = 0;
for (int j = 0; j < n / 2; j++) {
if (i & (1 << j))
sum2[i] += c2[n / 2 - 1 - j], cnt2[i]++;
}
}
for (int i = 0; i < (1 << (n / 2)); i++) {
if (cnt2[i] % 2 == 1 && a1.find(-sum2[i]) != a1.end())
result += a1[-sum2[i]];
if (cnt2[i] % 2 == 0 && a2.find(-sum2[i]) != a2.end())
result += a2[-sum2[i]];
}
if (n % 2 == 1) { //如果是奇数 就将最后一个拿在最后单独讨论 没有选的前面已经讨论过了,这里直接选上。
for (int i = 0; i < (1 << (n / 2)); i++) sum2[i] += c2[n / 2], cnt2[i]++;
for (int i = 0; i < (1 << (n / 2)); i++) {
if (cnt2[i] % 2 == 1 && a1.find(-sum2[i]) != a1.end())
result += a1[-sum2[i]];
if (cnt2[i] % 2 == 0 && a2.find(-sum2[i]) != a2.end())
result += a2[-sum2[i]];
}
}
delete[] sum2, delete[] cnt2;
cout << result - 1;
return 0;
}
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