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
#include <cstdio>
#include <vector>
#include <map>
using namespace std;

// -----------------------------------------

#define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i))

// -----------------------------------------

int n, m, p;
int main() {
    scanf("%d %d %d", &n, &m, &p);
    map<pair<int, int>, int> X[2];

    int phase = 1;
    FOR(i1,0,m) FOR(i2,i1,m) X[phase][make_pair(i1, i2)] = 1;

    FOR(k,1,n) {
        int old_phase = phase;
        phase = 1 - phase;

        FOR(i1,0,m) FOR(i2,i1,m) X[phase][make_pair(i1, i2)] = 0;
        FOR(i1,0,m) FOR(i2,i1,m) {
            int old_res = X[old_phase][make_pair(i1, i2)];
            FOR(j1,0,i2+1) FOR(j2,max(i1,j1),m) X[phase][make_pair(j1, j2)] = (X[phase][make_pair(j1, j2)] + old_res) % p;
        }
    }

    int res = 0;
    FOR(i1,0,m) FOR(i2,i1,m) res = (res + X[phase][make_pair(i1, i2)]) % p;
    printf("%d\n", res);
}