이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
using namespace std;
inline bool check(ll s, ll conf, int bit) {
return ((s | conf) >> bit) == (conf >> bit);
}
inline ll solve1(int n, int a, int b, vector <int> &arr) {
ll sum = 0;
for(auto it : arr) {
sum += it;
}
ll conf = 0;
for(int bit = 60; bit >= 0; bit--) {
if((1LL << bit) > sum) {
continue;
}
vector < vector <bool> > dp;
int i, j;
dp.resize(n + 1);
for(i = 0; i <= n; i++) {
dp[i].resize(b + 1);
}
dp[0][0] = 1;
for(i = 1; i <= n; i++) {
for(int k = 1; k <= b; k++) {
ll s = 0;
for(j = i - 1; j >= 0; j--) {
s += arr[j + 1];
if(dp[j][k - 1] && check(s, conf, bit)) {
dp[i][k] = 1;
break;
}
}
}
}
bool ok = 0;
for(i = a; i <= b; i++) {
if(dp[n][i]) {
ok = 1;
}
}
if(!ok) {
conf += (1LL << bit);
}
}
return conf;
}
inline ll solve2(int n, vector <int> &arr, int b) {
ll sum = 0;
for(auto it : arr) {
sum += it;
}
ll conf = 0;
for(int bit = 60; bit >= 0; bit--) {
if((1LL << bit) > sum) {
continue;
}
vector < pair <bool, int> > dp(n + 1);
int i, j;
dp[0] = {1, 0};
for(i = 1; i <= n; i++) {
ll s = 0;
dp[i].second = n + 1;
for(j = i - 1; j >= 0; j--) {
s += arr[j + 1];
if(dp[j].first && check(s, conf, bit)) {
dp[i].first = 1;
dp[i].second = min(dp[i].second, dp[j].second + 1);
}
}
}
if(!dp[n].first || dp[n].second > b) {
conf += (1LL << bit);
}
}
return conf;
}
int main() {
//ifstream cin("C.in");
//ofstream cout("C.out");
int i, n, a, b;
ios::sync_with_stdio(false);
cin >> n >> a >> b;
vector <int> arr(n + 1);
for(i = 1; i <= n; i++) {
cin >> arr[i];
}
if(n <= 100) {
cout << solve1(n, a, b, arr);
}
else {
cout << solve2(n, arr, b);
}
//cin.close();
//cout.close();
return 0;
}
# | 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... |