#P5695. Arctan Equation Minimum Sum

    ID: 18923 Type: Default 1000ms 256MiB

Arctan Equation Minimum Sum

Arctan Equation Minimum Sum

We are given the following trigonometric equation:

$$ \arctan\Big(\frac{1}{a}\Big)=\arctan\Big(\frac{1}{b}\Big)+\arctan\Big(\frac{1}{c}\Big) $$

where a, b, c are positive integers (\(\mathbb{N}^+\)).

Your task is: For a given positive integer a, find positive integers b and c satisfying the above equation such that if there are multiple solutions, the sum b + c is minimized. Finally, output the value of b + c.

Hint: By using the tangent addition formula, the given equation can be transformed into

$$ \frac{b+c}{bc-1}=\frac{1}{a} $$

which simplifies to

$$ bc-a(b+c)=1. $$

Furthermore, by rearranging the above equation, one can derive the equivalent form

$$ (b-a)(c-a)=a^2+1. $$

This formulation may help in designing an efficient solution.

inputFormat

The input consists of a single line containing one positive integer a (\(1 \le a \le 10^9\)).

outputFormat

Output a single integer, the minimum possible sum b + c for the corresponding solution.

sample

1
5