# e-olymp 94. Problem of prime numbers!

The task is taken from e-olymp

One of the most difficulties of an instructor is question design for the final-term exam. Dr. Ghavamnia teaches “Fundamentals of Algorithms” at University of Isfahan (UI) and has designed a good practical algorithmic problem for his exam. He wants that students use all of their algorithmic skills to code this problem as best and efficient as they can. The problem is as follows: a ring is composed of $N$ circles numbered from $1$ to $N$ as shown in diagram. You are given $N$ integer numbers. Put these numbers into each circle separately with the condition that sum of numbers in three adjacent circles must be a prime.
Each test case may have several solutions and your task is to find the one with minimum weighted sum. Weighted sum is defined as the sum of products of each number in circle by the sequence number of that circle. For instance, the weighted sum in above diagram is $36$ (and also it is the minimum).

# Input

The first line of input contains a single integer, the number of test cases. Following, there is one line for each test case. Each test case begins with one integer, $N$ ($3 \leq N \leq 15$), which is the number of circles. Next $N$ integers are the given numbers to put in circles (all between $1$ and $100$, inclusively).

# Output

There should be one line for each test case in output. If there’s no way for building the ring, the word «impossible» should be outputted, or otherwise, a single integer which is the minimum weighted sum.

# Tests

 № Inputs Outputs 1 5 4 1 3 7 9 4 1 1 3 5 15 11 6 2 3 2 14 1 2 10 7 2 2 3 6 2 9 1 1 1 2 2 2 2 2 2 7 1 2 3 4 5 6 7 36 imposible 396 72 imposible

# Solution

We need all permutations of our array and if permutation suits us update $min$ value. To check if number is prime I use Sieve of Eratosthenes. To get all permutation I use recursive function which works in this way:
We accumulate an array in all possible ways. What we do:

• Fixed empty position in array with a value
• Remember that we used this value
• Find all permutations of array with size which is smaller by 1
• Forget that we used this value (to use it in next permutations)

We stop when we accumulate an array with size we need and check if this permutation suits us(if sum of all triples of this circle are prime). If yes update answer(if it is less than answer). So if we send this solution to server we will get time limit. We need to reduce permutations which do not suit us. To speed up our program we can reduce amount of permutations in this way: if we find out that sum of previous three numbers, which are fixed, is not prime, we do not need to continue this branch of recursion. Really if in our array we found that one sum of three number is not prime we do not need to accumulate this array further. But anyway we get time limit.
Let’s see the differences in complete array where any some of three numbers is prime number.
Input: 8 2 1
Let’s find all suitable permutations:

1. 8 2 1
2. 2 8 1
3. 2 1 8
4. 1 8 2
5. 8 1 2
6. 1 2 8

As you can see first three are different with shifts. Last three are also different with shifts. So we can fix one element find all permutations we need with less by 1 amount of elements and then shift all elements of every array to get all possible permutations which are correct as you can see above (1,2,3) (4,5,6). In the worst case it will work not for $15!$ but for $14!$. And now we get Accept.
The complexity is $O(n!)$
Why I did not get time limit at all (because of complexity)? In task said than amount of test which works with $O(n!)$ can be huge. But who will make so many tests? In our case there are near 5 tests in one.