跳到正文

剪贴板

剪贴板 octv2xgb

原文
剪贴板信息

剪贴板信息

收录
更新
C++
namespace hezlik{

  template<class T,
           class container_T=std::vector<T>,
           class type_equal_T=std::equal_to<T>,
           class type_add_T=add_group<T>,
           class type_mul_T=mul_group<T>>
  class polynomial
  :public container_T{

  public:
    using type=T;
    using container_type=container_T;
    using type_equal_type=type_equal_T;
    using type_add_type=type_add_T;
    using type_mul_type=type_mul_T;

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

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

  public:
    static constexpr type_equal_type type_equal=type_equal_type();
    static constexpr type_add_type type_add=type_add_type();
    static constexpr type_mul_type type_mul=type_mul_type();

    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;

    explicit polynomial(size_type n):container_type(n,type_add()){}

    polynomial(const container_type &other):container_type(other){}
    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(size_type n){resize(n,type_add());}
    void resize(){for (;!empty()&&type_equal(back(),type_add());pop_back());}

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

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

    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=type_add();
      for (auto it=rbegin();it!=rend();++it)
        res=type_add(type_mul(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 (!type_equal(a[i],b[i])) return 0;
      for (size_type i=a.size();i<b.size();++i)
        if (!type_equal(b[i],type_add())) return 0;
      for (size_type i=b.size();i<a.size();++i)
        if (!type_equal(a[i],type_add())) 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]=type_add(a[i],b[i]);
      return a;
    }

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

    poly &operator ++ (){return *this+=type_mul();}
    poly operator ++ (int){poly res=*this;*this+=type_mul();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=type_add(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]=type_add(a[i],type_add(b[i]));
      return a;
    }

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

    poly &operator -- (){return *this-=type_mul();}
    poly operator -- (int){poly res=*this;*this-=type_mul();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=type_mul(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=type_mul(b);
      for (type &x:*this) x=type_mul(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,type_add());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]=type_add(res[i+j],type_mul(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){
      a.resize();
      b.resize();
      size_type n=a.size(),m=b.size();
      if (n<m) return std::make_pair(poly(),a);
      type t=type_mul(b.back());
      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]=type_add(a[i-m+j],type_add(type_mul(a[i-1],b[j])));
      }
      a.resize();
      return std::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;}

  };

}

namespace std{

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

}