Inclusion and exclusion practice

Inverse linear factorial

int fac[N], ifac[N]
void init() {
	fac[0]=1;
	REP(i,1,N-1) fac[i]=(ll)fac[i-1]*i%P;
	ifac[N-1] = qpow(fac[N-1],P-2);
	PER(i,1,N-1) ifac[i-1]=(ll)ifac[i]*i%P;
}

 

Board 1. n * n, n <= 1e6, three colors, each one of the grid must be dyed, having at least one row or find a number (CF 998E) of the same color scheme

Provided $ f (i, j) $ of at least $ I $ row and at least $ J $ number of programs same color columns. Case limited only rows or columns, each row or column of color of the color may be different, otherwise because cross , the entire block must communicate with the same color, you may be obtained

$$f(i,j) =   \begin{cases} 3^{n(n-i-j)+i+j},  & \text{$i=0$或$j=0$} \\ 3^{(n-i)(n-j)+1}, & \text{其它} \end{cases}$$

By the inclusion and exclusion can be obtained $ ans = \ sum \ limits _ {\ substack {1 \ le i \ le n \\ 1 \ le j \ le n}} (- 1) ^ {i + j-1} \ binom {n } {i} \ binom {n} {j} f (i, j) $

For $ i = $ 0 or $ j = 0 $ directly $ O (n) $ can be determined, considering $ i> 0 $ and where $ j> 0 $, there is

$\begin{align}\notag ans & =\sum\limits_{\substack{1\le i\le n \\ 1\le j \le n}}(-1)^{i+j-1}\binom{n}{i}\binom{n}{j}3^{(n-i)(n-j)+1} \\ & =  \sum\limits_{i=1}^n\Bigg((-1)^{i+1}\binom{n}{i}3^{n^2-ni+1}\sum\limits_{j=1}^n(-1)^j\binom{n}{j}3^{(i-n)j}\Bigg) \notag \\ & = \sum\limits_{i=1}^n\Bigg((-1)^{i+1}\binom{n}{i}3^{n^2-ni+1}((1-3^{i-n})^n-1)\Bigg)\notag \end{align}$

It can be linearly calculated.

 

Guess you like

Origin www.cnblogs.com/uid001/p/10929163.html