Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

时间:2023-03-10 02:53:39
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

B. "Or" Game

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/578/problem/B

Description

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 Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心 as large as possible, where Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心 denotes the bitwise OR.

Find the maximum possible value of Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心 after performing at most k operations optimally.

Input

The first line contains three integers nk 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.

Sample Input

3 1 2
1 1 1

Sample Output

3

HINT

题意

给你n个数,你可以操作k次,使得其中的某一个数乘以x

要求最后得到的所有数的或值最大

题解:

首先dp是错的,因为a>b不能保证a|c>b|c

这儿有一个贪心的操作,如果又一次乘法给了a1,那么剩下的都得给a1,这样才能得到最优值

因为x>=2可以保证最高位的1在不断增加

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000000 + 500
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
long long n , k , x; struct tree
{
int L , R ;
ll sum;
}; tree a[maxn * ];
ll d[maxn];
void build(int x,int l,int r)
{
a[x].L = l,a[x].R = r;
if(l == r)
{
a[x].sum = d[l];
return;
}
else
{
int mid = (l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].sum = a[x<<].sum | a[x<<|].sum;
}
} ll query(int o,int QL,int QR)
{
int L = a[o].L , R = a[o].R;
if (QL <= L && R <= QR) return a[o].sum;
else
{
int mid = (L+R)>>;
ll res = ;
if (QL <= mid) res |= query(*o,QL,QR);
if (QR > mid) res |= query(*o+,QL,QR);
return res;
}
}
int main()
{
cin>>n>>k>>x; for(int i = ; i <= n ; ++ i)
{
scanf("%I64d",&d[i]);
}
build( , , n+);
ll ans = ;
ll temp = ;
for(int i=;i<=k;i++)
temp *= x;
for(int i = ; i <= n ; ++ i)
{
ans = max( ans , (d[i]*temp) | query(,,i-) | query(,i+,n+));
}
cout<<ans<<endl;
}