#P6949. Counting Triangles
Counting Triangles
Counting Triangles
For your trip to Beijing, you have brought plenty of puzzle books, many of them containing challenges like the following: how many triangles can be found in Figure I.1?
Figure I.1: Illustration of Sample Input \(2\).
Inspired by this puzzle, your task is to count the number of triangles in a triangular grid. The grid is formed by arranging smaller triangular units to construct a larger equilateral triangle. The challenge is to count all triangles that can be found in the grid, including those composed of multiple small triangles.
More formally, given a positive integer \(n\) representing the size of the triangular grid (i.e. the number of small triangles along one edge), you have to compute the total number of triangles in the grid.
The mathematical formulation is as follows:
Let \(T(n)\) denote the number of triangles for a grid of size \(n\). Then, it can be shown that:
[ T(n)=\begin{cases} \frac{n(n+2)(2n+1)}{8} & \text{if } n \text{ is even},\[8pt] \frac{n(n+2)(2n+1)-1}{8} & \text{if } n \text{ is odd}. \end{cases} ]
Your program should implement this calculation and output \(T(n)\) as an integer.
inputFormat
The input consists of a single integer \(n\) \((1 \leq n \leq 10^4)\), representing the size of the triangular grid.
outputFormat
Output a single integer, the total number of triangles in the grid.
sample
1
1