"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;
    }

}


全部评论

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务