NC13611

NC13611

題意

一棵有n個結點的樹,我們有k種不同顏色的染料給樹染色。當且僅當對於所有相同顏色的點對(x,y),x到y的路徑上的所有點的顏色都要與x和y相同時,染色方案是合法的。請統計方案數。

思路

把題目轉化爲給你一顆n結點的樹,將其分成i (1ik)i ~ (1\leq i \leq k)個連通塊塗上不同的顏色,此時發現染色方案的數量與這棵樹的形狀無關,僅與k和n有關,將其看爲一個點集,每個點集可以塗一種顏色。

  1. DP:f[i][j]=f[i1][j]+f[i1][j1](k(j1))DP :f[i][j]=f[i-1][j]+f[i-1][j-1]*(k-(j-1))

  2. 組合數學:分成ii個連通塊可看做斷i1i-1條邊,則可選邊的方案數爲C_{n-1}^{i-1},每一個塊可以取的顏色有AkiA_{k}^{i}種,又可以將連通塊分爲i (1ik)i ~ (1\leq i \leq k)塊不同顏色,最後總的方案數i=1min(n,k)Cn1i1Aki\sum_{i=1}^{min(n,k)}C_{n-1}^{i-1}A_{k}^{i}

DP

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;
const int N = 300 + 5;

ll n,k,f[N][N],res;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>k;
	f[0][0]=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k;j++){
			f[i][j]=(f[i-1][j]%mod+f[i-1][j-1]*(k-(j-1))%mod)%mod;
		}
	}
	for(int i=1;i<=k;i++){
		res=(res+f[n][i])%mod;
	}
	cout<<res<<'\n';
	return 0;
}

組合數學

下面的代碼還可以用一些算法優化求組合數,這裏未優化複雜度爲O(n2)O(n^2)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;
const int N = 300 + 5;

ll n,k,res,p=mod;

ll qpow(ll a,ll b){
	ll res=1;
	for(;b>0;b>>=1,a=a*a%mod)
		if(b&1) res=res*a%mod;
	return res;
}

inline ll C(ll n,ll m){
	if(n<m) return 0;//組合數n<m特判
	if(m>n-m) m=n-m;//組合數性質
	ll a=1,b=1;
	for(int i=0;i<m;i++){
		a=a*(n-i)%p;//組合數分子 a 
		b=b*(i+1)%p;//組合數分母 b
	} 
	return a*qpow(b,p-2)%p;//費馬小定理 a/b=a*b^(p-2) 
}

inline ll A(ll n,ll m){
	if(n<m) return 0;
	ll a=1;
	for(int i=n-m+1;i<=n;i++){
		a=a*i%p;
	} 
	return a;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>k;
	for(int i=1;i<=min(n,k);i++){
		res=(res+C(n-1,i-1)*A(k,i)%mod)%mod;
	}
	cout<<res<<'\n';
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章