#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second /* 1 0 1 2 1 2 3 1 4 2 2 4 1 8 2 16 5 1 16 2 100 3 4 6 1 32 2 616 3 72 7 1 64 2 4024 3 952 8 1 128 2 28512 3 11680 9 1 256 2 219664 3 142800 4 160 10 1 512 2 1831712 3 1788896 4 7680 11 1 1024 2 16429152 3 23252832 4 233792 12 1 2048 2 157552000 3 315549312 4 5898240 */ const int N = 1010; const int K = 13; int n,k; ll p; ll dp[K][N][2]; ll c[N][N]; int main() { scanf("%d%d%lld", &n, &k, &p); if (k >= K) { printf("0\n"); return 0; } for (int i = 0; i <= n; i++) { c[i][i] = c[i][0] = 1; for (int j = 1; j < i; j++) { c[i][j] = c[i-1][j] + c[i-1][j-1]; if (c[i][j] >= p) c[i][j] -= p; } } for (int i = 0; i <= k; i++) dp[i][0][0] = dp[i][0][1] = 1; for (int m = 1; m <= k; m++) { // <=m krokow // ograniczenie z jednej strony for (int l = 1; l <= n; l++) { // przedzial dlugosci l for (int i = 0; i < l; i++) { // najwyzszy na pozycji i (0-based) int lsize = i, rsize = l-i-1; int mul = c[l-1][lsize]; //printf("%d %d 0 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m-1][lsize][1], dp[m][rsize][0]); dp[m][l][0] = (1LL * mul * dp[m-1][lsize][1] % p * dp[m][rsize][0] + dp[m][l][0]) % p; } //printf("%d %d 0 = %d\n", m, l, dp[m][l][0]); } // ograniczenie z obu stron for (int l = 1; l <= n; l++) { // przedzial dlugosci l for (int i = 0; i < l; i++) { // najwyzszy na pozycji i (0-based) int lsize = i, rsize = l-i-1; int mul = c[l-1][lsize]; dp[m][l][1] = (1LL * mul * dp[m-1][lsize][1] % p * dp[m][rsize][1] + dp[m][l][1]) % p; dp[m][l][1] = (1LL * mul * dp[m][lsize][1] % p * dp[m-1][rsize][1] + dp[m][l][1]) % p; dp[m][l][1] = (dp[m][l][1] - 1LL * mul * dp[m-1][lsize][1] % p * dp[m-1][rsize][1] % p + p) % p; //dp[m][l][1] = dp[m][l][1] - 1LL * mul * dp[m-1][lsize][1] % p * dp[m-1][rsize][1] % p; //printf("%d %d 1 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m-1][lsize][1], dp[m][rsize][1]); //printf("%d %d 1 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m][lsize][1], dp[m-1][rsize][1]); } //printf("%d %d 1 = %d\n", m, l, dp[m][l][1]); } } /*for (int q = 0; q < 2; q++) { for (int m = 1; m <= k; m++) { for (int i = 1; i <= n; i++) { printf("%lld\t", dp[m][i][q]-dp[m-1][i][q]); } printf("\n"); } printf("\n"); }*/ ll res = 0; for (int i = 0; i < n; i++) { int lsize = i, rsize = n-i-1; int mul = c[n-1][lsize]; res = (1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]+p) % p * dp[k][rsize][0] + res) % p; res = (1LL * mul * dp[k][lsize][0] % p * (dp[k][rsize][0]-dp[k-1][rsize][0]+p) + res) % p; res = (p - 1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]+p) % p * (dp[k][rsize][0]-dp[k-1][rsize][0]+p) % p + res) % p; //res = (1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]) % p * dp[k][rsize][0] + res) % p; //res = (1LL * mul * dp[k][lsize][0] % p * (dp[k][rsize][0]-dp[k-1][rsize][0]) + res) % p; //res = (-1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]) % p * (dp[k][rsize][0]-dp[k-1][rsize][0]) + res) % p; } printf("%lld\n", res); 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 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second /* 1 0 1 2 1 2 3 1 4 2 2 4 1 8 2 16 5 1 16 2 100 3 4 6 1 32 2 616 3 72 7 1 64 2 4024 3 952 8 1 128 2 28512 3 11680 9 1 256 2 219664 3 142800 4 160 10 1 512 2 1831712 3 1788896 4 7680 11 1 1024 2 16429152 3 23252832 4 233792 12 1 2048 2 157552000 3 315549312 4 5898240 */ const int N = 1010; const int K = 13; int n,k; ll p; ll dp[K][N][2]; ll c[N][N]; int main() { scanf("%d%d%lld", &n, &k, &p); if (k >= K) { printf("0\n"); return 0; } for (int i = 0; i <= n; i++) { c[i][i] = c[i][0] = 1; for (int j = 1; j < i; j++) { c[i][j] = c[i-1][j] + c[i-1][j-1]; if (c[i][j] >= p) c[i][j] -= p; } } for (int i = 0; i <= k; i++) dp[i][0][0] = dp[i][0][1] = 1; for (int m = 1; m <= k; m++) { // <=m krokow // ograniczenie z jednej strony for (int l = 1; l <= n; l++) { // przedzial dlugosci l for (int i = 0; i < l; i++) { // najwyzszy na pozycji i (0-based) int lsize = i, rsize = l-i-1; int mul = c[l-1][lsize]; //printf("%d %d 0 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m-1][lsize][1], dp[m][rsize][0]); dp[m][l][0] = (1LL * mul * dp[m-1][lsize][1] % p * dp[m][rsize][0] + dp[m][l][0]) % p; } //printf("%d %d 0 = %d\n", m, l, dp[m][l][0]); } // ograniczenie z obu stron for (int l = 1; l <= n; l++) { // przedzial dlugosci l for (int i = 0; i < l; i++) { // najwyzszy na pozycji i (0-based) int lsize = i, rsize = l-i-1; int mul = c[l-1][lsize]; dp[m][l][1] = (1LL * mul * dp[m-1][lsize][1] % p * dp[m][rsize][1] + dp[m][l][1]) % p; dp[m][l][1] = (1LL * mul * dp[m][lsize][1] % p * dp[m-1][rsize][1] + dp[m][l][1]) % p; dp[m][l][1] = (dp[m][l][1] - 1LL * mul * dp[m-1][lsize][1] % p * dp[m-1][rsize][1] % p + p) % p; //dp[m][l][1] = dp[m][l][1] - 1LL * mul * dp[m-1][lsize][1] % p * dp[m-1][rsize][1] % p; //printf("%d %d 1 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m-1][lsize][1], dp[m][rsize][1]); //printf("%d %d 1 moze przyjsc z %d --> %d*%d*%d\n", m, l, i, mul, dp[m][lsize][1], dp[m-1][rsize][1]); } //printf("%d %d 1 = %d\n", m, l, dp[m][l][1]); } } /*for (int q = 0; q < 2; q++) { for (int m = 1; m <= k; m++) { for (int i = 1; i <= n; i++) { printf("%lld\t", dp[m][i][q]-dp[m-1][i][q]); } printf("\n"); } printf("\n"); }*/ ll res = 0; for (int i = 0; i < n; i++) { int lsize = i, rsize = n-i-1; int mul = c[n-1][lsize]; res = (1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]+p) % p * dp[k][rsize][0] + res) % p; res = (1LL * mul * dp[k][lsize][0] % p * (dp[k][rsize][0]-dp[k-1][rsize][0]+p) + res) % p; res = (p - 1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]+p) % p * (dp[k][rsize][0]-dp[k-1][rsize][0]+p) % p + res) % p; //res = (1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]) % p * dp[k][rsize][0] + res) % p; //res = (1LL * mul * dp[k][lsize][0] % p * (dp[k][rsize][0]-dp[k-1][rsize][0]) + res) % p; //res = (-1LL * mul * (dp[k][lsize][0]-dp[k-1][lsize][0]) % p * (dp[k][rsize][0]-dp[k-1][rsize][0]) + res) % p; } printf("%lld\n", res); return 0; } |