[51nod1236] 序列求和 V3(斐波那契数列)
题面
题解
把求和的柿子用斐波那契数列的通项公式展开
\[\begin{aligned}
Ans
&=\sum\limits_{i = 1}^{n} \left(\frac{(\frac{1 + \sqrt{5}}{2})^{i} - (\frac{1 - \sqrt{5}}{2})^{i}}{\sqrt{5}}\right)^{k} \\
&= \left(\frac{1}{\sqrt{5}}\right)^{k}\sum\limits_{i = 1}^{n} \sum\limits_{j = 0}^{k}{k \choose j}(-1)^{k - j}\left(\frac{1 + \sqrt{5}}{2}\right)^{ij}\left(\frac{1 - \sqrt{5}}{2}\right)^{i(k - j)} \\
&= \left(\frac{1}{\sqrt{5}}\right)^{k}\sum\limits_{j = 0}^{k}{k \choose j}(-1)^{k - j}\sum\limits_{i = 1}^{n} \left(\left(\frac{1 + \sqrt{5}}{2}\right)^{j}\left(\frac{1 - \sqrt{5}}{2}\right)^{k - j}\right)^{i}\\
\end{aligned}
\]
后面就是个等比数列求和公式,直接算就行了
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
const int N=1e5+5,P=1e9+9,s=383008016;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R ll y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x);
return res;
}
int inv[N],fac[N],ifac[N],v1[N],v2[N],k,res,p,q;ll n;
inline int C(R int n,R int m){return m>n?0:1ll*fac[n]*ifac[m]%P*ifac[n-m]%P;}
inline int Inv(R int n){return n<N?inv[n]:mul(P-P/n,Inv(P%n));}
void init(int n){
v1[0]=v2[0]=inv[0]=inv[1]=ifac[0]=ifac[1]=fac[0]=fac[1]=1;
fp(i,2,n){
fac[i]=mul(fac[i-1],i),
inv[i]=mul(P-P/i,inv[P%i]),
ifac[i]=mul(ifac[i-1],inv[i]);
}
v1[1]=mul(s+1,inv[2]),v2[1]=mul(P+1-s,inv[2]);
fp(i,2,n)v1[i]=mul(v1[i-1],v1[1]),v2[i]=mul(v2[i-1],v2[1]);
}
int main(){
// freopen("testdata.in","r",stdin);
init(N-1);
int T;scanf("%d",&T);
while(T--){
scanf("%lld%d",&n,&k),res=0;
fp(j,0,k){
p=mul(v1[j],v2[k-j]),
q=(p==1)?n%P:mul(dec(ksm(p,n+1),p),Inv(p-1)),
q=mul(q,C(k,j)),
res=add(res,(k-j)&1?P-q:q);
// printf("%d %d\n",p,q);
}
printf("%d\n",mul(res,ksm(s,1ll*k*(P-2))));
}
return 0;
}
深深地明白自己的弱小