#include<bits/stdc++.h>
#define el '\n'
using namespace std ;
const int MN = 2e5 + 50;
int n;
namespace sub_trau{
int64_t dp[MN];
// check whether existing a way to divide array into 2 parts with sum = x;
void solve(){
vector<int> a(n + 5);
for ( int i = 1 ; i <= n ; i++ ) cin >> a[i];
if (n & 1){
cout << 0;
return;
}
bool odd = false , even = false;
for ( int i = 1 ; i <= n ; i++ ){
odd |= (a[i] & 1);
even |= (a[i] % 2 == 0);
}
if (odd && even){
cout << 0;
return;
}
int sum = accumulate(a.begin() + 1 , a.begin() + n + 1 , 0);
int mn = *min_element(a.begin() + 1 , a.begin() + n + 1);
dp[0] = 1;
for ( int i = 1 ; i <= n ; i++ ){
for ( int j = sum ; j >= a[i] ; j-- ) dp[j] += dp[j - a[i]];
}
if (!dp[sum / 2] || (sum & 1)){
cout << 0;
return;
}
vector<int> ans , candidates;
for ( int i = 1 ; i <= n ; i++ ) candidates.push_back(a[i]);
for ( int val = odd ? 1 : 2 ; val <= sum - mn ; val += 2 ){
int all = sum + val;
candidates.push_back(val);
for ( int j = all ; j >= val ; j-- ) dp[j] += dp[j - val];
bool check = true;
for ( int turn = 0 ; turn < n ; turn++ ){
if (!check) break;
int need = all - candidates[turn];
if (need & 1){
check = false;
break;
}
for ( int j = candidates[turn] ; j <= need / 2 ; j++ ){
dp[j] -= dp[j - candidates[turn]];
}
if (!dp[need / 2]){
check = false;
}
for ( int j = need / 2 ; j >= candidates[turn] ; j-- ) dp[j] += dp[j - candidates[turn]];
}
if (check) ans.push_back(val);
candidates.pop_back();
for ( int j = val ; j <= all ; j++ ) dp[j] -= dp[j - val];
}
cout << ans.size() << el;
for ( auto x : ans ) cout << x << " ";
}
}
namespace sub_final{
__int128 dp[MN];
bool choose[MN];
void solve(){
vector<int> a(n + 5);
for ( int i = 1 ; i <= n ; i++ ) cin >> a[i];
bool odd = false , even = false;
for ( int i = 1 ; i <= n ; i++ ){
odd |= (a[i] & 1);
even |= (a[i] % 2 == 0);
}
if (odd && even){
cout << 0;
return;
}
int sum = accumulate(a.begin() + 1 , a.begin() + n + 1 , 0);
dp[0] = 1;
for ( int i = 1 ; i <= n ; i++ ){
for ( int j = sum ; j >= a[i] ; j-- ) dp[j] += dp[j - a[i]];
}
if (!dp[sum / 2] || (sum & 1)){
cout << 0;
return;
}
memset(choose , true , sizeof(choose));
for ( int i = odd ? 2 : 1 ; i <= sum ; i += 2 ) choose[i] = false;
for ( int i = 1 ; i <= n ; i++ ){
vector<int64_t> ndp(sum + 5);
for ( int j = 0 ; j <= sum ; j++ ) ndp[j] = dp[j];
for ( int j = a[i] ; j <= sum ; j++ ) ndp[j] -= ndp[j - a[i]];
for ( int val = odd ? 1 : 2 ; val <= sum ; val += 2 ){
int new_sum = sum - a[i] + val;
if ((new_sum & 1) || (new_sum / 2 < val) || !ndp[new_sum / 2 - val]) choose[val] = false;
}
}
vector<int> ans;
for ( int i = 1 ; i <= sum ; i++ ){
if (choose[i]) ans.push_back(i);
}
cout << ans.size() << el;
for ( auto x : ans ) cout << x << " ";
}
}
int32_t main (){
// freopen("bootfall.inp" , "r" , stdin);
// freopen("bootfall.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
if (n <= 30) sub_trau::solve();
else sub_final::solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
4 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
4 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
10 ms |
2756 KB |
Output is correct |
38 |
Correct |
10 ms |
2752 KB |
Output is correct |
39 |
Correct |
36 ms |
3164 KB |
Output is correct |
40 |
Correct |
22 ms |
2908 KB |
Output is correct |
41 |
Correct |
46 ms |
3164 KB |
Output is correct |
42 |
Correct |
20 ms |
3180 KB |
Output is correct |
43 |
Correct |
35 ms |
3104 KB |
Output is correct |
44 |
Correct |
37 ms |
3136 KB |
Output is correct |
45 |
Correct |
35 ms |
3228 KB |
Output is correct |
46 |
Correct |
20 ms |
2916 KB |
Output is correct |
47 |
Correct |
21 ms |
2908 KB |
Output is correct |
48 |
Correct |
39 ms |
3172 KB |
Output is correct |
49 |
Correct |
38 ms |
3180 KB |
Output is correct |
50 |
Correct |
29 ms |
2908 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
49 ms |
3208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
4 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
10 ms |
2756 KB |
Output is correct |
38 |
Correct |
10 ms |
2752 KB |
Output is correct |
39 |
Correct |
36 ms |
3164 KB |
Output is correct |
40 |
Correct |
22 ms |
2908 KB |
Output is correct |
41 |
Correct |
46 ms |
3164 KB |
Output is correct |
42 |
Correct |
20 ms |
3180 KB |
Output is correct |
43 |
Correct |
35 ms |
3104 KB |
Output is correct |
44 |
Correct |
37 ms |
3136 KB |
Output is correct |
45 |
Correct |
35 ms |
3228 KB |
Output is correct |
46 |
Correct |
20 ms |
2916 KB |
Output is correct |
47 |
Correct |
21 ms |
2908 KB |
Output is correct |
48 |
Correct |
39 ms |
3172 KB |
Output is correct |
49 |
Correct |
38 ms |
3180 KB |
Output is correct |
50 |
Correct |
29 ms |
2908 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
49 ms |
3208 KB |
Output is correct |
53 |
Correct |
35 ms |
3064 KB |
Output is correct |
54 |
Correct |
50 ms |
3136 KB |
Output is correct |
55 |
Correct |
109 ms |
3784 KB |
Output is correct |
56 |
Correct |
43 ms |
3236 KB |
Output is correct |
57 |
Correct |
114 ms |
3836 KB |
Output is correct |
58 |
Correct |
67 ms |
3164 KB |
Output is correct |
59 |
Correct |
67 ms |
3304 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
75 ms |
3332 KB |
Output is correct |
62 |
Correct |
78 ms |
3676 KB |
Output is correct |
63 |
Correct |
85 ms |
3732 KB |
Output is correct |
64 |
Correct |
83 ms |
3724 KB |
Output is correct |
65 |
Correct |
36 ms |
3240 KB |
Output is correct |
66 |
Correct |
39 ms |
3000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
4 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
10 ms |
2756 KB |
Output is correct |
38 |
Correct |
10 ms |
2752 KB |
Output is correct |
39 |
Correct |
36 ms |
3164 KB |
Output is correct |
40 |
Correct |
22 ms |
2908 KB |
Output is correct |
41 |
Correct |
46 ms |
3164 KB |
Output is correct |
42 |
Correct |
20 ms |
3180 KB |
Output is correct |
43 |
Correct |
35 ms |
3104 KB |
Output is correct |
44 |
Correct |
37 ms |
3136 KB |
Output is correct |
45 |
Correct |
35 ms |
3228 KB |
Output is correct |
46 |
Correct |
20 ms |
2916 KB |
Output is correct |
47 |
Correct |
21 ms |
2908 KB |
Output is correct |
48 |
Correct |
39 ms |
3172 KB |
Output is correct |
49 |
Correct |
38 ms |
3180 KB |
Output is correct |
50 |
Correct |
29 ms |
2908 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
49 ms |
3208 KB |
Output is correct |
53 |
Correct |
35 ms |
3064 KB |
Output is correct |
54 |
Correct |
50 ms |
3136 KB |
Output is correct |
55 |
Correct |
109 ms |
3784 KB |
Output is correct |
56 |
Correct |
43 ms |
3236 KB |
Output is correct |
57 |
Correct |
114 ms |
3836 KB |
Output is correct |
58 |
Correct |
67 ms |
3164 KB |
Output is correct |
59 |
Correct |
67 ms |
3304 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
75 ms |
3332 KB |
Output is correct |
62 |
Correct |
78 ms |
3676 KB |
Output is correct |
63 |
Correct |
85 ms |
3732 KB |
Output is correct |
64 |
Correct |
83 ms |
3724 KB |
Output is correct |
65 |
Correct |
36 ms |
3240 KB |
Output is correct |
66 |
Correct |
39 ms |
3000 KB |
Output is correct |
67 |
Correct |
82 ms |
3300 KB |
Output is correct |
68 |
Correct |
55 ms |
2488 KB |
Output is correct |
69 |
Incorrect |
267 ms |
6128 KB |
Output isn't correct |
70 |
Halted |
0 ms |
0 KB |
- |