## 【51NOD 1806】wangyurzee的树

1）可能有多个限制条件针对一个点，在容斥的时候要排除这种情况
2）容斥的时候，所有点的度都确定了，但度数不等于n-2的情况容斥也要排除掉

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int M = 20;
const int N = 1000000+9;
const int MX = 1000000;
const int MOD = 1000000007;

int POW[N],REV[N],lim[M],id[M],n,m,vout,vis[N];
set<pair<int,int> > S;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline int pow(int w, int t) {
if (!w || t <= 0) return 1; int ret = 1;
while (t) {
if (t & 1) ret = (LL)ret * w % MOD;
w = (LL)w * w % MOD; t >>= 1;
} return ret;
}

inline int C(int x, int y) {
int ret = (LL)POW[y] * REV[x] % MOD;
return (LL)ret * REV[y-x] % MOD;
}

void DFS(int t, int tot, int cnt, int sol, int f) {
if (tot < 0 || (tot > 0 && cnt == n)) return;
else if (t >= m + 1) {
if (!cnt) return;
else {
sol = (LL)sol * pow(n - cnt,tot) % MOD;
vout = ((vout + f*sol) % MOD + MOD) % MOD;
}
} else {
DFS(t+1, tot, cnt, sol, f);
if (!vis[id[t]]) {
vis[id[t]] = 1;
DFS(t+1, tot - lim[t], cnt + 1, (LL)sol * C(lim[t],tot) % MOD, -f);
vis[id[t]] = 0;
}
}
}

int main(){
for (int i=1;i<=m;i++) {
if (S.count(make_pair(id[i],lim[i]))) {
i--; m--;
} else {
S.insert(make_pair(id[i],lim[i]));
}
}
POW[1] = REV[1] = POW[0] = REV[0] = 1;
for (int i=2;i<=MX;i++) POW[i] = (LL)POW[i-1] * i % MOD;
REV[MX] = pow(POW[MX], MOD - 2);
for (int i=MX-1;i;i--) REV[i] = (LL)REV[i+1] * (i + 1) % MOD;

vout = pow(n, n-2);
DFS(1,n-2,0,1,1);
printf("%d\n",vout);
return 0;
}


## 【BZOJ 1005】[HNOI2008] 明明的烦恼

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1000+9;
const int MX = 1000;

int C[N][N],res,n,emp;
LL vout=1;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline void prework(){
C[0][0] = C[1][1] = C[0][1] = 1;
for (int j=1;j<=MX;j++) {
C[0][j] = 1;
for (int i=1;i<=j;i++) {
C[i][j] = C[i][j-1] + C[i-1][j-1];
}
}
}

int main(){
else puts("1");
} else if (n == 2) {
else puts("0");
} else {
prework(); emp = n - 2;
for (int i=1,tmp;i<=n;i++) {
res++;
} else {
vout *= C[tmp-1][emp];
emp -= tmp - 1;
}
}
if (emp < 0) puts("0");
else {
for (int i=1;i<=emp;i++) {
vout *= res;
}
cout<<vout;
}
}
return 0;
}


## 【BZOJ 1211】[HNOI2004] 树的计数

#include<bits/stdc++.h>
#define LL long long
using namespace std;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline int C(const int &a, const int &b) {
static int ret; ret = 1;
for (int i=1;i<=b;i++) ret *= i;
for (int i=1;i<=a;i++) ret /= i;
for (int i=1;i<=b-a;i++) ret /= i;
return ret;
}

int main(){
if (n == 1) {
else puts("0");
} else if (n == 2) {
else puts("0");
} else {
n -= 2; LL vout = 1;
for (int i=n+2,tmp;i;i--) {
n -= tmp;
}
if (n == 0) cout<<vout;
else puts("0");
}
return 0;
}


## 【BZOJ 1430】小猴打架

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int MOD = 9999991;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

int main(){
int n = read(), vout = 1;
for (int i=1;i<=n-2;i++) {
vout = (LL)vout * n % MOD;
}
for (int i=n-1;i;i--) {
vout = (LL)vout * i % MOD;
}
cout<<vout<<endl;
return 0;
}