Under construction

Still being built. Things may change

Binary vs Decimal

Time limit1sMemory limit256 MB

Summary
Find the n-th positive integer whose decimal digits form a suffix of its binary representation.
Level

Medium7 of 10

Topics
Number theory, Bit manipulation, Brute force
Solved
No attempts yet

Problem

A positive integer is bindecimal if its decimal representation is a suffix of its binary representation. Both representations are written without leading zeros.

For example, 1010=1010210_{10} = 1010_2, and the decimal representation 10 matches the last two digits of the binary representation 1010, so 10 is bindecimal. On the other hand 101010=111111001021010_{10} = 1111110010_2, and 1010 is not a suffix of 1111110010, so 1010 is not bindecimal. Neither is 42, because 4210=101010242_{10} = 101010_2 and 42 is not a suffix of 101010.

Find the nn-th smallest bindecimal number.

Input

The first and only line contains one integer nn. (1≤n≤100001 \le n \le 10000)

Output

Print the nn-th smallest bindecimal number in decimal notation.

Hint

A binary representation uses only the digits 0 and 1, so the decimal representation of a bindecimal number also uses only 0 and 1. Here are the smallest numbers whose decimal representation contains only 0's and 1's.

DecimalBinaryComment
111st bindecimal number
1010102nd bindecimal number
1110113rd bindecimal number
10011001004th bindecimal number
10111001015th bindecimal number
11011011106th bindecimal number
11111011117th bindecimal number
100011111010008th bindecimal number
100111111010019th bindecimal number
10101111110010Not a bindecimal number
10111111110011Not a bindecimal number
11001000100110010th bindecimal number

Examples3

  1. Example 1

    Input
    1
    
    Expected output
    1
    
  2. Example 2

    Input
    2
    
    Expected output
    10
    
  3. Example 3

    Input
    10
    
    Expected output
    1100