aniervs's blog

By aniervs, history, 4 months ago, In English

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

You're given $$$K$$$ and $$$n$$$ and you must compute for all $$$k \in [0, K]$$$

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

Obviously, we want it $$$mod$$$ some $$$M$$$, but let's ignore it for now.

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:

$$$\displaystyle\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:

$$$\displaystyle\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$$$
$$$\displaystyle\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$$$
$$$\displaystyle (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:

$$$\displaystyle 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:

$$$\displaystyle 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}$$$, and the sum of the products $$$A_i \cdot B_j$$$ is 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 this trick 2 years ago or more):

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, in a very Pythonic style:

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[1...r-l], B[l...mid])
    update S[mid+1...r] with the results from the convolution
    solve(mid + 1, r)

The final answer is computed by calling solve(0, K). Given that the sequences being convolved are of size $$$\mathcal{O}(r - l)$$$, we can convolve them in $$$\mathcal{O}((r - l)\log (r - l))$$$ time via FFT, giving a total time complexity of $$$\mathcal{O}(K\log^2 K)$$$.

As for modular arithmetics, notice we need the multiplicative inverse of all numbers $$$j \in [1, K+1]$$$, hence the modulo better be good (a big prime and also FFT-friendly).

That's all, I hope you like it. There are other ways of computing $$$S$$$, some of them quite interesting as well. I recommend going through them too.

Full text and comments »

  • Vote: I like it
  • +133
  • Vote: I do not like it

By aniervs, history, 3 years ago, In English

Hello. Recently, I started participating on AtCoder contests, and in many cases, I don't see announcements on codeforces, or any posts for discussing the problems. I remember that before, it was very frequent to see at least one AtCoder Contest-related post among the recent actions.

Is there a reason behind it?

Also, maybe we can use this post to discuss about the last ABC.

Full text and comments »

  • Vote: I like it
  • +47
  • Vote: I do not like it

By aniervs, history, 3 years ago, In English

Hello everybody. I wonder if there are some dates scheduled for the next SWERC. And there's no way of assuming a date, because the dates of past SWERC editions have changed in terms of months from one year to the other.

Does someone know?

Full text and comments »

  • Vote: I like it
  • +19
  • Vote: I do not like it

By aniervs, history, 3 years ago, In English

Hello everyone. I saw recently a comment about an app for macOS that allows you to use CompetitiveCompanion in Safari (Big Sur); it's basically a translation of the original extension. It's called Codeforces Web Tool. It also has the Codeforces Rating Prediction feature. Since I didn't know about it until some months/weeks ago, this could have happened to other folks too.

This is the app.

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it

By aniervs, history, 4 years ago, In English

Hello Codeforces. Tomorrow begins the CIIC 2020 (Ibero-American Contest in Informatics); it is the regional competition for Ibero-American Countries, similar to CEOI, but it is online. I just want to know which are the contestants from each participating country.

I think these are the ones going from Cuba (this is unofficially):

UPD: This is the ranking of countries (sorry for bad quality of image).

UPD: Here is the individual ranking,

UPD: Now you can do virtual participation here. Unfortunately it is only available in Spanish.

Full text and comments »

  • Vote: I like it
  • +97
  • Vote: I do not like it

By aniervs, history, 4 years ago, In English

Recently I've been studying some geometry, and now I want to know the different ways of computing the extremal Points on a set of points for a fixed direction, please leave it in the comments.

Thanks in advance.

Full text and comments »

  • Vote: I like it
  • +30
  • Vote: I do not like it

By aniervs, history, 5 years ago, In English

Hello, how can Kruskal's algorithm be modified to run in O(n^2) time in a dense graph of n nodes??

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it

By aniervs, history, 5 years ago, In English

Does anyone know when the equipment and software that will be used in IOI 2019 will be published, and why have not they done so yet?

Full text and comments »

  • Vote: I like it
  • +18
  • Vote: I do not like it