Davis the magician

3.5

13 votes
Problem

Davis the magician showed a trick to Yash . He ask Yash to choose a number between 1 and N. ( 1<=N<=10^18 ) After choosing the numbers magician picks up a card list. Each card have a sorted list of numbers written on top of it. (A card can have minimum of 1 and maximum of N numbers written on it.) Cards are arranged in dictionary order (eg. {1,4} comes before {3,4} in dictionary order and {3,6,7} comes before {5,8}) and no two consecutive cards can have common numbers ( eg. {1,4},{2,4} is not allowed)

Then magician showed each card to Yash and asked him to tell whether the number appears in it or not. Finally as there is no card left in deck he tells the number chosen by Yash. By seeing Yash puzzled Davis gives a hint that he just added the very first digit of card on which the number appears. at home Yash tried to do the trick but failed as he has no clues of minimum number of cards.write a code to find out the minimum no. of cards.

INPUT: A single line containing N. OUTPUT: minimum no. of cards required Example

sample Input 1: 1

Sample Output 1: 1

sample Input 2: 4

Sample Output 2: 3

Note:- See sample explanation

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In example 1, only 1 card containing {1} will work. In example 2, make 3 cards containing {1,4}, {2} and {3,4}. Assume you thought of 1, then you will select the 1st card {1,4}, then she will correctly figure out the integer you thought being 1. Assume you thought of 2, then you will select the 2nd card {2}, then she will correctly figure out the integer you thought being 2. Assume you thought of 3, then you will select the 3rd card {3,4}, then she will correctly figure out the integer you thought being 3. Assume you thought of 4, then you will select 1st card {1,4} and 3rd card {3,4}, then she will calculate the sum of the first integers of the two card 1 + 3 = 4, and she will answer it.

Editor Image

?