跳到正文

题解

题解:P14720 [RMI 2025] 鼠皇 / King of rats

_JoeyJ_约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年12月11日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P14720

版本

  1. v1当前

考虑为啥是 2×n2\times n,发现不同的联通块不共列。

故放弃直接对连通块计数,转而对连通块左右端点计数,最后除以 22 即可。

考察如下 2×22\times 2 矩形:

  • 包含一个端点的:

    代码
    1 0 | 0 1 | 1 0 | 0 1 | 0 0 | 0 0
    1 0 | 0 1 | 0 0 | 0 0 | 1 0 | 0 1
  • 包含两个端点的:

    代码
    1 0 | 0 1
    0 1 | 1 0

那么端点之间的计数就独立了,把每种矩形枚举一遍,剩下的 11 安排的方案数直接组合数计算。因为 kk 很小,所以可以直接暴力计算阶乘和下降幂,时间复杂度 O(T(k+log⁡n))O(T(k+\log n))。

代码对相同的组合数进行了压缩。

C++
#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
using ll=long long;
const int mod=998244353;

inline void AddAs(int &x,int y){if((x+=y)>=mod) x-=mod;}
inline void SubAs(int &x,int y){if((x-=y)<0) x+=mod;}
inline void MulAs(int &x,int y){x=1ll*x*y%mod;}
inline int Add(int x,int y){if((x+=y)>=mod) x-=mod;return x;}
inline int Sub(int x,int y){if((x-=y)<0) x+=mod;return x;}
inline int Mul(int x,int y){return 1ll*x*y%mod;}
inline int QPow(int x,int y){
	int res=1;
	while(y){
		if(y&1) MulAs(res,x);
		MulAs(x,x);
		y>>=1;
	}
	return res;
}
inline int Inv(int x){return QPow(x,mod-2);}

inline int B(int n,int m){
	if(m>n||m<0) return 0;
	int fac=1,res=1;
	for(int i=1;i<=m;i++) MulAs(fac,i);
	for(int i=1;i<=m;i++) MulAs(res,n-i+1);
	return Mul(res,Inv(fac));
}

void prec(int subtask_id){}
int solve(int n,int k){
	int ans=0;
	AddAs(ans,B(2*(n-1),k-2));
	AddAs(ans,Mul(2,B(2*(n-1),k-1)));
	AddAs(ans,Mul(3,Mul(n-1,B(2*(n-2),k-2))));
	AddAs(ans,Mul(2,Mul(n-1,B(2*(n-2),k-1))));
	return Mul(ans,Inv(B(2*n,k)));
}

signed main(){
	cin.tie(0),cout.tie(0);
	ios::sync_with_stdio(0);

	int Tid,T;
	cin>>Tid>>T;
	prec(T);
	while(T--){
		int n,k;
		cin>>n>>k;
		cout<<solve(n,k)<<endl;
	}

	return 0;
}
原文