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,其余全部任意值
那么
显然
显然满足二项式定理
复杂度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;
}