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
81
#include <iostream>
#include <vector>
#include <cmath>

template <typename T1>
T1 strong(T1 x)
{
    if(x < 1)
        return 1;
    else
        return strong<T1>(x - 1) * x;
}

struct Element
{
    std::vector<unsigned> table;
    unsigned inversion;
    Element(std::vector<unsigned> _table)
    {
        this->table = _table;
        this->inversion = 0;
        for(unsigned i = 0; i<_table.size(); ++i)
            for(unsigned c = i + 1; c<_table.size(); ++c)
                if(table[i] > table[c])
                    this->inversion++;
    }
};

template <typename t1>
inline void swap(t1 &a, t1 &b)
{
    t1 c = a;
    a = b;
    b = c;
}

void permutation(std::vector<std::vector<unsigned>> &x, std::vector<unsigned> table, unsigned n)
{
  if(n < table.size() - 1)
  {
    for(unsigned i = n; i<table.size(); i++)
    {
      swap<unsigned>(table[n], table[i]);
      permutation(x, table, n+1);
      swap<unsigned>(table[n], table[i]);
    }
  }
  else
    x.push_back(table);
}

int main()
{
    unsigned n, k;
    std::cin>>n>>k;
    if((1u <= n <= 250000u) && (1u <= k <= pow(10.0, 18.0)))
    {
        std::vector<std::vector<unsigned>> table;
        std::vector<unsigned> _table;
        for(unsigned i = 1; i - 1<n; ++i)
            _table.push_back(i);
        permutation(table, _table, 0);
        std::vector<Element> etable;
        for(unsigned i = 0; i<table.size(); ++i)
            etable.push_back(Element(table[i]));
        std::vector<std::vector<unsigned>> stable;
        for(unsigned i = 0; i<table.size(); ++i)
            if(etable[i].inversion == k)
                stable.push_back(etable[i].table);
        if(stable.size() >= k)
        {
            std::cout<<"TAK"<<std::endl;
            for(unsigned i = 0; i < n - 1; ++i)
                std::cout<<stable[k - 1][i]<<" ";
            std::cout<<stable[k - 1][n - 1];
        }
        else
            std::cout<<"NIE";
    }
    return 0;
}