#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int N, L, R;
vector<lint> A;
vector<lint> prefix_sum;
vector<vector<int8_t>> General_DP;
vector<int> Special_DP;
inline lint get(int l, int r) {
return prefix_sum[r] - ((l > 0)? prefix_sum[l - 1] : 0);
}
int8_t GeneralDP(int n, int groups, lint mask) { // O(N ^ 3) for any L, R, finds if solution exists
if (n == N)
return (L <= groups && groups <= R);
if (n > N)
return false;
if (groups > R)
return false;
if (General_DP[n][groups] != -1)
return General_DP[n][groups];
int8_t &res = General_DP[n][groups] = 0;
for (int i = n; i < N; i++)
if ((get(n, i) | mask) == mask)
res |= GeneralDP(i + 1, groups + 1, mask);
return res;
}
int SpecialDP(int n, lint mask) { // O(N ^ 2) for L = 1, finds minimum number of groups
if (n == N)
return 0;
if (n > N)
return N + 1;
if (Special_DP[n] != -1)
return Special_DP[n];
int &res = Special_DP[n] = N + 1;
for (int i = n; i < N; i++)
if ((get(n, i) | mask) == mask)
res = min(res, 1 + SpecialDP(i + 1, mask));
return res;
}
bool solve(lint mask) { // Check if it is possible to get beauty of a subset of mask
if (L == 1) {
Special_DP.assign(N, -1);
return (SpecialDP(0, mask) <= R);
} else {
General_DP.assign(N, vector<int8_t>(R + 1, -1));
return GeneralDP(0, 0, mask);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> L >> R;
A.reserve(N), A.resize(N);
prefix_sum.reserve(N), prefix_sum.resize(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
prefix_sum[i] = ((i > 0)? prefix_sum[i - 1] : 0) + A[i];
}
lint ans = (1ll << 62ll) - 1;
for (lint bit = 61; bit >= 0; bit--)
if (solve(ans - (1ll << bit)))
ans -= (1ll << bit);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
4 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2 ms |
256 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
380 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
380 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
3 ms |
380 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
252 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
4 ms |
376 KB |
Output is correct |
29 |
Correct |
4 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
376 KB |
Output is correct |
31 |
Correct |
4 ms |
376 KB |
Output is correct |
32 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
7 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
3 ms |
376 KB |
Output is correct |
43 |
Correct |
3 ms |
376 KB |
Output is correct |
44 |
Correct |
2 ms |
372 KB |
Output is correct |
45 |
Correct |
2 ms |
256 KB |
Output is correct |
46 |
Correct |
3 ms |
376 KB |
Output is correct |
47 |
Correct |
3 ms |
256 KB |
Output is correct |
48 |
Correct |
3 ms |
376 KB |
Output is correct |
49 |
Correct |
8 ms |
256 KB |
Output is correct |
50 |
Correct |
4 ms |
376 KB |
Output is correct |
51 |
Correct |
4 ms |
420 KB |
Output is correct |
52 |
Correct |
4 ms |
376 KB |
Output is correct |
53 |
Correct |
4 ms |
376 KB |
Output is correct |
54 |
Correct |
4 ms |
376 KB |
Output is correct |
55 |
Correct |
4 ms |
376 KB |
Output is correct |
56 |
Correct |
2 ms |
256 KB |
Output is correct |
57 |
Correct |
3 ms |
376 KB |
Output is correct |
58 |
Correct |
3 ms |
376 KB |
Output is correct |
59 |
Correct |
3 ms |
376 KB |
Output is correct |
60 |
Correct |
3 ms |
376 KB |
Output is correct |
61 |
Correct |
3 ms |
376 KB |
Output is correct |
62 |
Correct |
3 ms |
376 KB |
Output is correct |
63 |
Correct |
3 ms |
376 KB |
Output is correct |
64 |
Correct |
52 ms |
376 KB |
Output is correct |
65 |
Correct |
6 ms |
376 KB |
Output is correct |
66 |
Correct |
6 ms |
376 KB |
Output is correct |
67 |
Correct |
13 ms |
504 KB |
Output is correct |
68 |
Correct |
27 ms |
376 KB |
Output is correct |
69 |
Correct |
36 ms |
376 KB |
Output is correct |
70 |
Correct |
35 ms |
396 KB |
Output is correct |
71 |
Correct |
4 ms |
504 KB |
Output is correct |
72 |
Correct |
38 ms |
392 KB |
Output is correct |
73 |
Correct |
38 ms |
376 KB |
Output is correct |
74 |
Correct |
38 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
252 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
252 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
3 ms |
380 KB |
Output is correct |
35 |
Correct |
0 ms |
376 KB |
Output is correct |
36 |
Correct |
3 ms |
376 KB |
Output is correct |
37 |
Correct |
4 ms |
376 KB |
Output is correct |
38 |
Correct |
4 ms |
376 KB |
Output is correct |
39 |
Correct |
4 ms |
376 KB |
Output is correct |
40 |
Correct |
16 ms |
376 KB |
Output is correct |
41 |
Correct |
4 ms |
376 KB |
Output is correct |
42 |
Correct |
4 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
376 KB |
Output is correct |
44 |
Correct |
3 ms |
376 KB |
Output is correct |
45 |
Correct |
3 ms |
376 KB |
Output is correct |
46 |
Correct |
3 ms |
376 KB |
Output is correct |
47 |
Correct |
3 ms |
376 KB |
Output is correct |
48 |
Correct |
3 ms |
376 KB |
Output is correct |
49 |
Correct |
4 ms |
376 KB |
Output is correct |
50 |
Correct |
3 ms |
424 KB |
Output is correct |
51 |
Correct |
3 ms |
376 KB |
Output is correct |
52 |
Correct |
32 ms |
376 KB |
Output is correct |
53 |
Correct |
45 ms |
380 KB |
Output is correct |
54 |
Correct |
99 ms |
504 KB |
Output is correct |
55 |
Correct |
87 ms |
472 KB |
Output is correct |
56 |
Correct |
297 ms |
504 KB |
Output is correct |
57 |
Correct |
289 ms |
504 KB |
Output is correct |
58 |
Correct |
300 ms |
596 KB |
Output is correct |
59 |
Correct |
330 ms |
604 KB |
Output is correct |
60 |
Correct |
311 ms |
632 KB |
Output is correct |
61 |
Correct |
3 ms |
376 KB |
Output is correct |
62 |
Correct |
46 ms |
376 KB |
Output is correct |
63 |
Correct |
100 ms |
472 KB |
Output is correct |
64 |
Correct |
77 ms |
376 KB |
Output is correct |
65 |
Correct |
148 ms |
632 KB |
Output is correct |
66 |
Correct |
229 ms |
632 KB |
Output is correct |
67 |
Correct |
374 ms |
632 KB |
Output is correct |
68 |
Correct |
377 ms |
504 KB |
Output is correct |
69 |
Correct |
287 ms |
508 KB |
Output is correct |