Hi everyone!
Today I would like to write about some identities that might come handy when using generating functions to solve competitive programming problems. I will also try to include some useful examples about them.
Some notation
For brevity, we will sometimes skip the specific bounds in indexed sums, meaning that the summation happens among all valid indices. Please also read the information below if you're not familiar with generating functions, or want to brush up on some aspects.
Multinomial coefficients
If you're not familiar with them, multinomial coefficients are defined as
where $$$a_1+\dots+a_r=n$$$. When dealing with identities that contain binomial coefficients, we often can combine them and "reshuffle" into another combination of binomial coefficients which is more convenient to us. This is because multinomial coefficients can be expanded as
In other words, we can represent any multinomial coefficient as a chain of binomial coefficients which account for each $$$a_i$$$ one piece at a time. We will see a lot of examples of this below, mostly with trinomial coefficients and for now we can look on the following one:
which allows to fully exclude $$$n$$$ from one of the coefficients which will then simplify our work with the part that is related to $$$n$$$.
Binomial theorem
Let's start with the simplest substitution ever. We most likely know the binomial theorem:
This allows us, whenever we have a binomial coefficient in our expression, to substitute it with
The last identity is especially useful, as it allows to drag $$$[x^n]$$$ outside of the summation.
Example 1. Consider the sum from one of Elegia's blog posts:
Assume that you're given several values of $$$n$$$ and you need to compute it in $$$O(1)$$$ with $$$O(n)$$$ pre-processing.
Exercise 1: Solve ProjectEuler 831 — Triple Product.
Negative binomial expansion
Now, let's learn few more well-known expansions. You most likely know that
This expression is also the consequence of the binomial expansion, as it also allows to use values of $$$n$$$ that are not positive integer. So, being more general, we can write that
but at the same time the binomial coefficient rewrites as
The result above allows us to rewrite
This, in turn, means that while $$$\binom{n}{k}$$$ is often substituted with $$$[x^k]{}(1+x)^n$$$, there is another useful substitution:
To differentiate between the two, the substitution $$$\binom{n}{k}=[x^k]{}(1+x)^n$$$ is typically more useful when $$$n$$$ is constant and summation goes over $$$k$$$. Conversely, when $$$k$$$ is constant and summation goes over $$$n$$$, it would likely be more convenient to use the substitution above.
Inverse square root expansion
Another important case of binomial expansion is when $$$n=-\frac{1}{2}$$$. Consider the expansion
We can rewrite the fractional binomial as
where $$$k!! = k(k-2)(k-4)\dots$$$ is the double factorial of $$$k$$$. Note that $$$(2k)!! (2k-1)!! = (2k)!$$$ and that $$$(2k)!! = 2^k k!$$$.
Thus, multiplying the numerator and denominator with $$$2^k k!$$$, we get
which means that
This identity allows to collapse some more complicated sums that involve $$$\binom{2k}{k}$$$, as it means that
Example 2. The problem 446227I — O Andar do Trêbado reduces to computing
It's well known that this is doable in $$$O(n)$$$. But in this problem we need to do it for all $$$n$$$ up to some limit.
Note that the linked problem also allowed $$$O(n^2)$$$ solutions. Thanks to tfg for telling me about the problem!
Example 3. The function $$$f(n,m)$$$ defined below can be computed in $$$O(1)$$$ with $$$O(n+m)$$$ pre-computation:
Example 4. Find the closed form of a function
Example 5. Find the closed form of a function
where $$$P_n(x)$$$ is the sequence of Legendre polynomials, defined for this example as
Note: Although examples 4 and 5 don't look useful for competitive programming, they can be used to solve the example 3, which is based on a real problem from some training camp (and also some prior publications, apparently).
Powers of $$$n$$$
Another less known, but quite useful substitution involves the exponential function:
from which it follows that we can use the substitution
Let's see how it can be used to solve some problems.
Example 6. Given $$$m$$$, we want to compute $$$f(k)$$$ in $$$O(1)$$$ per $$$k$$$ with $$$O(k \log k)$$$ pre-computation:
Solution: Using the substitution above, we turn the sum into the geometric progression:
Well, not exactly new, as I already wrote a blog about it quite some time ago...
Example 7. Given $$$A(x) = a_0 + a_1 x + \dots$$$, we want to quickly compute $$$f(k)$$$ defined as
Solution: Using the same substitution, we get
Well, still not new, as I had another blog about it some time ago.
At the same time, the result above assumes that we're able to compute series expansion of $$$A(e^x)$$$ up to $$$k$$$ sufficiently quickly. Which might turn out to be quite problematic if $$$A(x)$$$ is sufficiently arbitrary. However, when $$$A(x)$$$ is sufficiently nice, there is an interesting side effect of such solution. It allows $$$A(x)$$$ to have quite a large amount of terms if it still can be represented in a "nice" way.
For this, we rely on the following statement:
Conjecture: Let $$$F(x)$$$, $$$G(x)$$$ and $$$H(x)$$$ be formal power series, and let $$$G_0 = [x^0] G(x)$$$. Then the following implication stands:
In particular, as $$$[x^0] e^x = 1$$$, it means that
From the result above it follows that to find first $$$k$$$ elements of $$$F(e^x)$$$, we can instead find $$$H(x)$$$ such that
and compute first $$$k$$$ elements of $$$H(e^x)$$$ instead. This allows to efficiently solve the following problem:
Example 8. 392C - Еще одна последовательность чисел. Let $$$f_n=f_{n-1} + f_{n-2}$$$. Given $$$m$$$ and $$$k$$$, compute $$$f(k)$$$ in $$$O(k \log k)$$$:
Note: Apparently, this problem is also a subject of one of Elegia's blogs, but it is difficult for me to translate on my own...
Exercise 2. Find a way to solve example 8 in $$$O(k)$$$.
Exercise 3. Solve Library Judge — Sum of Exponential times Polynomial. The problem asks to, given $$$r$$$, $$$d$$$ and $$$n$$$, compute
Exercise 4. Solve Library Judge — Sum of Exponential times Polynomial Limit. The problem asks to, given $$$r$$$ and $$$d$$$, compute
It seems that they're also solvable with this technique (except for $$$r=1$$$ which should be accounted for separately due to degeneracy).
Exercise 5. Solve CodeChef — Quasi-Polynomial Sum.