一道简单的数学题
题意:给定$n,k$,求$\sum_{i=1}^ni^k$。
柿子题。
根据二项式定理:
$$(n-1)^{k+1}=\sum_{r=0}^{k+1}\binom{k+1}{r}n^r\times (-1)^{k+1-r}$$
写出来:
$$(n-1)^{k+1}=n^{k+1}-\binom{k+1}{k}n^k + \sum_{r=0}^{k-1}\binom{k+1}{r}n^r\times (-1)^{k+1-r}$$
移项:
$$\binom{k+1}{k}n^k =n^{k+1}-(n-1)^{k+1}+ \sum_{r=0}^{k-1}\binom{k+1}{r}n^r\times (-1)^{k+1-r}$$
累加起来:
$$\binom{k+1}{k}\sum_{i=1}^ni^k =n^{k+1}+ \sum_{i=1}^n\sum_{r=0}^{k-1}\binom{k+1}{r}i^r\times (-1)^{k+1-r}$$
改变求和顺序:
$$\binom{k+1}{k}\sum_{i=1}^ni^k =n^{k+1}+ \sum_{r=0}^{k-1}\binom{k+1}{r}(-1)^{k+1-r}\sum_{i=1}^ni^r$$
完成!
O(klogk) -> https://www.luogu.org/problem/CF622F