#P7573. Fair Cake Division

    ID: 20767 Type: Default 1000ms 256MiB

Fair Cake Division

Fair Cake Division

There are \( n \) people, and lhm has a cake of mass \(1\). Everyone wants to have a share of lhm's cake. To ensure fairness, lhm wants to use the minimum number of knife cuts to divide the cake into \( n \) equal shares (a share can consist of several pieces).

The cake is considered as a circle. Note that every cut must be made along a diameter of the cake.

In the end, although the number of pieces each person receives can vary, each person must receive a total mass of \( \frac{1}{n} \).

Your task is to determine the minimum number of diameter cuts needed by lhm.

Analysis:
A single diameter cut divides the cake into 2 pieces. In general, using \( k \) cuts all through the center, you can obtain up to \(2k\) pieces. In the case when \( n=1 \), no cut is needed. For \( n>1 \):

  • If \( n \) is even, you can evenly cut the cake into \( n \) pieces with \( \frac{n}{2} \) cuts (by making the cuts equally spaced, each piece will have mass \(\frac{1}{n}\)).
  • If \( n \) is odd, it is impossible to directly obtain exactly \( n \) equal parts since every cut (except when no cut is made) produces an even number of pieces. However, you can make \( \frac{n+1}{2} \) cuts to get \( n+1 \) pieces and then combine some of the pieces appropriately so that each person gets a total mass of \( \frac{1}{n} \).

Thus, the answer can be formulated as:

[ \text{ans} = \begin{cases} 0, & \text{if } n = 1, \ \frac{n}{2}, & \text{if } n > 1 \text{ and } n \text{ is even}, \ \frac{n+1}{2}, & \text{if } n > 1 \text{ and } n \text{ is odd}. \end{cases} ]

</p>

inputFormat

The input consists of a single line containing an integer \( n \) (where \( n \ge 1 \)), which is the number of people.

outputFormat

Output a single integer representing the minimum number of diameter cuts required.

sample

1
0