Segments in a Regular Polygon
Time limit2sMemory limit512 MB
Count the orders in which the remaining polygon vertices can be visited so each new segment crosses an existing one and the path closes back to P0.
- Level
Hard8 of 10
- Topics
- Backtracking, Dynamic programming, Bit manipulation, Geometry
- Solved
- No attempts yet
Problem
Dohyun marked points on a sheet of paper to form a regular -gon. The vertices are numbered through clockwise.
He then drew segments. He connected the vertices in that order: to , to , and so on up to to .
Now he wants to keep drawing so that every remaining vertex is visited exactly once and the drawing returns to . If the remaining vertices are visited in the order , he connects to , to , and so on up to to , and finally to . Every vertex that does not appear in appears in exactly once.
Every newly drawn segment must cross at least one segment that is already on the paper. The segments already on the paper include the ones drawn along and the ones drawn earlier in this process. Two segments cross when they share a point that lies in the interior of both, so two segments that share only an endpoint do not cross.
Given , , and , write a program that counts the possible orders .
Input
The first line contains and , separated by a space. (, )
The second line contains the numbers through in order. Each number is between and , and all of them are different.
Output
Print the number of possible orders on the first line.