Lord of the Ring
Time limit1sMemory limit128 MB
Given all pairwise distances between bushes on a line, reconstruct the actual bush positions (the classic turnpike/beltway problem) and output the product of adjacent gaps, or report no solution.
- Level
Hard8 of 10
- Topics
- Backtracking, Combinatorics, Brute force
- Solved
- No attempts yet
Problem
Frodo must accomplish a noble and difficult mission: he must destroy a wicked magic ring. In this quest he must travel to a dangerous place called Mordor and throw the ring into a crevice of fire. He has been away from home for some time and is currently following a straight, quite long road that has bushes here and there. Being very tired, Frodo thinks he had better take some rest.
The only safe place along the road is a single bush whose position can be computed with a magic formula. That formula uses a value , which is the product of the distances between adjacent pairs of bushes along the road. Unfortunately, all Frodo knows are the distances between every pair of bushes along the road and the magic formula; he does not know the value of . Can you help him?
Input
Each data set in the file stands for one collection of distances between pairs of bushes on the road Frodo is traveling along. Each data set starts with the number of distances, followed by the distances in nondecreasing order. White space may occur freely in the input.
There are at least and at most bushes along the road. Moreover, the value of cannot exceed .
Output
For each data set, the program prints the value of to standard output, each at the beginning of a separate line. If the given distances correspond to a set of bushes on the road, that set is unique up to reflection, so is well-defined. If no arrangement of bushes matches the distances, it prints No solution.