cf111D Petya and Coloring 组合数学,二项式反演

时间:2023-03-09 00:40:22
cf111D Petya and Coloring 组合数学,二项式反演

http://codeforces.com/contest/111/problem/D

Little Petya loves counting. He wants to count the number of ways to paint a rectangular checkered board of size n × m (n rows, m columns) in k colors. Besides, the coloring should have the following property: for any vertical line that passes along the grid lines and divides the board in two non-empty parts the number of distinct colors in both these parts should be the same. Help Petya to count these colorings.

Input

The first line contains space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 106) — the board's vertical and horizontal sizes and the number of colors respectively.

Output

Print the answer to the problem. As the answer can be quite a large number, you should print it modulo 109 + 7 (1000000007).

Examples
Input
2 2 1
Output
1
Input
2 2 2
Output
8
Input
3 2 2
Output
40

题意:
给出一个 n*m 的矩阵,用sum种颜色染色
满足:任意一条竖直线(纵线)把矩阵划分成的2个部分,2个部分的不同的颜色数相同
求方案数
n,m <= 10^3,sum <= 10^6 solution:
注意到n,m的范围不大 显然有以下性质:
1.第1列和第m列的颜色数一定相等
2.2~m-1列的颜色只能从1,m列的颜色的交集中选择 m=1的时候,特殊处理,ans=k^n
m>1的时候,我们只需要考虑1,m列的颜色选择还有交集大小 预处理g[i]表示恰好用i种颜色涂满n个格子的方案数
(此时n是一个常量) 如果用递推式,求g[i]需要O(n^2),求g数组需要O(n^3),tle
考虑二项式反演求g数组:
设h(i)表示用i种颜色染n个格子的方案数,则h(i) = i^n
g(i)表示恰好用i种颜色染n个格子的方案数,
有:h(y) = sigma(i=0,i<=y)(C(y,i)*g(i))
则:g(y) = sigma(i=0,i<=y)((-1)^(y-i) * C(y,i) * h(i))
     = sigma(i=0,i<=y)((-1)^(y-i) * C(y,i) * i^n) 这样求g[i]需要O(nlogn),求g数组需要O(n^2*logn)
当然也可以优化到O(n^2)求g数组 主要的预处理部分搞定了,然后就是答案了
退下公式,得到:
ans = sigma(j=0,j<=min(n,sum))C(sum,j) * j^(m*n-2*n) *
    (sigma(i=0,i<=min(n-j,(sum-j)/2))(C(sum-j,i)*C(sum-j-i,i)*g(i+j)^2))
   //File Name: cf111D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月16日 星期一 01时13分24秒 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h> #define LL long long using namespace std; const int MAXN = + ;
const int MAXM = + ;
const int MOD = (int)1e9 + ; LL jie[MAXM];
LL g[MAXN]; LL qp(LL x,LL y){
LL res = ;
while(y){
if(y & ) res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL get_c(LL x,LL y){
if(x < y) return ;
if(x == y || y == ) return ;
return jie[x] * qp(jie[y] * jie[x - y] % MOD,MOD - ) % MOD;
} void init(int sum,int n){
jie[] = ;
for(int i=;i<MAXM;i++)
jie[i] = jie[i-] * i % MOD;
int ma = min(sum,n);
LL now;
for(int i=;i<=ma;i++){
for(int k=;k<=i;k++){
now = get_c(i,k) * qp(k,n) % MOD;
if((i - k) % )
g[i] = (g[i] - now + MOD) % MOD;
else
g[i] = (g[i] + now) % MOD;
}
}
} LL solve(int n,int m,int sum){
if(m == ) return qp(sum,n);
init(sum,n);
LL ans = ,now,tmp;
int ma = min(sum,n);
for(int j=,ma2;j<=ma;j++){
now = ;
ma2 = min(n - j,(sum - j) / );
for(int i=;i<=ma2;i++){
(now += jie[sum-j] * qp(jie[sum-j-*i]*jie[i]%MOD*jie[i]%MOD,MOD - ) % MOD
* g[i+j] % MOD * g[i+j] % MOD) %= MOD;
}
tmp = qp(j,(m - ) * n) % MOD;
(ans += get_c(sum,j) * tmp % MOD * now % MOD) %= MOD;
//cout << j << " " <<ans << endl;
}
return ans;
} int main(){
int n,m,k;
scanf("%d %d %d",&n,&m,&k);
printf("%d\n",(int)solve(n,m,k));
return ;
}