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. Vue2.0---webpack打包知识点-2

    先贴一篇对vue-cli#2.0进行webpack配置的详细分析: https://zhuanlan.zhihu.com/p/24322005 一.看一下webpack的打包流程 1.package. ...

  2. C++中一个类(非继承类)对象,所占内存空间大小

    离职后在家里带了半年多了,这半年多里没有编写过一行代码,倒是看过一些书,但是差不多也都是囫圃吞枣.房子也快要装修,也得赶快找一个工作了,不然养车,还要玩摄影,没收入的日子真是不好过啊.呵呵. 按惯例, ...

  3. Vue.use

    不管是对象还是函数install 是Vue.use()必须要有的方法 否则无法使用(Vue.use(MintUI))但axios 不需要Vue.use(axios) 可以直接使用 因为axios没有i ...

  4. 电脑同时安装python2和python3 ,默认使用python3

    python3环境变量放在python2前面: 此电脑--属性--高级系统设置--高级--环境变量--系统变量--Path--编辑 D:\Python36\Scripts;D:\Python36;C: ...

  5. Ptyhon变量,常量,注释

    变量的命名规则: 1.变量由字母,数字,下划线搭配而成 2.变量不能以数字开头 3.变量也不能是Python的关键字. 4.变量不要有中文 5.名字要有意义 6.名字不要太长 变量的两种命名方式: 1 ...

  6. js常用扩展方法

    在日常的开发过程中,经常会碰到javaScript原生对象方法不够用的情况,所以经常会对javaScript原生方法进行扩展.下面就是在实际工作时,经常使用的一些方法,做一下记录,有需要的可以拿去. ...

  7. js常用转义字符列表

    转义字符 含义 \n 换行 \t 制表符 \b 空格 \r 回车 \f 换页符 \ 反斜杠 ' 单引号 '' 双引号 \0nnn 八进制代码 nnn 表示的字符( n 是 0 到 7 中的一个八进制数 ...

  8. Jquery的$.ajax、$.get、$.post发送、接收JSON数据及回调函数用法

    平时研究代码时,经常会遇到AJAX的相关用法,做项目时才真正体会到Ajax的强大之处(与服务器数据交互如此之便捷,更新DOM节点而不用刷新整个页面),以及运用的频繁程度.今天整理了一下自己之前没搞清楚 ...

  9. linux c 链接详解4-共享库

    4. 共享库 4.1. 编译.链接.运行 组成共享库的目标文件和一般的目标文件有所不同,在编译时要加-fPIC选项,例如: $ gcc -c -fPIC stack/stack.c stack/pus ...

  10. Linux用户的基本操作3 (组的基本管理,用户提权)

    目录 组的基本原理 用户身份切换 5.用户身份提权 组的基本原理 组账户信息保存在/etc/group 和/etc/gshadow 两个文件中. /etc/group组账户信息 [root@zls ~ ...