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
| #define int long long #define maxn 2005 int mod; int c[maxn][maxn]; int n,dp[maxn][maxn],s[maxn][maxn],fs[maxn][maxn],fp[maxn][maxn],ft[maxn],g[maxn],o[maxn]; vector<int>to[maxn]; void add(int &x,int y) {x=(x+y)%mod;} const int id=1; void solve(int x,int pre) { int i,j,y; for (auto y:to[x]) if (y^pre) solve(y,x); for (j=0;j<=n;j++) dp[x][j]=1; if (x!=id) { int nums=to[x].size(),tot=-1; for (i=0;i<nums;i++) if ((y=to[x][i])^pre) { o[y]=++tot; for (j=1;j<=n;j++) dp[x][j]=dp[x][j]*s[y][j]%mod; for (j=0;j<=n;j++) { if (tot>0) fs[tot][j]=fs[tot-1][j]*s[y][j]%mod; else fs[tot][j]=s[y][j]; } } for (i=nums-1;i>=0;i--) if ((y=to[x][i])^pre) { for (j=0;j<=n;j++) if (o[y]<tot) fp[o[y]][j]=fp[o[y]+1][j]*s[y][j]%mod; else fp[o[y]][j]=s[y][j]; } for (auto y:to[x]) if (y^pre) { for (j=0;j<=n;j++) ft[j]=(o[y]?fs[o[y]-1][j]:1)*(o[y]<tot?fp[o[y]+1][j]:1)%mod; for (j=1;j<=n;j++) add(ft[j],ft[j-1]),add(dp[x][j],dp[y][j]*ft[j-1]); } } else { for (auto y:to[x]) if (y^pre) { for (j=1;j<=n;j++) dp[x][j]=dp[x][j]*s[y][j-1]%mod; } } s[x][0]=1; for (j=1;j<=n;j++) add(s[x][j],s[x][j-1]+dp[x][j]); } signed main(void){
int i,j,x,y; read(n);read(mod); for (i=0;i<=n;i++) { c[i][0]=1; for (j=1;j<=i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod; } for (i=1;i<=n;i++) dp[i][0]=1; for (i=1;i<n;i++) { read(x),read(y); to[x].push_back(y); to[y].push_back(x); } solve(1,0); for (i=1;i<n;i++) for (j=1;j<=i;j++) { int sum=c[i][j]*dp[id][j]%mod; if ((i-j)%2==0) add(g[i],sum); else add(g[i],mod-sum); } for (i=1;i<n;i++) printf("%lld ",g[i]); return 0; }
|