Official

G - OR Sum Editorial by en_translator


This problem can be solved with convolution.

Since \(A\) becomes the original state by \(N\) operations, it is sufficient to consider that the operation is performed \(j\) times such that \(0\leq j\leq N-1\). After \(j\) operations, the value \(\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)\) is represented by the original given sequences \(A\) and \(B\) (before the operations) as \(\displaystyle\sum_{i=0}^{N-1} (A_{(i+j)\% N}|B_i)\), so it is sufficient to find the maximum value within \(0\leq j\leq N-1\).

Now, consider evaluating this value for \(0\leq j\leq N-1\). Under the Constraints, \(0\leq A_i,B_i\leq 31\), but the logical sum can be computed bitwise (because it does not cause a carry), so it is sufficient to consider \(0\leq A_i,B_i\leq 1\); then, we can consider \(2^0\)s, \(2^1\)s, \(2^2\)s, \(2^3\)s, and \(2^4\)s place independently. That is, the value equals

\[ \displaystyle\sum_{i=0}^{N-1} (A_{(i+j)\% N}|B_i) =\displaystyle\sum_{k=0}^{4}2^k\cdot\left(\displaystyle\sum_{i=0}^{N-1} (A_{(i+j)\% N,k}|B_{i,k})\right), \]

where \(A_{i,k}\) and \(B_{i,k}\) are \(k\)-th least significant bit of \(A_i\) and \(B_i\).

For \(a=(a_0,a_1,\ldots,a_{N-1})\) and \(b=(b_0,b_1,\ldots,b_{N-1})\) \((0\leq a_i,b_i\leq 1)\), how can we evaluate \(\displaystyle\sum_{i=0}^{N-1} (a_{(i+j)\% N}|b_{i})\) with convolution?
First, (in order to find the sum over such pairs of indices as a convolution,) let \(a'=(a'_{0},a'_{1},\ldots, a'_{2N-1})=(a_{0},a_{1},\ldots,a_{N-1},a_{0},a_{1},\ldots,a_{N-1} )\) be the concatenation of two copies of \(a\), \(b'=(b'_{0},b'_{1},\ldots, b'_{N-1})=(b_{N-1},b_{N-2},\ldots,b_{0})\) be the reverse of \(b\), and \(c'=a'*b'=(c'_0,c'_1,\ldots, c'_{3N-1})\) be their convolution; then

\[ c'_{N-1+j}=\displaystyle\sum_{i=0}^{N-1} a'_{i+j}\cdot b'_{N-1-i}=\displaystyle\sum_{i=0}^{N-1} a_{(i+j)\% N}\cdot b_i . \]

Here, for \(0\leq x,y\leq 1\), \(x\cdot y\) yields \(x\cdot y=1\) if \(x=y=1\) and \(x\cdot y=0\) otherwise, so \(x\cdot y=x\& y\) (where \(\&\) is the logical product (AND operation)). Furthermore, \(\overline{(x|y)}=\bar{x}\&\bar{y}\) holds, where \(\bar{x}\) is the negation of \(x\), so the convolution of \(\bar{a'}=(\bar{a}'_{0},\bar{a}'_{1},\ldots, \bar{a}'_{2N-1})\) and \(\bar{b'}=(\bar{b}'_{0},\bar{b}'_{1},\ldots, \bar{b}'_{N-1})\) yields

\[ (\bar{a'}*\bar{b'})_{N-1+j} =\displaystyle\sum_{i=0}^{N-1} \bar{a}_{(i+j)\% N}\cdot \bar{b}_i =\displaystyle\sum_{i=0}^{N-1} \bar{a}_{(i+j)\% N}\& \bar{b}_i =\displaystyle\sum_{i=0}^{N-1} \overline{a_{(i+j)\% N}|b_i} =\displaystyle\sum_{i=0}^{N-1} (1-(a_{(i+j)\% N}|b_i)) =N-\displaystyle\sum_{i=0}^{N-1} (a_{(i+j)\% N}|b_i). \]

Therefore, for \(0\leq j\leq N-1\), we can evaluate \(\displaystyle\sum_{i=0}^{N-1} (a_{(i+j)\% N}|b_{i})\) as \(\displaystyle\sum_{i=0}^{N-1} (a_{(i+j)\% N}|b_{i})=N-(\bar{a'}*\bar{b'})_{N-1+j}\), a single convolution (with time complexity \(O(N\log N)\)).

All that left is to compute this value for each bit, obtain \(\displaystyle\sum_{i=0}^{N-1} (A_{(i+j)\% N}|B_i)\), and choose the maximum among them. Here, the total time complexity is \(O(N\log N\log M))\) (where \(M=\max(a_0,a_1,\ldots,a_{N-1},b_0,b_1,\ldots,b_{N-1})\)), which is fast enough.

Sample code in C++:

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

int main() {
	int n;
	cin>> n;

	vector<int>a(n),b(n),c(n),aa(2*n),bb(n),cc;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	for(int j=0;j<5;j++){
		for(int i=0;i<n;i++){
			aa[i]=(~a[i]>>j)&1;
			aa[n+i]=aa[i];
			bb[n-i-1]=(~b[i]>>j)&1;
		}
		cc=convolution(aa, bb);
		for(int i=0;i<n;i++){
			c[i]+=(n-cc[n+i-1])<<j;
		}
	}

	int ans=0;
	for(int i=0;i<n;i++)ans=max(ans,c[i]);
	cout<<ans<<endl;
	return 0;
}

posted:
last update: