#P11871. Tree-shaped Light Wiring

    ID: 13973 Type: Default 1000ms 256MiB

Tree-shaped Light Wiring

Tree-shaped Light Wiring

During the festival, Manman is preparing a tree‐shaped decorative light. The tree has several layers. In a complete tree with n layers, the i-th layer contains \(2i-1\) lights (for \(1\le i\le n\)). The wiring is done as follows:

  • For each layer \(i\) (where \(1\le i<n\)), each light in that layer is connected by wires to the three lights in the next layer at positions \(j\), \(j+1\) and \(j+2\) (if we index the lights in each row from left to right starting at 1).
  • For the last layer (i.e. the \(n\)-th layer), every two adjacent lights are connected by a wire.

Manman has already wired a complete tree with \(n=3\) layers (a total of \(9\) lights) so that the wiring work for that tree used \(3(3-1)^2 + (2\times3-2)=16\) wires. However, he needs to add t more lights. The extra lights are attached after the complete \(3\)-layer tree. When adding these extra lights, they are arranged in the same tree pattern: first the existing complete tree (with 9 lights) followed by filling the next layer from left to right. Note that in a complete (\(L+1\))-th layer the number of lights should be \(2(L+1)-1\); if the number of extra lights is less than that, the layer is incomplete.

For a tree with a total of \(T=9+t\) lights, let \(L=\lfloor\sqrt{T}\rfloor\) (so that the first \(L\) layers are completely filled since a complete tree with \(L\) layers has \(L^2\) lights) and let \(R=T-L^2\) be the number of lights in the incomplete layer (if any). The wiring is computed as follows:

For a complete tree with \(L\) layers:

[ \text{FullWires}(L)=3(L-1)^2+(2L-2). ]

For the incomplete \((L+1)\)-th layer (when \(R>0\)):

  • Each light in the complete \(L\)-th layer (which has \(2L-1\) lights) is supposed to connect to up to three children. In the complete case, the light \(j\) in layer \(L\) connects to positions \(j\), \(j+1\), and \(j+2\) in the next layer. When the layer is incomplete, only those children with indices \(\le R\) are added.
  • The total number of vertical (inter-layer) wires from layer \(L\) to the incomplete layer is computed by summing for each child position \(p\) (from 1 to \(R\)) the number of possible parents that would connect to it. In particular, the contribution per position is:
  1. If \(p=1\): contributes 1 wire.
  2. If \(p=2\): contributes 2 wires.
  3. If \(3\le p\le 2L-1\) (if \(R\) is in this range): contributes 3 wires each (so in total \(3R-3\) for \(R\ge3\)).
  4. If \(p=2L\): contributes 2 wires (since the parent with index \(2L\) does not exist as the \(L\)-th layer only has \(2L-1\) lights).

Let Vertical be the total wires from the last complete layer to the incomplete layer and let Horizontal be the wires connecting adjacent lights in the incomplete layer (which is \(R-1\) if \(R\ge1\)).

Then the total number of wires for the entire tree is:

[ \text{Answer} = \text{FullWires}(L) \quad \text{if}\ R=0, ]

[ \text{Answer} = \text{FullWires}(L) + \text{Vertical} + (R-1) \quad \text{if}\ R>0. ]

Your task is to compute the total number of wires given an integer t of extra lights (attached after the initial 9 lights).

inputFormat

The input consists of a single non-negative integer t representing the number of extra lights added to the initial complete tree of 9 lights.

It is guaranteed that t is an integer such that the total number of lights \(T=9+t\) is at least 9.

outputFormat

Output a single integer representing the total number of wires used after connecting all \(T=9+t\) lights according to the described wiring rules.

sample

0
16