Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF1228E Another Filling the Grid

LuoGu: CF1228E Another Filling the Grid

CF: E. Another Filling the Grid

记 $f(x, y)$ 表示至少有 $x$ 行 $y$ 列没有 $1$ ,显然有 $f(x, y) = \binom n x \binom n y k ^ {n \times n - (n - x) \times (n - y)} (k - 1) ^ {(n - x) \times (n - y)}$ 。

记 $g(x, y)$ 表示刚好有 $x$ 行 $y$ 列没有 $1$ ,那么有 $f(x, y) = \sum _ {i = x} ^ n \sum _ {j = y} ^ n \binom i x \binom j y g(i, j)$ ,上二维二项式反演,那么有 $g(x, y) = \sum _ {i = x} ^ n \sum _ {j = y} ^ n \binom i x \binom j y (-1) ^ {i + j - x - y}f(i, j)$ 。

带入 $x = 0, y = 0$ 即为答案 $\sum _ {i = 0} ^ n \sum _ {j = 0} ^ n (-1) ^ {i + j} \binom n i \binom n j k ^ {n \times n - (n - i) \times (n - j)} (k - 1) ^ {(n - i) \times (n - j)}$。

可以进一步优化,固定 $i$,整理得 $\sum _ {i = 0} ^ n \binom n i (-1) ^ i (k - 1) ^ {in} k ^ {n ^ 2 - ni} \sum _ {j = 0} ^ n \binom n j (-\frac {(k - 1) ^ n} {(k - 1) ^ {in} k ^ {n ^ 2 - ni} }) ^ j$ 。对后部分上二项式定理,那么有 $\sum _ {i = 0} ^ n \binom n i (-1) ^ i (k - 1) ^ {in} k ^ {n ^ 2 - ni} (1 -\frac {(k - 1) ^ n} {(k - 1) ^ {in} k ^ {n ^ 2 - ni} }) ^ n$ 。 可以做到 $O(n)$ 。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstdio>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 62510, mod = 1e9 + 7;
int n, m;
int inv[N], fact[N], ifact[N];
int binpow (int b, int k)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void init ()
{
inv[1] = 1;
for (int i = 2; i < N; ++i)
inv[i] = -(LL)(mod / i) * inv[mod % i] % mod;
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i)
{
fact[i] = (LL)fact[i - 1] * i % mod;
ifact[i] = (LL)ifact[i - 1] * inv[i] % mod;
}
}
int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; }
int main ()
{
init();
read(n, m);
int res = 0;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
{
int tot = (n - i) * (n - j);
(res += (i + j & 1 ? -1ll : 1ll) * C(n, i) * C(n, j) % mod * binpow(m, tot) % mod * binpow(m - 1, n * n - tot) % mod) %= mod;
}
write((res + mod) % mod);
return 0;
}