Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

### Elegia's blog

By Elegia, history, 3 months ago,

This is a part of my report on China Team Selection 2021. I might translate some other parts later if you are interested.

I'm going to show that we can calculate $f(G)$ for any polynomial $f\in R[x]$ and set power series $G: 2^{n} \rightarrow R$ under $\Theta(n^2 2^n)$ operations on $R$.

Let's first consider a special case: calculate $F = \exp G$. We consider set power series as truncated multivariate polynomial $R[x_1,\dots,x_n]/(x_1^2,\dots,x_n^2)$. We can see that $F' = FG'$ whichever partial difference $\frac{\partial}{\partial x_k}$ we take. Then we can rewrite this equation as $[x_n^1] F = ([x_n^1]G)\cdot [x_n^0]F$. Hence we reduced the problem into calculating $[x_n^0] F$, and then one subset convolution gives the rest part. The time complexity is $T(n)=\Theta(n^22^n) + T(n-1)$, which is exactly $\Theta(n^22^n)$. I call this method "Point-wise Newton Iteration".

This method sounds more reasonable than calculating the $\exp$ on each rank vector. Because $\frac{G^k}{k!}$ counts all "partitions" with $k$ nonempty sets in $G$. So it exists whenever $1\sim n$ is invertible in $R$.

Now we try to apply this kind of iteration into arbitrary polynomial $f$. Let $F=f(G)$, then $F'=f'(G)G'$. Then we reduced the problem into calculating $[x_n^0]f(G)$ and $[x_n^0]f'(G)$. This becomes two problems! But don't worry. In the second round of recursion, it becomes $[x_{n-1}^0 x_n^0]$ of $f(G), f'(G), f"(G)$. The number of problem grows linearly. You will find out that the complexity is $\sum_{0\le k\le n} (n-k) \cdot \Theta(k^2 2^k)$.

It's not hard to see that the complexity is still $\Theta(n^2 2^n)$, because $\sum_{k\ge 0} k\cdot 2^{-k}$ converges.

Noted that $\frac{(A+B)^2}2-\frac{A^2}2-\frac{B^2}2=AB$, we proved the equivalence between the composition and subset convolution.

Here are some remarks:

• If $G$ has no constant term, $f$ is better to be comprehended as an EGF.
• This algorithm holds the same complexity on calculating $f(A, B)$ and so on.
• We can optimize the constant of this algorithm by always using the rank vectors during the iteration. Then it can beat lots of methods that work for some specialized $f$.
• If you have some knowledge of the "Transposition Principle", you will find out that, for any fixed $F, G: 2^n \rightarrow R$, the transposed algorithm gives $\sum_S F_S \cdot (G^k)_S$ for all $k$. It immediately gives an $\Theta(n^22^n)$ algorithm to calculate the whole chromatic polynomial of a graph with $n$ vertices.

Read more »

• +312

By Elegia, 7 months ago,

The China team selection ended today, the top 4 are:

Yu Haoxiang (yhx-12243),
Deng Mingyang (Miracle03),
Qian Yi (skip2004) and
Dai Chenxin (gtrhetr).

They will participate in IOI2021. Good luck!

Read more »

• +692

By Elegia, history, 16 months ago,

We will get the bivariate generating function $\widehat F(z, t) = \sum_{n\ge 1}\sum_{k=1}^n A_{n,k}\frac{z^nt^k}{n!}$, where $A_{n,k}$ is the sum of occurrences of $k$ in all good sequence of length $n$. Consider the inclusion-exclution principle. For all contribution with $\max k = m$, we have

$\sum_{j=1}^m \sum_{S \subseteq \{1, 2, \cdots m-1\}} (-1)^{|S|} Q(S, z, t, j)$

This means that for all $i \in S$, we force all recurrences of $i$ are before $i + 1$, and the occurrences of $j$ are counted.

After some tough calculation, we will found out that this equates to $\frac{t(\mathrm e^{z(1-t)}-1)}{(1-z) (1-t \mathrm e^{z(1-t)})}$. Here are the details:

Details

Now our goal is to calculate

$[z^n]\frac{t(\mathrm e^{z(1-t)}-1)}{(1-z) (1-t \mathrm e^{z(1-t)})}$

We consider $\left([z^n]\frac{t(\mathrm e^{z(1-t)}-1)}{(1-z) (1-t \mathrm e^{z(1-t)})}\right) + 1 = (1-t)[z^n] \frac 1{(1-z)(1-t\mathrm e^{z(1-t)})}$, which looks simpler. We let $z = \frac u{1-t}$, then

$[z^n]\frac 1{(1-z)(1-t\mathrm e^{z(1-t)})} = (1-t)^n[u^n] \frac1{(1-\frac u{1-t})(1-t\mathrm{e}^u)}$

Hence we have

\begin{aligned} (1-t)[z^n] \frac 1{(1-z)(1-t\mathrm e^{z(1-t)})} &= [u^n]\frac{(1-t)^{n+2}}{(1-\frac{t}{1-u})(1-t\mathrm e^u)(1-u)}\\&= (1-t)^{n+2} [u^n] \left(\frac{-\mathrm e^u}{\left(\mathrm e^u u-\mathrm e^u+1\right) \left(1-t \mathrm e^u\right)}+\frac{\frac{1}{1-u}}{\left(\mathrm e^u u-\mathrm e^u+1\right) (1-\frac{t}{1-u})}\right)\end{aligned}

Noticed that $[u^m]\mathrm{e}^{ku} = \frac{k^m}{m!}$, this could be calculated straightly through multipoint evaluation with time complexity of $\Theta(n\log^2 n)$. And $[u^m] \frac1{(1-u)^k} = \binom{n+k-1}{n} = \frac{(n+k-1)!}{n!(k-1)!}$ so this part could be calculated through a convolution. It will pass if your implementation doesn't have a big constant.

It could also be improved to $\Theta(n\log n)$ through the Lagrange Inversion formula similar to the original solution, I leave this as an exercise.

UPD1: simplified some deduction.

Read more »

• +172