# CFR 732 F. Anton and School ( Ad hoc, Binary Enumeration )

Problem - F - Codeforces

Codeforces Round #379 (Div. 2) F. Anton and School - pekempeyのブログ pekempeyさんの記事読んでやっとわかった。

x + y = ( x & y ) + ( x | y )

B[ i ] + C[ i ] = N * A[ i ] + SIGMA{ A[ j ] | 0 ≤ j < N }
SIGMA{ B[ i ] + C[ i ] | 0 ≤ i < N } = 2 * N * SIGMA{ A[ j ] | 0 ≤ j < N }

O( N lg N )

```int N;
vi B;
vi C;

void init(){
cin >> N;
B = vi( N );
for( int i = 0; i < N; ++i )
cin >> B[ i ];
C = vi( N );
for( int i = 0; i < N; ++i )
cin >> C[ i ];
}

vi A;
vi fbit_A;

void preprocess(){
ll suma = 0;
for( int i = 0; i < N; ++i )
suma += B[ i ] + C[ i ];
suma /= 2 * N;
A = vi( N );
for( int i = 0; i < N; ++i )
A[ i ] = ( B[ i ] + C[ i ] - suma ) / N;
fbit_A = vi( 32 ); // frequency of a bit in all As
for( int i = 0; i < N; ++i )
for( int j = 0; j < 32; ++j ) if( A[ i ] >> j & 1 )
++fbit_A[ j ];
}

void solve(){
for( int i = 0; i < N; ++i ){
ll b = 0, c = 0;
for( int j = 0; j < 32; ++j )
b += 1LL * fbit_A[ j ] * ( A[ i ] >> j & 1 ) * ( 1 << j ),
c += 1LL * max( fbit_A[ j ], ( A[ i ] >> j & 1 ) ? N : 0 ) * ( 1 << j );
if( b != B[ i ] or c != C[ i ] )
cout << -1 << endl,
exit( 0 );
}
for( int i = 0; i < N; ++i )
cout << A[ i ] << " \n"[ i + 1 == N ];
}
```