【21.37%】【codeforces 579D】Or Game
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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.
【題目鏈接】:http://codeforces.com/contest/579/problem/D
【題解】
顯然,二進制位1最靠左的那個數字乘上數字會讓最靠左的數字再靠左一點;
這樣做是最優的策略;
但是
如果有多個數字二進制1最靠左,即最靠左的1相同;則不能隨便選;
比如
1 0 0 0
1 0 1 1
1 1 0 1
這3個數字;
假設要乘的數字x是2;只能乘1次;
這三個數字依次是
8
11
13
如果把2乘最大的那個數字13
0 1 0 0 0
0 1 0 1 1
1 1 0 1 0
最后取or運算
結果為1 1 0 1 1
如果把2乘8
1 0 0 0 0
0 1 0 1 1
0 1 1 0 1
則再取or運算
結果為
1 1 1 1 1
顯然后者更大;
所以不能單純地就認為乘那個最大的數字就好了;
但如果最靠左的1的位置不是所有數字中最靠左的;那就不可能去乘它了;
所以乘了一次之后;肯定是繼續去乘剛才乘的數字;
比如剛才的乘2之后變成了
1 0 0 0 0
0 1 0 1 1
0 1 1 0 1
顯然繼續讓那個10000乘2是更優的;因為最高位移動一下幅度更大;
枚舉要乘哪個數字、然后一直乘就可以了;
處理出前綴or和后綴or;預處理出x^k;x為要乘的數字;k為允許乘的次數
【完整代碼】
轉載于:https://www.cnblogs.com/AWCXV/p/7632062.html
總結
以上是生活随笔為你收集整理的【21.37%】【codeforces 579D】Or Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux中Cache内存占用过高解决办
- 下一篇: 我们越来越浮躁的心靠什么去滋润