CF2048G Kevin and Matrices

题意

对满足以下条件的大小为 n×m 值域为 [1,k] 的矩阵计数:

  • min1in(max1jmai,j)max1jm(mini=1nai,j)

模数 998244353

nk106,m109

分析

不妨记 ri=max1jmai,j,cj=min1inai,j,我们需要 minirimaxjcj,换句话说,需要存在至少一对 i,j 使得 ricj

性质 1:若 ricj,则 ri=cj=ai,j

证明:ai,jricjai,j

性质 2:若 i1i2,j1j2ri1cj1,ri2cj2,则
ri1=ri2=cj1=cj2

证明:cj2ai1,j2ri1,cj1ai2,j1ri2,由于 ri1cj1,ri2cj2,性质 2 自然成立。

考虑容斥,钦定一部分点 (i,j) 满足 ricj,根据性质 2,被钦定的点一定形如 (x,y),S{1,2,,n},T{1,2,,m},xS,yT。简单来说就是 |S| 条横线和 |T| 条竖线形成的所有网格格点。

枚举 x,y(式子里的 i,j)以及矩阵内填的数 p(根据性质 1 和性质 2 矩阵内的数相同),可以得出式子

ans=i=1nj=1m(1)x+y(ni)(mj)z=1kk(ni)(mj)zx(my)(kz+1)y(nx)

由于 i=1,j=1 对答案的贡献为正,所以不需要额外乘 1

j 枚举到后面并二项式定理,推一推可得

ans=i=1np=1k(1)ipmi(ni)(kniqn(pq)i)mkm(ni)

时间复杂度 O(nklogm)

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e6+5,inf=0x3f3f3f3f,mod=998244353;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m,k;
int ksm(int x,int y){
	int res=1;
	for(;y;y>>=1,x=x*x%mod)if(y&1)res=res*x%mod;
	return res;
}
int fac[maxn],inv[maxn];
void init(int lim=1e6){
	fac[0]=1;rep(i,1,lim)fac[i]=fac[i-1]*i%mod;
	inv[lim]=ksm(fac[lim],mod-2);per(i,lim-1,0)inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int x,int y){
	return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
inline void solve_the_problem(){
	n=rd(),m=rd(),k=rd();
	int ans=0;
	rep(i,1,n)rep(p,1,k){
		const int q=k-p+1;
		int f1=(ksm(k,n-i)+mod-ksm(q,n)*ksm(ksm(p*q%mod,i),mod-2)%mod)%mod,f2=ksm(k,m*(n-i));
		f1=(ksm(f1,m)-f2+mod)%mod;
		int res=ksm(p,m*i)*C(n,i)%mod*f1%mod;
		if(i&1)ans=(ans+mod-res)%mod;
		else ans=(ans+res)%mod;
	}
	write(ans);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();init();
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18662965

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示