Under construction

Still being built. Things may change

Suffix Array for Thue-Morse

Time limit2sMemory limit512 MB

Summary
Given the order k of a Thue-Morse string of length 2^k, answer q queries for suf[p], the suffix array entries, with k up to 60.
Level

Hard9 of 10

Topics
Array, String, Divide and conquer, Recursion
Solved
No attempts yet

Problem

A Thue-Morse string of order kk is a string of length 2k2^{k} in which the ii-th symbol is A if the number of 11-bits in the binary representation of i−1i - 1 is even, and B if it is odd.

A suffix array for a string ss of length nn is a permutation sufsuf of the integers from 11 to nn such that s[suf[1]..n],s[suf[2]..n],…,s[suf[n]..n]s[suf[1]..n], s[suf[2]..n], \ldots, s[suf[n]..n] is the list of all non-empty suffixes of ss in lexicographical order.

Let sufsuf be the suffix array for the Thue-Morse string of order kk. Compute the qq values suf[p1],suf[p2],…,suf[pq]suf[p_1], suf[p_2], \ldots, suf[p_q].

Input

The first line contains two integers kk and qq, the order of the Thue-Morse string and the number of queries (0≤k≤600 \le k \le 60, 1≤q≤1051 \le q \le 10^5).

The second line contains qq integers p1,p2,…,pqp_1, p_2, \ldots, p_q, separated by spaces. These are the indices to query (1≤pi≤2k1 \le p_i \le 2^k).

Output

Print the qq answers separated by spaces.

Hint

The Thue-Morse string of order 33 is ABBABAAB.

Examples1

  1. Example 1

    Input
    3 8
    1 2 3 4 5 6 7 8
    
    Expected output
    6 7 4 1 8 5 3 2