#include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; int count_inversions(vector<int>& nums) { int n = nums.size(); int result = 0; FOR(x, 0, n) { FOR(y, x + 1, n) { if (nums[x] > nums[y]) { result++; } } } return result; } int count_inversions_inverted(vector<int>& nums) { int n = nums.size(); int result = 0; for (int x = n - 1; x >= 0; --x) { for (int y = x - 1; y >= 0; --y) { if (nums[x] > nums[y]) { result++; } } } return result; } #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<int> nums(n); REP(x, n) { nums[x] = x + 1; } int k = n * (n - 1) / 2; if (k % 2 != 0) { cout << "NIE\n"; return 0; } k /= 2; do { if (count_inversions(nums) == k) { if (--m == 0) { cout << "TAK\n"; for (auto it : nums) cout << it << " "; cout << endl; return 0; } } } while (next_permutation(nums.begin(), nums.end())); cout << "NIE\n"; return 0; } #endif
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 | #include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; int count_inversions(vector<int>& nums) { int n = nums.size(); int result = 0; FOR(x, 0, n) { FOR(y, x + 1, n) { if (nums[x] > nums[y]) { result++; } } } return result; } int count_inversions_inverted(vector<int>& nums) { int n = nums.size(); int result = 0; for (int x = n - 1; x >= 0; --x) { for (int y = x - 1; y >= 0; --y) { if (nums[x] > nums[y]) { result++; } } } return result; } #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<int> nums(n); REP(x, n) { nums[x] = x + 1; } int k = n * (n - 1) / 2; if (k % 2 != 0) { cout << "NIE\n"; return 0; } k /= 2; do { if (count_inversions(nums) == k) { if (--m == 0) { cout << "TAK\n"; for (auto it : nums) cout << it << " "; cout << endl; return 0; } } } while (next_permutation(nums.begin(), nums.end())); cout << "NIE\n"; return 0; } #endif |