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

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k, w;
    cin >> n >> k >> w;
    vector<int> p(n);
    for (int i = 0; i < n; ++i)
        p[i] = i;
    int odp = 0;
    do
    {
        int il = 0;
        vector<int> t = p;
        bool przed = false;
        while (t.size() > 1)
        {
            vector<int> nast;
            for (int i = 0; i < t.size(); ++i)
                if (!((i - 1 >= 0 && t[i - 1] > t[i]) || (i + 1 < t.size() && t[i + 1] > t[i])))
                    nast.push_back(t[i]);
            t = nast;
            ++il;
            if (il == k && t.size() != 1)
            {
                przed = true;
                break;
            }
        }
        if (il == k && !przed)
            ++odp;
    } while (next_permutation(p.begin(), p.end()));
    cout << odp;
    return 0;
}