This page is still under construction.

Parts of this page are still being built. What you see may change.

Smallest LNR Sequence

Time limit1sMemory limit128 MB

Summary
Given n and a binary string s, find the position of s in the lexicographically smallest de Bruijn sequence of order n.
Level

Medium7 of 10

Topics
Graph, Greedy, DFS
Solved
No attempts yet

Problem

An nn-bit shift register is a memory that holds nn bits, and its contents can be changed in only two ways. The contents are shifted left by one position, and the rightmost bit is given a 0 or a 1. One such change is called a move. For example, when the contents of a 4-bit shift register are 0001, one move makes the contents either 0010 (a 0 is shifted in) or 0011 (a 1 is shifted in).

Start from an nn-bit shift register whose contents are all zeroes. Build a sequence of moves so that after every move the contents differ from all past contents. An nn-bit shift register has 2n2^n possible contents, so the longest such sequence has length 2n2^n. For every nn, at least one sequence of length 2n2^n exists. For n=3n = 3 there are two sequences of length 23=82^3 = 8.

AA = 000 001 010 101 011 111 110 100

BB = 000 001 011 111 110 101 010 100

LNR (Longest Non-Repeating) sequences

Given a positive integer nn, consider any sequence with the following five properties.

  1. Each element of the sequence is a bit-string of length nn.
  2. The sequence has 2n2^n elements.
  3. No element repeats.
  4. All nn bits of the first element are zero.
  5. Every element other than the first comes from the element before it by one move as described above.

Such a sequence is a Longest-Non-Repeating sequence, or LNR sequence. For a positive integer nn, the set of all LNR sequences is written S(n)S(n).

The smallest LNR sequence

An LNR sequence s∈S(n)s \in S(n) gets a value value(s)value(s) as follows. Concatenate the 2n2^n bit-strings of ss into a single bit-string, then read that bit-string as a binary number. For the LNR sequence AA above,

value(A)value(A) = 000001010101011111110100

The smallest LNR sequence ssmall(n)s_{small}(n) is the sequence in S(n)S(n) with the smallest value(s)value(s). For n=3n = 3 the only LNR sequences are AA and BB, and value(A)<value(B)value(A) < value(B), so ssmall(3)=As_{small}(3) = A. The positions of the bit-strings 000, 001, 010, 101, 011, 111, 110, 100 in AA are 1, 2, 3, 4, 5, 6, 7, 8 respectively.

What you need to do

Given a positive integer n≤10n \le 10 and a bit-string ss of length nn, compute ssmall(n)s_{small}(n) and print the position of ss in it. Print 1 if ss is the first element of ssmall(n)s_{small}(n), print 2 if it is the second element, and so on.

Input

The input has two lines. The first line contains a positive integer n≤10n \le 10. The second line contains a bit-string ss of length nn.

Output

Print a single positive integer, the position of ss in ssmall(n)s_{small}(n).

Hint

Do not construct all LNR sequences and then pick the smallest one. You can find the smallest LNR sequence by trying the move that shifts in a 0 before the move that shifts in a 1.

Examples1

  1. Example 1

    Input
    3
    111
    
    Expected output
    6