#include <cstdio> #include <algorithm> #include <map> const int MAX = 250250; int n; long long k; int perm[MAX]; void brutal(long long k = -1) { for (int c = 0; c < n; c++) { perm[c] = c + 1; } std::map<int, int> kCnt; int invNum = n * (n - 1) / 2; do { int iCnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { iCnt += (perm[i] > perm[j]); } } //if (2 * iCnt == invNum) { // for (int c = 0; c < n; c++) { // printf("%3i", perm[c]); // } // printf("\n"); //} kCnt[iCnt]++; if (k > 0 && 2 * iCnt == invNum && kCnt[iCnt] == k) { return; } } while (std::next_permutation(perm, perm + n)); //for (auto iter = kCnt.begin(); iter != kCnt.end(); iter++) { // printf("%2i -> %2i\n", iter->first, iter->second); //} } std::map<std::pair<int, long long>, long long> cNum; long long num(int n, long long k) { if (k == 0) { return 1; } if (n == 1 && k == 1) { return 0; } if (k < 0 || k > (n*(n - 1) / 2)) { return 0; } std::pair<int, long long> key(n, k); if (cNum.count(key) == 0) { cNum[key] = num(n - 1, k) + num(n, k - 1) - num(n - 1, k - n); } return cNum[key]; } int main(int argc, char *argv[]) { scanf("%i%lli", &n, &k); int invNum = n * (n - 1) / 2; if ((invNum & 1) || num(n, invNum / 2) < k) { printf("NIE\n"); } else { printf("TAK\n"); brutal(k); for (int c = 0; c < n; c++) { printf("%i ", perm[c]); } printf("\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 70 71 72 73 74 75 76 77 78 79 80 | #include <cstdio> #include <algorithm> #include <map> const int MAX = 250250; int n; long long k; int perm[MAX]; void brutal(long long k = -1) { for (int c = 0; c < n; c++) { perm[c] = c + 1; } std::map<int, int> kCnt; int invNum = n * (n - 1) / 2; do { int iCnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { iCnt += (perm[i] > perm[j]); } } //if (2 * iCnt == invNum) { // for (int c = 0; c < n; c++) { // printf("%3i", perm[c]); // } // printf("\n"); //} kCnt[iCnt]++; if (k > 0 && 2 * iCnt == invNum && kCnt[iCnt] == k) { return; } } while (std::next_permutation(perm, perm + n)); //for (auto iter = kCnt.begin(); iter != kCnt.end(); iter++) { // printf("%2i -> %2i\n", iter->first, iter->second); //} } std::map<std::pair<int, long long>, long long> cNum; long long num(int n, long long k) { if (k == 0) { return 1; } if (n == 1 && k == 1) { return 0; } if (k < 0 || k > (n*(n - 1) / 2)) { return 0; } std::pair<int, long long> key(n, k); if (cNum.count(key) == 0) { cNum[key] = num(n - 1, k) + num(n, k - 1) - num(n - 1, k - n); } return cNum[key]; } int main(int argc, char *argv[]) { scanf("%i%lli", &n, &k); int invNum = n * (n - 1) / 2; if ((invNum & 1) || num(n, invNum / 2) < k) { printf("NIE\n"); } else { printf("TAK\n"); brutal(k); for (int c = 0; c < n; c++) { printf("%i ", perm[c]); } printf("\n"); } return 0; } |