import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/*
* This Java source file was auto generated by running 'gradle buildInit --type java-library'
* by 'mwypych' at '22.11.16 00:44' with Gradle 3.0
*
* @author Michal Wypych, @date 22.11.16 00:44
*/
public class tas {
/** code from https://www.cpe.ku.ac.th/~jim/java-io.html
* Class for buffered reading int and double values */
private static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
public static void shuffle(int[] cards, int start, int end) {
if (start >= end)
return;
int middle = (end + start)/2;
shuffle(cards,start,middle);
shuffle(cards,middle+1,end);
swap(cards,start,end);
}
public static void swap(int[] cards, int start, int end) {
final int middle = (end + start)/2;
final int offset = (end - start)/2 + 1;
for(int i=start; i<= middle; i++) {
int tmp = cards[i];
cards[i] = cards[i+offset];
cards[i+offset] = tmp;
}
}
public static void reverse(int[] cards,int start, int end) {
final int middle = (end + start)/2;
for(int i=start; i<= middle; i++) {
int tmp = cards[i];
cards[i] = cards[end-i];
cards[end-i] = tmp;
}
}
public static void shuffle2(int[] cards) {
shuffle(cards,0,cards.length-1);
}
public static void shuffle(int[] cards) {
reverse(cards,0,cards.length-1);
}
public static int[] readTable(int nElements) throws IOException {
int[] t = new int[nElements];
for (int i = 0; i < nElements; i++) {
t[i] = Reader.nextInt();
}
return t;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n = Reader.nextInt();
int nShuffles = Reader.nextInt();
int nElements = 1<<n;
int[] cards = readTable(nElements );
nShuffle(nShuffles, cards);
print(cards);
}
private static void print(int[] cards) {
for (int v : cards) {
System.out.print(v);
System.out.print(" ");
}
System.out.println();
}
private static void nShuffle(int nShuffles, int[] cards) {
if (nShuffles % 2 == 1) {
shuffle(cards);
}
}
}
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * This Java source file was auto generated by running 'gradle buildInit --type java-library' * by 'mwypych' at '22.11.16 00:44' with Gradle 3.0 * * @author Michal Wypych, @date 22.11.16 00:44 */ public class tas { /** code from https://www.cpe.ku.ac.th/~jim/java-io.html * Class for buffered reading int and double values */ private static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } public static void shuffle(int[] cards, int start, int end) { if (start >= end) return; int middle = (end + start)/2; shuffle(cards,start,middle); shuffle(cards,middle+1,end); swap(cards,start,end); } public static void swap(int[] cards, int start, int end) { final int middle = (end + start)/2; final int offset = (end - start)/2 + 1; for(int i=start; i<= middle; i++) { int tmp = cards[i]; cards[i] = cards[i+offset]; cards[i+offset] = tmp; } } public static void reverse(int[] cards,int start, int end) { final int middle = (end + start)/2; for(int i=start; i<= middle; i++) { int tmp = cards[i]; cards[i] = cards[end-i]; cards[end-i] = tmp; } } public static void shuffle2(int[] cards) { shuffle(cards,0,cards.length-1); } public static void shuffle(int[] cards) { reverse(cards,0,cards.length-1); } public static int[] readTable(int nElements) throws IOException { int[] t = new int[nElements]; for (int i = 0; i < nElements; i++) { t[i] = Reader.nextInt(); } return t; } public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int nShuffles = Reader.nextInt(); int nElements = 1<<n; int[] cards = readTable(nElements ); nShuffle(nShuffles, cards); print(cards); } private static void print(int[] cards) { for (int v : cards) { System.out.print(v); System.out.print(" "); } System.out.println(); } private static void nShuffle(int nShuffles, int[] cards) { if (nShuffles % 2 == 1) { shuffle(cards); } } } |
English