[洛谷4948]数列求和

题目传送门:https://www.luogu.org/problemnew/show/P4948

题目大意:令\(A_n=n^k\times q^n\),求\(\sum\limits_{i=1}^nA_i\)


其实很久以前数学课学了数列就开始想这题了……

但最近才会解法……

我们令\(S_k(n)=\sum\limits_{i=1}^ni^k\times q^i\),对其扰动可得
\[ \begin{align}S_k(n)&=\sum\limits_{i=1}^n(i+1)^k\times q^{i+1}-(n+1)^k\times q^{n+1}+q\nonumber\\&=\sum\limits_{i=1}^n\sum\limits_{j=0}^k\binom{k}{j}i^j\times q^{i+1}-(n+1)^k\times q^{n+1}+q\nonumber\\&=q\sum\limits_{j=0}^k\binom{k}{j}S_j(n)-(n+1)^k\times q^{n+1}+q\nonumber\end{align} \]
所以我们就得到了\(S_k(n)\)的递推式
\[ S_k(n)=\dfrac{(n+1)^k\times q^{n+1}-\sum\limits_{j=0}^{k-1}\binom{k}{j}S_j(n)-q}{q-1} \]

但是这仅限于\(q>1\)的情况,那么\(q=1\)的情况呢,那就是幂和的形式,具体方法可以看这篇博客

/*program from Wolfycz*/
#include
#include
#include
#include
#include
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
templateinline T frd(T x){
    int f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
templateinline T read(T x){
    int f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=2e3,p=1e9+7;
int fac[N+10],inv[N+10],T[N+10];
void prepare(){
    fac[0]=inv[0]=inv[1]=1;
    for (int i=1;i<=N;i++)  fac[i]=1ll*i*fac[i-1]%p;
    for (int i=2;i<=N;i++)  inv[i]=1ll*(p-p/i)*inv[p%i]%p;
    for (int i=1;i<=N;i++)  inv[i]=1ll*inv[i-1]*inv[i]%p;
}
int C(int n,int m){return 1ll*fac[n]*inv[m]%p*inv[n-m]%p;}
int mlt(ll a,ll b){
    int res=1; a%=p; b%=(p-1);
    for (;b;b>>=1,a=1ll*a*a%p)  if (b&1)    res=1ll*res*a%p;
    return res;
}
int main(){
    prepare();
    ll n=read(0ll); int a=read(0),k=read(0);
    if (a==1){
        T[0]=n%p;
        for (int i=1;i<=k;i++){
            int res=0;
            for (int j=0;j

你可能感兴趣的:([洛谷4948]数列求和)