NC13884 Paint Box(容斥)

題目鏈接

題意:
nm共有n個空盒子,m種顏色
使爲盒子塗色使得相鄰顏色不同
k並且恰好塗了k種不同顏色
 mod 1e9+7問有多少種塗色方案~mod~1e9+7
題解:
n,m<=1e9,k<=1e6n,m<=1e9,k<=1e6
恰好的問題,一般轉換成至少或者不超過然後用容斥去做
k要轉換的問題就是不超過用k種顏色的塗色方案
那麼就是塗一個顏色,每個顏色與其上一個顏色不同
k(k1)n1這樣算出就是k*(k-1)^{n-1}
但是當前的問題是不超過的方案數
所以需要用容斥進行轉換成恰好
k1即減去用不超過k-1種顏色的方案數
k2加上不超過k-2種顏色的方案數
1…………直到1種顏色爲止

但現在有了另一個問題
km他的總顏色數不止k個,有m個顏色
mk所以想要做到這個操作需要從m箇中選出k個
Cmk那麼就是結果乘上C_m^k
m但是m這個數較大,不能進行預處理的數組來求得
kk比較小,可以通過組合數的定義求得

AC代碼

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double,double> pdd;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[8][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

ll fact[maxn],inv1[maxn];
ll Pow(ll a, ll b){
	ll ans = 1;
	while(b > 0){
		if(b & 1){
			ans = ans * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}
//逆元
ll inv(ll b){
    return Pow(b,mod-2)%mod;
}
ll C(ll n,ll m){
    if(m>n||n<0||m<0)return 0;
    if(m==0||m==n) return 1;
    ll res=(fact[n]*inv1[m]%mod*inv1[n-m])%mod;
    return res;
}

void init() {
	fact[0] = 1;
	for (int i = 1; i < maxn; i++) {
		fact[i] = fact[i - 1] * i %mod;
	}
	inv1[maxn - 1] = Pow(fact[maxn - 1], mod - 2);
	for (int i = maxn - 2; i >= 0; i--) {
		inv1[i] = inv1[i + 1] * (i + 1) %mod;
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    init();
    int _;
    cin>>_;
    while(_--){
        ll n,m,k;
        cin>>n>>m>>k;
        ll ans=0;
        for(int i=0,cur=1;i<k;i++,cur=-cur)
            ans=(ans+cur*C(k,k-i)*(k-i)%mod*Pow(k-i-1,n-1)%mod+mod)%mod;
        for(int i=0;i<k;i++)
            ans=ans*(m-i)%mod*inv(k-i)%mod;
        cout<<ans<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章