Thue-Morse String
InterviewTime limit1sMemory limit256 MB
Find the character at position k in the Thue-Morse sequence, where k can be as large as 10^18.
- Level
Easy3 of 10
- Topics
- Bit manipulation, Math, Recursion
- Solved
- No attempts yet
Problem
There is an infinite string X made of 0s and 1s. It is built as follows.
- X starts as "0".
- Form the string X' by swapping every 0 in X to 1 and every 1 to 0.
- Redefine X as the string obtained by appending X' to the end of X.
- Repeat steps 2 and 3 forever.
So X starts as "0", then becomes "01", then "0110", then "01101001", and so on, giving
"011010011001011010010110011010011001011001101001⋯⋯"
Given a positive integer k, find which character stands at position k of X.
Input
The first line gives the positive integer k (1 ≤ k ≤ 10^18).
Output
Print the character at position k on the first line.