#include <iostream> using namespace std; bool genNextPerm(int perm[], int n); bool inwersje(int perm[], int n); void showArr(int arr[], int arrSize); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (((n*(n-1))/2) % 2 == 1) { cout << "NIE"; return false; } if (n <= 150) { long double silnia = 1; for (int i = n; i > 1; i--) silnia *= i; if (silnia < k) { cout << "NIE"; return 0; } } int* perm = new int[n]; for (int index = 0; index < n; index++) { perm[index] = index+1; } int licz = 0; while(licz < k and genNextPerm(perm, n)) if (inwersje(perm, n)) licz++; if (licz == k) { cout << "TAK" << endl; showArr(perm, n); return 0; } else { cout << "NIE"; return 0; } } bool genNextPerm(int perm[], int n) { if (perm[0] == n) return false; int index = n-1; while (index > 0 and perm[index] < perm[index-1]) index--; int tmp = perm[index-1]; int nTmp = n-1; while (perm[index-1] > perm[nTmp]) { nTmp--; } perm[index-1] = perm[nTmp]; perm[nTmp] = tmp; while (index < n-1) { int tmp = perm[index]; perm[index] = perm[n-1]; perm[n-1] = tmp; n--; index++; } return true; } bool inwersje(int perm[], int n) { int a = 0; for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) if (perm[i] > perm[j]) a++; } if (a == ((n * (n-1))/4)) return true; else return false; } void showArr(int arr[], int arrSize) { for (int index = 0; index < arrSize; index++) cout << arr[index] << " "; }
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 | #include <iostream> using namespace std; bool genNextPerm(int perm[], int n); bool inwersje(int perm[], int n); void showArr(int arr[], int arrSize); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (((n*(n-1))/2) % 2 == 1) { cout << "NIE"; return false; } if (n <= 150) { long double silnia = 1; for (int i = n; i > 1; i--) silnia *= i; if (silnia < k) { cout << "NIE"; return 0; } } int* perm = new int[n]; for (int index = 0; index < n; index++) { perm[index] = index+1; } int licz = 0; while(licz < k and genNextPerm(perm, n)) if (inwersje(perm, n)) licz++; if (licz == k) { cout << "TAK" << endl; showArr(perm, n); return 0; } else { cout << "NIE"; return 0; } } bool genNextPerm(int perm[], int n) { if (perm[0] == n) return false; int index = n-1; while (index > 0 and perm[index] < perm[index-1]) index--; int tmp = perm[index-1]; int nTmp = n-1; while (perm[index-1] > perm[nTmp]) { nTmp--; } perm[index-1] = perm[nTmp]; perm[nTmp] = tmp; while (index < n-1) { int tmp = perm[index]; perm[index] = perm[n-1]; perm[n-1] = tmp; n--; index++; } return true; } bool inwersje(int perm[], int n) { int a = 0; for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) if (perm[i] > perm[j]) a++; } if (a == ((n * (n-1))/4)) return true; else return false; } void showArr(int arr[], int arrSize) { for (int index = 0; index < arrSize; index++) cout << arr[index] << " "; } |