Codeforces Round #320 (Div. 1) [Bayan Thanks-Round]

题目链接:B. "Or" Game

You are given \(n\) numbers \(a_1, a_2, ..., a_n\). You can perform at most \(k\) operations. For each operation you can multiply one of the numbers by \(x\). We want to make \(a_1 | a_2 | ... | a_n\) as large as possible, where \(|\) denotes the bitwise OR.

Find the maximum possible value of \(a_1 | a_2 | ... | a_n\) 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 \(a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9)\).

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

For the first sample, any possible choice of doing one operation will result the same three numbers \(1, 1, 2\) so the result is \(1 | 1 | 2 = 3\).

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.

Solution

题意

给定 \(n\) 个数 \(a_1 ... a_n\),可以进行 \(k\) 次操作,每次可以给任意一个数乘上 \(x\),求 \(a_1 | a_2 | ... | a_n\) 最大为多少。

题解

贪心 前缀和

因为 \(x \ge 2\),因此一个数乘上 \(x\) 后二进制位数必然增加一位。

由于或运算是 \(0 | 1\) 时才会使答案增加,因此让 \(k\) 个 \(x\) 乘在同一个数上就行。

计算一下前缀和和后缀和,然后暴力枚举每一个数乘以 \(x^k\),找到最大值即可。

Code

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll; ll a[maxn], s1[maxn], s2[maxn]; int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, k, x;
cin >> n >> k >> x;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
s1[i] = s1[i - 1] | a[i];
}
for(int i = n; i >= 1; --i) {
s2[i] = s2[i + 1] | a[i];
}
ll ans = 0;
ll tmp = x;
for(int i = 2; i <= k; ++i) {
tmp *= x;
}
for(int i = 1; i <= n; ++i) {
ans = max(ans, s1[i - 1] | tmp * a[i] | s2[i + 1]);
}
cout << ans << endl;
return 0;
}

Codeforces 578B "Or" Game (前缀和 + 贪心)的更多相关文章

  1. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  2. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  3. leetcode1546题解【前缀和+贪心】

    leetcode1546.和为目标值的最大数目不重叠非空子数组数目 题目链接 算法 前缀和+贪心 时间复杂度O(n). 1.对nums数组求前缀和: 2.在求前缀和过程中将前缀和sum插入到set集合 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  6. Codeforces Testing Round #12 B. Restaurant 贪心

    B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...

  7. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

  8. CodeForces - 777B Game of Credit Cards 贪心

    题目链接: http://codeforces.com/problemset/problem/777/B 题目大意: A, B玩游戏,每人一串数字,数字不大于1000,要求每人从第一位开始报出数字,并 ...

  9. Codeforces 1082C Multi-Subject Competition 前缀和 A

    Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...

随机推荐

  1. Kindeditor在线文本编辑器过滤HTML

    KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', { filterMode ...

  2. cross-env

    cross-env跨平台设置环境变量 安装npm install --save-dev cross-env config文件下新建环境对应文件 新建编译命令 修改build/webpack.prod. ...

  3. python基本数据类型的问题

    关于python中的索引和切片: 在之前看的视屏中是这么描述的:索引值以 0 为开始值,-1 为从末尾的开始位置. 然后今天忽然有了醍醐灌顶的感觉,索引值以 0 为开始值: 就是说从左向右以0开始递增 ...

  4. 消息 245,级别 16,状态 1,第 1 行 在将 varchar 值 '2,8' 转换成数据类型 int 时失败。

    错误问题: 消息 245,级别 16,状态 1,第 1 行在将 varchar 值 '2,8' 转换成数据类型 int 时失败. ps: 这是在后台分配菜单权限这个功能时出现的问题 一,解决方法: 将 ...

  5. 向服务器发送post请求

    /** * 通过HttpClient发送Post请求 * @param path 请求路径 * @param params 请求参数 * @param encoding 编码 * @return 请求 ...

  6. go中浮点型用法总结

    示例 // 浮点型的用法 package main import ( "fmt" "unsafe" ) func main() { // 如果浮点数声明时未指定 ...

  7. 本地存储(sessionStrorage,localStorage)

    1.本地存储特性 1. 数据存储在用户浏览器中 2. 设置,读取方便,设置页面刷新不丢失数据 3. 容量较大,sessionStorage约5M,localStorage约20M 4. 只能存储字符串 ...

  8. shell变量及相关命令

  9. Linux历史命令管理以及用法

    history [-c] [-d offset] [n] history -anrw [filename] history -ps arg [arg...] -c: 清空命令历史 -d offset: ...

  10. ThreadLocal的使用和理解

    ThreadLocal是个threadlocalvariable(线程局部变量),其实就是为每一个使用该变量的线程都提供一个变量值的副本,从线程的角度看,每个线程都保持一个对其线程局部变量副本的隐式引 ...