No.774 tatyam and Prime Daifugo
Problem Statement
tatyam has split into multiple clones and is playing a card game called Prime Daifugo.
In Prime Daifugo, several playing cards are dealt as a hand, and the person who gets rid of their hand wins.
The player whose turn it is may choose some cards from their hand, concatenate them in any order, and play them if the result is a prime number.
tatyam is aiming to win in one go by using all the cards in the hand.
Find the largest prime number that can be made using all the cards in the hand.
Input
\(n\) \(a_1\) \(a_2\) … \(a_n\)
All input values are integers.
The first line contains the number of cards dealt \(n\) (\(1 \leq n \leq 9\)).
The second line contains the dealt cards \(a_i\) (\(1 \leq a_1 \leq a_2 \leq … \leq a_n \leq 13\)).
Output
Output the largest prime number that can be made by rearranging and using all the cards in the hand.
If no prime number can be made, output -1.
Print a newline at the end.
Sample
Sample 1
Input
3 1 7 9
Output
971
Among the numbers that can be made by rearranging 1, 7, 9, the primes are 179, 197, 719, 971, so output the largest of them, 971.
Sample 2
Input
9 13 13 13 13 13 13 13 13 13
Output
-1
Any arrangement results in a multiple of 13.
Sample 3
Input
7 1 2 4 6 8 10 12
Output
862412101
This is a famous even-card-consuming prime.
Sample 4
Input
5 1 7 7 9 13
Output
971713
This is larger than 971371.
Sample 5
Input
6 8 13 13 13 13 13
Output
81313131313
Be careful of overflow.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
tatyam