997C - Sky Full of Stars - CodeForces Solution


combinatorics math *2500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
int mod=998244353,n;

long long mypow(long long m,long long y){
	if(y==0){
		return 1;
	}
	long long p=mypow(m,(y>>1));
	p*=p;
	p%=mod;
	if(y&1){
		p*=m;
		p%=mod;
	}
	return p;
}
long long fact[maxn],revfact[maxn];

void aval(){
	fact[0]=1;
	for(int i=1;i<maxn;i++){
		fact[i]=fact[i-1]*i%mod;
	}
	//cout<<fact[maxn-1]<<"\n";
	revfact[maxn-1]=mypow(fact[maxn-1],mod-2);
	for(int i=maxn-2;i>=0;i--){
		revfact[i]=revfact[i+1]*(i+1)%mod;
	}
}

int c(int i,int j){
	if(i<0||j<0||i<j){
		return 0;
	}
	int ret=1ll*fact[i]*revfact[j]%mod*revfact[i-j]%mod;
	return ret;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	aval();
	cin>>n;
	long long res=0;
	long long cnt=1ll*n*n;
	for(int i=1;i<=n;i++){
		cnt-=n;
		long long fake=1ll*c(n,i)*mypow(3,cnt)%mod*mypow(3,i)%mod;
		//cout<<c(n,i)<<" "<<n<<" "<<i<<" "<<fact[1]<<" "<<revfact[1]<<" "<<revfact[0]<<"\n";
		if((i&1)==0){
			res+=mod+mod-fake;
		}
		else{
			res+=fake;
		}
		res%=mod;
	}
	//cout<<res<<"\n";
	cnt=1ll*n*n;
	for(int i=1;i<=n;i++){
		cnt-=n;
		long long fake=1ll*c(n,i)*mypow(3,cnt)%mod*((mypow(3,i)+mod-3)%mod)%mod;
		fake+=1ll*c(n,i)*3*mypow(mypow(3,n-i)-1,n)%mod;
		fake%=mod;
		//cout<<i<<" "<<fake<<"\n";
		if((i&1)==0){
			res+=mod+mod-fake;
		}
		else{
			res+=fake;
		}
		res%=mod;
	}
	cout<<res<<"\n";
}


Comments

Submit
0 Comments
More Questions

810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array