This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << ": " << x << endl;
const int INF = 1e9;
const int MAX_BIT = 44;
const int MAX_N = 2000 + 5;
const int MAX_X = 20;
int a[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, l, r;
cin >> n >> l >> r;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
// minimize (a1 + a2 + ... + a_(k - 1)) | (a_k + a_(k + 1) + ...) | ...
// IMPORTANT NOTE: IN LAST GROUP n = 2000 and l = 1
// maybe greedy bit by bit? minimize first, then second, ...
if (l == 1) {
auto check = [&](ll mask) {
vector<int> dp(n + 1, INF);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
ll s = 0;
for (int j = i; j > 0; --j) {
s += a[j];
if ((s & mask) == 0) {
dp[i] = min(dp[i], dp[j - 1] + 1);
}
}
}
return dp[n] <= r;
};
ll ans = 0;
ll mask = 0;
for (int bit = MAX_BIT; bit >= 0; --bit) {
if (!check(mask | (1LL << bit))) {
ans |= 1LL << bit;
} else {
mask |= 1LL << bit;
}
}
cout << ans << '\n';
} else {
auto check = [&](ll mask) {
vector<vector<bool>> dp(n + 1, vector<bool>(n + 1));
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
ll s = 0;
for (int j = i; j > 0; --j) {
s += a[j];
if ((s & mask) == 0) {
for (int k = 0; k <= j - 1; ++k) {
dp[i][k + 1] = dp[i][k + 1] | dp[j - 1][k];
}
}
}
}
for (int i = l; i <= r; ++i) {
if (dp[n][i]) {
return true;
}
}
return false;
};
ll ans = 0;
ll mask = 0;
for (int bit = MAX_BIT; bit >= 0; --bit) {
if (!check(mask | (1LL << bit))) {
ans |= 1LL << bit;
} else {
mask |= 1LL << bit;
}
}
cout << ans << '\n';
}
return 0;
}
/*
6 1 3
8 1 2 1 5 4
5 5 5
1 1 1 1 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |