[CF997C] Sky Full of Stars

\(\text{Problem}:\)Sky Full of Stars

\(\text{Solution}:\)

答案即总方案数减去没有一行或一列是同种颜色的方案数。

\(f_{i,j}\) 表示恰好有 \(i\)\(j\) 列是同种颜色的方案数,\(g_{i,j}\) 表示钦定有 \(i\)\(j\) 列是同种颜色的方案数,有:

\[\begin{aligned} g_{i,j}&=\binom{n}{i}\binom{n}{j}3^{(n-i-j)n+ij+1},i\geq 1,j\geq 1\\ g_{0,j}&=\binom{n}{j}3^{(n-j)n+j},i=0\\ g_{i,0}&=\binom{n}{i}3^{(n-i)n+i},j=0\\ f_{i,j}&=\sum\limits_{x=i}^{n}\sum\limits_{y=j}^{n}(-1)^{x+y-i-j}\binom{x}{i}\binom{y}{j}g_{x,y} \end{aligned} \]

要求的显然为 \(f_{0,0}\),考虑把答案分成三部分计算。\(i=0\)\(j=0\) 的部分可以暴力计算,下面只考虑 \(i\geq 1,j\geq 1\) 的情况,有:

\[\begin{aligned} f_{0,0}&=res+\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}(-1)^{i+j}\binom{n}{i}\binom{n}{j}3^{(n-i-j)n+ij+1}\\ &=res+3\sum\limits_{i=1}^{n}(-1)^{i}\binom{n}{i}\left(\sum\limits_{j=0}^{n}(-1)^{j}\binom{n}{j}3^{(n-i)(n-j)}-3^{n(n-i)}\right)\\ &=res+3\sum\limits_{i=1}^{n}(-1)^{i}\binom{n}{i}((3^{n-i}-1)^{n}-3^{n(n-i)})\\ ans&=3^{n^2}-f_{0,0} \end{aligned} \]

总时间复杂度 \(O(n\log n)\)

\(\text{Code}:\)

#include <bits/stdc++.h>
#pragma GCC optimize(3)
#define int long long
#define ri register
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define is insert
#define es erase
#define vi vector<int>
#define vpi vector<pair<int,int>>
using namespace std; const int N=1000010, Mod=998244353;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n,pw[N+5],fac[N+5],inv[N+5];
inline int ksc(int x,int p) { int res=1; for(;p;p>>=1, x=1ll*x*x%Mod) if(p&1) res=1ll*res*x%Mod; return res; }
inline int C(int x,int y) { if(x<y||x<0||y<0) return 0; return 1ll*fac[x]*inv[x-y]%Mod*inv[y]%Mod; }
signed main()
{
	pw[0]=1;
	for(ri int i=1;i<=N;i++) pw[i]=3ll*pw[i-1]%Mod;
	fac[0]=1;
	for(ri int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%Mod;
	inv[N]=ksc(fac[N],Mod-2);
	for(ri int i=N;i;i--) inv[i-1]=1ll*inv[i]*i%Mod;
	n=read();
	int ans=0;
	for(ri int i=0;i<=n;i++)
	{
		int w=1ll*C(n,i)*ksc(3,(1ll*(n-i)*n%(Mod-1)+i)%(Mod-1))%Mod;
		if(i) w=(w<<1)%Mod;
		if(i&1) ans=(ans-w+Mod)%Mod;
		else ans=(ans+w)%Mod;
	}
	for(ri int i=1;i<=n;i++)
	{
		int w=1ll*C(n,i)*(ksc(pw[n-i]-1,n)-ksc(3,1ll*n*(n-i)%(Mod-1))+Mod)%Mod;
		w=3ll*w%Mod;
		if(i&1) ans=(ans-w+Mod)%Mod;
		else ans=(ans+w)%Mod;
	}
	printf("%d\n",(ksc(3,1ll*n*n%(Mod-1))-ans+Mod)%Mod);
	return 0;
}
posted @ 2021-04-27 19:40  zkdxl  阅读(35)  评论(0编辑  收藏  举报