#include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<ll> #define pi pair<int, int> #define MAXN 100001 using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, p; cin >> n >> m >> p; ll M = 0; map<pi, ll> segment_to_id; for (int i = 0; i < m; i++) for (int j = i; j < m; j++) { segment_to_id[make_pair(i, j)] = M; M++; } vector<vi> V(M); // cout << M << endl; for (int i = 0; i < m; i++) { for (int j = i; j < m; j++) { for (int p = 0; p < m; p++) { for (int q = p; q < m; q++) { if ((j >= p && p >= i) || (q >= i && i >= p)) { ll idij = segment_to_id[make_pair(i, j)]; ll idpq = segment_to_id[make_pair(p, q)]; V[idij].pb(idpq); } } } } } vi C(M, 1); vi Cnext(M); for (int i = 1; i < n; i++) { fill(Cnext.begin(), Cnext.end(), 0); for (ll j = 0; j < M; j++) { for (ll k : V[j]) { Cnext[j] += C[k]; Cnext[j] %= p; } } swap(C, Cnext); } ll sum = 0; for (auto& n : C) { sum += n; sum %= p; } cout << sum << endl; return 0; }
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 <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<ll> #define pi pair<int, int> #define MAXN 100001 using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, p; cin >> n >> m >> p; ll M = 0; map<pi, ll> segment_to_id; for (int i = 0; i < m; i++) for (int j = i; j < m; j++) { segment_to_id[make_pair(i, j)] = M; M++; } vector<vi> V(M); // cout << M << endl; for (int i = 0; i < m; i++) { for (int j = i; j < m; j++) { for (int p = 0; p < m; p++) { for (int q = p; q < m; q++) { if ((j >= p && p >= i) || (q >= i && i >= p)) { ll idij = segment_to_id[make_pair(i, j)]; ll idpq = segment_to_id[make_pair(p, q)]; V[idij].pb(idpq); } } } } } vi C(M, 1); vi Cnext(M); for (int i = 1; i < n; i++) { fill(Cnext.begin(), Cnext.end(), 0); for (ll j = 0; j < M; j++) { for (ll k : V[j]) { Cnext[j] += C[k]; Cnext[j] %= p; } } swap(C, Cnext); } ll sum = 0; for (auto& n : C) { sum += n; sum %= p; } cout << sum << endl; return 0; } |