#include <bits/stdc++.h> using namespace std; #define ff first #define dd second #define mp make_pair #define lld long long #define pb emplace_back #define sz size() #define For(i,s,a) for(lld i=(lld)s;i<(lld)a;++i) #define pii pair<int,int> #define pll pair<lld,lld> #define rpt(S,it) for(auto it=S.begin();it!=S.end();++it) #define mod (lld)(1e9+7) #define scanf(...) scanf(__VA_ARGS__)?:0 #define P first #define S second #ifdef __WIN32__ #define gcx getchar #elif __linux__ #define gcx getchar_unlocked #endif #define piii pair<pii,pii> lld dwu[1024][1024]; #define fmod(x,d) ((x>=d)?x%d:x) void cbin(lld P) { dwu[1][1]=1; dwu[1][0]=1; For(i,2,1001) For(j,0,i+1) { if(j==0 || j==i)dwu[i][j]=1; else dwu[i][j]=dwu[i-1][j-1]+dwu[i-1][j]; dwu[i][j]=fmod(dwu[i][j],P); } } lld P; lld dp[1024][1024][2]; bitset<1024>calc[1024][2]; void f(lld a, lld b, lld p, lld k) { if(b<1)return; if(a<b){calc[a][b][0]=calc[a][b][1]=0;return;} // cout<<a<<" "<<b<<" "<<p<<" "<<k<<endl; if(calc[a][b][p|k])return; calc[a][b][p|k]=1; if(p){ if(!calc[a-1][b-1][0|k])f(a-1,b-1,0,k); dp[a][b][p|k]+=dp[a-1][b-1][0|k]; // cout<<"wywalam początek z "<<a<<" "<<b<<" "<<k<<" "<<dp[a][b][p|k]<<" korzystając z "<<a-1<<" "<<b-1<<" "<<k<<" "<<dp[a-1][b-1][k]<<endl; } if(k){ if(!calc[a-1][b-1][p|0])f(a-1,b-1,p,0); dp[a][b][p|k]+=dp[a-1][b-1][p|0]; // cout<<"wywalam koniec z "<<a<<" "<<b<<" "<<p<<" "<<dp[a][b][p|k]<<" korzystając z "<<a-1<<" "<<b-1<<" "<<p<<" "<<dp[a-1][b-1][p]<<endl; } if(!p){ if(!calc[a-1][b][0|k])f(a-1,b,0,k); dp[a][b][p|k]+=dp[a-1][b][k|0]; // cout<<"dolczam poczatek "<<a<<" "<<b<<" "<<k<<" "<<dp[a][b][p|k]<<" wraz z "<<a-1<<" "<<b<<" "<<k<<" "<<dp[a-1][b][k]<<endl; } if(!k){ if(!calc[a-1][b][p|0])f(a-1,b,p,0); dp[a][b][p|k]+=dp[a-1][b][p|0]; // cout<<"dolczam koniec__ "<<a<<" "<<b<<" "<<p<<" "<<dp[a][b][p|k]<<" wraz z "<<a-1<<" "<<b<<" "<<p<<" "<<dp[a-1][b][p]<<endl; } //to przymus, nie szansa (?) dp[a][b][p|k]=fmod(dp[a][b][p|k],P); ///divide and queer For(i,2,a) { if(p&&k) { if(!calc[i-1][b-1][p])f(i-1,b-1,p,0); For(j,1,b){if(!calc[a-i][j][k])f(a-i,j,0,k); dp[a][b][1]+=fmod(dp[i-1][b-1][p]*dp[a-i][j][k],P)*dwu[a-1][i-1]; // printf("!%d %d 1 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,b-1,p,dp[i-1][b-1][k], a-i,j,k,dp[a-i][j][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} if(!calc[a-i][b-1][k])f(a-i,b-1,0,k); For(j,1,b-1){if(!calc[i-1][j][p])f(i-1,j,p,0); dp[a][b][1]+=fmod(dp[i-1][j][p]*dp[a-i][b-1][k],P)*dwu[a-1][i-1]; // printf("@%d %d 1 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,j,p,dp[i-1][j][p], a-i,b-1,k,dp[a-i][b-1][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } else if(!p && !k) { if(!calc[i-1][b-1][p])f(i-1,b-1,0,0); if(!calc[a-i][b-1][k])f(a-i,b-1,0,0); dp[a][b][0]+=fmod(dp[i-1][b-1][0]*dp[a-i][b-1][0],P)*dwu[a-1][i-1]; // printf("#%d %d 0 0 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][0], i-1,b,p,dp[i-1][b][p], a-i,b,k,dp[a-i][b][k],dwu[a-1][i-1]); dp[a][b][0]=fmod(dp[a][b][0],P); } else if(p) { if(!calc[a-i][b-1][0])f(a-i,b-1,0,0); For(j,1,b){ if(!calc[i-1][j][p])f(i-1,j,p,0); dp[a][b][1]+=fmod(dp[i-1][j][p]*dp[a-i][b-1][0],P)*dwu[a-1][i-1]; // printf("$%d %d 1 0 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-b,j,p,dp[i-1][j][p], a-i,b-1,k,dp[a-i][b-1][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } else if(k) { if(!calc[i-1][b-1][0])f(i-1,b-1,0,0); For(j,1,b){ if(!calc[a-i][j][k])f(a-i,j,0,k); dp[a][b][1]+=fmod(dp[i-1][b-1][0]*dp[a-i][j][k],P)*dwu[a-1][i-1]; // printf("^%d %d 0 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,b-1,p,dp[i-1][b-1][p], a-i,j,k,dp[a-i][j][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } } // cout<<a<<" "<<b<<" "<<p<<" "<<k<<" "<<dp[a][b][p|k]<<endl; } int main() { calc[1][0][0]=calc[1][0][1]=calc[1][1][0]=calc[1][1][1]=1; lld a,b; scanf("%lld%lld%lld", &a,&b,&P); cbin(P);++b; if(b>15)puts("0"); else { dp[1][1][1]=1;//=dp[1][1][0]=1; dp[1][1][0]=1; // For(i,1,b+1) f(a,b,1,1); printf("%lld",dp[a][b][1]); } /* puts("\n\n"); For(i,1,6) { For(j,1,i+1) cout<<dp[i][j][0]<<" "<<dp[i][j][1]<<" "; puts(""); }*/ } //jeszcze gorzej.
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 | #include <bits/stdc++.h> using namespace std; #define ff first #define dd second #define mp make_pair #define lld long long #define pb emplace_back #define sz size() #define For(i,s,a) for(lld i=(lld)s;i<(lld)a;++i) #define pii pair<int,int> #define pll pair<lld,lld> #define rpt(S,it) for(auto it=S.begin();it!=S.end();++it) #define mod (lld)(1e9+7) #define scanf(...) scanf(__VA_ARGS__)?:0 #define P first #define S second #ifdef __WIN32__ #define gcx getchar #elif __linux__ #define gcx getchar_unlocked #endif #define piii pair<pii,pii> lld dwu[1024][1024]; #define fmod(x,d) ((x>=d)?x%d:x) void cbin(lld P) { dwu[1][1]=1; dwu[1][0]=1; For(i,2,1001) For(j,0,i+1) { if(j==0 || j==i)dwu[i][j]=1; else dwu[i][j]=dwu[i-1][j-1]+dwu[i-1][j]; dwu[i][j]=fmod(dwu[i][j],P); } } lld P; lld dp[1024][1024][2]; bitset<1024>calc[1024][2]; void f(lld a, lld b, lld p, lld k) { if(b<1)return; if(a<b){calc[a][b][0]=calc[a][b][1]=0;return;} // cout<<a<<" "<<b<<" "<<p<<" "<<k<<endl; if(calc[a][b][p|k])return; calc[a][b][p|k]=1; if(p){ if(!calc[a-1][b-1][0|k])f(a-1,b-1,0,k); dp[a][b][p|k]+=dp[a-1][b-1][0|k]; // cout<<"wywalam początek z "<<a<<" "<<b<<" "<<k<<" "<<dp[a][b][p|k]<<" korzystając z "<<a-1<<" "<<b-1<<" "<<k<<" "<<dp[a-1][b-1][k]<<endl; } if(k){ if(!calc[a-1][b-1][p|0])f(a-1,b-1,p,0); dp[a][b][p|k]+=dp[a-1][b-1][p|0]; // cout<<"wywalam koniec z "<<a<<" "<<b<<" "<<p<<" "<<dp[a][b][p|k]<<" korzystając z "<<a-1<<" "<<b-1<<" "<<p<<" "<<dp[a-1][b-1][p]<<endl; } if(!p){ if(!calc[a-1][b][0|k])f(a-1,b,0,k); dp[a][b][p|k]+=dp[a-1][b][k|0]; // cout<<"dolczam poczatek "<<a<<" "<<b<<" "<<k<<" "<<dp[a][b][p|k]<<" wraz z "<<a-1<<" "<<b<<" "<<k<<" "<<dp[a-1][b][k]<<endl; } if(!k){ if(!calc[a-1][b][p|0])f(a-1,b,p,0); dp[a][b][p|k]+=dp[a-1][b][p|0]; // cout<<"dolczam koniec__ "<<a<<" "<<b<<" "<<p<<" "<<dp[a][b][p|k]<<" wraz z "<<a-1<<" "<<b<<" "<<p<<" "<<dp[a-1][b][p]<<endl; } //to przymus, nie szansa (?) dp[a][b][p|k]=fmod(dp[a][b][p|k],P); ///divide and queer For(i,2,a) { if(p&&k) { if(!calc[i-1][b-1][p])f(i-1,b-1,p,0); For(j,1,b){if(!calc[a-i][j][k])f(a-i,j,0,k); dp[a][b][1]+=fmod(dp[i-1][b-1][p]*dp[a-i][j][k],P)*dwu[a-1][i-1]; // printf("!%d %d 1 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,b-1,p,dp[i-1][b-1][k], a-i,j,k,dp[a-i][j][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} if(!calc[a-i][b-1][k])f(a-i,b-1,0,k); For(j,1,b-1){if(!calc[i-1][j][p])f(i-1,j,p,0); dp[a][b][1]+=fmod(dp[i-1][j][p]*dp[a-i][b-1][k],P)*dwu[a-1][i-1]; // printf("@%d %d 1 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,j,p,dp[i-1][j][p], a-i,b-1,k,dp[a-i][b-1][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } else if(!p && !k) { if(!calc[i-1][b-1][p])f(i-1,b-1,0,0); if(!calc[a-i][b-1][k])f(a-i,b-1,0,0); dp[a][b][0]+=fmod(dp[i-1][b-1][0]*dp[a-i][b-1][0],P)*dwu[a-1][i-1]; // printf("#%d %d 0 0 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][0], i-1,b,p,dp[i-1][b][p], a-i,b,k,dp[a-i][b][k],dwu[a-1][i-1]); dp[a][b][0]=fmod(dp[a][b][0],P); } else if(p) { if(!calc[a-i][b-1][0])f(a-i,b-1,0,0); For(j,1,b){ if(!calc[i-1][j][p])f(i-1,j,p,0); dp[a][b][1]+=fmod(dp[i-1][j][p]*dp[a-i][b-1][0],P)*dwu[a-1][i-1]; // printf("$%d %d 1 0 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-b,j,p,dp[i-1][j][p], a-i,b-1,k,dp[a-i][b-1][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } else if(k) { if(!calc[i-1][b-1][0])f(i-1,b-1,0,0); For(j,1,b){ if(!calc[a-i][j][k])f(a-i,j,0,k); dp[a][b][1]+=fmod(dp[i-1][b-1][0]*dp[a-i][j][k],P)*dwu[a-1][i-1]; // printf("^%d %d 0 1 %lld to %d %d %d %d razy %d %d %d %d %d\n",a,b,dp[a][b][1], i-1,b-1,p,dp[i-1][b-1][p], a-i,j,k,dp[a-i][j][k],dwu[a-1][i-1]); dp[a][b][1]=fmod(dp[a][b][1],P);} } } // cout<<a<<" "<<b<<" "<<p<<" "<<k<<" "<<dp[a][b][p|k]<<endl; } int main() { calc[1][0][0]=calc[1][0][1]=calc[1][1][0]=calc[1][1][1]=1; lld a,b; scanf("%lld%lld%lld", &a,&b,&P); cbin(P);++b; if(b>15)puts("0"); else { dp[1][1][1]=1;//=dp[1][1][0]=1; dp[1][1][0]=1; // For(i,1,b+1) f(a,b,1,1); printf("%lld",dp[a][b][1]); } /* puts("\n\n"); For(i,1,6) { For(j,1,i+1) cout<<dp[i][j][0]<<" "<<dp[i][j][1]<<" "; puts(""); }*/ } //jeszcze gorzej. |