跳到正文

剪贴板

剪贴板 9am7xv6u

原文
剪贴板信息

剪贴板信息

收录
更新
C++
namespace hezlik{

  template<class T,
           class container_T=std::vector<T>>
  class mod_polynomial
  :public container_T{

  public:
    using type=T;
    using container_type=container_T;

    using size_type=std::size_t;
    using difference_type=std::ptrdiff_t;

  private:
    using poly=mod_polynomial;
    using _init_list=std::initializer_list<type>;

  public:
    using container_type::container_type;
    using container_type::size;
    using container_type::empty;
    using container_type::resize;
    using container_type::front;
    using container_type::back;
    using container_type::begin;
    using container_type::end;
    using container_type::rbegin;
    using container_type::rend;
    using container_type::push_back;
    using container_type::pop_back;
    using container_type::insert;
    using container_type::erase;

    mod_polynomial(const container_type &other):container_type(other){}
    mod_polynomial(container_type &&other):container_type(other){}

    poly &operator = (const container_type &other){return *this=poly(other);}
    poly &operator = (container_type &&other){return *this=poly(other);}

    poly &operator = (const _init_list init){return *this=poly(init);}

    poly &operator = (const type &other){return *this=poly({other});}
    poly &operator = (type &&other){return *this=poly({other});}

    void resize(){for (;!empty()&&!back();pop_back());}

    mod_polynomial(size_type n,const poly &other):container_type(other){resize(n);}
    mod_polynomial(size_type n,poly &&other):container_type(other){resize(n);}

    mod_polynomial(size_type n,const container_type &other):container_type(other){resize(n);}
    mod_polynomial(size_type n,container_type &&other):container_type(other){resize(n);}

    mod_polynomial(size_type n,const _init_list init):container_type(init){resize(n);}

    explicit operator container_type ()const{return *this;}
    container_type &container(){return *this;}
    const container_type &container()const{return *this;}

    void swap(poly &other){container_type::swap(other);}

    type operator () (const type &x)const{
      type res=0;
      for (auto it=rbegin();it!=rend();++it)
        res=res*x+*it;
      return res;
    }

    friend bool operator == (const poly &a,const poly &b){
      for (size_type i=0;i<a.size()&&i<b.size();++i)
        if (a[i]!=b[i]) return 0;
      for (size_type i=a.size();i<b.size();++i)
        if (b[i]) return 0;
      for (size_type i=b.size();i<a.size();++i)
        if (a[i]) return 0;
      return 1;
    }

    friend bool operator == (const poly &a,const type &b){return a==poly({b});}
    friend bool operator == (const type &a,const poly &b){return poly({a})==b;}
    friend bool operator != (const poly &a,const poly &b){return !(a==b);}
    friend bool operator != (const poly &a,const type &b){return a!=poly({b});}
    friend bool operator != (const type &a,const poly &b){return poly({a})!=b;}

    explicit operator bool ()const{return *this!=poly();}
    bool operator ! ()const{return *this==poly();}

    poly operator + ()const{return *this;}

    poly &operator += (const poly &b){
      poly &a=*this;
      if (a.size()<b.size()) a.resize(b.size());
      for (size_type i=0;i<b.size();++i) a[i]+=b[i];
      return a;
    }

    poly &operator += (const type &b){
      poly &a=*this;
      if (a.empty()) a.resize(1);
      a[0]+=b;
      return a;
    }

    poly &operator ++ (){return *this+=1;}
    poly operator ++ (int){poly res=*this;*this+=1;return res;}
    friend poly operator + (const poly &a,const poly &b){return poly(a)+=b;}
    friend poly operator + (const poly &a,const type &b){return poly(a)+=b;}
    friend poly operator + (const type &a,const poly &b){return poly(b)+=a;}

    poly operator - ()const{
      poly res=*this;
      for (type &x:res) x=-x;
      return res;
    }

    poly &operator -= (const poly &b){
      poly &a=*this;
      if (a.size()<b.size()) a.resize(b.size());
      for (size_type i=0;i<b.size();++i) a[i]-=b[i];
      return a;
    }

    poly &operator -= (const type &b){
      poly &a=*this;
      if (a.empty()) a.resize(1);
      a[0]-=b;
      return a;
    }

    poly &operator -- (){return *this-=1;}
    poly operator -- (int){poly res=*this;*this-=1;return res;}
    friend poly operator - (const poly &a,const poly &b){return poly(a)-=b;}
    friend poly operator - (const poly &a,const type &b){return poly(a)-=b;}
    friend poly operator - (const type &a,const poly &b){return poly({a})-=b;}

    poly &operator *= (const type &b){
      for (type &x:*this) x*=b;
      return *this;
    }

    friend poly operator * (const poly &a,const type &b){return poly(a)*=b;}
    friend poly operator * (const type &a,const poly &b){return poly(b)*=a;}

    poly &operator /= (const type &b){
      type t=b.inv();
      for (type &x:*this) x*=t;
      return *this;
    }

    friend poly operator / (const poly &a,const type &b){return poly(a)/=b;}

    poly &operator >>= (size_type n){erase(begin(),begin()+n);return *this;}
    poly &operator <<= (size_type n){insert(begin(),n,0);return *this;}
    poly operator >> (size_type n)const{return poly(*this)>>=n;}
    poly operator << (size_type n)const{return poly(*this)<<=n;}

    friend poly operator * (const poly &a,const poly &b){
      size_type n=a.size()+b.size();
      if (n) --n;
      poly res(n);
      for (size_type i=0;i<a.size();++i)
        for (size_type j=0;j<b.size();++j)
          res[i+j]+=a[i]*b[j];
      return res;
    }

    poly &operator *= (const poly &b){return *this=*this*b;}

    static std::pair<poly,poly> div(poly a,poly b){
      b.resize();
      size_type n=a.size(),m=b.size();
      if (n<m) return make_pair(poly(),poly(m-1,a));
      type t=b.back().inv();
      b*=t;
      poly res(n-m+1);
      for (size_type i=n;i>=m;--i){
        res[i-m]=a[i-1];
        for (size_type j=0;j<m;++j)
          a[i-m+j]-=a[i-1]*b[j];
      }
      a.resize();
      return make_pair(res*t,a);
    }

    friend poly operator / (const poly &a,const poly &b){return div(a,b).first;}
    poly &operator /= (const poly &b){return *this=div(*this,b).first;}
    friend poly operator % (const poly &a,const poly &b){return div(a,b).second;}
    poly &operator %= (const poly &b){return *this=div(*this,b).second;}

    poly inv(size_type n)const{
      if (!n) return poly();
      const poly &a=*this;
      poly res(n);
      res[0]=a[0].inv();
      for (size_type i=1;i<n;++i){
        for (size_type j=1;j<a.size()&&j<=i;++j)
          res[i]-=a[j]*res[i-j];
        res[i]*=res[0];
      }
      return res;
    }

    poly inv()const{return inv(size());}

  protected:
    class pretreatment{

    public:
      std::vector<type> inv;

      pretreatment()=default;

      void resize_inv(size_type n){
        if (n<=inv.size()) return;
        size_type m=inv.size()+inv.empty();
        inv.resize(n);
        for (size_type i=m;i<n;++i)
          inv[i]=i==1?1:(type::umod()-type::umod()/i)*inv[type::umod()%i];
      }

    };
    static pretreatment pre;

  public:
    template<class index_type>
    poly _pow(const index_type &_k,size_type n)const{
      const poly &a=*this;
      type k=type(_k%type::umod());
      poly res(n);
      pre.resize_inv(n);
      res[0]=a[0].pow(_k);
      type t=a[0].inv();
      for (size_type i=1;i<n;++i){
        for (size_type j=1;j<a.size()&&j<i;++j)
          res[i]+=a[j]*res[i-j]*(k*j-i+j);
        res[i]*=pre.inv[i];
        if (i<a.size()) res[i]+=k*res[0]*a[i];
        res[i]*=t;
      }
      return res;
    }

    template<class index_type>
    poly pow(const index_type &k,size_type n)const{
      if (!n) return poly();
      if (k==0) return poly(n,{1});
      if (k==1) return poly(n,*this);
      const poly &a=*this;
      size_type m=0;
      for (;m<a.size()&&m<n&&!a[m];++m);
      if (m>=a.size()||m>=n||m&&k>=n/m) return poly(n);
      return (a>>m)._pow(k,n-m*k)<<m*k;
    }

    template<class index_type>
    poly pow(index_type k)const{return pow(k,size());}

    poly de(size_type n)const{
      const poly &a=*this;
      poly res(n);
      for (size_type i=0;i<n;++i)
        res[i]=i+1<size()?a[i+1]*(i+1):0;
      return res;
    }

    poly de()const{return empty()?poly():de(size()-1);}

    poly in(size_type n)const{
      const poly &a=*this;
      poly res(n);
      pre.resize_inv(std::min(n,size()+1));
      for (size_type i=1;i<n;++i)
        res[i]=i<=size()?a[i-1]*pre.inv[i]:0;
      return res;
    }

    poly in()const{return in(size()+1);}

    type slope(const type &x)const{return de()(x);}

    type area(const type &l,const type &r)const{
      poly t=in();
      return t(r)-t(l);
    }

    poly ln(size_type n)const{
      const poly &a=*this;
      poly res(n);
      pre.resize_inv(n);
      res[0]=0;
      for (size_type i=1;i<n;++i){
        for (size_type j=1;j<a.size()&&j<i;++j)
          res[i]+=a[j]*(i-j)*res[i-j];
        res[i]=a[i]-pre.inv[i]*res[i];
      }
      return res;
    }

    poly ln()const{return ln(size());}

    poly exp(size_type n)const{
      const poly &a=*this;
      poly res(n);
      pre.resize_inv(n);
      res[0]=1;
      for (size_type i=1;i<n;++i){
        for (size_type j=1;j<a.size()&&j<=i;++j)
          res[i]+=j*a[j]*res[i-j];
        res[i]*=pre.inv[i];
      }
      return res;
    }

    poly exp()const{return exp(size());}

  };
  template<class type,class container>
  typename mod_polynomial<type,container>::pretreatment
  mod_polynomial<type,container>::pre;

}

namespace std{

  template<class type,class container>
  void swap(hezlik::mod_polynomial<type,container> &a,
            hezlik::mod_polynomial<type,container> &b){a.swap(b);}

}