题目:http://codeforces.com/gym/101933/problem/K
其实每个点的颜色只要和父亲不一样即可;
所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \),设为 \( f(i) \),设恰好 i 种颜色为 \( g(i) \)
那么 \( f(i) = \sum\limits_{j=0}^{i} C_{i}^{j} * g(j) \)
二项式反演得到 \( g(i) = \sum\limits_{j=0}^{k} (-1)^{k-j} * C_{k}^{j} * f(j) \)
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int rd() { int ret=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();} while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar(); return f?ret:-ret; } int const xn=2505,mod=1e9+7; int n,k,f[xn],c[xn][xn]; int upt(int x){while(x>=mod)x-=mod; while(x<0)x+=mod; return x;} ll pw(ll a,int b){ll ret=1; for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod; return ret;} void init() { for(int i=0;i<=k;i++)c[i][0]=1; for(int i=1;i<=k;i++) for(int j=1;j<=i;j++) c[i][j]=upt(c[i-1][j]+c[i-1][j-1]); } int main() { n=rd(); k=rd(); init(); for(int i=1;i<n;i++)rd(); for(int i=1;i<=k;i++)f[i]=(ll)i*pw(i-1,n-1)%mod; int ans=0; for(int i=0;i<=k;i++)ans=upt(ans+(ll)f[i]*c[k][i]%mod*(((k-i)&1)?-1:1)); printf("%d\n",ans); return 0; }