#P9224. 「PEOI Rd1」k 叉堆(heap)

「PEOI Rd1」k 叉堆(heap)

Background

Constraints are stronger than during the contest.

  • 2023.04.25: The 1.50 s time limit was too tight due to constant factors, so it was increased to 2.00 s.

Problem Description

Given a sequence of positions 1n1 \sim n, each position ii has kk parameters ai,1,ai,2,,ai,ka_{i,1}, a_{i,2}, \dots, a_{i,k}. It is known that this sequence is obtained by taking the BFS order of a max-heap.

The BFS order is the order of the red numbered nodes in the figure below:

A max-heap satisfies the condition if and only if, for every child node, all of its kk values are less than or equal to those of its parent, i.e., $\forall u \in [1,n], \forall v \in son(u), \forall j \in [1,k], a_{v,j} \leq a_{u,j}$.

Assume this max-heap is a complete mm-ary tree. Find all m[1,n1]m \in [1,n-1] such that this mm-ary heap satisfies the condition.

Input Format

The first line contains two integers n,kn, k.

Then follow kk lines, each containing nn integers, describing the given sequence. Specifically, the value in row i+1i+1 and column jj is aj,ia_{j,i}.

You can understand this as: all kk parameters for all positions are given, split into kk lines.

Output Format

The first line contains an integer AA, the number of possible values of mm.

The next line contains AA integers, the possible values of mm.

3 2
1 1 1
1 1 1
2
1 2
6 1
2 1 2 1 1 2
2
2 5

Hint

Sample Explanation

In Sample 11, both 11-ary and 22-ary heaps obviously satisfy the condition.


Constraints

Subtask ID nn \leq Score
11 10310^3 2020
22 5×1045 \times 10^4
33 2×1052 \times 10^5 6060

For 100%100\% of the testdata, it is guaranteed that 1n2×1051 \leq n \leq 2 \times 10^5, 1k81 \leq k \leq 8, and 107ai,j107-10^7 \leq a_{i,j} \leq 10^7.

Translated by ChatGPT 5