祎隋

高精度模板
全套的2333#include<bits/stdc++.h> using namespace std;...
扫描右侧二维码阅读全文
18
2019/03

高精度模板

全套的2333

#include<bits/stdc++.h>
using namespace std;
struct node{
    int a[1005],len;
    node(){
        memset(a,0,sizeof(a));
        len=1;
    }
    void operator = (int x){
        len=0;
        if(x==0) len=1,a[1]=0;
        else{
            while(x>0) a[++len]=x%10,x/=10;
        }
    }
    void operator = (char *s){
        len=strlen(s);
        for(int i=0;i<len;i++) a[len-i]=s[i]-'0';
        while(len>1&&a[len]==0) len--;
    } 
    void operator = (long long x){
            len=0;
            if(x==0) a[1]=0,len=1;
            else{
                while(x>0) a[++len]=x%10,x/=10;
            }
    }
    friend node operator +(node a,node b){
        int len=max(a.len,b.len);
        node c;
        for(int i=1;i<=len;i++){
            c.a[i]=a.a[i]+b.a[i];
        } 
        len++;
        for(int i=1;i<len;i++){
                c.a[i+1]+=c.a[i]/10;
                c.a[i]=c.a[i]%10;
            }
        while(len>1&&c.a[len]==0) len--;
        c.len=len;
        return c;
    }
    friend node operator -(node a,node b){
        if(a<b) swap(a,b);
        int len=a.len;
        node c;
        for(int i=1;i<=len;i++){
            c.a[i]=a.a[i]-b.a[i];
        }
        for(int i=1;i<len;i++){
            if(c.a[i]<0) c.a[i]+=10;
            c.a[i+1]--;
        }
        while(len>1&&c.a[len]==0) len--;
        c.len=len;
        return c;
    }
    friend node operator *(node a,node b){
        int len=a.len+b.len;
        node c;
        for(int i=1;i<=a.len;i++)
        for(int j=1;j<=b.len;j++){
            c.a[i+j-1]+=a.a[i]*b.a[j];
        }
        for(int i=1;i<=len;i++){
            c.a[i+1]+=c.a[i]/10;
            c.a[i]=c.a[i]%10;
        }
        while(len>1&&c.a[len]==0) len--;
        c.len=len;
        return c;
    }
    friend node operator *(node a,int b){
        int len=a.len+20;//注意这里20是非高精度最大位数 
        node c;
        for(int i=1;i<=a.len;i++) c.a[i]=a.a[i]*b;
            
        for(int i=1;i<=len;i++){
            c.a[i+1]+=c.a[i]/10;
            c.a[i]=c.a[i]%10;
        }
        while(len>1&&c.a[len]==0) len--;
        c.len=len;
        return c;
    }
    friend node operator /(node a,node b){
       node c,d;
       int len=a.len;
       for(int i=len;i>=1;i--){
           d=d*10;
           d.a[1]=a.a[i];
           while(d>=b) c.a[i]++,d=d-b;     
       }    
       while(len>1&&c.a[len]==0) len--;
       c.len=len;
       return c;
    }
    friend node operator /(node a,int b){
        node c;
        int d;
        int len=a.len;
        for(int i=len;i>=1;i--){
            d=d*10+a.a[i];
            c.a[i]=d/b,d=d%b;
        }
        while(len>1&&c.a[len]==0) len--;
        c.len=len;
        return c;
    }
    friend node operator %(node a,node b){
        node d;
        int len=a.len;
        for(int i=len;i>=1;i--){
            d=d*10;
            d.a[1]=a.a[i];
            while(d>=b) d=d-b; 
        }
        while(len>1&&d.a[len]==0) len--;
        d.len=len;
        return d;
    }
    friend int operator %(node a,int b){
        int d;
        int len=a.len;
        for(int i=len;i>=1;i--){
            d=d*10+a.a[i];
            d=d%b;
        }
        return d;
    }
    friend bool operator <(node a,node b){
        if(a.len!=b.len) return a.len<b.len;
        for(int i=a.len;i>=1;i--){
            if(a.a[i]!=b.a[i]) return a.a[i]<b.a[i];
        }
        return false;
    }
    friend bool operator >(node a,node b){
        return b<a;
    }
    friend bool operator >=(node a,node b){
        return !(a<b);
    }
    friend bool operator <= (node a,node b){
            return !(a>b);
    }
    friend bool operator != (node a,node b){
            return (a<b)||(b<a);
    }
    friend bool operator == (node a,node b){
            return a>=b && b>=a;
    }
    friend node gcd(node a,node b){
        node t;
        t=0;
        if(b==t) return a;
        return gcd(b,a%b);
    }
    friend node lcm(node a,node b){
        node t=gcd(a,b);
        node t1=a*b;
        return t1/t;
    }
    void printff(node a){
    for(int i=len;i>=1;i--) printf("%d",a.a[i]);
    printf("\n");
    }
};
int main(){
        
    return 0;
}
Last modification:March 18th, 2019 at 11:57 am

Leave a Comment