#include <iostream> #include <vector> using namespace std; typedef long long ll; int m, n; ll p; ll mod(ll v) { return ((v % p) + p) % p; } int brute(int a, int b, int l) { if (l == 0) return 1; int acc = 0; for (int x = 1;x <= b;x++) { for (int y = max(a, x);y <= m;y++) { acc += brute(x, y, l - 1); } } return acc; } ll** arr; ll* arr_ex; void metrix_update() { arr_ex[0] = arr[0][0]; //cout << arr_ex[0] << " "; for (int j = 1;j < m;j++) { ll a = 0; for (int i = 0;i <= j;i++) a += arr[i][j]; arr_ex[j] = mod(a + arr_ex[j - 1]); // cout << arr_ex[j] << " "; } //cout << "\n"; } void printAcc(int** a) { for (int i = 0;i < m;i++) { for (int j = 0;j < m;j++) cout << a[i][j] << " "; cout << "\n"; } } ll get_ex(int i) { if (i <= 0) return 0; return arr_ex[i-1]; } void addOne() { metrix_update(); for (int i = 0;i < m;i++) for (int j = i;j < m;j++) { arr[i][j] = mod(arr_ex[m - 1] - get_ex(i) - get_ex(m - j - 1)); } } ll sol() { arr = new ll * [m]; arr_ex = new ll[m]; for (int i = 0;i < m;i++) { arr[i] = new ll[m] {0}; } for (int i = 0;i < m;i++) for (int j = i;j < m;j++) { arr[i][j] = 1; } //printAcc(arr); for (int k = 1;k < n;k++) addOne(); metrix_update(); //printAcc(arr); return arr_ex[m-1]; } int main() { cin >> n >> m >> p; //cout << brute(1, m, n) << "\n"; cout << sol(); }
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 | #include <iostream> #include <vector> using namespace std; typedef long long ll; int m, n; ll p; ll mod(ll v) { return ((v % p) + p) % p; } int brute(int a, int b, int l) { if (l == 0) return 1; int acc = 0; for (int x = 1;x <= b;x++) { for (int y = max(a, x);y <= m;y++) { acc += brute(x, y, l - 1); } } return acc; } ll** arr; ll* arr_ex; void metrix_update() { arr_ex[0] = arr[0][0]; //cout << arr_ex[0] << " "; for (int j = 1;j < m;j++) { ll a = 0; for (int i = 0;i <= j;i++) a += arr[i][j]; arr_ex[j] = mod(a + arr_ex[j - 1]); // cout << arr_ex[j] << " "; } //cout << "\n"; } void printAcc(int** a) { for (int i = 0;i < m;i++) { for (int j = 0;j < m;j++) cout << a[i][j] << " "; cout << "\n"; } } ll get_ex(int i) { if (i <= 0) return 0; return arr_ex[i-1]; } void addOne() { metrix_update(); for (int i = 0;i < m;i++) for (int j = i;j < m;j++) { arr[i][j] = mod(arr_ex[m - 1] - get_ex(i) - get_ex(m - j - 1)); } } ll sol() { arr = new ll * [m]; arr_ex = new ll[m]; for (int i = 0;i < m;i++) { arr[i] = new ll[m] {0}; } for (int i = 0;i < m;i++) for (int j = i;j < m;j++) { arr[i][j] = 1; } //printAcc(arr); for (int k = 1;k < n;k++) addOne(); metrix_update(); //printAcc(arr); return arr_ex[m-1]; } int main() { cin >> n >> m >> p; //cout << brute(1, m, n) << "\n"; cout << sol(); } |