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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n,m,p;
    cin >> n >> m >> p;

    vector<vector<long long>>Current(m, vector<long long>(m));
    vector<vector<long long>>Previous(m, vector<long long>(m));
    Current[0][m-1]=1;

    for(int r=0;r<n;r++){
        Current.swap(Previous);

        for(int i=0;i<m;i++)
            for(int j=i;j<m;j++){
                Current[i][j]=0;
                for(int k=0;k<m;k++)
                    for(int l=k;l<m;l++)
                        if(!(l<i || k>j))
                            Current[i][j] = (Current[i][j]+Previous[k][l])%p;
            }
    }

    long long result=0;
    for(int i=0;i<m;i++)
        for(int j=i;j<m;j++)
            result = (result+Current[i][j])%p;
    cout << result << "\n";

    return 0;
}