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 <iostream>
#include <vector>
#include <algorithm>
#define upmax(a, b) a = max(a, b);
#define upmin(a, b) a = min(a, b);
#define pr(x) cout << x << endl;
#define wpr(x) cout << #x << ": " << x << endl;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
const ll inf = 1e18;
const ll bits = 62;
ll n, a, b;
vll arr;
inline bool check(ll bit, ll mask, ll x)
{
mask >>= bit;
x >>= bit;
return (mask | x) == mask;
}
bool check1(ll mask, ll bit)
{
// dp[i] = minimum groups needed to make i first vals good.
vll dp(n + 1, inf);
dp[0] = 0;
for (ll i = 0; i <= n; i++) {
if (dp[i] == inf) continue;
ll csum = 0;
for (ll j = i + 1; j <= n; j++) {
csum += arr[j - 1];
if (check(bit, mask, csum)) {
upmin(dp[j], dp[i] + 1);
}
}
}
return dp[n] <= b;
}
bool check2(ll mask, ll bit)
{
// dp[i][j] = is it possible to make it ok for mask with i first vals using j groups?
vvb dp(n + 1, vb(b+1, 0));
dp[0][0] = 1;
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j < b; j++) {
if (!dp[i][j]) continue;
ll csum = 0;
for (ll k = i + 1; k <= n; k++) {
csum += arr[k - 1];
if (check(bit, mask, csum)) {
dp[k][j + 1] = 1;
}
}
}
}
for (ll j = a; j <= b; j++) {
if (dp[n][j]) return true;
}
return false;
}
int main()
{
cin >> n >> a >> b;
arr.resize(n);
for (ll i = 0; i < n; i++) cin >> arr[i];
ll mask = 0;
for (ll bit = bits - 1; bit >= 0; bit--) {
if (a == 1) {
if (!check1(mask, bit)) mask += (ll)1 << bit;
}
else {
if (!check2(mask, bit)) mask += (ll)1 << bit;
}
}
pr(mask);
}
/*
6 1 3
8 1 2 1 5 4
8 1 4
1 2 1 1 1 0 4 6
3 2 3
1 3 4
*/
# | 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... |