de Bruijn has a digital combination lock with buttons numbered to where . The lock opens when the last buttons pressed match the preset combination.
Unfortunately he has forgotten the combination. He creates a sequence of these digits which contains every possible combination of length . Then by pressing the buttons in this order he is sure to open the lock.
Consider all sequences of shortest possible length that contains every possible combination of the digits. Denote by the lexicographically smallest of these.
For example, 0010211220.
Define the sequence by and
Interpret each as a -digit combination, adding leading zeros for any with less than digits.
Given a positive integer , we are interested in the order the combinations appear in . Denote by the place, numbered , in which appears out of . Define .
For example, the combination is entered before . Therefore: