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
#include <cstdio>
#include <algorithm>

int n, k, p;
int* buf;

int stime(int* perm) {
	int ret = 0;
	int len = n;
	for (int i = 0; i < n; ++i) {
		buf[i] = perm[i];
	}
	while (len > 1) {
		int nlen = 0;
		for (int i = 0; i < len; ++i) {
			if (((i == 0) || (buf[i - 1] < buf[i])) && ((i == len - 1) || (buf[i + 1] < buf[i]))) {
				buf[nlen] = buf[i];
				++nlen;
			}
		}
		len = nlen;
		++ret;
	}
	return ret;
}

int main () {
	scanf("%d %d %d", &n, &k, &p);
	if (k == 1) {
		int x = 1;
		for (int i = 1; i < n; ++i) {
			x = (x << 1) % p;
		}
		printf("%d\n", x);
		return 0;
	}
	int kceil = 1;
	int pwr = 0;
	while (kceil < n) {
		kceil <<= 1;
		++pwr;
	}
	if (k > pwr) {
		printf("0\n");
		return 0;
	}
	buf = (int*)malloc((n + 8) * sizeof(int));
	int* perm = (int*)malloc((n + 8) * sizeof(int));
	for (int i = 0; i < n; ++i) {
		perm[i] = i + 1;
	}
	int cnt = 0;
	do {
		if (stime(perm) == k) {
			cnt = (cnt + 1) % p;
		}
	} while (std::next_permutation(perm, perm + n));
	printf("%d\n", cnt);
	return 0;
}