Let us consider how the fire spreads on an infinite triangular grid:
Initial vertex. At time , only the initial vertex is burning. This contributes the first unit to the total number of burning vertices.
First level (after second). The vertices that are directly adjacent to the initial one catch fire after 1 second. In a triangular grid, each vertex has neighbors. Therefore, the first level contains vertices.
Second level (after seconds). In the next second, the vertices that are adjacent to the already burning vertices of the first level, but are not yet burning, catch fire. The second level contains vertices.
One can observe the following pattern: each new level adds 6 more vertices than the previous one.
First level: vertices
Second level: vertices
Third level: vertices
... and so on.
Thus, we obtain an arithmetic progression with the first term a_1 = 6 and common difference d = 6.
After seconds, the number of burning vertices is equal to
Using the formula for the sum of an arithmetic progression \frac{2a_1+d(n-1)}{2} n, we obtain that
Algorithm implementation
Read the input value of .
scanf("%lld",&n);Compute and print the answer.
res = 1 + 3 * n * (n + 1);
printf("%lld\n",res);