#4666. OR Sum

0

OR Sum

OR Sum

题目描述

There are length-NN sequences A=(A0,A1,…,AN−1)A=(A_0,A_1,\ldots,A_{N-1}) and B=(B0,B1,…,BN−1)B=(B_0,B_1,\ldots,B_{N-1}).
Takahashi may perform the following operation on AA any number of times (possibly zero):

apply a left cyclic shift to the sequence AA. In other words, replace AA with A′A' defined by Ai′=A(i+1)%NA'_i=A_{(i+1)\% N}, where x%Nx\% N denotes the remainder when xx is divided by NN.

Takahashi's objective is to maximize ∑i=0N−1(Ai∣Bi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i), where x∣yx|y denotes the bitwise logical sum (bitwise OR) of xx and yy. Find the maximum possible ∑i=0N−1(Ai∣Bi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i).

What is the bitwise logical sum (bitwise OR)? The logical sum (or the OR operation) is an operation on two one-bit integers ($0$ or $1$) defined by the table below.
The bitwise logical sum (bitwise OR) is an operation of applying the logical sum bitwise.
$x$ $y$ $x|y$
$0$ $0$ $0$
$0$ $1$ $1$
$1$ $0$ $1$
$1$ $1$ $1$
The logical sum yields $1$ if at least one of the bits $x$ and $y$ is $1$. Conversely, it yields $0$ only if both of them are $0$.
Example
0110 | 0101 = 0111

输入格式

The input is given from Standard Input in the following format: NN A0A_0 A1A_1 …\ldots AN−1A_{N-1} B0B_0 B1B_1 …\ldots BN−1B_{N-1}

输出格式

Print the maximum possible ∑i=0N−1(Ai∣Bi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i).

3
0 1 3
0 2 3
8
5
1 6 1 4 3
0 6 4 0 1
23

提示

2≤N≤5×1052 \leq N \leq 5\times 10^5 0≤Ai,Bi≤310\leq A_i,B_i \leq 31 All values in the input are integers.If Takahashi does not perform the operation, AA remains (0,1,3)(0,1,3), and we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(0|0)+(1|2)+(3|3)=0+3+3=6$;
if he performs the operation once, making A=(1,3,0)A=(1,3,0), we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(1|0)+(3|2)+(0|3)=1+3+3=7$; and
if he performs the operation twice, making A=(3,0,1)A=(3,0,1), we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(3|0)+(0|2)+(1|3)=3+2+3=8$.
If he performs the operation three or more times, AA becomes one of the sequences above, so the maximum possible ∑i=0N−1(Ai∣Bi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i) is 88, which should be printed.

The value is maximized if he performs the operation three times, making A=(4,3,1,6,1)A=(4,3,1,6,1),
where $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(4|0)+(3|6)+(1|4)+(6|0)+(1|1)=4+7+5+6+1=23$.

标签: AtCoder|abc291G

来源

AtCoder|abc291G