#P2696. Josephus Gold Distribution

    ID: 15961 Type: Default 1000ms 256MiB

Josephus Gold Distribution

Josephus Gold Distribution

You are given an integer N representing the total number of people standing in a circle, labelled from 1 to N. A game is played in rounds following these rules:

  • In each round, the players are arranged in a circle in increasing order of their labels.
  • Starting from the first person, every alternate person is temporarily removed until only one person remains. This survivor is determined by the classical Josephus elimination process with a step size of 2. In fact, if there are k players in the round, the survivor’s label is given by \[ J(k)=2\Bigl(k-2^{\lfloor\log_2(k)\rfloor}\Bigr)+1. \]
  • After the survivor is identified, every player whose label is greater than the survivor’s label receives 1 coin and is permanently removed from the game.
  • The remaining players (those with labels less than or equal to the survivor's) participate in the next round.
  • This process is repeated until a round occurs in which no one is removed. This happens if the survivor is the player with the highest label among those playing. In that case, each of the remaining players receives 2 coins and the game ends.

Your task is to calculate the total number of coins that will be distributed by the end of the game.

inputFormat

The input consists of a single integer N (1 \leq N \leq 10^9), the total number of players in the game.

outputFormat

Output a single integer, representing the total number of coins distributed in the game.

sample

1
2