ZOJ3774_Power of Fibonacci

时间:2023-03-08 21:53:51

求fibonacci数列前N个数的K次方和。

通项公式:F[n]=((1+sqrt(5))/sqrt(5)-(1-sqrt(5))/sqrt(5))/sqrt(5)。

有点乱,不过由于可以保证最后的结果是一个整数,所有所有的根号都可以化为整数进行取模和逆元运算。

首先解二次同余方程,X^2=n (mod M),显然,当n=5的时候,X就可以相当于5了。

后面的都可以替换掉。

然后就变成了 F[n]=(A^n+B^n)*C。

这里通过二项式展开,分别对每一项进行计算,同时又可以发现,每一项求和其实是一个等比数列,于是,就可以直接搞了。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define M 1000000009
#define maxn 100100
typedef long long ll;
using namespace std; ll w,a,m,root,inv;
ll A[maxn],B[maxn];
ll n,k,ans,cur,now;
ll AAA,BBB;
int T; struct twice{
ll A,B;
twice() {}
twice(ll AA,ll BB) { A=AA,B=BB; }
void mul(twice T){
ll aa=A*T.A+(B*T.B)%M*w,bb=A*T.B+B*T.A;
A=aa%M,B=bb%M;
}
}; ll power(ll A,ll B,ll C)
{
ll tot=;
while (B){
if (B&) tot=tot*A%C;
A=A*A%C,B>>=;
}
return tot;
} twice power(twice T,ll y)
{
twice C(,);
while (y){
if (y&) C.mul(T);
T.mul(T),y>>=;
}
return C;
} ll getroot()
{
for (;;){
a=rand()%M;
w=(a*a-+M)%M;
if (power(w,M/,M)!=) break;
}
return power(twice(a,),(M+)/).A;
} void _init()
{
root=getroot();
A[]=B[]=;
for (int i=; i<maxn; i++){
A[i]=(A[i-]*i)%M;
B[i]=power(A[i],M-,M);
}
inv=B[];
AAA=(+root)*inv%M;
BBB=(M+-root)*inv%M;
} int main()
{
_init();
scanf("%d",&T);
while (T--){
scanf("%lld%lld",&n,&k);
ans=;
for (int i=; i<=k; i++){
cur=A[k]*(B[i]*B[k-i]%M)%M;
if (i&) cur=M-cur;
now=power(AAA,k-i,M)*power(BBB,i,M)%M;
if (now>) now=((power(now,n+,M)-now)*power(now-,M-,M)%M+M)%M;
else now=now*n%M;
(ans+=cur*now)%=M;
}
ans=ans*power(root,k*(M-),M)%M;
printf("%d\n",(int)ans);
}
return ;
}