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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#define REP(x, n)  for(int x = 0; x < n; x++)
#define FOR(x, b, e) for (int x = (b); x <= (e); x++)
#define LL long long
#define ULL unsigned long long
#define MP std::make_pair
#define ST first
#define ND second
#define MAX 1006
#define MAX_LOG 12

typedef std::pair<int, int> PII;
typedef std::vector<int> VI;
using namespace std;
LL ret[1006][MAX_LOG+1]={
	{0,0,0,0,0,0,0,0,0,0,0,0},
	{1,0,0,0,0,0,0,0,0,0,0,0},
	{0,2,0,0,0,0,0,0,0,0,0,0},
	{0,4,2,0,0,0,0,0,0,0,0,0},
	{0,8,16,0,0,0,0,0,0,0,0,0},
	{0,16,100,4,0,0,0,0,0,0,0,0},
	{0,32,688,0,0,0,0,0,0,0,0,0},
	{0,64,4024,952,0,0,0,0,0,0,0,0},
	{0,128,28512,11680,0,0,0,0,0,0,0,0},
	{0,256,219664,142800,160,0,0,0,0,0,0,0},
	{0,512,1831712,1788896,7680,0,0,0,0,0,0,0}

};


LL domo[MAX][MAX_LOG+1]= {
{0,0,0,0,0,0,0,0,0,0,0,0},
	{0,1,0,0,0,0,0,0,0,0,0,0},
	{0,2,0,0,0,0,0,0,0,0,0,0},
	{0,4,2,0,0,0,0,0,0,0,0,0},
	{0,8,16,0,0,0,0,0,0,0,0,0},
	{0,16,104,0,0,0,0,0,0,0,0,0},
	{0,32,616,72,0,0,0,0,0,0,0,0},
	{0,64,4896,80,0,0,0,0,0,0,0,0},
	{0,128,37792,2400,0,0,0,0,0,0,0,0},
	{0,256,315328,47296,0,0,0,0,0,0,0,0}
};// elementy które po k kolejkach rozpadają się przy założeniue że z obu stron jest większy element



LL doml[MAX][MAX_LOG+1]={
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,5,0,0,0,0,0,0,0,0,0},
{0,1,21,2,0,0,0,0,0,0,0,0},
{0,1,93,26,0,0,0,0,0,0,0,0},
{0,1,459,260,0,0,0,0,0,0,0,0},
{0,1,2531,2508,0,0,0,0,0,0,0,0},
{0,1,15419,24820,80,0,0,0,0,0,0,0},
{0,1,102619,257140,3120,0,0,0,0,0,0,0}

};// elementy które po k kolejkach rozpadają się przy założeniue że z lewej strony jest większy element
// int domp[MAX][MAX_LOG];// elementy które po k kolejkach rozpadają się przy założeniue że z prawej strony jest większy element
// int domz[MAX][MAX_LOG];

LL C(int n, int k) {
    if (n == k || k == 0) {
        return 1; // There's exactly one way to select n or 0 objects out of n
    }
    return C(n - 1, k - 1) * n / k;
}

LL minn(LL x){
	return x;

	LL i=1;
	if (x==0)
		return i;
}

