#P12142. Maximum Distinct Matrix Reconstruction
Maximum Distinct Matrix Reconstruction
Maximum Distinct Matrix Reconstruction
A hacker has scrambled a sequence of n*m + 2
positive integers. Originally, the first two numbers represented the dimensions n and m of an n × m matrix and the remaining n*m numbers formed the matrix. After the scramble, all the numbers are given in one line, separated by spaces.
Your task is to determine the maximum possible number of different original matrices that could have produced the scrambled list.
More formally, let the total number of integers be T (T = n*m + 2) and define X = T - 2. An original matrix can be reconstructed as follows: choose an ordered pair of two distinct numbers a and b from the list (to serve as n and m respectively) such that
[ a \times b = X, ]
and arrange the remaining T - 2 numbers in a fixed order (for example, row-major order) to form an a × b matrix. Two matrices are considered different if either the dimensions differ or there is at least one position where the entries differ.
Assuming the input numbers can be chosen in an optimal way (for maximum count) and treating all numbers as distinct, compute the maximum number of different original matrices. This number is given by:
[ \text{Answer} = (\text{Number of valid ordered pairs }(a,b)) \times (T-2)! ]
Note: Since the input list may contain duplicate numbers, the number of valid ordered pairs is defined as the total count over all ordered pairs of distinct positions i and j such that if a is taken from position i and b from position j, then a \times b = T-2.
inputFormat
The input consists of a single line containing T space‐separated positive integers. Here, T = n*m + 2 for some positive integers n and m.
outputFormat
Output a single integer, the maximum number of different original matrices that can be reconstructed.
sample
1 1 2
2