C/C++教程

CF997C Sky Full of Stars

本文主要是介绍CF997C Sky Full of Stars,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

CF997C Sky Full of Stars

首先进行容斥,用行中存在同色加列中存在同色减去行列均有同色的方案数

则为:

\[\begin{aligned}\left(2\sum_{i=1}^n(-1)^{i-1}3^{i+n(n-i)}{n\choose i}\right)+\left(\sum_{i=1}^n\sum_{j=1}^n(-1)^{i+j}3^{(n-i)(n-j)+1}{n\choose i}{n\choose j} \right)\end{aligned} \]

前面的直接枚举计算,考虑化简后面的

\[\begin{aligned}&\sum_{i=1}^n\sum_{j=1}^n(-1)^{i+j}3^{(n-i)(n-j)+1}{n\choose i}{n\choose j}\\=&3\sum_{i=0}^{n-1}(-1)^i{n\choose i}\sum_{j=0}^{n-1}(-1)^j3^{ij}{n\choose j}\\=&3\sum_{i=0}^{n-1}(-1)^i{n\choose i}\left((1-3^i)^j-(-1)^n3^{ni}\right)\end{aligned} \]

直接计算即可

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

#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
# define ll long long
# define mod 998244353
ll ans,n,fac[1000005],inv[1000005];
void init(const int N=1000000){
	fac[0]=fac[1]=inv[0]=inv[1]=1;
	for(int i=2;i<=N;++i)fac[i]=fac[i-1]*i%mod,inv[i]=(mod-mod/i)*inv[mod%i]%mod;
	for(int i=2;i<=N;++i)inv[i]=inv[i-1]*inv[i]%mod;
}
ll C(int x,int y){return x<y||y<0?0:fac[x]*inv[y]%mod*inv[x-y]%mod;}
ll P3(ll x){
	if(!x)return 1;
	ll v=P3(x>>1);
	v=v*v%mod;
	if(x&1)v=v*3%mod;
	return v;
}
ll qkpow(ll n,ll m){
	ll t=1;
	for(;m;m>>=1,n=n*n%mod)
		if(m&1)t=t*n%mod;
	return t;
}
int main(){init();
	n=read;
	for(int i=1;i<=n;++i)
		if(i&1)ans=(ans+C(n,i)*P3(i)%mod*P3((n-i)*n))%mod;
		else ans=(ans-C(n,i)*P3(i)%mod*P3((n-i)*n))%mod;
	ans=ans*2%mod;
	ll t=0;
	for(int i=0;i<n;++i)
		t=(t+(i&1?1:-1)*C(n,i)*(qkpow(1-P3(i),n)-qkpow(-P3(i),n)))%mod;
	cout<<((ans+t*3)%mod+mod)%mod;
	return 0;
}
这篇关于CF997C Sky Full of Stars的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!