#include<iostream>
using namespace std;
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
unsigned int n,m,p,wynik=0;
cin>>m>>n>>p;
unsigned int dp[n+1][n+1][2];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
dp[i][j][1]=1;
for(int i=2;i<=m;i++)
{
for(int w=1;w<=n;w++)
{
for(int e=w;e<=n;e++)
{
dp[w][e][i%2]=0;
for(int u=1;u<=e;u++)
{
for(int o=max(u,w);o<=n;o++)
{
if(u<=e && o>=w)
{
dp[w][e][i%2]+=dp[u][o][(i-1)%2];
dp[w][e][i%2]%=p;
}
}
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
wynik+=dp[i][j][m%2];
wynik%=p;
}
}
cout<<wynik;
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 | #include<iostream> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); unsigned int n,m,p,wynik=0; cin>>m>>n>>p; unsigned int dp[n+1][n+1][2]; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) dp[i][j][1]=1; for(int i=2;i<=m;i++) { for(int w=1;w<=n;w++) { for(int e=w;e<=n;e++) { dp[w][e][i%2]=0; for(int u=1;u<=e;u++) { for(int o=max(u,w);o<=n;o++) { if(u<=e && o>=w) { dp[w][e][i%2]+=dp[u][o][(i-1)%2]; dp[w][e][i%2]%=p; } } } } } } for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { wynik+=dp[i][j][m%2]; wynik%=p; } } cout<<wynik; return 0; } |
English