There are N monsters, numbered 1,2,…,N.
Initially, the health of Monster $i$ is $A_i$.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
Find the minimum possible final health of the last monster alive.
Input is given from Standard Input in the following format:
1 | N |
Print the minimum possible final health of the last monster alive.
1 | 4 |
1 | 2 |
When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.
1 | 4 |
1 | 1 |
1 | 3 |
1 | 1000000000 |
Assume we have only two numbers $A_1 = 5, A_2 = 8$, we let them attack each other which means we let $A_1-A_2 $ or $A_2-A_1$. First we let $A_2= A_2-A_1=3$, then let $A_1 = A_1-A_2=2$, and then $A_2= A_2-A_1 = 1$. Once the $1$ occurs, we let it to attack other monsters until they all die and the rest is $1$.
Seeing the operation, we can easily think of Euclidean algorithm which is the way to calculate GCD.
So the answer is the GCD of these numbers.
1 |
|