#include <bits/stdc++.h> using namespace std; long long m; vector<int> P; long long liczba_inwersji() { long long l = 0; for (int i = 0; i < (int)P.size(); ++i) { for (int j = i + 1; j < (int)P.size(); ++j) { l += P[i] > P[j]; } } return l; } bool jest_stabilna() { return liczba_inwersji() == m; } int main() { int k, n; scanf("%d %d", &n, &k); m = (long long)(n - 1) * n / 2; if (m & 1) { puts("NIE"); return 0; } m /= 2; for (int i = 1; i <= n; ++i) { P.push_back(i); } int l = 0; do { if (!jest_stabilna()) { continue; } ++l; if (l < k) { continue; } puts("TAK"); for (int i = 0; i < (int)P.size(); ++i) { printf("%d ", P[i]); } puts(""); return 0; } while (next_permutation(P.begin(), P.end())); puts("NIE"); 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 | #include <bits/stdc++.h> using namespace std; long long m; vector<int> P; long long liczba_inwersji() { long long l = 0; for (int i = 0; i < (int)P.size(); ++i) { for (int j = i + 1; j < (int)P.size(); ++j) { l += P[i] > P[j]; } } return l; } bool jest_stabilna() { return liczba_inwersji() == m; } int main() { int k, n; scanf("%d %d", &n, &k); m = (long long)(n - 1) * n / 2; if (m & 1) { puts("NIE"); return 0; } m /= 2; for (int i = 1; i <= n; ++i) { P.push_back(i); } int l = 0; do { if (!jest_stabilna()) { continue; } ++l; if (l < k) { continue; } puts("TAK"); for (int i = 0; i < (int)P.size(); ++i) { printf("%d ", P[i]); } puts(""); return 0; } while (next_permutation(P.begin(), P.end())); puts("NIE"); return 0; } |