传送门

You are given $n$ numbers $a_1, a_2, \dots, 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\mid a_2\mid \dots\mid a_n$ as large as possible, where $\mid$ denotes the bitwise OR. Find the maximum possible value of $a_1\mid a_2\mid \dots\mid a_n$ after performing at most $k$ operations optimally.

Input

The first line contains three integers $n$, $ k $ and $ x $ ($1 \le n \le 200000$, $1 \le k \le 10$, $2 \le x \le 8$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$).

Output

Output the maximum value of a bitwise OR of sequence elements after performing operations.

Sample test(s)
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 .

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

注意到 $2\le x\le 8$,意味着:每次不论选那个数乘以 $x$,这个数的二进制位数都会增加。

(其实这不算什么Key Observation,当 $x>1$ 时,$x$ 乘任何正整数,该数的二进制位数都会增加)

所以要取得最大值,$k$ 次必然都是乘以同一个数,结果的位数就是这个数最后的位数

所以算法是:

将输入数组从大到小排序,枚举乘 $x^k$ 后长度最长的数,更新答案。

为此,预处理出输入数组前缀和后缀取或(|)的结果。

Implementation

#include <bits/stdc++.h>
using namespace std;
const int N(2e5+);
typedef long long ll;
typedef pair<int,int> P;
P a[N];
int f[N], b[N];
int high_bit(ll x){
for(int i=; i>=; i--){
if(x&(ll)<<i) return i;
}
}
int main(){
int n, k, s;
scanf("%d%d%d", &n, &k, &s);
for(int i=, x; i<=n; i++) scanf("%d", &x), a[i]={x, i}; f[]=;
for(int i=; i<=n; i++) f[i]=f[i-]|a[i].first;
b[n+]=;
for(int i=n; i; i--) b[i]=b[i+]|a[i].first; sort(a+, a+n+, greater<P>());
if(!a[].first){puts(""); return ;}
ll _=; for(int i=; i<k; i++) _*=s; ll ans=, lar=_*a[].first;
int id, h=high_bit(lar);
for(int i=; i<=n; i++){
lar=_*a[i].first;
if(high_bit(lar)<h) break;
id=a[i].second;
ans=max(ans, f[id-]|b[id+]|lar);
}
printf("%lld\n", ans);
}

比赛时把

typedef long long ll;

写成了

typedef long ll;

结果交上去连样例都没过,但在自己电脑上却没问题, 这是由于 long / long long 的具体长度依赖于机器 (machine-dependent),

C++ 标准只规定了其最小长度(minimum size),long(32 bits)long long(64 bits).

除去这个细节不谈,比赛时还犯了个算法上的错误:

枚举原来最长的数而不是乘 $x^k$ 后最长的数。

UPD 2018/3/18

难道不是用最大的数乘 $x^k$ 吗?

Codeforces 578B "Or" Game的更多相关文章

  1. Codeforces 578B "Or" Game (前缀和 + 贪心)

    Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) ...

  2. codeforces 578b//"Or" Game// Codeforces Round #320 (Div. 1)

    题意:n个数字,最多操作k次,每次乘x,要使结果数组的与值最大. 能推断出结果是对一个元素操作k次,并且这个元素的二进制最高位比较大.并不一定是取最大的,比如1100和1010,乘以一次2,两种选法分 ...

  3. Codeforces 578B. "Or" Game(思维题)

    我们知道所有sigma(2^i){i<n}比2^n小,所以我们肯定是把这k次操作全部丢到一个数上看看能不能凑出二进制下一个更高位的1. 因为k最大只有10,我们可以求出每一个数乘以k次之后的值, ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

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

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

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. html5中上传图片

    从相册中选择图片上传 function uploadFromAlbum(type) { var dirtype = ""; if ("pick_store_license ...

  2. createElement创建标签及appendChild添加到元素的后面

    var p = document.createElement('p'); var box = document.getElementsByTagName('div')[0]; box.appendCh ...

  3. linux输入输出重定向

    http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...

  4. 加密算法使用(一):用CRC32来压缩32uuid字符串

    CRC32相比MD5重复率较高, 不过我们仍然可以使用CRC32然后转长整形的方式将32位的UUID字符串压缩成更短的整形唯一标识. /** * * @Title: getCRC32Value * @ ...

  5. C# == equals 本质理解

    using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...

  6. C#中事件的继承

    C#中的子类无法调用父类的事件,可以通过在父类中创建一个方法来调用父类的事件,而子类通过调用父类的方法来触发事件. class parent { protected string name; publ ...

  7. String类及常用方法

    在学习String类之前,先看一道笔试题:new String("abc")创建了几个对象? 答案: 两个对象, 一个对象是 位于字符串常量池中,一个对象是位于堆内存中. 原因:主 ...

  8. Hello,cnblog‘s world!

    纠结了许久,终于选在这个地方安家.之所以选在这里,是因为这里是个很干净的社区. 之前一直在其他博文网站里发文章,可是越到后来,发现页面广告越来越多.真正对自己有价值的内容越来越少,虽然已经使用过几年了 ...

  9. matlab取消和添加注释以及一些快捷键

    1 matlab中关于注释: 多行注释: 选中要注释的若干语句,工具栏菜单Text->Comment,或者鼠标右击选"Comment",或者快捷键Ctrl+R 取消注释: 选 ...

  10. openssl实践总结

    openssl实验总结 OPENSSL简介 OpenSSL项目是一个协作开发一个健壮的,商业级的,全功能的,并且开放源代码工具包,它实现了安全套接字层(SSL v2/v3)和传输层安全(TLS v1) ...