#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) int N, K, p; int binom_tab[1009][1009]; int alpha_1_tab[1009]; const int K_ZERO=11; int beta_tab[1009][K_ZERO]; int gamma_tab[1009][K_ZERO]; int binom(int n, int k) { return binom_tab[n][k]; } int beta(int n, int k) { return beta_tab[n][k]; } int gamma(int n, int k) { return gamma_tab[n][k]; } void fill_binom_tab(int N) { binom_tab[0][0] = 1; FOR(k,1,K) binom_tab[0][k] = 0; FOR(n,1,N) binom_tab[n][0] = 1; FOR(n, 1, N) FOR(k, 1, n) binom_tab[n][k] = (binom_tab[n-1][k-1] + binom_tab[n-1][k]) %p; } void fill_alpha_1_tab(int n) { int curr = 1; alpha_1_tab[0] = 1; FOR(nn, 1, n) { alpha_1_tab[nn] = curr; curr *= 2; curr = curr % p; } } int alpha(int n, int k) { if ((n==0) || (n==1)) return 0; if (k==1) return alpha_1_tab[n]; if (k>=K_ZERO) return 0; LL sum = 0; REP(i, n) { sum += ((((LL) binom(n-1, i) * beta(i, k)) % p) * beta(n-i-1, k)) % p; } return sum % p; } void fill_beta_tab(int N, int K) { FOR(k, 1, K) beta_tab[0][k] = 1; FOR(k, 1, K) beta_tab[1][k] = 1; FOR(n, 2, N) beta_tab[n][1] = 1; FOR(n, 2, N) { FOR(k, 2, K) { LL sum = 0; REP(i,n) { sum += ((((LL) binom(n-1,i) * beta(i, k)) % p) * gamma(n-i-1, k-1)) % p; } beta_tab[n][k] = sum % p; } } } void fill_gamma_tab(int N, int K) { FOR(k, 1, K) gamma_tab[0][k] = 1; FOR(k, 1, K) gamma_tab[1][k] = 1; FOR(n, 2, N) gamma_tab[n][1] = alpha(n, 1); FOR(n, 2, N) { FOR(k, 2, K) { LL sum = (LL) gamma(n-1,k) * 2; FOR(i,1,n-2) { LL gammas_sum = 0; gammas_sum += ((LL) gamma(i, k-1) * gamma(n-i-1, k-1)) % p; gammas_sum += ((((LL) gamma(i, k) + p - gamma(i, k-1)) % p) * gamma(n-i-1, k-1)) % p; gammas_sum += ((LL) gamma(i, k-1) * ((gamma(n-i-1, k) + p - gamma(n-i-1, k-1) % p))) % p; gammas_sum = gammas_sum % p; sum += ((LL) binom(n-1,i) * gammas_sum) % p; } gamma_tab[n][k] = sum % p; } } } int proper_alpha(int n, int k) { if ((n == 1) || (k == 1)) return alpha(n,k); return (alpha(n,k) + (p - alpha(n, k-1))) % p; } int main() { scanf("%d %d %d", &N, &K, &p); if (K >= K_ZERO) { printf("0\n"); return 0; } fill_binom_tab(N); fill_alpha_1_tab(N); fill_gamma_tab(N, K); fill_beta_tab(N, K); printf("%d\n", proper_alpha(N, K)); 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 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) int N, K, p; int binom_tab[1009][1009]; int alpha_1_tab[1009]; const int K_ZERO=11; int beta_tab[1009][K_ZERO]; int gamma_tab[1009][K_ZERO]; int binom(int n, int k) { return binom_tab[n][k]; } int beta(int n, int k) { return beta_tab[n][k]; } int gamma(int n, int k) { return gamma_tab[n][k]; } void fill_binom_tab(int N) { binom_tab[0][0] = 1; FOR(k,1,K) binom_tab[0][k] = 0; FOR(n,1,N) binom_tab[n][0] = 1; FOR(n, 1, N) FOR(k, 1, n) binom_tab[n][k] = (binom_tab[n-1][k-1] + binom_tab[n-1][k]) %p; } void fill_alpha_1_tab(int n) { int curr = 1; alpha_1_tab[0] = 1; FOR(nn, 1, n) { alpha_1_tab[nn] = curr; curr *= 2; curr = curr % p; } } int alpha(int n, int k) { if ((n==0) || (n==1)) return 0; if (k==1) return alpha_1_tab[n]; if (k>=K_ZERO) return 0; LL sum = 0; REP(i, n) { sum += ((((LL) binom(n-1, i) * beta(i, k)) % p) * beta(n-i-1, k)) % p; } return sum % p; } void fill_beta_tab(int N, int K) { FOR(k, 1, K) beta_tab[0][k] = 1; FOR(k, 1, K) beta_tab[1][k] = 1; FOR(n, 2, N) beta_tab[n][1] = 1; FOR(n, 2, N) { FOR(k, 2, K) { LL sum = 0; REP(i,n) { sum += ((((LL) binom(n-1,i) * beta(i, k)) % p) * gamma(n-i-1, k-1)) % p; } beta_tab[n][k] = sum % p; } } } void fill_gamma_tab(int N, int K) { FOR(k, 1, K) gamma_tab[0][k] = 1; FOR(k, 1, K) gamma_tab[1][k] = 1; FOR(n, 2, N) gamma_tab[n][1] = alpha(n, 1); FOR(n, 2, N) { FOR(k, 2, K) { LL sum = (LL) gamma(n-1,k) * 2; FOR(i,1,n-2) { LL gammas_sum = 0; gammas_sum += ((LL) gamma(i, k-1) * gamma(n-i-1, k-1)) % p; gammas_sum += ((((LL) gamma(i, k) + p - gamma(i, k-1)) % p) * gamma(n-i-1, k-1)) % p; gammas_sum += ((LL) gamma(i, k-1) * ((gamma(n-i-1, k) + p - gamma(n-i-1, k-1) % p))) % p; gammas_sum = gammas_sum % p; sum += ((LL) binom(n-1,i) * gammas_sum) % p; } gamma_tab[n][k] = sum % p; } } } int proper_alpha(int n, int k) { if ((n == 1) || (k == 1)) return alpha(n,k); return (alpha(n,k) + (p - alpha(n, k-1))) % p; } int main() { scanf("%d %d %d", &N, &K, &p); if (K >= K_ZERO) { printf("0\n"); return 0; } fill_binom_tab(N); fill_alpha_1_tab(N); fill_gamma_tab(N, K); fill_beta_tab(N, K); printf("%d\n", proper_alpha(N, K)); return 0; } |