zoukankan      html  css  js  c++  java
  • [模板] 高精度运算

    使用vector作为基类,优点是空间动态分配,缺点是STL重度依赖,离开O2就很慢(除法未优化)

    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<vector>
    
    using namespace std;
    
    struct bign:vector<int>{
      bign(int n=0){
        push_back(n);
        check();
      }
      bign& check(){
        while(!empty()&&!back())pop_back();
        if(empty())return *this;
        for(unsigned int i=1;i<size();i++){
          (*this)[i]+=(*this)[i-1]/10;
          (*this)[i-1]%=10;
        }
        while(back()>=10){
          push_back(back()/10);
          (*this)[size()-2]%=10;
        }
        return *this;
      }
    };
    istream& operator>>(istream &is,bign &n){
      string s;is>>s;n.clear();
      for(int i=s.size()-1;i>=0;i--)n.push_back(s[i]-'0');
      return is;
    }
    ostream& operator<<(ostream &os,const bign &n){
      if(n.empty()) os<<0;
      for(int i=n.size()-1;i>=0;i--)os<<n[i];
      return os;
    }
    bool operator<(const bign &a,const bign &b){
      if(a.size()!=b.size()) return a.size()<b.size();
      for(int i=a.size()-1;i>=0;i--)
        if(a[i]!=b[i]) return a[i]<b[i];
      return 0;
    }
    bool operator>(const bign &a,const bign &b){return b<a;}
    bool operator<=(const bign &a,const bign &b){return !(a>b);}
    bool operator>=(const bign &a,const bign &b){return !(a<b);}
    bool operator==(const bign &a,const bign &b){return (a<=b)&&(b<=a);}
    bool operator!=(const bign &a,const bign &b){return !(a==b);}
    bign& operator+=(bign &a,const bign &b){
      if(a.size()<b.size())a.resize(b.size());
      for(unsigned int i=0;i!=b.size();i++)a[i]+=b[i];
      return a.check();
    }
    bign operator+(bign a,const bign &b){return a+=b;}
    bign& operator-=(bign &a,bign b){
      if(a<b) swap(a,b);
      for(unsigned int i=0;i!=b.size();a[i]-=b[i],i++){
        if(a[i]>=b[i])continue;
        int j=i+1;
        while(!a[j]) j++;
        while(j>i) --a[j],a[--j]+=10;
      }
      return a.check();
    }
    bign operator-(bign a,bign b){return a-=b;}//abs
    bign operator*(const bign &a,const bign &b){
      bign n;
      n.assign(a.size()+b.size()-1,0);
      for(unsigned int i=0;i!=a.size();i++)
        for(unsigned int j=0;j!=b.size();j++)
          n[i+j]+=a[i]*b[j];
      return n.check();
    }
    bign& operator*=(bign &a,bign &b){return a=a*b;}
    bign divmod(bign &a,const bign &b){
      bign ret,d,c;
      for(int t=a.size()-b.size();a>=b;--t){
        d.assign(t+1,0);d.back()=1;c=b*d;
        while(a>=c)a-=c,ret+=d;
      }
      return ret;
    }
    bign operator/(bign a,const bign &b){return divmod(a,b);}
    bign operator/=(bign &a,const bign &b){return a=a/b;}
    bign &operator%=(bign &a,const bign &b){divmod(a,b);return a;}
    bign operator%(bign a,const bign &b){return a%=b;}
    int main(){
      bign a,b;
      cin>>a>>b;
      cout<<a+b<<endl;
      if(a<b)cout<<"-";
      cout<<a-b<<endl;
      cout<<a*b<<endl;
      cout<<divmod(a,b)<<endl;
      cout<<a<<endl;
    }
    
  • 相关阅读:
    mybatis-Generator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap
    Linux 常用的命令
    Linux 查找文件
    eclipse tasks
    Myeclipse 文件注释和解注释
    proxool连接池 异常
    EL 表达式 函数 操作 字符串
    Myeclipse 选中高亮
    Spring 定时作业
    linux配置java环境变量(详细)
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9893482.html
Copyright © 2011-2022 走看看