Associativity Degree

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

요약
n과 여러 k가 주어질 때, 결합 법칙이 성립하는 삼중항의 개수가 정확히 k인 이항 연산을 구성하거나 불가능함을 판정한다.
난이도

어려움10점 중 9점

유형
수학, 조합론, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

Consider a binary operation ◦ defined on numbers 1 through n:

◦ : {1, . . . , n} × {1, . . . , n} → {1, . . . , n}.

Let us define its associativity degree as the number of triplets i, j, k ∈ {1, . . . , n} for which ◦ is associative:

i ◦ (j ◦ k) = (i ◦ j) ◦ k.

Your task is, given n and k, to construct an operation ◦ such that its associativity degree is exactly k.

입력

The first line of input contains two integers n and q (1 ≤ n ≤ 64, 1 ≤ q · n2 ≤ 106).

The i-th of the next q lines contains a single integer ki (0 ≤ ki ≤ n3).

It is guaranteed that all ki are distinct.

출력

For each given value of ki, do the following:

If there is no operation ◦ with associativity degree ki for given n, output “NO” on a single line.

Otherwise, output “YES” on the first line, followed by n lines containing n integers each. The j-th integer in the i-th line must be equal to i ◦ j.

힌트

The operation from the first sample can be concisely described as i ◦ j = 1 + ((i − 1) · (j − 1)) mod 3, and it is fully associative.

예제2

  1. 예제 1

    입력
    3 1
    27
    
    예상 출력
    YES
    1 1 1
    1 2 3
    1 3 2
    
  2. 예제 2

    입력
    1 2
    0
    1
    
    예상 출력
    NO
    YES
    1