Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define gc c=getchar()
- #define r(x) read(x)
- #define ll long long
- template<typename T>
- inline void read(T&x){
- T k=1;char gc;x=0;
- while(!isdigit(c)){if(c=='-')k=-1;gc;}
- while(isdigit(c))x=x*10+c-'0',gc;x*=k;
- }
- const int p=1e9+7;
- const int N=1e6+7;
- inline int add(int a,int b){
- return (a+=b)>=p?a-p:a;
- }
- inline int sub(int a,int b){
- return (a-=b)<0?a+p:a;
- }
- inline ll qpow(ll a,ll b){
- ll ans=1;
- for(;b;b>>=1,(a*=a)%=p)if(b&1)(ans*=a)%=p;
- return ans;
- }
- namespace BerlekampMassey{
- typedef vector<int> poly;
- #define len(A) (int)(A.size())
- inline poly mul(const poly &A,const poly&B,const poly&F){
- poly ret(len(F)*2-1);
- for(int i=0;i<len(F);++i){
- for(int j=0;j<len(F);++j){
- ret[i+j]=add(ret[i+j],(ll)A[i]*B[j]%p);
- }
- }
- for(int i=len(F)*2-2;i>=len(F);--i){
- for(int j=0;j<len(F);++j){
- ret[i-j-1]=add(ret[i-j-1],(ll)ret[i]*F[j]%p);
- }
- }
- ret.resize(len(F));
- return ret;
- }
- inline int solve(const poly &A,const poly &F,ll n){
- if(n<len(A))return A[n];
- poly base(len(F)),ans(len(F));
- base[1]=ans[0]=1;
- for(;n;n>>=1){
- if(n&1)ans=mul(ans,base,F);
- base=mul(base,base,F);
- }
- int ret=0;
- for(int i=0;i<len(F);++i)ret=add(ret,(ll)A[i]*ans[i]%p);
- return ret;
- }
- inline poly BM(const poly& A){
- poly F,F0;
- for(int i=0,d,d0,p0;i<len(A);++i){
- d=0;
- for(int j=0;j<len(F);++j){
- d=add(d,(ll)F[j]*A[i-j-1]%p);
- }
- d=sub(d,A[i]);
- if(!d)continue;
- if(!len(F)){
- F.resize(i+1);
- d0=d;
- p0=i;
- continue;
- }
- poly G(i-p0-1);
- ll t=qpow(d0,p-2)*d%p;
- G.push_back(t);
- t=sub(0,t);
- for(int j=0;j<len(F0);++j){
- G.push_back(t*F0[j]%p);
- }
- if(len(G)<len(F))G.resize(len(F));
- for(int j=0;j<len(F);++j){
- G[j]=add(G[j],F[j]);
- }
- if(len(F0)-p0>=len(F)-i){
- F0=F;
- d0=d;
- p0=i;
- }
- F=G;
- }
- // assert(len(F)<=20);
- return F;
- }
- inline int main(const poly &A,ll n){
- return solve(A,BM(A),n);
- }
- }
- int f[2][N];
- vector<int>G;
- int main(){
- freopen("string.in","r",stdin);
- freopen("string.out","w",stdout);
- int n;r(n);
- ll m;r(m);
- int x=n/2;
- for(int i=1;i<=n;++i){
- f[1][i]=1;
- }
- for(int i=2;i<=50;++i){
- int sum=0;
- for(int k=x+1;k<=n;++k){
- sum=add(sum,f[i&1^1][k]);
- }
- G.push_back(sum);
- for(int j=1,k=1;j<=n;++j){
- while(2*k<=j)sum=add(sum,f[i&1^1][k]),++k;
- f[i&1][j]=sum;
- }
- }
- printf("%d\n",BerlekampMassey::main(G,m-1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement