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
35
36
37
38
39
40
41
42
import java.util.Arrays;
import java.util.Scanner;

public class tas {

  public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
      int n = scanner.nextInt();
      int t = scanner.nextInt();
      int cards[] = new int[(int) Math.pow(2, n)];

      for (int i = 0; i < cards.length; i++) {
        cards[i] = scanner.nextInt();
      }

      calculateAndPrintResultUsingSimpleMethod(t, cards);
    }
  }

  private static void calculateAndPrintResultUsingSimpleMethod(int t, int[] cards) {
    if (t % 2 == 0) {
      printCards(cards);
      return;
    }
    int halfIndex = (cards.length - 1) / 2;
    for (int i = 0; i <= halfIndex; i++) {
      swap(cards, i, cards.length - i - 1);
    }
    printCards(cards);
  }

  private static void swap(int[] cards, int leftIndex, int rightIndex) {
    int tmp = cards[rightIndex];
    cards[rightIndex] = cards[leftIndex];
    cards[leftIndex] = tmp;
  }

  private static void printCards(int[] cards) {
    Arrays.stream(cards).forEach(card -> System.out.print(card + " "));
  }

}