convolution present on 1^k + 2^k + ... + n^k

Revision en1, by aniervs, 2024-07-30 07:58:28

The following is a very well-known problem, yet I recently found a nice solution involving convolutions.

You're given $$$n, m$$$ and you must compute

$$$S_k = 1^k + 2^k + \dots + n^k \pmod m $$$

for all $k$.

There're many ways of solving this problem, but here I focus on a particular one:

We start with some standard stuff, by expanding $$$\sum_{i=1}^n (i+1)^{k + 1}$$$. Through the Binomial Theorem we get:

$$$\sum_{i=1}^n (i+1)^{k + 1} = \sum_{i=1}^n \sum_{j=0}^{k+1} \binom{k+1}{j} i^j = \sum_{j=0}^{k+1} \binom{k+1}{j} \sum_{i=1}^n i^j = \sum_{i=1}^n i^{k + 1} + \binom{k + 1}{k} S_{k}+ \sum_{j=0}^{k-1} \binom{k+1}{j} S_j$$$

.

Then, we can actually cancel out the LHS via some telescopic sum:

$$$\sum_{i=1}^n (i+1)^{k + 1} = \sum_{i=1}^n i^{k + 1} + \binom{k + 1}{k} S_{k}+ \sum_{j=0}^{k-1} \binom{k+1}{j} S_j$$$

.

$$$\sum_{i=1}^n (i+1)^{k + 1} - \sum_{i=1}^n i^{k + 1} = \binom{k + 1}{k} S_{k}+ \sum_{j=0}^{k-1} \binom{k+1}{j} S_j$$$

.

$$$(n+1)^{k + 1} - 1 = (k + 1) S_{k}+ \sum_{j=0}^{k-1} \binom{k+1}{j} S_j$$$

.

From there, we get a recurrence:

$$$S_{k} = \frac{1}{k + 1} \left[(n+1)^{k + 1} - 1 - \sum_{j=0}^{k-1} \binom{k+1}{j} S_j \right]$$$

Now, let's expand the binomial coefficients and we get:

$$$S_{k} = \frac{1}{k + 1} \left[(n+1)^{k + 1} - 1 - (k+1)! \cdot \sum_{j=0}^{k-1} \frac{1}{(k+1-j)!} \cdot \frac{1}{j!} S_j \right]$$$

.

Now, let's define for each $$$t$$$: $$$A_{t}$$$ as $$$\frac{1}{(t+1)!}$$$ and $$$B_{t}$$$ as $$$\frac{1}{t!} S_t$$$. Notice that now the inner sum in the recurrence it's almost a convolution:

We have pairs $$$A_{i}, B_{j}$$$ contributing to $$$S_{i+j}$$$. The main issue is that we can't do a single convolution between $$$A$$$ and $$$B$$$ in order to compute $$$S$$$, because we need $$$S$$$ to define $$$B$$$.

The alternative is to do some Divide and Conquer (thanks to Errichto who taught me that when computing Catalan numbers as convolutions):

let's say we want to compute $$$S_k$$$ for every $$$k \in [l, r]$$$. Let's say $$$p=\lfloor \frac{l+r}{2} \rfloor$$$. We'll have a recursive algorithm:

The idea is to first compute $$$S_k$$$ for each $$$k \in [l, p]$$$ recursively. Then to compute $$$\mathrm{B}[]$$$ for those values of $$$k$$$ and to apply the convolution. Then to update $$$S_k$$$ for all $$$k \in [p+1, r]$$$. Lastly, solve recursively for the other half ($$$[p+1,r]$$$).

Something like this:

def solve(l, r):
    if l == r:
        # base case, add the parts not related to the convolution
        # use proper modular arithmetics 
        # ...
        return
    mid = (l + r) // 2
    solve(l, mid)
    convolve(A, S[l...mid])
    update S[mid+1...r] with the convolution's results
    solve(mid + 1, r)

Given that the sequences being convolved are of size $$$\mathcal{O}(r - l)$$$

Tags convolutions, math, divide and conquer

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en10 English aniervs 2024-07-30 15:24:58 11 Tiny change: 'nvolve(A, S[l...mid])\n upd' -> 'nvolve(A, B)\n upd'
en9 English aniervs 2024-07-30 08:36:42 27
en8 English aniervs 2024-07-30 08:30:53 2 Tiny change: 's $j \in [0, K+1]$, h' -> 's $j \in [1, K+1]$, h'
en7 English aniervs 2024-07-30 08:19:25 81 Tiny change: ':\n\n\n$$\sum_{i=1}' -> ':\n\n\n$$\displaystyle\sum_{i=1}'
en6 English aniervs 2024-07-30 08:17:38 2 (published)
en5 English aniervs 2024-07-30 08:17:16 10 Tiny change: 'ough them as well.\n\n' -> 'ough them too.\n\n'
en4 English aniervs 2024-07-30 08:16:54 199
en3 English aniervs 2024-07-30 08:12:42 331
en2 English aniervs 2024-07-30 08:08:08 366 Tiny change: ' like this:\n\n~~~~~' -> ' like this, in a very Pythonic style:\n\n~~~~~'
en1 English aniervs 2024-07-30 07:58:28 2774 Initial revision (saved to drafts)