Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] -- B. "Or" Game (容斥定理)
来源:互联网 发布:诺基亚按键机java游戏 编辑:程序博客网 时间:2024/05/14 12:55
B. "Or" Game
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given n numbers a1, a2, ..., an. You can perform at mostk operations. For each operation you can multiply one of the numbers byx. We want to make as large as possible, where denotes the bitwise OR.
Find the maximum possible value of after performing at mostk 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 21 1 1
Output
3
Input
4 2 31 2 4 8
Output
79
Note
大体题意:
给你n 个元素的集合!
可以进行k 个操作,每个操作可以给一个数乘以x
问最后的集合 进行异或运算最大值是多少!
思路:
利用容斥定理,先正着求一遍 异或运算
在反着求一遍。
最后枚举每一个数 乘以 k 个x 连乘
用这个数的前面异或这个数在异或后面的数即可!
ll tmp = bef[i-1] | (a[i]*x) | aft[i+1];
大体题意:
给你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;}
0 0
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] -- B. "Or" Game (容斥定理)
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B "Or" Game
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] -- D. "Or" Game
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D. "Or" Game 贪心
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D. "Or" Game (贪心法)
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A B C
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] (ABCDE题解)
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C 数学
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]B Finding Team Member
- Raising Bacteria (Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] )
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 1)(组合数学+容斥)
- Codeforces Round #320 (Div. 1) B. "Or" Game
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C. A Problem about Polyline 精度控制
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C. A Problem about Polyline
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]A Raising Bacteria
- 为什么程序员需要关心顺序一致性(Sequential Consistency)而不是Cache一致性(Cache Coherence?)
- Android学习笔记开篇
- 提高工作效率的16条Android开发小经验
- Stack栈 链式存储 实现
- hdoj 2046 骨牌铺方格(递推)
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] -- B. "Or" Game (容斥定理)
- Strust+hibernate+strust2的整合方案(1)
- 【Halcon】Halcon与OpenCV介绍、比较
- 第十三周实践项目2.3-动物这样叫
- vim配置及插件安装管理(超级详细)
- 数据清洗的一些梳理
- HTML获取触发事件的元素id
- ios JSON NSString to NSMutableDictionary
- 16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数