#P4904. Photo Arrangement

    ID: 18145 Type: Default 1000ms 256MiB

Photo Arrangement

Photo Arrangement

XHY needs to create a background image by arranging N photos, each of size $4\times3$ (in non‐rotated orientation) or $3\times4$ (when rotated). The photos must be placed without overlapping or gaps to form a perfect rectangle. In the arrangement, all photos must share the same orientation so that every row has the same height and every column the same width; that is, either using all photos in the non‐rotated configuration (size $4\times3$) or all photos rotated (size $3\times4$).

Using the non‐rotated orientation the overall rectangle will have height $3R$ and width $4C$, where the photos are arranged in a grid of R rows and C columns (with $R\times C=N$). Similarly, using the rotated orientation gives height $4R$ and width $3C$. For aesthetic reasons, XHY requires that the difference between the two sides of the rectangle be as small as possible, i.e. to minimize:

3R4C(for non-rotated arrangement)|3R-4C| \quad\text{(for non-rotated arrangement)}

In addition, if the best (most nearly square) arrangement gives a square (i.e. $3R=4C$), then the number of rotated photos should be minimized. Note that when using the non‐rotated configuration, no photo is rotated, while in the rotated configuration every photo is rotated. Thus, an optimal solution is obtained by considering grid factorizations $R\times C=N$ and choosing the one that minimizes |3R - 4C| and, when a square is possible, selecting the non-rotated arrangement.

Your task is to write a program which, given N, finds a factorization $R\times C=N$ that minimizes |3R-4C| and outputs the overall rectangle's width, height and the number of rotated photos (which will be zero when using the non-rotated configuration). The rectangle dimensions will be computed as:

  • Height = 3R
  • Width = 4C

It is guaranteed that an answer always exists.

inputFormat

The input consists of a single integer N ($1 \leq N \leq 10^9$), representing the number of photos.

outputFormat

Output three space‐separated integers: the width and height of the arranged background image, and the minimum number of rotated photos used.

sample

12
12 12 0