#P7127. Sum of Triangle Areas
Sum of Triangle Areas
Sum of Triangle Areas
For an integer parameter k, define Sk as the area of the triangle formed by the x-axis and the lines
\(l_{k1}:y=kx+k-1\) and \(l_{k2}:y=(k+1)x+k\)
and their point of intersection. Note that the intersection point of \(l_{k1}\) and \(l_{k2}\) is always \((-1,-1)\).
The other two vertices are obtained by the intersection of each line with the x-axis:
- For \(l_{k1}\): set \(kx+k-1=0\) to obtain \(x=\frac{1}{k}-1\) (provided \(k\ne0\)).
- For \(l_{k2}\): set \((k+1)x+k=0\) to obtain \(x=-\frac{k}{k+1}\) (provided \(k\ne-1\)).
Using the three vertices, the area of the triangle can be computed as
\(S_k=\frac{1}{2}\Big|\frac{1}{k} -1+\frac{k}{k+1}\Big|\).
Simplifying the expression, we find that
\(\frac{1}{k} -1 +\frac{k}{k+1}= \frac{1}{k(k+1)}\), so that
\(S_k=\frac{1}{2k(k+1)}\).
For a given positive integer n, we define the sum
\(\sum_{i=1}^{n} S_i = \frac{1}{2}\sum_{i=1}^{n} \frac{1}{i(i+1)}\).
Notice that the above sum telescopes and the closed form is
\(\frac{1}{2}\Big(1-\frac{1}{n+1}\Big)\) for \(n\ge1\). For \(n<1\), the sum is defined to be 0.
There are t test cases. In each test case, you are given an integer \(n_i\) and are required to output the value of the sum for \(n=n_i\).
inputFormat
The input begins with an integer t representing the number of test cases. Each of the following t lines contains an integer \(n_i\).
Note: If \(n_i < 1\), the sum is defined as 0.
outputFormat
For each test case, output a single line containing the value of \( \frac{1}{2}\Big(1-\frac{1}{n_i+1}\Big)\) if \(n_i\ge1\), or 0 if \(n_i < 1\). The answer should be printed as a floating-point number with 6 decimal places.
sample
3
1
2
3
0.250000
0.333333
0.375000
</p>