printРабочее место участника

printЗадачи

1157. Fibonacci System

Ограничения: время – 1s/2s, память – 64MiB Ввод: input.txt или стандартный ввод Вывод: output.txt или стандартный вывод copy
Послать решение Blockly Посылки Темы Где Обсудить (0)

Little John studies numeral systems. After learning all about fixed-base systems, he became interested in more unusual cases. Among those cases he found a Fibonacci system, which represents all natural numbers in an unique way using only two digits: zero and one. But unlike usual binary scale of notation, in the Fibonacci system you are not allowed to place two 1s in adjacent positions.
One can prove that if you have number `N\ =\ a_n\ a_{n-1}\ …\ a_{1F}` in Fibonacci system, its value is equal to `N\ =\ a_n*F_n\ +a_{n-1}*F_{n-1}\ +\ …\ +\ a_1*F_1`, where `F_k` is a usual Fibonacci sequence defined by `F_0\ =\ F_1\ =\ 1`, `F_i\ =\ F_{i-1}\ +\ F_{i-2}`.
For example, first few natural numbers have the following unique representations in Fibonacci system:
`1\ =\ 1_F`
`2\ =\ 10_F`
`3\ =\ 100_F`
`4\ =\ 101_F`
`5\ =\ 1000_F`
`6\ =\ 1001_F`
`7\ =\ 1010_F`
John wrote a very long string (consider it infinite) consisting of consecutive representations of natural numbers in Fibonacci system. For example, the first few digits of this string are 110100101100010011010…
He is very interested, how many times the digit 1 occurs in the `N`-th prefix of the string. Remember that the `N`-th prefix of the string is just a string consisting of its first `N` characters.
Write a program which determines how many times the digit 1 occurs in `N`-th prefix of John's string.
Input
The input file contains a single integer `N` (`0\ ≤\ N\ ≤\ 10^15`).
Output
Output a single integer – the number of 1s in `N`-th prefix of John's string.

Sample Input

21

Sample Output

10
Source: ACM ICPC NEERC, 2008
loading