CF111D:Petya and Coloring(组合数)

D. Petya and Coloring
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya loves counting. He wants to count the number of ways to paint a rectangular checkered board of size n × m (n rows, mcolumns) 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 nm 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的矩阵,用K种颜色将他们涂满,要求对于每一列i,1~i列和i+1~m列的颜色种类数相等,问方案数。

思路:突破点是第1列和第m列,有如下的事实:

①第1列和第m列的颜色种数要相等,否则当i=1和i=m时肯定有一个不满足要求(中间有其他的颜色种类)。

②中间的列颜色来源于第1列和第m列中的共同颜色,理由同上。

那么枚举首尾列的共同颜色数i和不同颜色数j即可,同时dp预处理,dp[i]表示N个格恰有i种颜色的方案数。那么显然dp[i] = i^n - sigma[dp[j]*C(i,j)],j∈[1, i-1],最后特别处 理下M=1和2的情况就好,组合数用逆元打表快很多。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6;
const LL mod = 1e9+7;
LL inv[maxn+3] = {1,1}, fac[maxn+3] = {1,1}, fi[maxn+3] = {1,1};
LL dp[1003];
LL qmod(LL a, LL b)
{
    LL ans = 1;
    for(;b;b>>=1)
    {
        if(b&1) ans = ans*a%mod;
        a = a*a%mod;
    }
    return ans;
}
void init()
{
    for(int i=2; i<=maxn; ++i)
    {
        fac[i] = fac[i-1]*i%mod;
        inv[i] = (mod-mod/i)*inv[mod%i]%mod;
        fi[i] = fi[i-1]*inv[i]%mod;
    }
}
LL c(LL n, LL m)
{
    return fac[n]*fi[n-m]%mod*fi[m]%mod;
}
int main()
{
    init();
    LL n, m, k;
    scanf("%I64d%I64d%I64d",&n,&m,&k);
    LL ans = 0;
    if(m == 1)
        return 0*printf("%I64d\n",qmod(k, n));
    for(LL i=1; i<=min(n,k); ++i)
    {
        dp[i] = qmod(i, n);
        for(LL j=1; j<i; ++j)
            dp[i] = (dp[i] - dp[j]*c(i,j)%mod+mod)%mod;
    }
    if(m == 2)
    {
        for(LL i=1; i<=min(n,k); ++i)
        {
            LL tmp = dp[i]*dp[i]%mod;
            tmp = tmp*c(k,i)%mod*c(k,i)%mod;
            ans = (ans + tmp)%mod;
        }
    }
    else
    {
        for(LL i=1; i<=min(k, n); ++i)
        {
            for(LL j=0; j<=min(k, n); ++j)
            {
                if(j+j+i>k || i+j>n) break;
                LL tmp = dp[i+j]*dp[i+j]%mod;
                tmp = tmp*c(k, i)%mod*c(k-i, j)%mod*c(k-i-j, j)%mod;
                tmp = tmp*qmod(i,(m-2)*n)%mod;
                ans = (ans + tmp)%mod;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值