It doesn't get simpler!
Time limit6sMemory limit1024 MB
Given n up to 10^500000, find the n-th element of the sequence built by concatenating blocks 1, 1 2 1, 1 2 3 2 1, and so on.
- Level
Medium4 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Returning home from school, Ivan thought for a long time about that day's math club session, where the teacher talked about infinite number sequences. As one example, the teacher showed the following interesting sequence of positive integers:
The teacher explained that every positive integer appears infinitely many times in this sequence. Ivan, however, was curious about another question: how can you find the number at position of the sequence? The teacher said that this is very simple and suggested that Ivan work out the problem on his own.
Ivan likes programming as well as math, so he wants to implement an algorithm that answers this question quickly for very large values of . Please help him.
Input
The first line contains one integer ().
Output
Print one integer without spaces and without leading zeros: the -th element of the given sequence.
Hint
The jury does not guarantee that a solution exists that runs within twice the time limit on all tests.