zoukankan      html  css  js  c++  java
  • 高精度

    #include<map>
    #include<ctime>
    #include<cmath>
    #include<queue>
    #include<cctype>
    #include<vector>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    #define ll long long
    const int base=10000000;
    struct big{
        ll num[1005];
        big(){memset(num,0,sizeof(num));num[0]=1;}
        void zero(){while(!num[num[0]]&&num[0]>1) num[0]--;}
        void print(){
            printf("%lld",num[num[0]]);
            for(int i=num[0]-1;i;i--) printf("%07lld",num[i]);
            putchar('
    ');
        }
        void leftplus(ll val){
            for(int i=++num[0];i>=2;i--) num[i]=num[i-1];
            num[1]=val;
            zero();
        }
    }a,b;
    
    ll max(ll a,ll b){return a>=b ? a : b ;}
    ll min(ll a,ll b){return a<=b ? a : b ;}
    
    bool operator < (big x,big y){
        if(x.num[0]!=y.num[0]) return x.num[0]<y.num[0];
        for(int i=x.num[0];i;i--)
         if(x.num[i]!=y.num[i]) return x.num[i]<y.num[i];
        return false;
    }
    
    bool operator >= (big x,big y){return !(x<y);}
    
    bool operator > (big x,big y){return y<x;}
    
    bool operator <= (big x,big y){return !(x>y);}
    
    bool operator == (big x,big y){
        if(x.num[0]!=y.num[0]) return false;
        for(int i=x.num[0];i;i--)
         if(x.num[i]!=y.num[i]) return false;
        return true;
    }
    
    bool operator != (big x,big y){return !(x==y);}
    
    big operator + (big x,ll val){
        x.num[1]+=val;
        for(int i=1;i<=x.num[i];i++)
         if(x.num[i]>=base){
             x.num[i+1]+=x.num[i]/base;
             x.num[i]%=base;
             if(i+1>x.num[0]) x.num[0]=i+1;
         }
         else break;
        return x;
    }
    
    big operator + (ll val,big x){return x+val;}
    
    big operator - (big x,ll val){
        x.num[1]-=val;
        for(int i=1;i<=x.num[0];i++)
         if(x.num[i]<0){
             ll now=x.num[i]%base+base;
             x.num[i+1]-=(now-x.num[i])/base;
             x.num[i]=now;
         }
         else break;
        x.zero();
        return x;
    }
    
    big operator * (big x,ll val){
        for(int i=1;i<=x.num[0];i++) x.num[i]*=val;
        for(int i=1;i<=x.num[0];i++)
         if(x.num[i]>=base){
             x.num[i+1]+=x.num[i]/base;
             x.num[i]%=base;
             if(i+1>x.num[0]) x.num[0]=i+1;
         }
        return x;
    }
    
    big operator * (ll val,big x){return x*val;}
    
    big operator / (big x,ll val){
        ll sum=0;
        for(int i=x.num[0];i;i--){
            sum=sum*base+x.num[i];
            if(sum<val) continue;
            x.num[i]=sum/val;
            sum=sum-val*x.num[i];
        }
        x.zero();
        return x;
    }
    
    big operator + (big x,big y){
        x.num[0]=max(x.num[0],y.num[0]);
        for(int i=1;i<=x.num[0];i++) x.num[i]+=y.num[i];
        for(int i=1;i<=x.num[0];i++)
         if(x.num[i]>=base){
             x.num[i+1]+=x.num[i]/base;
             x.num[i]%=base;
             if(i+1>x.num[0]) x.num[0]=i+1;
         }
        return x;
    }
    
    big operator - (big x,big y){
        for(int i=x.num[0];i>=1;i--) x.num[i]=x.num[i]-y.num[i];
        for(int i=1;i<=x.num[0];i++)
         if(x.num[i]<0){
             x.num[i]+=base;
             x.num[i+1]--;
         }
        x.zero();
        return x;
    }
    
    big operator * (big x,big y){
        big z;
        z.num[0]=x.num[0]+y.num[0];
        for(int i=1;i<=x.num[0];i++)
         for(int j=1;j<=y.num[0];j++)
          z.num[i+j-1]+=x.num[i]*y.num[i];
        for(int i=1;i<=z.num[0];i++)
         if(z.num[i]>=base){
             z.num[i+1]+=z.num[i]/base;
             z.num[i]%=base;
         }
        z.zero();
        return z;
    }
    
    big operator / (big x,big y){
        big z,sum;
        z.num[0]=x.num[0];
        for(int i=x.num[0];i;i--){
            sum.leftplus(x.num[i]);
            sum.print();
            if(sum<y) continue;
            ll l=0,r=base,mid,ans;
            while(l<=r){
                mid=(l+r)>>1;
                if(y*mid<=sum) l=mid+1,ans=mid;
                else r=mid-1;
            }
            z.num[i]=ans,sum=sum-y*ans;
        }
        z.zero();
        return z;
    }
    
    int main(){
        return 0;
    }
    高精度
  • 相关阅读:
    python的多线程是否没有用了
    解决“mongoengine.fields.ImproperlyConfigured: PIL library was not found”报错
    Django JSON 时间
    伪装浏览器根据经纬度解析地理位置
    Excel——使用VLOOKUP函数关联跨工作薄数据
    Python发送邮件(支持中文)
    Excel实用技巧
    修改tomcat应用日志默认编码格式
    AWS多个EIP的解决方案
    解决荣耀7烦人的情景智能提醒
  • 原文地址:https://www.cnblogs.com/sto324/p/15340015.html
Copyright © 2011-2022 走看看