Tritwise Mex

시간 제한4초메모리 제한512 MB

요약
3진법 자릿수별 mex가 k인 모든 쌍 (i, j)에 대해 a_i·b_j의 합을 구해 c_k를 출력한다. 3^k는 최대 531441이다.
난이도

어려움10점 중 8점

유형
분할 정복, 수학, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

Let us denote \(\text{mex}(a, b)\) (minimum excludant) as the minimum non-negative integer which is neither equal to \(a\) nor equal to \(b\). It always holds that \(\text{mex}(a, b)\) < 3, thus we can define tritwise \(\text{mex}\). If we write \(a\) and \(b\) in ternary notation:

 \[a = \sum_{i=0}^{k-1}{a_i \cdot 3^i}, b = \sum_{i=0}^{k-1}{b_i \cdot 3^i},\] 

where \(a_i\) and \(b_i\) are integers from 0 to 2, we define \(\text{mex}_3\) as follows:

 \[\text{mex}_3(a, b) = \sum_{i=0}^{k-1}{\text{mex}(a_i, b_i) \cdot 3^i}, \] 

You are given two sequences \(a_0, \dots, a_{3^k-1}\) and \(b_0, \dots, b_{3^k-1}\). You have to compute the sequence \(c_0, \dots, c_{3^k-1}\):

 \[c_k = \sum_{\text{mex}_3(i, j) = k}{a_i \cdot b_j}\]

입력

The first line of input contains a single integer \(k\) (\(1 \le k \le 12\)).

The second line of input contains \(3^k\) integers \(a_0, \dots, a_{3^k-1}\) (\(0 \le a_i ≤ 10^3\)).

The third line of input contains \(3^k\) integers \(b_0, \dots, b_{3^k-1}\) (\(0 \le b_i ≤ 10^3\)).

출력

Output \(3^k\) integers \(c_0, \dots, c_{3^k-1}\) separated by spaces.

힌트

For reference: \(3^{12} = 531 441\).

예제2

  1. 예제 1

    입력
    1
    1 1 1
    1 1 1
    
    예상 출력
    4 3 2
    
  2. 예제 2

    입력
    2
    1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1
    
    예상 출력
    16 12 8 12 9 6 8 6 4