Given $$$n, k(1 \leq n \leq 10^{18}, 1 \leq k \leq 500)$$$. Caculating $$$1^k + 2^k + 3^k + ... + n^k$$$ $$$\pmod{10^9 + 7}$$$.
Generally, we can use lagrange interpolation to solve but in this post, I am going to introduce an other 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 turn 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{10^9 + 7}$$$. 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{10^9 + 7}$$$
Use Newton's Binomial Theorem, we have:
$$$(p + i)^y = \sum_{d=0}^{y} \binom{y}{d} * p^d * i^{y - d}$$$
Substituting this into 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{10^9 + 7}$$$
Factoring every $$$ \binom{y}{d} * p^d$$$ with $$$0 \leq d \leq y$$$ out of the sum gives, for each $$$ \binom{y}{d} * p^d$$$, it contributes $$$\binom{y}{d} * p^d * (1 ^ {y - d} + 2 ^ {y - d} + ... + p ^ {y - d})$$$ into the expression above. Obviously $$$1 ^{y - d} + 2 ^{y - d} + ... + p^{y - d} = f(p,y - d)$$$ so we can rewrite it to $$$\binom{y}{d} * p^d * f(p, y - d)$$$.
Also, $$$1 ^ y + 2 ^ y + ... + p ^ y = f(p, y)$$$
Write again 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{10^9 + 7}$$$
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$$$



