1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

const int MAX_N = 20;
int t;
int a[1 << MAX_N];

void printTree(int begin, int end) {
    const int len = end - begin;
    if (len == 1) {
        cout << a[begin] << " ";
    } else {
        const int halfLen = len / 2;
        if ((t & 1) == 0) {
            printTree(begin, begin + halfLen);
            printTree(begin + halfLen, end);
        } else {
            printTree(begin + halfLen, end);
            printTree(begin, begin + halfLen);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n >> t;
    const int npow = 1 << n;
    for (int i = 0; i < npow; ++i)
        cin >> a[i];
    printTree(0, npow);
    return 0;
}