Under construction

Still being built. Things may change

Safe Unlock Code

Time limit1sMemory limit128 MB

Summary
For each n, print the lexicographically smallest de Bruijn sequence of order n over the digits 0 to 9, whose length is 10^n + n - 1.
Level

Medium7 of 10

Topics
Graph, DFS, Greedy, Implementation
Solved
No attempts yet

Problem

KEY Inc., a leading maker of security hardware, has built a new kind of safe. To open it you do not use a key; instead you must type the correct nn-digit code on a keypad. Models range from toy safes for children (a 2-digit code) up to the military version (a 6-digit code).

The safe opens the instant the last digit of the correct code is entered — there is no "Enter" key. If you type more than nn digits, only the last nn digits matter. For example, in the 4-digit version, if the correct code is 4567 and you type the sequence 1234567890, the door opens the moment you press 7.

Internally the safe is always in one of 10n−110^{n-1} states, where the current state is simply the last n−1n-1 digits entered. One of these states (456 in the example) is marked as the unlocked state. If the safe is in the unlocked state and the correct next key (7 in the example) is pressed, the door opens; otherwise the safe moves to a new state. For example, from state 456, pressing 8 moves the safe to state 568.

A naive way to open the safe is to try every code in turn, which in the worst case needs n⋅10nn \cdot 10^n keystrokes. With a well-chosen digit sequence, however, you can guarantee the safe opens after at most 10n+n−110^n + n - 1 keystrokes: all you need is a digit sequence that contains every nn-digit sequence exactly once.

Input

The input contains several test cases. Each test case is a single integer nn with 1≤n≤61 \le n \le 6. The last test case is followed by a line containing 0, which must not be processed.

Output

For each test case nn, print a single line: the lexicographically smallest sequence of 10n+n−110^n + n - 1 digits (each digit 0–9) in which every nn-digit sequence occurs exactly once as a contiguous substring.

Examples3

  1. Example 1

    Input
    1
    2
    0
    
    Expected output
    0123456789
    00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    0123456789
    
  3. Example 3

    Input
    3
    0
    
    Expected output
    000100200300400500600700800901101201301401501601701801902102202302402502602702802903103203303403503603703803904104204304404504604704804905105205305405505605705805906106206306406506606706806907107207307407507607707807908108208308408508608708808909109209309409509609709809911121131141151161171181191221231241251261271281291321331341351361371381391421431441451461471481491521531541551561571581591621631641651661671681691721731741751761771781791821831841851861871881891921931941951961971981992223224225226227228229233234235236237238239243244245246247248249253254255256257258259263264265266267268269273274275276277278279283284285286287288289293294295296297298299333433533633733833934434534634734834935435535635735835936436536636736836937437537637737837938438538638738838939439539639739839944454464474484494554564574584594654664674684694754764774784794854864874884894954964974984995556557558559566567568569576577578579586587588589596597598599666766866967767867968768868969769869977787797887897987998889899900