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 - Yet Another Number Sequence. 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)$$$.
Example 9. Solve Library Judge — Sum of Exponential times Polynomial Limit. Given $$$r \neq 1$$$ and $$$d$$$, compute $$$f(d)$$$ in $$$O(d)$$$, where
Exercise 3. Solve Library Judge — Sum of Exponential times Polynomial. Given $$$r$$$, $$$d$$$ and $$$n$$$, compute $$$f(d)$$$ in $$$O(d)$$$, where
Exercise 4. Solve CodeChef — Quasi-Polynomial Sum.
This might be a good practice of negative binomial expansion GR21-pE
Thank you for this wonderful blog, it may not be such a big deal but there is a flaw in the last trick, for $$$f(g(x))$$$ to be defined in a formal series, either $$$f(x)$$$ should be finite or $$$g(0) = 0$$$ , hence $$$f(e^x)$$$ may not be defined in formal series.
The way to rectify that would be:
$$$F(x) \equiv H(x) mod (x^k) \Rightarrow F(G(x)-G(0)) \equiv H(G(x)-G(0)) mod (x^k)$$$,
Where $$$F$$$, $$$G$$$ and $$$H$$$ are formal series Following this the proof almost remains the same, just shifted,
This is mostly what has been used in the following sections
References: Chapter 2 — https://www2.math.upenn.edu/~wilf/gfologyLinked2.pdf
counting reorders
a difficult task on cses, i solved it with laguerre poly and unsigned lah numbers
https://en.wikipedia.org/wiki/Laguerre_polynomials#Explicit_examples_and_properties_of_the_generalized_Laguerre_polynomials
https://dmtcs.episciences.org/2369/pdf
You overcomplicated. I solved with dp and inclusion/exclusion principle.
I got the idea before that somehow inclusion exclusion is used. but was not able to reduce the complexity , like mine was going around n^3.
but then i found this, and then got the How n^2 could be implemented using inclusion exclusion from the generating function implemention. but it was very good task, learned something new.
Wait... there is something called multinomial coefficient? :o
I finally worked out Elegia's blog seemed to work only if we are able to come up with recurrence relations.
The hint is actually misleading because if you think about it the numerator in the non-reduced and reduced version should at least be equal in $$$ mod(t^{k+1}) $$$ which wouldn't happen in this case.
The easiest way I could come up with to solve in $$$O(k)$$$ was to use the fact that fib numbers are powers of golden ratio and using exercise 3 one could obtain exercise 2 as well, just $$$r$$$ would be of the from $$$a+b\sqrt{5}$$$