# 求解自然数幂和的若干种方法

#### 3. 差分法

$(n+1)^k-1 =\sum_{i=1}^n (i+1)^k-i^k \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{i=1}^n\sum_{j=0}^{k-1}C_k^ji^j\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{i=0}^{k-1}C_k^i\sum_{j=1}^n j^i\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{i=0}^{k-1}C_k^i S(i) \therefore (n+1)^{k+1}-1=\sum_{i=0}^kC_{k+1}^iS(i)$

$i=k$时移项，可以得到$$(k+1)S(k)=(n+1){k+1}-1-\sum_{i=0}{k-1}C_{k+1}^iS(i)$$

#### 3. 倍增

$n$是奇数的时候直接由$f_{n-1,k}+n^k$转移过来。偶数的时候拆开来，运用简单的二项式定理，一波式子推得：$$f(n,k)=f(\frac{n}{2},k)+\sum_{j=0}kC_kjf(\frac{n}{2},j)\frac{n}{2}^{k-j}$$

#### 5. 第一类斯特林数

$x^{n\downarrow}=x\cdot (x-1)\cdot (x-2)\cdots (x-n+1)=\sum_{k=0}^ns_s(n,k)\cdot x^kx^{n\uparrow}=x\cdot (x+1)\cdot (x+2)\cdots(x+n-1)=\sum_{k=0}^ns_u(n,k)\cdot x^k$

### 7. 差分表

#### 8. 伯努利数

$B_n(t+1)-B_n(t)=\sum_{k=0}^{n-1} B_{k}*\lgroup(t+1)^{n-k} - t^{n-k}\rgroup C_{n}^{k}=\sum_{k=0}^{n-1} B_{k}*(\sum_{i=0}^{n-k-1} C_{n-k}^{i} * t^{i})* C_{n}^{k}=\sum_{k=0}^{n-1} B_{k}*(\sum_{i=0}^{n-k-1} C_{n-k}^{i} * t^{i}*C_{n}^{k})=\sum_{i=0}^{n-1}B_k*\sum_{i=0}^{n-k-1}\frac{n!t^i}{i!k!(n-k-i)!}=\sum_{k=0}^{n-1} B_{k}*(\sum_{i=0}^{n-k-1} C_{n-i}^{k} * t^{i}*C_{n}^{i})=\sum_{i=0}^{n-1} C_{n}^{i}*t^{i}*\sum_{k=0}^{n-1-i} B_{k}*C_{n-i}^{k}$

#### 9. 拉格朗日插值法

1. 快速根据点值逼近原函数.

2. 取点值对大于$n$唯一确定$n$次多项式.

###### General method

$f_i(x)=\frac{\prod\limits_{j\neq i}(x-x_j)}{\prod\limits_{j\neq i}(x_i-x_j)}*y_ig(x)=\sum_{i=0}^nf_i(x)$

###### Practice

$S(n)=\sum_{i=1}^n i^k$, 则

$S(n)=\sum_{i=1}^{k+2}y_i\prod_{j=1,i\neq j}^{k+2}\frac {n-x_j}{x_i-x_j}=\sum_{i=1}^{k+2}y_i\frac {\prod_{j=1,i\neq j}^{k+2}(n-j)}{\prod_{j=1,i\neq j}^{k+2}(i-j)}$

$Code$

#include <bits/stdc++.h>

#define F(i,a,b) for (int i = a; i <= b; i ++)
#define G(i,a,b) for (int i = a; i >= b; i --)

const int Mo = 998244353, M = 1e6 + 10;

using namespace std;

int l, r, k, m, y[M], z[M], jc[M], suf[M], pre[M];
bool bz[M];

int ksm(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (1ll * x * x) % Mo)
if (y & 1)
ans = (1ll * ans * x) % Mo;
return ans;
}

void Init() {
scanf("%d%d%d", &l,&r,&k), y[1] = 1, m = k + 2;
F(i, 2, m) {
if (!bz[i])
z[++ z[0]] = i, y[i] = ksm(i, k);
F(j, 1, z[0]) {
if (z[j] * i > m) break;
bz[z[j] * i] = 1;
y[z[j] * i] = (1ll * y[z[j]] * y[i]) % Mo;
if (i % z[j] == 0) break;
}
}
F(i, 2, m)
y[i] = (y[i - 1] + y[i]) % Mo;
jc[0] = 1;
F(i, 1, m)
jc[i] = 1ll * jc[i - 1] * i % Mo;
jc[m] = ksm(jc[m], Mo - 2);
G(i, m - 1, 1)
jc[i] = 1ll * jc[i + 1] * (i + 1) % Mo;
}

int Solve(int n) {
pre[0] = suf[m + 1] = 1;
F(i, 1, m)
pre[i] = 1ll * pre[i - 1] * (n - i) % Mo;
G(i, m, 1)
suf[i] = 1ll * suf[i + 1] * (n - i) % Mo;

int Ans = 0;
F(i, 1, m)
Ans = (Ans + 1ll * y[i] * pre[i - 1] % Mo * suf[i + 1] % Mo * (((k-i+2)&1) ? (-1) : 1) * jc[i - 1] % Mo * jc[k + 2 - i] % Mo) % Mo;
return Ans;
}

int main() {
Init();

printf("%d\n", (Solve(r) - Solve(l - 1) + Mo) % Mo);
}

posted @ 2019-04-07 11:06  proking  阅读(844)  评论(0编辑  收藏  举报