import java.util.Scanner; /** * Created by Patrycja on 22.11.2016. */ public class tas { private int pow(int a, int b) { if ( b == 0) return 1; if ( b == 1) return a; if (b%2 == 0) return pow (a*a, b/2); else return a*pow(a*a, b/2); } private void change(int[] tab, int i, int j) { int n = j - i + 1; for(int k = i, l = 0; l < n/2; k++, l++) { int tmp = tab[k]; tab[k] = tab[k+n/2]; tab[k+n/2] = tmp; } } private void func(int[] tab, int i, int j) { if(i == j) return; int tmp = i + (j+1-i)/2; func(tab, i, tmp-1); func(tab, tmp, j); change(tab, i, j); } public static void main(String[] args) { tas ts = new tas(); Scanner in = new Scanner(System.in); int n = in.nextInt(); n = ts.pow(2, n); int t = in.nextInt(); int[] tab = new int[n]; for(int i = 0; i < n; i++) { tab[i] = in.nextInt(); } in.close(); for(int i = 0; i < t%2; i++) { ts.func(tab, 0, n - 1); } System.out.print(tab[0] + " "); for(int i = 1; i < n - 1; i++) { System.out.print(tab[i] + " "); } System.out.print(tab[n-1]); System.out.println(); } }
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 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.util.Scanner; /** * Created by Patrycja on 22.11.2016. */ public class tas { private int pow(int a, int b) { if ( b == 0) return 1; if ( b == 1) return a; if (b%2 == 0) return pow (a*a, b/2); else return a*pow(a*a, b/2); } private void change(int[] tab, int i, int j) { int n = j - i + 1; for(int k = i, l = 0; l < n/2; k++, l++) { int tmp = tab[k]; tab[k] = tab[k+n/2]; tab[k+n/2] = tmp; } } private void func(int[] tab, int i, int j) { if(i == j) return; int tmp = i + (j+1-i)/2; func(tab, i, tmp-1); func(tab, tmp, j); change(tab, i, j); } public static void main(String[] args) { tas ts = new tas(); Scanner in = new Scanner(System.in); int n = in.nextInt(); n = ts.pow(2, n); int t = in.nextInt(); int[] tab = new int[n]; for(int i = 0; i < n; i++) { tab[i] = in.nextInt(); } in.close(); for(int i = 0; i < t%2; i++) { ts.func(tab, 0, n - 1); } System.out.print(tab[0] + " "); for(int i = 1; i < n - 1; i++) { System.out.print(tab[i] + " "); } System.out.print(tab[n-1]); System.out.println(); } } |