#include <iostream> #include <vector> #include <algorithm> #include <set> #include <iterator> using namespace std; using int64 = long long; int64 numberOfPairs(int64 n) { return (n-1)*n/2; } bool isNO(int64 pairs) { return pairs%2 != 0; } bool checkIfCorrectPairs(const std::vector<int64>& vec, int64 reqPairs) { int64 found = 0; std::set<int64> v; for (int64 i = 0;i<vec.size();i++){ found += std::distance(v.insert(vec[i]).first, v.end())-1; } return found == reqPairs; } std::vector<int64> solution(int64 N, int64 K, int64 reqPairs) { std::vector<int64> vec(N); int n=0; int p=1; std::generate(vec.begin(), vec.end(), [&p] () { return p++; }); do { if (checkIfCorrectPairs(vec, reqPairs)) { n++; } if (n == K) return vec; } while(std::next_permutation(vec.begin(), vec.end())); return {}; } int main() { int64 N = 0; int64 K = 0 ; scanf("%lld", &N); scanf("%lld", &K); if (isNO(numberOfPairs(N))){ cout << "NIE" << endl; return 1; } int64 requiredPairs = numberOfPairs(N)/2; auto solutionRes = solution(N,K,requiredPairs); if (solutionRes.empty()){ cout << "NIE" << endl; return 1; } cout << "TAK" << endl; for (auto p : solutionRes) { cout << p << " "; } cout << 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <iterator> using namespace std; using int64 = long long; int64 numberOfPairs(int64 n) { return (n-1)*n/2; } bool isNO(int64 pairs) { return pairs%2 != 0; } bool checkIfCorrectPairs(const std::vector<int64>& vec, int64 reqPairs) { int64 found = 0; std::set<int64> v; for (int64 i = 0;i<vec.size();i++){ found += std::distance(v.insert(vec[i]).first, v.end())-1; } return found == reqPairs; } std::vector<int64> solution(int64 N, int64 K, int64 reqPairs) { std::vector<int64> vec(N); int n=0; int p=1; std::generate(vec.begin(), vec.end(), [&p] () { return p++; }); do { if (checkIfCorrectPairs(vec, reqPairs)) { n++; } if (n == K) return vec; } while(std::next_permutation(vec.begin(), vec.end())); return {}; } int main() { int64 N = 0; int64 K = 0 ; scanf("%lld", &N); scanf("%lld", &K); if (isNO(numberOfPairs(N))){ cout << "NIE" << endl; return 1; } int64 requiredPairs = numberOfPairs(N)/2; auto solutionRes = solution(N,K,requiredPairs); if (solutionRes.empty()){ cout << "NIE" << endl; return 1; } cout << "TAK" << endl; for (auto p : solutionRes) { cout << p << " "; } cout << endl; return 0; } |