Grand Parade
Time limit2sMemory limit1024 MB
Given k and n, build an n by n matrix of primes up to 10^7 with exactly k distinct primes and equal divisor counts in every row and column, or print -1.
- Level
Medium6 of 10
- Topics
- Matrix, Number theory, Implementation
- Solved
- No attempts yet
Statement
Gru wants to hold a grand parade. The main part of the parade is a formation of his mighty army of minions.
The parade takes place on the Central Square, a square with side length meters. The square is split into cells of one meter by one meter, so it has cells. In other words, the Central Square is an matrix.
Gru gave each minion in his army a colored T-shirt with a prime number printed on it. Some T-shirts may show the same number. Now the minions must line up the way Gru wants. Each cell of the square holds exactly one minion.
The formation has two more requirements. First, the parade must include minions whose T-shirts show exactly distinct prime numbers. Second, the product of the numbers on the T-shirts in each row and in each column must have the same number of divisors. Gru has only T-shirts with primes not exceeding .
Build a formation that meets all requirements, or determine that it is impossible.
Input
The only line contains two integers and (, ), the number of required distinct primes and the size of the square.
Output
Print an matrix of primes not exceeding that satisfies all requirements. If it is impossible, print -1.
Hints
In the first sample, the row products are 6 and 35, and the column products are 10 and 21. Each of these numbers has 4 divisors. In the second sample, the products of the first row, the third row, the first column, and the third column are 12, while the second row and second column products are 18. Both numbers have 6 divisors each. The third sample is impossible.