There is a perfect binary tree of height N. It has 2N+1−1 nodes in total, and 2N of them are leaves at depth N.
You walk down the tree using an N-bit binary number X. Read the bits of X starting from the leftmost one: a 0 takes you to the left child, a 1 takes you to the right child. For example, when N=2, X=0=002 takes you to the leftmost leaf, and X=3=112 takes you to the rightmost leaf. With X=2=102 you move from the root to its right child, then to that child's left child.
At the very beginning X=0 and only the root has been visited. Write a program that processes the following two queries.
1 C: change X to (X+2C)mod2N. Then walk down from the root using the new X and visit every node you pass. The root and the leaf you land on both count as visited nodes.
2: print how many distinct nodes have been visited so far. A node visited several times is counted once.