B. "Or" Game
time limit per test
memory limit per test
input
output
You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make
as large as possible, where
denotes the bitwise OR. Find the maximum possible value of
after performing at most k
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 a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
Output the maximum value of a bitwise OR of sequence elements after performing operations.
Examples
Input
3 1 21 1 1
Output
3
Input
4 2 31 2 4 8
Output
79
Note
大体题意:
给你n 个元素的集合!
可以进行k 个操作,每个操作可以给一个数乘以x
问最后的集合 进行异或运算最大值是多少!
思路:
利用容斥定理,先正着求一遍 异或运算
在反着求一遍。
最后枚举每一个数 乘以 k 个x 连乘
用这个数的前面异或这个数在异或后面的数即可!
ll tmp = bef[i-1] | (a[i]*x) | aft[i+1];
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 10;
typedef long long ll;
ll bef[maxn],aft[maxn],a[maxn];
int main(){
ll n,k,x;
scanf("%I64d%I64d%I64d",&n,&k,&x);
for (int i = 1; i <= n; ++i){
scanf("%I64d",&a[i]);
}
ll xx= 1;
for (int i = 0; i < k; ++i)xx*=x;
x=xx;
for (int i = 1; i <= n; ++i){
bef[i] = bef[i-1] | a[i];
}
for (int i = n; i >= 1; --i){
aft[i] = aft[i+1] | a[i];
}
ll ans = 0;
for (int i = 1; i <= n; ++i){
ll tmp = bef[i-1] | (a[i]*x) | aft[i+1];
ans = max(ans,tmp);
}
printf("%I64d\n",ans);
return 0;
}