#include <iostream> #include <algorithm> using namespace std; bool isStablePermutation(int * arr, int n) { int inversions = 0; int goodInv = n % 4 == 0 ? n / 4 * (n - 1) : (n - 1) / 4.0 * n; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (arr[i] > arr[j]) ++inversions; } } return inversions==goodInv; } void show(int * arr, int n) { for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } } int main() { int n; long long k; cin >> n >> k; if (n % 4 == 0 || (n - 1) % 4 == 0) { int * arr = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = i + 1; } long long numberOfStablePermutations = 0; do { if (isStablePermutation(arr, n)) { if (++numberOfStablePermutations == k) { cout << "TAK" << endl; show(arr, n); cout << endl; delete[] arr; return 0; } } } while (next_permutation(arr, arr + n)); delete[] arr; cout << "NIE" << endl; } else cout << "NIE" << endl; return 0; }
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 | #include <iostream> #include <algorithm> using namespace std; bool isStablePermutation(int * arr, int n) { int inversions = 0; int goodInv = n % 4 == 0 ? n / 4 * (n - 1) : (n - 1) / 4.0 * n; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (arr[i] > arr[j]) ++inversions; } } return inversions==goodInv; } void show(int * arr, int n) { for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } } int main() { int n; long long k; cin >> n >> k; if (n % 4 == 0 || (n - 1) % 4 == 0) { int * arr = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = i + 1; } long long numberOfStablePermutations = 0; do { if (isStablePermutation(arr, n)) { if (++numberOfStablePermutations == k) { cout << "TAK" << endl; show(arr, n); cout << endl; delete[] arr; return 0; } } } while (next_permutation(arr, arr + n)); delete[] arr; cout << "NIE" << endl; } else cout << "NIE" << endl; return 0; } |