Advertisement
yicongli

T165T2-BM

Mar 31st, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     T k=1;char gc;x=0;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c))x=x*10+c-'0',gc;x*=k;
  14. }
  15.  
  16. const int p=1e9+7;
  17. const int N=1e6+7;
  18.  
  19. inline int add(int a,int b){
  20.     return (a+=b)>=p?a-p:a;
  21. }
  22.  
  23. inline int sub(int a,int b){
  24.     return (a-=b)<0?a+p:a;
  25. }
  26.  
  27. inline ll qpow(ll a,ll b){
  28.     ll ans=1;
  29.     for(;b;b>>=1,(a*=a)%=p)if(b&1)(ans*=a)%=p;
  30.     return ans;
  31. }
  32.  
  33. namespace BerlekampMassey{
  34.  
  35.     typedef vector<int> poly;
  36.    
  37.     #define len(A) (int)(A.size())
  38.    
  39.     inline poly mul(const poly &A,const poly&B,const poly&F){
  40.         poly ret(len(F)*2-1);
  41.         for(int i=0;i<len(F);++i){
  42.             for(int j=0;j<len(F);++j){
  43.                 ret[i+j]=add(ret[i+j],(ll)A[i]*B[j]%p);
  44.             }
  45.         }
  46.         for(int i=len(F)*2-2;i>=len(F);--i){
  47.             for(int j=0;j<len(F);++j){
  48.                 ret[i-j-1]=add(ret[i-j-1],(ll)ret[i]*F[j]%p);
  49.             }
  50.         }
  51.         ret.resize(len(F));
  52.         return ret;
  53.     }
  54.    
  55.     inline int solve(const poly &A,const poly &F,ll n){
  56.         if(n<len(A))return A[n];
  57.         poly base(len(F)),ans(len(F));
  58.         base[1]=ans[0]=1;
  59.         for(;n;n>>=1){
  60.             if(n&1)ans=mul(ans,base,F);
  61.             base=mul(base,base,F);
  62.         }
  63.         int ret=0;
  64.         for(int i=0;i<len(F);++i)ret=add(ret,(ll)A[i]*ans[i]%p);
  65.         return ret;
  66.     }
  67.    
  68.     inline poly BM(const poly& A){
  69.         poly F,F0;
  70.         for(int i=0,d,d0,p0;i<len(A);++i){
  71.             d=0;
  72.             for(int j=0;j<len(F);++j){
  73.                 d=add(d,(ll)F[j]*A[i-j-1]%p);
  74.             }
  75.             d=sub(d,A[i]);
  76.             if(!d)continue;
  77.             if(!len(F)){
  78.                 F.resize(i+1);
  79.                 d0=d;
  80.                 p0=i;
  81.                 continue;
  82.             }
  83.             poly G(i-p0-1);
  84.             ll t=qpow(d0,p-2)*d%p;
  85.             G.push_back(t);
  86.             t=sub(0,t);
  87.             for(int j=0;j<len(F0);++j){
  88.                 G.push_back(t*F0[j]%p);
  89.             }
  90.             if(len(G)<len(F))G.resize(len(F));
  91.             for(int j=0;j<len(F);++j){
  92.                 G[j]=add(G[j],F[j]);
  93.             }
  94.             if(len(F0)-p0>=len(F)-i){
  95.                 F0=F;
  96.                 d0=d;
  97.                 p0=i;
  98.             }
  99.             F=G;
  100.         }
  101.         // assert(len(F)<=20);
  102.         return F;
  103.     }
  104.    
  105.     inline int main(const poly &A,ll n){
  106.         return solve(A,BM(A),n);
  107.     }
  108. }
  109.  
  110. int f[2][N];
  111. vector<int>G;
  112.  
  113. int main(){
  114.     freopen("string.in","r",stdin);
  115.     freopen("string.out","w",stdout);
  116.     int n;r(n);
  117.     ll m;r(m);
  118.     int x=n/2;
  119.     for(int i=1;i<=n;++i){
  120.         f[1][i]=1;
  121.     }
  122.     for(int i=2;i<=50;++i){
  123.         int sum=0;
  124.         for(int k=x+1;k<=n;++k){
  125.             sum=add(sum,f[i&1^1][k]);
  126.         }
  127.         G.push_back(sum);
  128.         for(int j=1,k=1;j<=n;++j){
  129.             while(2*k<=j)sum=add(sum,f[i&1^1][k]),++k;
  130.             f[i&1][j]=sum;
  131.         }
  132.     }
  133.     printf("%d\n",BerlekampMassey::main(G,m-1));
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement