2218. The Monkey and the Coconuts

单点时限: 2.0 sec

内存限制: 256 MB

A well known problem in recreational mathematics is the following:

Five men and a monkey were shipwrecked on a desert island, and they spent the first day gathering coconuts for food. Piled them all up together and then went to sleep for the night.

But when they were all asleep one man woke up, and he thought there might be a fight over dividing the coconuts in the morning, so he decided to take his share in advance. He divided the coconuts into five piles and had one coconut left over which he threw back into the jungle for the monkey. He hid his pile and put the rest all back together.

By and by the next manwoke up and did the same thing. One after the other took a fifth of the coconuts in the pile having one left over for the monkey. In the morning they divided what coconuts were left, and they came out in five equal shares. Of course each one must have known there were coconuts missing; but each one felt guilty like the others, so they didn’t say anything.

How many coconuts were there in the beginning?

Your task is to write a program for solving generalizations of the problem given above. Consider N men and the monkey shipwrecked on an island, with 1 <=N <= 10. When dividing the remaining coconuts in N equal shares, the k-th man has ak coconuts left over for the monkey. In the morning, there are aN+1 coconuts left over for the monkey.

Write a program to find the smallest non-negative number of coconuts that satisfies these conditions. Numbers greater than 10,000,000 need not be considered, because there are never as many coconuts on a single island.

输入格式

The input consists of multiple scenarios, each in a single line.

The first number in each line gives the number N of men on the island, followed by the N+1 numbers a1,a2,…aN+1 of coconuts that subsequently are given to the monkey. The input file is terminated by a line containing a single zero.

输出格式

For each scenario print a line containing the smallest non-negtaive integer satisfying the conditions stated above. If there is no such number smaller than or equal to 10,000,000, print ‘No small solution.’.

样例

Input
5 1 1 1 1 1 0
2 1 0 1
5 1 2 3 4 5 6
0
Output
3121
5
No small solution.

4 人解决,4 人已尝试。

9 份提交通过,共有 27 份提交。

7.0 EMB 奖励。

创建: 16 年,3 月前.

修改: 7 年,2 月前.

最后提交: 16 年,1 月前.

来源: TU Darmstadt Programming Contest 1999

题目标签