#include <iostream>
#include <algorithm>
bool is_stable(const std::vector<int> &v, long long combinations)
{
long long invertions = 0;
for (size_t i = 0; i < v.size() - 1; ++i) {
for (size_t j = i + 1; j < v.size(); ++j) {
if (v[i] > v[j]) {
invertions++;
}
}
}
if (invertions == combinations) {
return true;
}
return false;
}
void print(const std::vector<int> &v)
{
for (const auto &i : v)
printf("%d ", i);
printf("\n");
}
int main()
{
std::ios_base::sync_with_stdio(0);
//
size_t N = 0;
long long K = 0;
std::cin >> N;
std::cin >> K;
//
std::vector<int> vec(N);
for (size_t i = 0; i < N; ++i) {
vec[i] = int(i + 1);
}
//
long long count = 0;
long long combinations = (N * (N - 1)) / 2;
if (N == 1 || combinations % 2 != 0) {
printf("NIE\n");
return 0;
}
combinations >>= 1;
do {
if (is_stable(vec, combinations)) { count++; }
if (count == K) { printf("TAK\n"); print(vec); return 0; }
} while (std::next_permutation(vec.begin(), vec.end()));
printf("NIE\n");
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 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <algorithm> bool is_stable(const std::vector<int> &v, long long combinations) { long long invertions = 0; for (size_t i = 0; i < v.size() - 1; ++i) { for (size_t j = i + 1; j < v.size(); ++j) { if (v[i] > v[j]) { invertions++; } } } if (invertions == combinations) { return true; } return false; } void print(const std::vector<int> &v) { for (const auto &i : v) printf("%d ", i); printf("\n"); } int main() { std::ios_base::sync_with_stdio(0); // size_t N = 0; long long K = 0; std::cin >> N; std::cin >> K; // std::vector<int> vec(N); for (size_t i = 0; i < N; ++i) { vec[i] = int(i + 1); } // long long count = 0; long long combinations = (N * (N - 1)) / 2; if (N == 1 || combinations % 2 != 0) { printf("NIE\n"); return 0; } combinations >>= 1; do { if (is_stable(vec, combinations)) { count++; } if (count == K) { printf("TAK\n"); print(vec); return 0; } } while (std::next_permutation(vec.begin(), vec.end())); printf("NIE\n"); return 0; } |
English