Problem summary

Given a radius of inner circle $r$ and the amount of outer circle $n$ to fill around the inner circle, find the radius of the outer circle $R$ up to 6 decimal points.

Approach method

Draw any valid combination for such circles, denote the first outer circle as $C_1$ and the other outer circle next to it $C_2$. We connect the center of $C_1$ to the center of $C_2$, then connect both points to the center of the inner circle. This forms a triangle that will be denoted as $\triangle ABC$, where B is the center of the inner circle.

Now observe that $\overline{AB} = \overline{AC}$ because $\triangle ABC$ is isoceles. Also observe that length of $\overline{AB} = \overline{AC} = r+R$. The angle of $\angle ABC$ is $\frac{2\pi}{n}$ since we have $n$ outer circle to fill with. With this in mind, we can apply the law of cosines.

$$ 4R^2 = (R+r)^2 + (R+r)^2 - 2(R+r)(R+r)\cos\left(\frac{2\pi}{n}\right) $$

This is a quadratic equation on R. Solving for positive side results in:

$$ R = \dfrac{\sqrt{2}\times\sqrt{-r^2\left(\cos\left(\dfrac{2\pi}{n}\right)-1\right)} + r\left(-\cos\left(\dfrac{2\pi}{n}\right)\right)+ r}{\cos\left(\dfrac{2\pi}{n}\right) + 1} $$

Such formulas can be simplified, but we just wanted to get the result as quickly as possible without sacrificing time and accuracy to potentially wrong simplification, so on to the implementation.

Constraints

$\{n\ |\ n \in \mathbb{Z},\ 3 \leq n \leq 100\}$
$\{r\ |\ r \in \mathbb{Z},\ 1 \leq r \leq 100\}$

Solution

Solution 1 - Direct computation

Implementation in C++:

#include <iostream>
#include <cmath>

using std::cos;
using std::sqrt;
int main() 
{
        std::ios::sync_with_stdio(0);
        std::cin.tie(0);
        
        double n;
        double r;
        std::cin >> n >> r;
        
        const double PI = 3.141592653589793238462643383279;
        double c_val = cos(2*PI/n);
        double res = (sqrt(2)
                      * sqrt(-(r*r)*(c_val-1)) 
                      + r*(-c_val) 
                      + r)
                     / (c_val + 1);

        std::cout.precision(20);
        std::cout << res << std::endl;

        return 0;
}

Time complexity: $O(1)$