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} p^d * i^{y - d}$$$




