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

using namespace std;

unsigned int n, m, p;

typedef struct {
        unsigned int a; // liczba kratek
        unsigned int b; // od ktorej kratki od gory
} kind;

vector<kind> v;
vector<vector<unsigned int>> sum_idx;

void fill_kind_vector(unsigned int k)
{
        kind x;
        unsigned int aa, bb;

        aa = k;
        bb = 1;

        while (aa > 0) {
                for(unsigned int i=0; i < bb; ++i) {
                        x.a = aa;
                        x.b = i;
                        v.push_back(x);
                }
                --aa;
                ++bb;
        }

}

void print_kind_vector()
{
        auto it = v.begin();

        while( it != v.end() ) {
                printf("%d %d\n", it->a, it->b);
                ++it;
        }
        printf("\n");
}

bool connected(const kind &x, const kind &y)
{
        if (x.b <= y.b) {
                return x.a + x.b - 1 >= y.b;
        } else {
                return y.a + y.b - 1 >= x.b;
        }
}

void fill_sum_idx()
{
        auto ita = v.begin();
        unsigned long long i = 0;
        unsigned long long j;

        sum_idx.resize( v.size() );
        while(ita != v.end()) {
                auto itb = v.begin();
                j = 0;
                while (itb != v.end()) {
                        if (!connected(*ita, *itb)) {
                                sum_idx[i].push_back(j);
                        }
                        ++itb;
                        ++j;
                }
                ++ita;
                ++i;
        }
}

void print_sum_idx()
{
        auto ita = sum_idx.begin();
        unsigned int a = 0;

        while( ita != sum_idx.end() ) {
                auto itb = ita->begin();

                printf("%d ->", a);
                while( itb != ita->end() ) {
                        printf(" %d", *itb);
                        ++itb;
                }
                printf("\n");
                ++a;
                ++ita;
        }
}

unsigned long long sum( const vector<unsigned long long> &v )
{
        unsigned long long result = 0;
        auto it = v.begin();

        while( it != v.end() ) {
                result = (result + *it) % p;
                ++it;
        }
        return result;
}

unsigned long long calc_result()
{
        vector<unsigned long long> a( v.size(), 1);
        vector<unsigned long long> b( v.size(), 1);

        auto pa = &a;
        auto pb = &b;
        auto pc = pa;

        for (unsigned int j = 1; j < n; ++j) {
                unsigned int i = 0;
                unsigned long long sumpa = sum( *pa );

                for(auto it = pb->begin(); it != pb->end(); ++it) {
                        unsigned long long tmp = sumpa;

        //                printf("i=%d\n", i);
                        for(auto it_idx = sum_idx[i].begin(); it_idx != sum_idx[i].end(); ++it_idx) {
                                if ((*pa)[*it_idx] > tmp) {
                                        tmp = tmp + p - (*pa)[*it_idx];
                                } else {
                                        tmp = tmp - (*pa)[*it_idx];
                                }
        //                        printf("idx = %d, tmp=%lld\n", *it_idx, tmp);
                        }
                        *it = tmp;
                        ++i;
                }
                pc = pb;
                pb = pa;
                pa = pc;
        }

        return sum( *pa );
}


int main()
{       
        scanf("%d %d %d\n", &n, &m, &p);

        fill_kind_vector(m);
//        print_kind_vector();

        fill_sum_idx();
//        print_sum_idx();

        printf("%lld\n", calc_result() );

        return 0;

}