杜教BM【转载】

  1 #include <bits/stdc++.h>
  2  
  3 using namespace std;
  4 #define rep(i,a,n) for (long long i=a;i<n;i++)
  5 #define per(i,a,n) for (long long i=n-1;i>=a;i--)
  6 #define pb push_back
  7 #define mp make_pair
  8 #define all(x) (x).begin(),(x).end()
  9 #define fi first
 10 #define se second
 11 #define SZ(x) ((long long)(x).size())
 12 typedef vector<long long> VI;
 13 typedef long long ll;
 14 typedef pair<long long,long long> PII;
 15 const ll mod=1e9+7;
 16 ll powmod(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;}
 17 // head
 18  
 19 long long _,n;
 20 namespace linear_seq
 21 {
 22     const long long N=10010;
 23     ll res[N],base[N],_c[N],_md[N];
 24  
 25     vector<long long> Md;
 26     void mul(ll *a,ll *b,long long k)
 27     {
 28         rep(i,0,k+k) _c[i]=0;
 29         rep(i,0,k) if (a[i]) rep(j,0,k)
 30             _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
 31         for (long long i=k+k-1;i>=k;i--) if (_c[i])
 32             rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
 33         rep(i,0,k) a[i]=_c[i];
 34     }
 35     long long solve(ll n,VI a,VI b)
 36     { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
 37 //        printf("%d\n",SZ(b));
 38         ll ans=0,pnt=0;
 39         long long k=SZ(a);
 40         assert(SZ(a)==SZ(b));
 41         rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
 42         Md.clear();
 43         rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
 44         rep(i,0,k) res[i]=base[i]=0;
 45         res[0]=1;
 46         while ((1ll<<pnt)<=n) pnt++;
 47         for (long long p=pnt;p>=0;p--)
 48         {
 49             mul(res,res,k);
 50             if ((n>>p)&1)
 51             {
 52                 for (long long i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
 53                 rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
 54             }
 55         }
 56         rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
 57         if (ans<0) ans+=mod;
 58         return ans;
 59     }
 60     VI BM(VI s)
 61     {
 62         VI C(1,1),B(1,1);
 63         long long L=0,m=1,b=1;
 64         rep(n,0,SZ(s))
 65         {
 66             ll d=0;
 67             rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
 68             if (d==0) ++m;
 69             else if (2*L<=n)
 70             {
 71                 VI T=C;
 72                 ll c=mod-d*powmod(b,mod-2)%mod;
 73                 while (SZ(C)<SZ(B)+m) C.pb(0);
 74                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
 75                 L=n+1-L; B=T; b=d; m=1;
 76             }
 77             else
 78             {
 79                 ll c=mod-d*powmod(b,mod-2)%mod;
 80                 while (SZ(C)<SZ(B)+m) C.pb(0);
 81                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
 82                 ++m;
 83             }
 84         }
 85         return C;
 86     }
 87     long long gao(VI a,ll n)
 88     {
 89         VI c=BM(a);
 90         c.erase(c.begin());
 91         rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
 92         return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
 93     }
 94 };
 95  
 96 int main()
 97 {
 98     while(~scanf("%I64d", &n))
 99     {   printf("%I64d\n",linear_seq::gao(VI{1,5,11,36,95,281,781,2245,6336,18061, 51205},n-1));
100     }
101 }

 

posted @ 2018-09-16 09:07  MekakuCityActor  阅读(283)  评论(0编辑  收藏  举报