I have a problem to solve from SPOJ-like platform and I can't come up with any idea of how to solve this. Here is the problem, translated with G translator but if something got lost i could try to translate it better
The entry gives the number of tests T (10 <= T <= 100). For each test, gives the number N (3 <= N <= 100). This number is an equilateral N angle (for example, an equilateral pentagon, for N = 5) with a side of 1. On each of the N vertices of the N-gon were seeded snails. Each snail as a "target" has set a snail to reach the other - the one who stood on the adjacent vertex (the fact that the direction of a neighboring node selection is the same all the time, ie, each snail "chasing" just one screw and each of the snail is "chased" by exactly one snail - the choice to be made by the snail only once at the beginning and does not change until the end of the chase). In one moment, snails begin to move toward its goal (at any time go exactly in a straight line to its target). It lasts until all the snails do not come in contact with each other at one point. To better illustrate this situation, please look at the picture below:
The arrows show how the chosen target, each of the snails. The cross indicates the approximate location where all come in contact with each other. Your task is to determine the distance that come each of snails (all will make exactly the same distance). If the result is more than two decimal places is to go round the second decimal place.
In summary:
input
Number of tests T
In the next T lines of N
output
For each test, the distance that come each of the snails during the chase (the result rounded to two decimal places).
Sample input:
5
3
5
7
9
91
output:
0.67
1.45
2.66
4.27
419.69
How to get the desired output from the sample input and what is some algorithm that could I use?
You need some Physics here. Look at it from the frame of reference of one of the ants. So one ant is always moving towards it. Now take the relative speed along the line joining the ants. This would come out to be v(1-cos(2*pi)/N)(Work this out. It's easy)
Now they meet when displacement equal to edge length. Hence time taken is 1/v(1-cos((2*pi)/n)). Distance travelled is v*t hence distance is 1/(1-cos((2*pi)/N)).
You can check the direct formula here.