Codeforces Round #320 (Div. 1) [Bayan Thanks-Round]
Topic link: B. "Or" Game
You are given \(n\) numbers \(a_1, a_2, ..., a_n\). You can perform at most \(k\) operations. For each operation you can multiply one of the numbers by \(x\). We want to make \(a_1 | a_2 | ... | a_n\) as large as possible, where \(|\) denotes the bitwise OR.
Find the maximum possible value of \(a_1 | a_2 | ... | a_n\) after performing at most \(k\) operations optimally.
Input
The first line contains three integers \(n\), \(k\) and \(x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8)\).
The second line contains \(n\) integers \(a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9)\).
Output
Output the maximum value of a bitwise OR of sequence elements after performing operations.
Examples
input
3 1 2 1 1 1
output
3
input
4 2 3 1 2 4 8
output
79
Note
For the first sample, any possible choice of doing one operation will result the same three numbers \(1, 1, 2\) so the result is \(1 | 1 | 2 = 3\).
For the second sample if we multiply \(8\) by \(3\) two times we'll get \(72\). In this case the numbers will become \(1, 2, 4, 72\) so the OR value will be \(79\) and is the largest possible result.
Solution
The meaning of problems
Given \ (n-\) number \ (A_1 ... A_N \) , may be \ (K \) operations, each time a number can be multiplied by any given \ (X \) , seeking \ (a_1 | a_2 | ... | a_n \) up to much.
answer
Prefix and greed
Since \ (the X-\ GE 2 \) , and therefore a number multiplied by \ (x \) after an increase in the number of bits inevitable.
Since the operation is or \ (0 | 1 \) if the answer will increase, so let \ (k \) a \ (x \) multiplied by the number on the same line.
Prefixes and suffixes and calculate and then multiplied by the number of violent enumerate each \ (the X-^ k \) , you can find the maximum value.
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
ll a[maxn], s1[maxn], s2[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, k, x;
cin >> n >> k >> x;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
s1[i] = s1[i - 1] | a[i];
}
for(int i = n; i >= 1; --i) {
s2[i] = s2[i + 1] | a[i];
}
ll ans = 0;
ll tmp = x;
for(int i = 2; i <= k; ++i) {
tmp *= x;
}
for(int i = 1; i <= n; ++i) {
ans = max(ans, s1[i - 1] | tmp * a[i] | s2[i + 1]);
}
cout << ans << endl;
return 0;
}