BM算法
不知道原理..
听说扔一定的前n项,然后就可以得到答案了.
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1000000007; ll pwr(ll a,ll b) { ll res=1; a%=mod; assert(b>=0); for( ; b; b>>=1) { if (b & 1) res=res*a%mod; a=a*a%mod; } return res; } ll n; namespace linear_seq { const ll N=10010; ll res[N],base[N],_c[N],_md[N]; vector<int> Md; void mul(ll *a,ll *b,int k) { int i, j; for (i=0; i<k+k; ++i) _c[i]=0; for (i=0; i<k; ++i) if (a[i]) for (j=0; j<k; ++j) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod; for (i=k+k-1;i>=k;i--) if (_c[i]) for(j=0; j<Md.size(); ++j) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod; for(i=0; i<k; ++i) a[i]=_c[i]; } int solve(ll n,vector<ll> a,vector<ll> b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+... // printf("%d\n",SZ(b)); ll ans=0,pnt=0; int p, i, j, k = a.size(); assert(a.size()==b.size()); for(i=0; i<k; ++i) _md[k-1-i]=-a[i];_md[k]=1; Md.clear(); for(i=0; i<k; ++i) if (_md[i]!=0) Md.push_back(i); for(i=0; i<k; ++i) res[i]=base[i]=0; res[0] = 1; while ( (1ll<<pnt) <= n) pnt++; for (p=pnt;p>=0;p--) { mul(res,res,k); if ((n>>p)&1) { for (i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0; for (j=0; j<Md.size(); ++j) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod; } } for(i=0; i<k; ++i) ans=(ans+res[i]*b[i])%mod; if (ans<0) ans+=mod; return ans; } vector<ll> BM(vector<ll> s) { vector <ll> C(1,1),B(1,1); int L = 0, m = 1, b = 1; int i, n; for (n=0; n<s.size(); ++n) { ll d=0; for(i=0; i<L+1; ++i) d=(d+(ll)C[i]*s[n-i])%mod; if (d==0) ++m; else if (2*L<=n) { vector<ll> T=C; ll c=mod-d*pwr(b,mod-2)%mod; while (C.size()<B.size()+m) C.push_back(0); for(i=0; i<B.size(); ++i) C[i+m]=(C[i+m]+c*B[i])%mod; L=n+1-L; B=T; b=d; m=1; } else { ll c=mod-d*pwr(b,mod-2)%mod; while (C.size()<B.size()+m) C.push_back(0); for(i=0; i<B.size(); ++i) C[i+m]=(C[i+m]+c*B[i])%mod; ++m; } } return C; } ll gao(vector<ll> a,ll n) { vector<ll> c=BM(a); c.erase(c.begin()); int i; for(i=0; i<c.size(); ++i) c[i]=(mod-c[i])%mod; return solve(n,c,vector<ll>(a.begin(),a.begin()+c.size())); } }; int main() { while (~scanf("%lld",&n)) { vector<ll>v; v.push_back(1); v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(5); v.push_back(8); //VI{1,2,4,7,13,24} printf("%lld\n",linear_seq::gao(v,n-1)); } }