日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【21.37%】【codeforces 579D】Or Game

發(fā)布時間:2024/8/26 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【21.37%】【codeforces 579D】Or Game 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

【題解】

顯然,二進(jìn)制位1最靠左的那個數(shù)字乘上數(shù)字會讓最靠左的數(shù)字再靠左一點;
這樣做是最優(yōu)的策略;
但是
如果有多個數(shù)字二進(jìn)制1最靠左,即最靠左的1相同;則不能隨便選;
比如
1 0 0 0
1 0 1 1
1 1 0 1
這3個數(shù)字;
假設(shè)要乘的數(shù)字x是2;只能乘1次;
這三個數(shù)字依次是
8
11
13
如果把2乘最大的那個數(shù)字13
0 1 0 0 0
0 1 0 1 1
1 1 0 1 0
最后取or運算
結(jié)果為1 1 0 1 1
如果把2乘8
1 0 0 0 0
0 1 0 1 1
0 1 1 0 1
則再取or運算
結(jié)果為
1 1 1 1 1
顯然后者更大;
所以不能單純地就認(rèn)為乘那個最大的數(shù)字就好了;
但如果最靠左的1的位置不是所有數(shù)字中最靠左的;那就不可能去乘它了;
所以乘了一次之后;肯定是繼續(xù)去乘剛才乘的數(shù)字;
比如剛才的乘2之后變成了
1 0 0 0 0
0 1 0 1 1
0 1 1 0 1
顯然繼續(xù)讓那個10000乘2是更優(yōu)的;因為最高位移動一下幅度更大;
枚舉要乘哪個數(shù)字、然后一直乘就可以了;
處理出前綴or和后綴or;預(yù)處理出x^k;x為要乘的數(shù)字;k為允許乘的次數(shù)


【完整代碼】

#include <bits/stdc++.h> #define LL long longusing namespace std;const int MAXN = 20e4+100;int n,k,x; LL a[MAXN],qz[MAXN],hz[MAXN];int main() {//freopen("F:\\rush.txt","r",stdin);cin >> n >> k >> x;for (int i = 1;i <= n;i++)cin >> a[i];for (int i = 1;i <= n;i++)qz[i] = qz[i-1] | a[i];for (int i = n;i >= 1;i--)hz[i] = hz[i+1] | a[i];LL temp = x;for (int i = 1;i <= k-1;i++)temp*=x;LL ans = 0;for (int i = 1;i <= n;i++){LL temp1 = a[i]*temp;ans = max(ans,qz[i-1] | temp1 | hz[i+1]);}cout << ans << endl;return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/AWCXV/p/7632062.html

總結(jié)

以上是生活随笔為你收集整理的【21.37%】【codeforces 579D】Or Game的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。