“Or" Game CodeForces - 578B (暴力)

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 koperations 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.
Example
Input
3 1 2
1 1 1
Output
3
Input
4 2 3
1 2 4 8
Output
79
题目大意:有n个数你能对他们进行k次操作每次操作对其中一个数乘x问如何操作之后使其两两按位或值最大
解题思路:为了使值最大很显然应该操作二进制位数最多的数,但注意不是想当然的对最大ai进行操作就会获得最大值二进制位数同等数量的值具有相同的机会所以应该枚举。但不能直接暴力每次从头到尾|这样会超时,可以使用前缀数组和后缀数组。
此处的前缀和指某数组的前i项和。
如给定数组a[n],求sum[n]。其中sum[i]=a[0]+a[1]+…+a[i]
这里的sum[]数组即为数组a的前缀和数组。
那么前缀和有什么用处呢?
假设我要求a[x]+a[x+1]+a[x+2]+…+a[y]
那么这个答案就等于 sum[y]-sumx-1。若x=0则等于sum[y]。
有一点:如果原数组的数字是非负的,那么sum数组则是非递减的,在搜索的时候可以考虑使用二分。(后缀同理)
ac代码

#include<bits/stdc++.h>
using namespace std;
#define max 200000+5
long long a[max],pre[max],beh[max];
int main() {
    long long n,k,x;
    scanf("%lld%lld%lld",&n,&k,&x);
    long long x1=1;
    for(int i=1;i<=k;i++)
    x1*=x;
    for(int i=1;i<=n;i++)
    {scanf("%lld",&a[i]);
     pre[i]=pre[i-1]|a[i];
    }
    for(int i=n;i;i--)
    {
        beh[i]=beh[i+1]|a[i];
    }
    long long ans=-1;
   for(int i=1;i<=n;i++)
   {
        long long sum=pre[i-1]|(a[i]*x1)|beh[i+1];
       if(sum>ans)
       ans=sum;
   }
   printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值