数数题还是要多练啊
code:
#include <cstdio> #include <cstring> #include <string> #include <vector> #include <algorithm> #define N 3004 #define ll long long #define mod 1000000007 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n,D,g[N],f[N][N],s[N][N],fac[N],inv[N]; vector<int>G[N]; int qpow(int x,int y) { int tmp=1; for(;y;y>>=1,x=(ll)x*x%mod) if(y&1) tmp=(ll)tmp*x%mod; return tmp; } int C(int x,int y) { return (ll)fac[x]*inv[y]%mod*inv[x-y]%mod; } void dfs(int x) { for(int i=1;i<=n;++i) f[x][i]=1; for(int i=0;i<G[x].size();++i) { int y=G[x][i]; dfs(y); for(int j=1;j<=n;++j) f[x][j]=1ll*f[x][j]*s[y][j]%mod; } for(int i=1;i<=n;++i) s[x][i]=(ll)(s[x][i-1]+f[x][i])%mod; } int main() { // setIO("input"); int i,j; scanf("%d%d",&n,&D); for(i=2;i<=n;++i) { int ff; scanf("%d",&ff),G[ff].push_back(i); } dfs(1); fac[0]=inv[0]=1; for(i=1;i<=n;++i) fac[i]=(ll)i*fac[i-1]%mod, inv[i]=qpow(fac[i],mod-2); for(i=1;i<=n;++i) { g[i]=f[1][i]; for(j=1;j<i;++j) g[i]=(ll)(g[i]-(ll)C(i-1,i-j)*g[j]%mod+mod)%mod; } int ans=0; int tmp=1; for(i=1;i<=min(D,n);++i) { tmp=(ll)tmp*qpow(i,mod-2)%mod*(D-i+1)%mod; ans=(ll)(ans+(ll)tmp*g[i]%mod)%mod; } printf("%d\n",ans); return 0; }
原文地址:https://www.cnblogs.com/guangheli/p/12174561.html
时间: 2024-10-30 04:05:11