"Or" Game(预处理优化,前后缀+暴力枚举)

ou 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 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 a1, a2, …, an (0 ≤ ai ≤ 109).

Output
Output the maximum value of a bitwise OR of sequence elements after performing operations.

SampleInput 1
3 1 2
1 1 1
SampleOutput 1
3
SampleInput 2
4 2 3
1 2 4 8
SampleOutput 2
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 .

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.

题意就是第一行输入n k x ,表示有n个数,可以进行k次操作,每次操作是一个数乘x,求进行k次操作后最后或和。
稍加分析可以知道,把这k次操作都乘到一个数上的时候,或和才会是最大的,比如第i个数 a[i]进行这k次操作,(记m=x^k)那么结果就是
qian[i-1] || a[i]*m || hou[i+1]
然后暴力枚举出最大值就是了。
下面是代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200005],qian[200005],hou[200005];
int main()
{
    ll n,k,x,i;
    while(cin>>n>>k>>x)
    {
        for(i=1;i<=n;i++)
            cin>>a[i];

        for(i=1;i<=n;i++)
            qian[i]=qian[i-1]|a[i];/// 前缀或和

        for(i=n;i>=1;i--)
            hou[i]=hou[i+1]|a[i];///后缀或和

        ll m=1;
        for(i=0;i<k;i++)
            m*=x;///x^k

        ll mmax=0;
        for(i=1;i<=n;i++)

            mmax = max(mmax,qian[i-1]|hou[i+1]|(m*a[i]));///枚举找最大

        cout<<mmax<<endl;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值