A —— Matrix
Fill an n×n matrix with numbers in [1,n2], where each number occurs exactly once.For a fixed number filling method, let ai be the mininum number in the ith row, and S = {a1,a2,…,an} ∩ {1,2,…,n}.You need to calculate ∑|S|( mod 998244353), i.e. the sum of the size of S over all possible methods.
input
This problem contains multiple test cases.
The first line contains a single integer T (1≤T≤30).
Then T cases follow, each of which contains a single interger n (1≤n≤5000).
Output
For each test case, output one line contains the value of ∑|S| (mod 998244353)
大致题意:给你一个n * n 的矩阵填充了[1 , n2] 的数,每一行都会贡献一个最小值ai,S = {a1,a2,…,an} ∩ {1,2,…,n} 求ΣS
分析:对于每一行来说 最小值只会再[1,n]产生,当选定ai之后,剩下n2- i个数再挑选 C1n2 - i 个这n个数全排列 n! 之后 (n2-i)个数全排列,再将n个排好的数插进去
所以ΣS = C(n2- i , n - 1) * n! * (n2 - n)! * n 组合数+快速幂+逆元+预处理
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5010 , p = 998244353;
const int u = 5000 * 5000;
int fact[N * N] , infact[N * N];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline int ksm(int a , int b)
{
int ans = 1;
while (b)
{
if (b & 1) ans = ans * a % p;
b >>= 1;
a = a * a % p;
}
return ans;
}
//inline void init() //这里预处理调用函数就会超时