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

using namespace std;

int main()
{
	int n,m;
	long long p;
	scanf("%d%d%lld",&m,&n,&p);
	long long sumpref[n+2][n+2];
	long long wyn[n+2][n+2];
	for (int i=1; i<=n; i++)
	{
		sumpref[i][0]=0LL;
		for (int j=1; j<=n; j++)
		{
			sumpref[i][j]=sumpref[i][j-1]+1LL;
		}
	}
	
	for (int i=2; i<=m; i++)
	{
		for (int dl=1; dl<=n; dl++) //dl aktualnego
		{
			wyn[dl][0]=0LL;
			for (int j=1; j<=n-dl+1; j++) //start aktualnego
			{
				wyn[dl][j]=wyn[dl][j-1];
				for (int k=1; k<=n; k++)  //dl wczesniejszego
				{
					//przedzial j..j+dl-1  sumpref[k]
					int pocz=j-k+1;
					int kon=j+dl-1;
					if (pocz<=0)
						pocz=1;
					if (kon+k>n+1)
						kon=n-k+1;
						
				//	cout<<k<<" "<<pocz<<" "<<kon<<" ";
				
					wyn[dl][j]+=(sumpref[k][kon]-sumpref[k][pocz-1]);
					wyn[dl][j]%=p;
					
					
				}
				
			}
		}
		for (int y=1; y<=n; y++)
		{
			for (int j=1; j<=n; j++)
			{
				sumpref[y][j]=wyn[y][j];
				wyn[y][j]=0LL;
			}
		}
		
	}
	long long S=0LL;
	for (int i=1; i<=n; i++)
		S=(S+sumpref[i][n-i+1])%p;
		
		
	printf("%lld", S%p);

	
	return 0;
}