library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/yukicoder/0104.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://yukicoder.me/problems/104

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../mod/mint.cpp"
#include "../../polynomial/berlekampmassey.cpp"
#include "../../convolution/naive.cpp"
#include "../../math/bostanmori.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  long long n;
  int p,c;
  cin>>n>>p>>c;

  using M = Mint<int>;
  using Poly = vector<M>;

  const int d = 1500;
  const int MAX = p+c+1;
  vector<Poly> cf(MAX,Poly(d,0));
  cf[0][0]=M(1);

  for(int v:{2,3,5,7,11,13}){
    vector<Poly> nx(MAX,Poly(d,0));
    for(int t=0;t<=p;t++)
      for(int i=0;i<d;i++)
        for(int j=0;t+j<=p&&i+v*j<d;j++)
          nx[t+j][i+v*j]+=cf[t][i];
    swap(cf,nx);
  }

  for(int v:{4,6,8,9,10,12}){
    vector<Poly> nx(MAX,Poly(d,0));
    for(int t=p;t<=p+c;t++)
      for(int i=0;i<d;i++)
        for(int j=0;t+j<=p+c&&i+v*j<d;j++)
          nx[t+j][i+v*j]+=cf[t][i];
    swap(cf,nx);
  }

  Poly dp(d*3,0),as(d*3,0);
  dp[0]=M(1);
  for(int i=0;i<(int)dp.size();i++){
    for(int j=0;j<d&&i+j<(int)dp.size();j++)
      dp[i+j]+=dp[i]*cf[p+c][j];

    for(int j=1;i+j<(int)dp.size();j++)
      as[i]+=dp[i+j];
  }
  as.resize(d*2);

  BostanMori<M> bm(naive<M>());
  cout<<bm.build(n-1,as,berlekamp_massey(as))<<endl;
  return 0;
}
#line 1 "test/yukicoder/0104.test.cpp"
// verification-helper: PROBLEM https://yukicoder.me/problems/104

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "mod/mint.cpp"

#line 3 "mod/mint.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename T, T MOD = 1000000007>
struct Mint{
  inline static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator+() const{return *this;}
  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "polynomial/berlekampmassey.cpp"

#line 3 "polynomial/berlekampmassey.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// construct a charasteristic equation from sequence
// return a monic polynomial in O(n^2)
template<typename T>
vector<T> berlekamp_massey(vector<T> &as){
  using Poly = vector<T>;
  int n=as.size();
  Poly bs({-T(1)}),cs({-T(1)});
  T y(1);
  for(int ed=1;ed<=n;ed++){
    int l=cs.size(),m=bs.size();
    T x(0);
    for(int i=0;i<l;i++) x+=cs[i]*as[ed-l+i];
    bs.emplace_back(0);
    m++;
    if(x==T(0)) continue;
    T freq=x/y;
    if(m<=l){
      for(int i=0;i<m;i++)
        cs[l-1-i]-=freq*bs[m-1-i];
      continue;
    }
    auto ts=cs;
    cs.insert(cs.begin(),m-l,T(0));
    for(int i=0;i<m;i++) cs[m-1-i]-=freq*bs[m-1-i];
    bs=ts;
    y=x;
  }
  for(auto &c:cs) c/=cs.back();
  return cs;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "convolution/naive.cpp"

#line 3 "convolution/naive.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// O(N M)
template<typename T>
decltype(auto) naive(){
  using Poly = vector<T>;
  auto conv=[](Poly as, Poly bs){
    Poly cs(as.size()+bs.size()-1,0);
    for(int i=0;i<(int)as.size();i++)
      for(int j=0;j<(int)bs.size();j++)
        cs[i+j]+=as[i]*bs[j];
    return cs;
  };
  return +conv;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "math/bostanmori.cpp"

#line 3 "math/bostanmori.cpp"
using namespace std;
#endif
// ref. https://qiita.com/ryuhe1/items/da5acbcce4ac1911f47a
//BEGIN CUT HERE
// Find k-th term of linear recurrence
// execute `conv` O(\log k) times
template<typename T>
struct BostanMori{
  using Poly = vector<T>;
  using Conv = function<Poly(Poly, Poly)>;

  Conv conv;
  BostanMori(Conv conv_):conv(conv_){}

  Poly sub(Poly as,int odd){
    Poly bs((as.size()+!odd)/2);
    for(int i=odd;i<(int)as.size();i+=2) bs[i/2]=as[i];
    return bs;
  }

  // as: initial values
  // cs: monic polynomial
  T build(long long k,Poly as,Poly cs){
    reverse(cs.begin(),cs.end());
    assert(cs[0]==T(1));
    int n=cs.size()-1;
    as.resize(n,0);
    Poly bs=conv(as,cs);
    bs.resize(n);
    while(k){
      Poly ds(cs);
      for(int i=1;i<(int)ds.size();i+=2) ds[i]=-ds[i];
      bs=sub(conv(bs,ds),k&1);
      cs=sub(conv(cs,ds),0);
      k>>=1;
    }
    return bs[0];
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 11 "test/yukicoder/0104.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  long long n;
  int p,c;
  cin>>n>>p>>c;

  using M = Mint<int>;
  using Poly = vector<M>;

  const int d = 1500;
  const int MAX = p+c+1;
  vector<Poly> cf(MAX,Poly(d,0));
  cf[0][0]=M(1);

  for(int v:{2,3,5,7,11,13}){
    vector<Poly> nx(MAX,Poly(d,0));
    for(int t=0;t<=p;t++)
      for(int i=0;i<d;i++)
        for(int j=0;t+j<=p&&i+v*j<d;j++)
          nx[t+j][i+v*j]+=cf[t][i];
    swap(cf,nx);
  }

  for(int v:{4,6,8,9,10,12}){
    vector<Poly> nx(MAX,Poly(d,0));
    for(int t=p;t<=p+c;t++)
      for(int i=0;i<d;i++)
        for(int j=0;t+j<=p+c&&i+v*j<d;j++)
          nx[t+j][i+v*j]+=cf[t][i];
    swap(cf,nx);
  }

  Poly dp(d*3,0),as(d*3,0);
  dp[0]=M(1);
  for(int i=0;i<(int)dp.size();i++){
    for(int j=0;j<d&&i+j<(int)dp.size();j++)
      dp[i+j]+=dp[i]*cf[p+c][j];

    for(int j=1;i+j<(int)dp.size();j++)
      as[i]+=dp[i+j];
  }
  as.resize(d*2);

  BostanMori<M> bm(naive<M>());
  cout<<bm.build(n-1,as,berlekamp_massey(as))<<endl;
  return 0;
}
Back to top page