Evaluate the K times and number of the first N in the Fibonacci series.
General Formula: F [N] = (1 + SQRT (5)/SQRT (5)-(1-sqrt (5)/SQRT (5 )) /SQRT (5 ).
It is a bit messy, but it can ensure that the final result is an integer. All root numbers can be converted into integers for modulo operation and inverse meta operation.
First, we solve the quadratic homogeneous equation, x ^ 2 = N (mod m). Obviously, when n = 5, X can be equivalent to 5.
Can be replaced later.
Then f [N] = (a ^ N + B ^ N) * C.
Here we use the binary extension to calculate each item separately. At the same time, we can find that the sum of each item is actually an equal-ratio series, so we can do it directly.
Summon code:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#define M 1000000009#define maxn 100100typedef 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=1; while (B){ if (B&1) tot=tot*A%C; A=A*A%C,B>>=1; } return tot;}twice power(twice T,ll y){ twice C(1,0); while (y){ if (y&1) C.mul(T); T.mul(T),y>>=1; } return C;}ll getroot(){ for (;;){ a=rand()%M; w=(a*a-5+M)%M; if (power(w,M/2,M)!=1) break; } return power(twice(a,1),(M+1)/2).A;}void _init(){ root=getroot(); A[0]=B[0]=1; for (int i=1; i<maxn; i++){ A[i]=(A[i-1]*i)%M; B[i]=power(A[i],M-2,M); } inv=B[2]; AAA=(1+root)*inv%M; BBB=(M+1-root)*inv%M;}int main(){ _init(); scanf("%d",&T); while (T--){ scanf("%lld%lld",&n,&k); ans=0; for (int i=0; i<=k; i++){ cur=A[k]*(B[i]*B[k-i]%M)%M; if (i&1) cur=M-cur; now=power(AAA,k-i,M)*power(BBB,i,M)%M; if (now>1) now=((power(now,n+1,M)-now)*power(now-1,M-2,M)%M+M)%M; else now=now*n%M; (ans+=cur*now)%=M; } ans=ans*power(root,k*(M-2),M)%M; printf("%d\n",(int)ans); } return 0;}