meaning of the title
\ (ntimesn) grid, dyed with three colors, and asked the number of schemes with one row/column all of the same color at the end.
Sol
Orz fjzzq
The final answer is this.
\[3^{n^2} - (3^n - 3)^n - \sum_{i = 1}^n (-1)^i C_n^i 3(3^{n-i} - 1)^n + (3^i - 3)(3^{(n-i)n}) \]
Let me force a wave of explanations.
First, you can make a transformation: the answer = the total scheme - any row / column contains at least two colors of the scheme
Let's consider columns first. Any column with two colors is((3 ^ n-3) ^ n(-3) because of the same three colors. But then we subtract more rows from the legitimate ones, so we need to add some schemes that satisfy the existence of at least one row with the same color and at least two colors in any column.
We find that "at least" is not very good. We can change it into "just" by tolerating it, that is, adding exactly one line of satisfaction, subtracting exactly two lines of satisfaction.
Then we need to classify and discuss the conditions that exactly satisfy the (i) line. Firstly, the number of alternatives selected is (C_n^i)
Next, there are two situations.
- The color of the selected (i) rows (each row is the same h) is any two different
This is a simple time, the number of options is ((3^i - 3) (3^{(n-i)n}). That is to say, subtract the three schemes in which each row has the same color, and the rest are optional.
- The selected lines of (i) have the same colour in pairs.
At this time, there are ((3^{n-i}-1)) schemes for each column, and there are (n) columns. At the same time, there are three different colours for this line.
So the number of schemes at this time is (3(3^{n-i}-1)^n)
Then work hard.
Complexity(O(nlog n)
#include<bits/stdc++.h> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #define se second #define int long long #define LL long long #define Fin(x) {freopen(#x".in","r",stdin);} #define Fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int MAXN = 1e6 + 10, mod = 998244353, INF = 1e9 + 10; const double eps = 1e-9; template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;} template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;} template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;} template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} template <typename A> inline void debug(A a){cout << a << '\n';} template <typename A> inline LL sqr(A x){return 1ll * x * x;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, fac[MAXN], ifac[MAXN]; int fp(int a, int p) { int base = 1; while(p) { if(p & 1) base = mul(base, a); p >>= 1; a = mul(a, a); } return base; } int C(int N, int M) { return mul(fac[N], mul(ifac[M], ifac[N - M])); } signed main() { cin >> N; fac[0] = 1; for(int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1]); ifac[N] = fp(fac[N], mod - 2); for(int i = N; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i); int ans = add(fp(3, N * N), -fp(fp(3, N) - 3, N)); for(int i = 1; i <= N; i++) { if(i & 1) { add2(ans, mul(C(N, i), add(mul(3, fp(fp(3, N - i) - 1, N)), mul(fp(3, i) - 3, fp(3, (N - i) * N))))); } else { add2(ans, -mul(C(N, i), add(mul(3, fp(fp(3, N - i) - 1, N)), mul(fp(3, i) - 3, fp(3, (N - i) * N))))); } } cout << ans; return 0; }