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;
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define out(file) freopen(file, "w", stdout)
#define in(file) freopen(file, "r", stdin)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
int MOD = 1e9 + 7;
const int N = 2001;
int y[N], a, b, n;
ll ps[N], dp[N][N];
void sub1() {
ll ans = inf;
for(int mask = 0; mask < (1 << n); mask++) {
if(((mask >> (n-1)) & 1) == 0) continue;
int cnt = __builtin_popcount(mask);
if(cnt < a or cnt > b) continue;
ll s = 0;
ll cur = 0;
// cerr << "CNT " << cnt << ln;
for(int i = 0; i < n; i++) {
s += y[i+1];
if((mask >> i) & 1) {
cur |= s;
// cerr << s << ' ';
s = 0;
}
}
// cerr << ln;
ans = min(ans, cur);
}
cout << ans;
}
bool f[51][51][1005];
void sub2() {
f[0][0][0] = 1;
for(int i = 1; i <= n; i++) {
f[i][1][ps[i]] = 1;
}
int mx = ps[n];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= b; j++) {
for(int k = 0; k <= mx; k++) {
for(int t = i + 1; t <= n; t++) {
f[t][j+1][k | (ps[t]-ps[i])] = f[t][j+1][k | (ps[t]-ps[i])] || f[i][j][k];
}
}
}
}
for(int k = 0; k <= mx; k++) {
for(int i = a; i <= b; i++) {
if(f[n][i][k]) {
cout << k;
return;
}
}
}
}
int g[101][2001];
void sub3() {
g[0][0] = 1;
int mx = ps[n];
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= mx; j++) {
g[i][j] = b + 1;
}
}
for(int i = 1; i <= n; i++) g[i][ps[i]] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= mx; j++) {
for(int t = i + 1; t <= n; t++) {
int cur = j | (ps[t] - ps[i]);
g[t][cur] = min(g[t][cur], g[i][j] + 1);
}
}
}
for(int i = 0; i <= mx; i++) {
if(g[n][i] <= b) {
cout << i;
return;
}
}
}
bool h[101][101];
void sub4() {
int k = __lg((ll)ps[n]);
int ans = 0;
// cerr << k << ln;
for(int z = k; z >= 0; z--) {
h[0][0] = 1;
// cerr << "BIT " << z << ln;
memset(h, 0, sizeof h);
for(int i = 1; i <= n; i++) {
if((ps[i] ^ ans) >= (1 << z)) h[i][1] = 0;
else h[i][1] = 1;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= b; j++) {
for(int t = i + 1; t <= n; t++) {
bool ok = 1;
int cur = ps[t] - ps[i];
if((cur ^ ans) >= (1 << z)) ok = 0;
h[t][j+1] = h[t][j+1] or (h[i][j] and ok);
}
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= b; j++) {
// cout << h[i][j] << ' ';
// }
// cout << ln;
// }
bool ok = 0;
for(int i = a; i <= b; i++) {
if(h[n][i]) {
ok = 1;
break;
}
}
if(not ok) ans = ans | (1 << z);
// cout << ans << ln;
}
cout << ans;
}
signed main() {
fast_cin();
cin >> n >> a >> b;
for(int i = 1; i <= n; i++) cin >> y[i], ps[i] = ps[i-1] + y[i];
// // if(n <= 20) sub1();
// if(n <= 20) sub1();
// else if(n <= 50) sub2();
// else if(n <= 100 and a == 1) sub3();
// else {
// sub4();
// }
sub4();
// cout << ln;
// sub2();
// sub3();
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | 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... |