CodeForces 997C Sky Full of Stars

洛谷传送门

CF 传送门

考虑容斥,钦定 \(i\)\(j\) 列放同一种颜色,其余任意。\(i = 0\)\(j = 0\) 的情况平凡,下面只考虑 \(i, j \ne 0\) 的情况。

答案为:

\[\sum\limits_{i = 1}^n \sum\limits_{j = 1}^n (-1)^{i + j + 1} \binom{n}{i} \binom{n}{j} 3^{(n - i)(n - j) + 1} \]

\[= -\sum\limits_{i = 1}^n \binom{n}{i} (-1)^i \sum\limits_{j = 1}^n (-1)^j \binom{n}{j} 3^{n^2 + 1} \times 3^{-ni} \times 3^{-nj} \times 3^{ij} \]

\[= - 3^{n^2 + 1} \sum\limits_{i = 1}^n \binom{n}{i} (-1)^i 3^{-ni} \sum\limits_{j = 1}^n (-1)^j \binom{n}{j} 3^{(-n + i)j} \]

\[= - 3^{n^2 + 1} \sum\limits_{i = 1}^n \binom{n}{i} (-1)^{n + i} 3^{-ni} ((3^{i - n} - 1)^n - 1) \]

然后就能算了。

code
// Problem: C. Sky Full of Stars
// Contest: Codeforces - Codeforces Round 493 (Div. 1)
// URL: https://codeforces.com/problemset/problem/997/C
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 1000100;
const ll mod = 998244353;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

const ll inv3 = qpow(3, mod - 2);

ll n, c[maxn];

void solve() {
	scanf("%lld", &n);
	c[0] = 1;
	for (int i = 1; i <= n; ++i) {
		c[i] = c[i - 1] * (n - i + 1) % mod * qpow(i, mod - 2) % mod;
	}
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans = (ans + ((i & 1) ? 1 : mod - 1) * c[i] % mod * qpow(3, n * (n - i) + i) % mod * 2 % mod) % mod;
	}
	ll coef = (mod - qpow(3, n * n + 1)) % mod;
	for (int i = 1; i <= n; ++i) {
		ll t = (qpow(inv3, n - i) + mod - 1) % mod;
		t = (qpow(t, n) + ((n & 1) ? 1 : mod - 1)) % mod;
		ans = (ans + (((n + i) & 1) ? mod - 1 : 1) * coef % mod * c[i] % mod * qpow(inv3, n * i) % mod * t % mod) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}
posted @ 2023-07-09 22:04  zltzlt  阅读(47)  评论(0编辑  收藏  举报