A new approach to a classical problem.

Revision en6, by XorReverse, 2026-12-03 18:08:28

Given $$$n, k(1 \leq n \leq 10^{9}, 1 \leq k \leq 100000)$$$. Calculate $$$1^k + 2^k + 3^k + ... + n^k$$$ $$$\pmod{998244353}$$$.

Generally, we can use Lagrange Interpolation to solve this but in this post, I am going to introduce another solution for this problem.

Background

One year ago, my friend asked me how to solve a certain problem. Although it could be solved using a simple mathematical formula, I wasn't good at math at that time so I turned it into a completely different problem—where the key to solving it is the above problem.

Idea

My solution uses DP, DNC and Newton's Binomial Theorem.

We define $$$f(x, y)$$$ as $$$1^y + 2^y + ... + x^y$$$ $$$\pmod{998244353}$$$ (with $$$f(x, 0) = x$$$ and $$$0^0 = 1$$$). Suppose that $$$x$$$ is even, we will try to calculate $$$f(x, y)$$$ with $$$0 \leq y \leq k$$$ from $$$f(\frac{x}{2}, y)$$$ with $$$0\leq y\leq k$$$.

Define $$$p = \frac{x}{2}$$$, we have:

$$$f(x, y) = 1^y + 2^y + ... + p^y + (p + 1) ^ y + (p + 2) ^ y + ... + (p + p) ^ y$$$ $$$\pmod{998244353}$$$

Using Newton's Binomial Theorem, we have:

$$$(p + i)^y = \sum_{d=0}^{y} \binom{y}{d} * p^d * i^{y - d}$$$

Substituting this into the expression above, we have:

$$$f(x, y) = 1^y + 2^y + ... + p^y + \sum_{d=0}^{y} \binom{y}{d} * p^d * 1 ^ {y - d} + \sum_{d=0}^{y} \binom{y}{d} * p^d * 2 ^ {y - d} + ... + \sum_{d=0}^{y} \binom{y}{d} * p^d * p ^ {y - d}$$$ $$$\pmod{998244353}$$$

Factoring every $$$ \binom{y}{d} * p^d$$$ with $$$0 \leq d \leq y$$$ out of the sum, each $$$ \binom{y}{d} * p^d$$$ appears $$$(1 ^ {y - d} + 2 ^ {y - d} + ... + p ^ {y - d})$$$ times in the expression above. Obviously, $$$1 ^{y - d} + 2 ^{y - d} + ... + p^{y - d} = f(p,y - d)$$$ so we can rewrite it as $$$\binom{y}{d} * p^d * f(p, y - d)$$$.

Also, $$$1 ^ y + 2 ^ y + ... + p ^ y = f(p, y)$$$

Rewriting the above expression, we have:

$$$f(x, y) = f(p, y) + \sum_{d=0}^{y} \binom{y}{d} * p^d * f(p, y - d)$$$ $$$\pmod{998244353}$$$

Now we can calculate $$$f(x, y)$$$ with $$$0 \leq y \leq k$$$ from $$$f(\frac{x}{2}, y)$$$ with $$$0\leq y \leq k$$$ in $$$O(k^2)$$$. When $$$x$$$ is odd, we can calculate $$$f(x - 1, y)$$$ and then add $$$x ^ y$$$ to it. We will use DNC for this so we will have approximately $$$\log n$$$ values of $$$x$$$ that need to be calculated. Time complexity for this solution: $$$O(k^2 \log n)$$$

Optimization

We can optimize the above solution to $$$O(k \log k \log n)$$$ by using NTT to calculate $$$f(x, y)$$$ with $$$0 \leq y \leq k$$$ in $$$O(k \log k)$$$. We will try to calculate $$$\sum_{d=0}^{y} \binom{y}{d} * p^d * f(p, y - d)$$$ $$$\pmod{998244353}$$$ for each $$$y$$$ $$$(0 \leq y \leq k)$$$

We have:

$$$\sum_{d=0}^{y} \binom{y}{d} * p^d * f(p, y - d) = \sum_{d=0}^{y} \frac{y!}{d!(y - d)!} * p^d * f(p, y - d) = {y!} * \sum_{d=0}^{y} \frac{1}{d!(y - d)!} * p^d * f(p, y - d)$$$ $$$\pmod{998244353}$$$

Now, we will try to calculate $$$\sum_{d=0}^{y} \frac{1}{d!(y - d)!} * p^d * f(p, y - d)$$$ $$$\pmod{998244353}$$$ for each $$$y (0 \leq y \leq k)$$$ :

$$$\sum_{d=0}^{y} \frac{1}{d!(y - d)!} * p^d * f(p, y - d) = \sum_{d=0}^{y} \frac{p^d}{d!} * \frac{f(p, y - d)}{(y - d)!}$$$ $$$\pmod{998244353}$$$

Define an array $$$A$$$ with $$$A_i = \frac{p^i}{i!} (0 \leq i \leq k)$$$ $$$\pmod{998244353}$$$ and an array $$$B$$$ with $$$B_i = \frac{f(p, i)}{i!}(0 \leq i \leq k)$$$ $$$\pmod{998244353}$$$. NTT can help us to calculate an array $$$C$$$ with $$$C_y = \sum_{i+j=y} A_i B_j$$$ $$$\pmod{998244353}$$$ in $$$O(k \log k)$$$ and we can precal $$$i!$$$ with $$$0 \leq i \leq k$$$ before. We can see that $$$C_y$$$ $$$\pmod{998244353}$$$ equals $$$\sum_{d=0}^{y} \frac{p^d}{d!} * \frac{f(p, y - d)}{(y - d)!}$$$ $$$\pmod{998244353}$$$.

Finally, we multiply $$$C_y$$$ by $$$y!$$$ and add $$$f(p, y)$$$ to get $$$f(x, y)$$$.

This solution works in $$$O(k \log k \log n)$$$ because for each $$$x$$$ we calculate $$$f(x, y)$$$ with $$$0 \leq y \leq k$$$ in $$$O(k \log k)$$$.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English XorReverse 2026-12-03 18:08:28 0 (published)
en5 English XorReverse 2026-12-03 18:08:09 93 Tiny change: 'k \log k)$' -> 'k \log k)$.'
en4 English XorReverse 2026-12-03 18:02:56 443 Tiny change: '0000)$. Caculating $1^k + 2^' -> '0000)$. Calculate $1^k + 2^'
en3 English XorReverse 2026-12-03 17:45:38 1472 Tiny change: 'O(k^2 log k)$' -> 'O(k^2 log n)$'
en2 English XorReverse 2026-12-03 17:12:32 940 Tiny change: ' = f(p,y)$' -> ' = f(p,y)$ so we can rewrite it to $\binom{y}{d} * p^d * f(p, y)$'
en1 English XorReverse 2026-12-03 16:47:35 1076 Initial revision (saved to drafts)