Triangular web#4368

Triangular web (Editorial)

Let us consider how the fire spreads on an infinite triangular grid:

One can observe the following pattern: each new level adds 6 more vertices than the previous one.

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

res = 1 + (6 + 12 + 18 + ... + (6 + 6 (n - 1)))

Using the formula for the sum of an arithmetic progression \frac{2a_1+d(n-1)}{2} n, we obtain that

res = 1 + \frac{2 \cdot 6 + 6(n - 1)}{2} n = 1 + (6 + 3 (n - 1)) n = 1 + 3 n (n + 1)

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);