import java.util.Scanner; public class tas { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int iterations = sc.nextInt(); int nums = powerOfTwo(n); int[] numbers = new int[nums]; for (int i = 0; i < nums; i++) { numbers[i] = sc.nextInt(); } if (iterations % 2 == 0) { for (int i = 0; i < nums; i++) { System.out.print(numbers[i]); System.out.print((i < nums - 1) ? " " : ""); } } else { for (int i = nums - 1; i >= 0; i--) { System.out.print(numbers[i]); System.out.print((i > 0) ? " " : ""); } } } public static int powerOfTwo(int n) { return powOfTwo(2, n); } private static int powOfTwo(int x, int n) { if (n == 1) { return x; } else if (n % 2 == 0) { return powOfTwo(x * x, n / 2); } else { return x * powOfTwo(x * x, n / 2); } } }
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 43 | import java.util.Scanner; public class tas { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int iterations = sc.nextInt(); int nums = powerOfTwo(n); int[] numbers = new int[nums]; for (int i = 0; i < nums; i++) { numbers[i] = sc.nextInt(); } if (iterations % 2 == 0) { for (int i = 0; i < nums; i++) { System.out.print(numbers[i]); System.out.print((i < nums - 1) ? " " : ""); } } else { for (int i = nums - 1; i >= 0; i--) { System.out.print(numbers[i]); System.out.print((i > 0) ? " " : ""); } } } public static int powerOfTwo(int n) { return powOfTwo(2, n); } private static int powOfTwo(int x, int n) { if (n == 1) { return x; } else if (n % 2 == 0) { return powOfTwo(x * x, n / 2); } else { return x * powOfTwo(x * x, n / 2); } } } |