# 51nod1236 序列求和 V3

1. 斐波那契数列通项公式

2. 等比数列求和公式

3. 二项式定理

\begin{aligned}ANS=& \sum_{i=1}^n Fib(i)^k\\=& \sum_{i=1}^{n} \Big({ {(1+\sqrt 5 )^i\over2 } -{(1-\sqrt 5)^i \over 2} \over \sqrt 5} \Big)^k \\=& \big({1\over \sqrt 5}\big)^k \sum_{i=1}^{n}\sum_{j=0}^k (-1)^{k-j} \begin{pmatrix} k\\j \end{pmatrix}\Big( {1+\sqrt 5 \over2 }\Big)^{ij} \Big( {1-\sqrt 5 \over2 }\Big)^{i(k-j)} \\=& \big({1\over \sqrt 5}\big)^k\sum_{j=0}^k (-1)^{k-j} \begin{pmatrix} k\\j \end{pmatrix} \sum_{i=1}^{n} \Big( \big( {1+\sqrt 5 \over2 }\big)^j \big( {1-\sqrt 5 \over2 }\big)^{k-j} \Big)^i \end{aligned}

# code

//by Judge
#include<cstdio>
#include<cstring>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define ll long long
using namespace std;
const int s5=383008016;
const int mod=1e9+9;
const int M=1e5+3;
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int MO(ll x){return x-x/mod*mod;}
inline int mul(int x,int y){return MO(1ll*x*y);}
inline int dec(int x,int y){return x<y?x-y+mod:x-y;}
inline int inc(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline ll read(){ ll x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} char sr[1<<21],z[20];int CCF=-1,Z;
inline void Ot(){fwrite(sr,1,CCF+1,stdout),CCF=-1;}
inline void print(int x,char chr='\n'){
if(CCF>1<<20)Ot();if(x<0)sr[++CCF]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++CCF]=z[Z],--Z);sr[++CCF]=chr;
} ll n; int k,t,MX=1e5,tmp,ans,inv[1000003]; arr fac,ifac,v1,v2;
inline void prep(Rg int n){
v1[0]=v2[0]=fac[0]=fac[1]=inv[0]=inv[1]=ifac[0]=ifac[1]=1;
fp(i,2,n) inv[i]=mul(mod-mod/i,inv[mod%i]);
fp(i,2,n) fac[i]=mul(fac[i-1],i),ifac[i]=mul(ifac[i-1],inv[i]);
v1[1]=mul(s5+1,inv[2]),v2[1]=mul(mod+1-s5,inv[2]);
fp(i,2,n) v1[i]=mul(v1[i-1],v1[1]),v2[i]=mul(v2[i-1],v2[1]);
fp(i,n+1,MX=5e5) inv[i]=mul(mod-mod/i,inv[mod%i]);
}
inline int qpow(int x,ll p=mod-2,int s=1){
for(;p;p>>=1,x=mul(x,x)) if(p&1) s=mul(s,x); return s;
}
inline int Inv(int x){return x<=MX?inv[x]:mul(mod-mod/x,Inv(mod%x));}
inline int C(int n,int m){return mul(fac[n],mul(ifac[m],ifac[n-m]));}
int main(){ prep(MX); int T=read(); ll t,tmp,ans,n,k;
for(;T;--T){ n=read(),k=read(),ans=0;
fp(j,0,k){ t=mul(v1[j],v2[k-j]),
tmp=t==1?n%mod:mul(dec(qpow(t,n+1),t),Inv(t-1));
tmp=mul(tmp,C(k,j)),ans=(k^j)&1?dec(ans,tmp):inc(ans,tmp);
} print(mul(ans,qpow(s5,(1ll*k*(mod-2)%(mod-1)+mod-1))));
} return Ot(),0;
}

10-18 34
03-23 142
01-11 391
12-06 219
10-05 326
08-06 46
09-19 122
04-23 163
10-20 113
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客