int main(){
	std::ios_base::sync_with_stdio(false);

	// ret[1]={1,0,0,0,0,0,0,0,0,0,0,0};
	// ret[2]={0,2,0,0,0,0,0,0,0,0,0,0};
	// ret[3]={0,4,2,0,0,0,0,0,0,0,0,0};
	// ret[4]={0,8,16,0,0,0,0,0,0,0,0,0};
	// ret[5]={0,16,100,4,0,0,0,0,0,0,0,0};
	// ret[6]={0,32,688,0,0,0,0,0,0,0,0,0,0};
	// ret[7]={0,64,4024,952,0,0,0,0,0,0,0,0};
	// ret[8]={0,128,28512,11680,0,0,0,0,0,0,0,0};
	// ret[9]={0,256,219664,142800,160,0,0,0,0,0,0,0};
	// ret[9]={0,0,0,0,0,0,0,0,0,0,0,0};

	// domo[1]={0,1,0,0,0,0,0,0,0,0,0,0};
	// domo[2]={0,2,0,0,0,0,0,0,0,0,0,0};
	// domo[3]={0,4,2,0,0,0,0,0,0,0,0,0};
	// domo[4]={0,8,16,0,0,0,0,0,0,0,0,0};
	// domo[5]={0,16,104,0,0,0,0,0,0,0,0,0};
	// domo[6]={0,32,616,72,0,0,0,0,0,0,0,0,0};
	// domo[7]={0,64,4896,80,0,0,0,0,0,0,0,0};
	// domo[8]={0,128,37792,2400,0,0,0,0,0,0,0,0};
	// domo[9]={0,256,315328,47296,0,0,0,0,0,0,0,0};


	// doml[1]={0,1,0,0,0,0,0,0,0,0,0,0};
	// doml[2]={0,1,1,0,0,0,0,0,0,0,0,0};
	// doml[3]={0,1,5,0,0,0,0,0,0,0,0,0};
	// doml[4]={0,1,21,2,0,0,0,0,0,0,0,0};
	// doml[5]={0,1,93,26,0,0,0,0,0,0,0,0};
	// doml[6]={0,1,459,260,0,0,0,0,0,0,0,0};
	// doml[7]={0,1,2531,2508,0,0,0,0,0,0,0,0};
	// doml[8]={0,1,15419,24820,80,0,0,0,0,0,0,0};
	// doml[9]={0,1,102619,257140,3120,0,0,0,0,0,0,0};




	int n, k, p;
	cin>>n>>k>>p;
	if(n<11){
		cout<<(ret[n][k])% p <<endl;
		return 0;
	}
	for (int x = 10; x <= n; x++){
		for (int y = 1; y <= MAX_LOG; y++){
			LL r,o,l,p;
			r=0; o=0; l=0;
			for(int d=1; d <= x-1; d++){
				LL opcje_srodka = (domo[d-1][y-1]) * C(x-2, d-1);
				for (int i = 0; i <= x - d - 1; i++) // pozycja pierwszej z liczb
				{	
					LL lefto=0, righto=0,rightret=0, leftret = 0;
					//for ret
					if(i==0){
						leftret = 1;
					//	lefto = 1;
					}
					if(x-i-d-1==0){
					//	righto = 1;
						rightret = 1;
					}
					for(int j=1;j<y; j++){
						lefto += domo[i][j];
						leftret += doml[i][j];

						righto += domo[x-i-d-1][j]; 
						rightret += doml[x-i-d-1][j];
					}

					r += 2*minn(opcje_srodka) * minn(leftret) * minn(rightret) * C(x-d-1,i);
					o += 2*minn(opcje_srodka) * minn(lefto) * minn(righto) * C(x-d-1,i);
					l += 2*minn(opcje_srodka) * minn(leftret) * minn(righto) * C(x-d-1,i);
					cout<<d<<" " <<y <<" "<<i<<" "<< x-i-d-1<<" OS:" << opcje_srodka<<": "<< (domo[d-1][y-1])<< ", " << C(x-2, d-1) << "; "<<lefto<< " " <<righto<<" "<<C(x-d-1,i)<<endl;
				}

			}
			ret[x][y]=r;
			domo[x][y]=o;
			doml[x][y]=l;
		}
		cout<<"X: "<<x<<endl;
		for (int y = 1; y <= MAX_LOG; y++)
			cout<<ret[x][y]<<" ";
		cout<<endl;
		for (int y = 1; y <= MAX_LOG; y++)
			cout<<domo[x][y]<<" ";
		cout<<endl;
				for (int y = 1; y <= MAX_LOG; y++)
			cout<<doml[x][y]<<" ";
		cout<<endl;
	}



	return 0;
}