编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#22855 #1086. jwp的幸运集合 Accepted 100 89 ms 784 K C++ / 995 B 电类935-郑新宇 2020-02-15 0:54:23
显示原始代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int n, k, a[40], ans;
map<int, int> m1, m2;
// m1表示奇数个,m2表示偶数个
int main() {
    cin >> n;
    k = n >> 1;
    for (int i = 0; i < n; i++) cin >> a[i];
    int ma = 1 << k;
    for (int i = 1; i < ma; i++) {
        int sum = 0, cnt = 0;
        //状态压缩
        for (int j = 0; j < k; j++)
            if ((i >> j) & 1)
                cnt++, sum += a[j];
        //右移j位若末位是1说明该选择中有a[j]
        // a[]的下标要从0开始,右移0位就是自身,同理退后
        if (cnt & 1)
            m1[sum]++;  //奇数个和为sum的数量加一
        else {
            m2[sum]++;  //偶数个和为sum的数量加一
            if (!sum)
                ans++;  //符合题意的
        }
    }
    ma = 1 << (n - k);
    for (int i = 1; i < ma; i++) {
        int sum = 0, cnt = 0;
        for (int j = 0; j < n - k; j++)
            if ((i >> j) & 1)
                cnt++, sum += a[j + k];
        if (cnt & 1)
            ans = ans + m1[-sum];  //两个奇数个集合,-sum保证和为0
        else {
            ans = ans + m2[-sum];
            if (!sum)
                ans++;
        }
    }
    cout << ans;
    return 0;
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:2 ms
内存:284 KiB

输入文件(1.in

5
1 5 -4 3 -1

答案文件(1.out

1

用户输出

1

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:3 ms
内存:308 KiB

输入文件(2.in

6
-1 2 2 5 5 -5

答案文件(2.out

2

用户输出

2

系统信息

Exited with return code 0
测试点 #3
Accepted
得分:100
用时:3 ms
内存:344 KiB

输入文件(3.in

10
5 -5 5 1 2 3 3 -1 3 1

答案文件(3.out

14

用户输出

14

系统信息

Exited with return code 0
测试点 #4
Accepted
得分:100
用时:2 ms
内存:300 KiB

输入文件(4.in

1
0

答案文件(4.out

0

用户输出

0

系统信息

Exited with return code 0
测试点 #5
Accepted
得分:100
用时:2 ms
内存:348 KiB

输入文件(5.in

4
1 4 -1 0

答案文件(5.out

1

用户输出

1

系统信息

Exited with return code 0
测试点 #6
Accepted
得分:100
用时:3 ms
内存:356 KiB

输入文件(6.in

3
0 0 0

答案文件(6.out

3

用户输出

3

系统信息

Exited with return code 0
测试点 #7
Accepted
得分:100
用时:16 ms
内存:712 KiB

输入文件(7.in

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>

答案文件(7.out

260459

用户输出

260459

系统信息

Exited with return code 0
测试点 #8
Accepted
得分:100
用时:16 ms
内存:784 KiB

输入文件(8.in

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>

答案文件(8.out

81861

用户输出

81861

系统信息

Exited with return code 0
测试点 #9
Accepted
得分:100
用时:6 ms
内存:356 KiB

输入文件(9.in

30
0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15

答案文件(9.out

16383

用户输出

16383

系统信息

Exited with return code 0
测试点 #10
Accepted
得分:100
用时:36 ms
内存:732 KiB

输入文件(10.in

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>

答案文件(10.out

2080461

用户输出

2080461

系统信息

Exited with return code 0