Codeforces Round #589 (Div. 2)E. Another Filling the Grid【容斥】+【二项式定理】

E. Another Filling the Grid

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have n×nn×n square grid and an integer kk. Put an integer in each cell while satisfying the conditions below.

  • All numbers in the grid should be between 11 and kk inclusive.
  • Minimum number of the ii-th row is 11 (1≤i≤n1≤i≤n).
  • Minimum number of the jj-th column is 11 (1≤j≤n1≤j≤n).

Find the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo (109+7)(109+7).

These are the examples of valid and invalid grid when n=k=2n=k=2.

Input

The only line contains two integers nn and kk (1≤n≤2501≤n≤250, 1≤k≤1091≤k≤109).

Output

Print the answer modulo (109+7)(109+7).

Examples

input

Copy

2 2

output

Copy

7

input

Copy

123 456789

output

Copy

689974806

Note

In the first example, following 77 cases are possible.

 

In the second example, make sure you print the answer modulo (109+7)(109+7).

 

 

 

分析:令f(a,b)为有a列元素全大于1,有b行元素全大于1,其余元素随意的方案数。

那么有na+nb-ab个元素大于1,其余全部任意值

那么f(a,b)=(k-1)^{na+nb-ab}*k^{n^2-ni-nj+ij}

显然ans = \sum _{i=0}^n \sum _{j=0}^nC_n^iC_n^j*f(i,j)*(-1)^{(i+j)}

ans= \sum _{i=0}^n \sum _{j=0}^nC_n^iC_n^j(-1)^{(i+j)}*(k-1)^{ni+nj-ij}*k^{n^2-ni-nj+ij}

ans= \sum _{i=0}^n C_n^i*(-1)^i\sum _{j=0}^nC_n^j(-1)^{j}*(k-1)^{ni+nj-ij}*k^{n^2-ni-nj+ij}

ans= \sum _{i=0}^n C_n^i*(-1)^i\sum _{j=0}^nC_n^j(-1)^{j}*(k^{n-j}*(k-1)^i)^{n-j}*((k-1)^n)^j

显然满足二项式定理

ans= \sum _{i=0}^n C_n^i*(-1)^i*((k^{n-j}*(k-1)^i)-(k-1)^n)^n

复杂度O(nlogn)

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5+7;
long long fac[maxn];
void init(){
    fac[0]=1;
    for (int i = 1; i < maxn; ++i) {
        fac[i] = fac[i-1]*i%mod;
    }
}
long long qk(long long a,long long n){
    long long res = 1;
    while(n){
        if(n&1)res=res*a%mod;
        n>>=1;
        a=a*a%mod;
    }
    return res;
}
long long inv(long long a){
    return qk(a,mod-2);
}
long long c(long long a,long long b){
    if(a<b)return 0;
    return fac[a] * inv(fac[b]*fac[a-b]%mod) % mod;
}

int main(){
    init();
    long long n,k;
    cin>>n>>k;
    long long ans = 0;
    for (int i = 0; i <= n; ++i) {
        long long temp = c(n,i) * qk((qk(k,n-i)*qk(k-1,i)%mod - qk(k-1,n) + mod)%mod,n)%mod;
        if(i&1){
            ans -= temp;
            ans += mod;
            ans %=mod;
        }
        else{
            ans += temp;
            ans %= mod;
        }
    }
    cout<<ans<<endl;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值