#include <bits/stdc++.h>
using namespace std;
int n,k;
int p=1000000007;
int tab[3005][3005];
int power[3005][3005];
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>k;
for(int i =0;i<n+1;i++){
for(int j=n;j>=0;j--){
if(i==0){power[i][j]=1;} else{
power[i][j]=((power[i-1][j]*(k-j)) % p);}
}
}
for(int j = n;j>=0;j--)
{
tab[2][j]=power[1][j];
tab[3][j]=power[2][j];
for(int i=4;i<=n;i++){
tab[i][j]=power[i-1][j];
for(int ii=2;ii<=i;ii++){
tab[i][j]+=tab[ii][j+1]*tab[n-ii][j];//cout<<tab[ii][j]<<'\n';
tab[i][j]=tab[i][j] % p;
}
}
}
cout<<tab[n][0];
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 | #include <bits/stdc++.h> using namespace std; int n,k; int p=1000000007; int tab[3005][3005]; int power[3005][3005]; int main() { ios_base::sync_with_stdio(false); cin>>n>>k; for(int i =0;i<n+1;i++){ for(int j=n;j>=0;j--){ if(i==0){power[i][j]=1;} else{ power[i][j]=((power[i-1][j]*(k-j)) % p);} } } for(int j = n;j>=0;j--) { tab[2][j]=power[1][j]; tab[3][j]=power[2][j]; for(int i=4;i<=n;i++){ tab[i][j]=power[i-1][j]; for(int ii=2;ii<=i;ii++){ tab[i][j]+=tab[ii][j+1]*tab[n-ii][j];//cout<<tab[ii][j]<<'\n'; tab[i][j]=tab[i][j] % p; } } } cout<<tab[n][0]; return 0; } |
English