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

unsigned simulate(std::vector<unsigned> conf, int i) {
    for (; conf.size() != 1; --i) {
        if (i < 0) { return false; }

        std::vector<bool> keep(conf.size());
        keep[0] = conf[0] > conf[1];
        keep[conf.size() - 1] = conf[conf.size() - 1] > conf[conf.size() - 2];
        for (size_t j = 1; j < conf.size() - 1; ++j) {
            keep[j] = conf[j] > conf[j - 1]
                   && conf[j] > conf[j + 1];
        }

        for (size_t j = 0; j < conf.size(); ++j) {
            if (!keep[j]) {
                conf.erase(conf.begin() + j);
                keep.erase(keep.begin() + j);
                --j;
            }
        }
    }

    return i == 0;
}

int main() {
    unsigned N, K, P;
    std::cin >> N >> K >> P;

    std::vector<unsigned> conf(N);
    for (size_t i = 0; i < N; ++i) { conf[i] = i + 1; }

    unsigned long long ret = 0;
    do {
        if (simulate(conf, K)) { ++ret; }
    } while (std::next_permutation(conf.begin(), conf.end()));

    std::cout << ret % P << "\n";
}