2021neuq暑期集训2部分题解(AKE)

这篇博客介绍了两道算法题,第一题涉及矩阵的填充,要求计算每行最小值集合的大小之和,通过组合数和数论方法求解。第二题是一个离线查询问题,涉及到并查集和图的构造,通过排序和联通块进行处理。
摘要由CSDN通过智能技术生成

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() //这里预处理调用函数就会超时
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值