Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] -- B. "Or" Game (容斥定理)

探讨了通过执行特定操作来最大化一组整数的异或运算值的方法。介绍了如何利用容斥原理正向与反向求解异或运算,最终实现最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        B. "Or" Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 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.

Examples
Input
3 1 2
1 1 1
Output
3
Input
4 2 3
1 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值