传送门
(n imes n)的矩阵,放置(n)个棋子,要满足两个条件。
首先,要满足第一个条件就必须使得每列或每行都有一个棋子。
那么只需要求出每列的情况,然后乘2就是答案。
假设每行都有一个棋子的情况下,我们讨论要几列,如果某列有(x)个棋子,那么就会产生贡献度为(x - 1),因为每行只有一个,所以列与列不会冲突,是独立的。那么假设有(y)列,(sum_{i = 1}^y x_i - 1 = k),解得(y = n - k),选择(C_{n}^{n-k})
那么就需要(n-k)列。而且每列里的棋子个数都至少一个。转换下模型,把每一列的棋子个数当成球,也就是(n)个球放在(n-k)个盒子里,不允许有空盒子。因为每一列都是有排列的,存在顺序。那么球是不同的,而且每一行也是有顺序的,相当于盒子也不同,那么只能用斯特林的模型
模型是(m!S(k,m)),那么答案就是(C_{n}^{n-k} * (n-k)!S(n, n - k))
把斯特林用定义式(S(n, m)=frac{1}{m !} sum_{i=0}^{m}(-1)^{i} * C_{m}^{i} *(m-i)^{n})
那么答案就是(C_{n}^{n - k} * sum_{i=0}^{m}(-1)^{i} * C_{m}^{i} *(m-i)^{n})
注意特判一下(k = 0)的情况,就是只下一个棋子,是(n!)
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define CASE int Kase = 0; cin >> Kase; for(int kase = 1; kase <= Kase; kase++)
using namespace std;
template<typename T = long long> inline T read() {
T s = 0, f = 1; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while(isdigit(ch)) {s = (s << 3) + (s << 1) + ch - 48; ch = getchar();}
return s * f;
}
#ifdef ONLINE_JUDGE
#define qaq(...) ;
#define qwq(c) ;
#else
#define qwq(a, b) for_each(a, b, [=](int x){cerr << x << " ";}), cerr << std::endl
template <typename... T> void qaq(const T &...args) {
auto &os = std::cerr;
(void)(int[]){(os << args << " ", 0)...};
os << std::endl;
}
#endif
const int N = 2e5 + 5, M = 1e6 + 5, MOD = 1e9 + 7, CM = 998244353, INF = 0x3f3f3f3f; const ll linf = 0x7f7f7f7f7f7f7f7f;
ll pow(ll a, ll b, ll p){
ll ans = 1; a %= p;
while(b) {
if(b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
}
namespace Combination{
const int MAXN = 2e5 + 5;
ll fac[MAXN], invfac[MAXN], mod;
void init(int n, ll MOD){
fac[0] = 1; mod = MOD;
for(int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % mod;
invfac[n] = pow(fac[n], mod - 2, mod);
for(int i = n; i >= 1; i--)
invfac[i - 1] = invfac[i] * i % mod;
}
ll C(ll n, ll m){
if(n < 0 || m < 0) return 0;
return n >= m ? fac[n] * invfac[n - m] % mod * invfac[m] % mod : 0;
}
}
ll F(ll n, ll m, ll MOD){
ll ans = 0; int base = 1;
for(int i = 0; i <= m; i++) {
ll now = base * Combination::C(m, m - i) % MOD * pow(m - i, n, MOD) % MOD;
now = (now + MOD) % MOD;
base *= -1;
ans = (ans + now) % MOD;
}
return ans;
}
void solve(int kase){
Combination::init(N - 5, CM);
ll n = read(), k = read();
if(k == 0) { // n!
ll ans = 1;
for(int i = 1; i <= n; i++) ans = ans * i % CM;
printf("%lld
", ans); return;
}
ll ans1 = Combination::C(n, n - k);
ll ans2 = 0;
for(int i = 0; i <= n - k; i++) {
ll now = ((i & 1) ? -1 : 1) * Combination::C(n - k, n - k - i) % CM;
now = (now + CM) % CM;
now = now * pow(n - k - i, n, CM) % CM;
ans2 = (ans2 + now) % CM;
}
printf("%lld
", 2 * ans1 * ans2 % CM);
}
const bool ISFILE = 0, DUO = 0;
int main(){
clock_t start, finish; start = clock();
if(ISFILE) freopen("/Users/i/Desktop/practice/in.txt", "r", stdin);
if(DUO) {CASE solve(kase);} else solve(1);
finish = clock();
qaq("
Time:", (double)(finish - start) / CLOCKS_PER_SEC * 1000, "ms
");
return 0;
}