#include<iostream> #include<cstring> using namespace std; int pow(int base, int pow) { if(pow <= 0) return 1; else { int res = 1; for(int i=0; i<pow; i++) { res *= base; } return res; } } void print(int * cards, int cards_num) { for(int i = 0; i < cards_num; i++) cout << cards[i] << " "; cout<<endl; } int main() { int t,n; cin>>n>>t; int cards_num = pow(2, n); int * cards; int * temp; cards = new int[n]; temp = new int[n]; for(int i = 0; i < cards_num; i++) cin >> cards[i]; if(t % 2 == 0) { print(cards, cards_num); } else { int swap_size = cards_num/2; for(int i = 0; i < n; i++) { for(int j = 0; j < cards_num; j = j + 2*swap_size) { //cout << "i = " << i <<" j = " << j <<endl; memcpy(temp, cards + j, sizeof(int)*swap_size); memcpy(cards + j, cards + j + swap_size, sizeof(int)*swap_size); memcpy(cards + j + swap_size, temp, sizeof(int)*swap_size); //print(cards, cards_num); } swap_size /= 2; } print(cards, cards_num); } }
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 | #include<iostream> #include<cstring> using namespace std; int pow(int base, int pow) { if(pow <= 0) return 1; else { int res = 1; for(int i=0; i<pow; i++) { res *= base; } return res; } } void print(int * cards, int cards_num) { for(int i = 0; i < cards_num; i++) cout << cards[i] << " "; cout<<endl; } int main() { int t,n; cin>>n>>t; int cards_num = pow(2, n); int * cards; int * temp; cards = new int[n]; temp = new int[n]; for(int i = 0; i < cards_num; i++) cin >> cards[i]; if(t % 2 == 0) { print(cards, cards_num); } else { int swap_size = cards_num/2; for(int i = 0; i < n; i++) { for(int j = 0; j < cards_num; j = j + 2*swap_size) { //cout << "i = " << i <<" j = " << j <<endl; memcpy(temp, cards + j, sizeof(int)*swap_size); memcpy(cards + j, cards + j + swap_size, sizeof(int)*swap_size); memcpy(cards + j + swap_size, temp, sizeof(int)*swap_size); //print(cards, cards_num); } swap_size /= 2; } print(cards, cards_num); } } |