Arranging pots in a greenhouse
Statement
There is a square greenhouse with N squares along one side. The floor of the greenhouse is divided into N rows and N columns, and one pot can be placed on each square. Row numbers are counted from the top and column numbers from the left, starting at 1.
The pots grown in this greenhouse are a variety with a very strong scent. The fans in the greenhouse send air along the rows, along the columns, and along the four diagonal directions. So if two pots are in the same row, or in the same column, or on the same diagonal, their scents mix. Two pots being on the same diagonal means that the difference between their row numbers and the difference between their column numbers are the same. For example, a pot in row 2 column 3 and a pot in row 4 column 5 are on the same diagonal, since their row numbers differ by 2 and their column numbers differ by 2 as well. A pot whose scent has mixed cannot be sold.
The keeper wants to place all N pots in the greenhouse with no scent mixing at all. Write a program that, given the number of squares N along one side of the greenhouse, works out the number of different ways to place the N pots. The pots are all the same variety and are not told apart, and two ways count as different when the set of squares holding pots is different.
For example, think about a greenhouse with N=4. There are only two ways to place 4 pots without the scents mixing. In the tables O is a square with a pot on it, and an empty cell is a square with nothing on it.
Here is the first way.
| Square | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | O | |||
| Row 2 | O | |||
| Row 3 | O | |||
| Row 4 | O |
Here is the second way.
| Square | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | O | |||
| Row 2 | O | |||
| Row 3 | O | |||
| Row 4 | O |
In both ways no two pots share a row, a column or a diagonal. So the answer for N=4 is 2. For N=2 or N=3, on the other hand, some pot always ends up with a mixed scent wherever the pots are placed, so the answer is 0.
Input and Output Examples
Example 1
Input
4
Output
2
Example 2
Input
2
Output
0
Example 3
Input
8
Output
92
Input
First the number of squares N along one side of the greenhouse is given on the first line. N is a natural number from 1 to 13. The input ends with this one line.
The floor of the greenhouse is divided into N rows and N columns, so there are N×N squares in all, and row numbers and column numbers are natural numbers from 1 to N. The number of pots to place is also N.
Output
Print the number of ways to place the N pots with no scent mixing at all on the first line. The number of ways is an integer of 0 or more, and if there is no way to place them, print 0.
Requirements
- Exactly N pots are placed
- If two pots are in the same row, in the same column, or on the same diagonal, their scents mix
- Two ways count as different when the set of squares holding pots is different
- If there is no way to place them, print 0
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 13 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 13 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 13 cases to solve this.
2
0
4
2
8
92
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.