Janosik
Time limit1sMemory limit128 MB
Count how many money bags Janosik pockets when n caskets holding 1 to n bags are emptied by the smallest-first split, pocket, or hand-out rule.
- Level
Medium7 of 10
- Topics
- Math, Bit manipulation
- Solved
- No attempts yet
Problem
Janosik, also known as Robin Hood, takes from the rich to give to the poor. He and his gang ambushed a convoy carrying gold to the counts' castle and made off with caskets. Once the loot reached their cave they counted it: casket (for ) holds exactly money-bags full of gold.
When a poor man comes asking for a few gold ducats, Janosik follows this procedure. He first picks a non-empty casket that holds the fewest money-bags.
- If that casket holds exactly one money-bag, he hands it to the man, who leaves happy.
- If it holds more than one money-bag and the count is odd, he slips one money-bag into his own pocket and starts the procedure again from the beginning.
- If the count is even, he takes out exactly half of the money-bags and puts them in an empty casket (spare caskets are plentiful in the cave), then starts the procedure again from the beginning.
As long as at least one non-empty casket is left, the visitor is sure to walk away with a money-bag of gold after some number of rounds of the procedure. The poor keep coming to the cave until every casket is empty.
The other robbers wonder whether their leader is ruining the good name of thugs. They want to know how many looted money-bags stay in Janosik's pocket once all the caskets are empty.
Input
The first and only line contains one integer (), the number of caskets that Janosik's gang robbed.
Output
Print the number of money-bags of gold that stay in Janosik's pocket after all the caskets are empty.