【模板】高精度

· · 个人记录

高精度的板子。
虽然是基础算法,但是已有的大多数板子都是一个光溜溜的计算,可扩展性不好。于是特地花了很长时间(两天www)整理了这个板子,需要顾及

另外还为C++的版本还设计了big_int大整数类,收录在FRSL中,还是比较不容易的。另外,有符号的情况还要麻烦一些,而OI场合正整数的情况较多,为此,这里提供了两个C语言风格的版本,分别对应有符号和无符号的版本。

其中,无符号版本中为了简单实现减法的符号输出,设计减法函数的返回值为结果的符号。

例程实现的是P1009阶乘之和。

无符号版

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=36000;
struct num{
    char n[MAX];    //number of int(not char!)
    int len;        //length
    char& operator[](int i){
        return n[i];
    }
    void clr(){
        memset(n,0,sizeof(n));
        len=1;
    }
};

inline void add(num &a,num &b,num &ans){
    num c;c.clr();
    int i,jw=0,maxlen=max(a.len,b.len);      //jinwei=0

    for(i=0;i<maxlen;i++){
        c[i]=a[i]+b[i]+jw;
        jw=c[i]/10;
        c[i]%=10;
    }
    if(jw!=0){              //the top 
        c[i]=jw;
        i++;
    }
    c.len=i;
    ans=c;
}

inline int numcmp(num &a,num &b){              //number compare 
                                        //a>b:1 a=b:0 a<b:-1
    if(a.len>b.len)return 1;
    if(a.len<b.len)return -1;

    for(int i=a.len-1;i>=0;i--)         //compare from high to low
        if(a[i]>b[i])return 1;
        else if(a[i]<b[i])return -1;
    return 0;
}

inline bool sub(num &a,num &b,num &ans){         //substract  answer>=0:false answer<0:true
    num c;c.clr();
    bool ansnega=0;
    int cmp=numcmp(a,b),i,jw=0,maxlen=max(a.len,b.len);
    if(cmp==0){                         //==
        c.len=1;
        return false;
    }
    if(cmp<0){                          //<
        swap(a,b);
        ansnega=true;
    }

    for(i=0;i<maxlen;i++){
        c[i]=a[i]-jw;
        jw=0;
        if(c[i]<b[i]){
            c[i]+=10;
            jw++;
        }
        c[i]-=b[i];
    }
    c.len=i;
    while(c[c.len-1]==0&&c.len>1)c.len--;
    ans=c;
    return ansnega;
}

inline void mul(num &a,num &b,num &ans){
    int i,j,jw;
    num c;c.clr();
    for(i=0;i<a.len;i++){
        jw=0;
        for(j=0;j<b.len;j++){
            c[i+j]=a[i]*b[j]+jw+c[i+j];
            jw=c[i+j]/10;
            c[i+j]%=10;
        }
        c[i+j]=jw;
    }

    c.len=a.len+b.len;
    while(c[c.len-1]==0&&c.len>1)c.len--;
    ans=c;
}

inline void split(num &a,int st,int ed,num &to){
    to.clr();
    to.len=0;
    for(int i=ed;i>=st;i--)
        to[to.len++]=a[a.len-i];
}

inline void numcpy(num &a,long long x){
    a.clr();
    if(x==0){a.len=1;return;}
    a.len=0;
    while(x!=0){
        a[a.len]=x%10;
        x/=10;
        a.len++;
    }
}

inline void gnum(num &a){              //get number
    a.clr();
    char t[MAX];
    scanf("%s",t);
    a.len=strlen(t);
    int st=0;
    while(t[st]=='0'&&st<a.len-1)st++;

    for(int i=st;i<a.len;i++)
        a[a.len-i-1]=t[i]-'0';
    a.len-=st;
}

inline void pnum(num &a){              //put number
    for(int i=a.len-1;i>=0;i--)
        printf("%d",a[i]);
    printf("\n");
}

int main(){
    int n;
    num ans,f,now;
    numcpy(ans,0);
    numcpy(f,1);
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
        numcpy(f,1);
        for(int j=1;j<=i;j++){
            numcpy(now,j);
            mul(f,now,f);
        }
        add(ans,f,ans);
    }

    pnum(ans);
    return 0;
}

有符号版

//繁琐、已经失去背板价值...

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=36000;
struct num{
    char n[MAX];    //number of int(not char!)
    int len;        //length
    bool nega;
};

void add(num &a,num &b,num &ans);
bool sub(num &a,num &b,num &ans);

inline void add(num &a,num &b,num &ans){
    if(a.nega^b.nega){
        num ta=a,tb=b;
        if(b.nega){
            tb.nega^=1;
            sub(ta,tb,ans);
            return;
        }
        else {
            ta=a;
            ta.nega^=1;
            sub(tb,ta,ans);
            return;
        }
    }

    num c;
    memset(c.n,0,sizeof(c.n));
    int i,jw=0,maxlen;      //jinwei=0

    maxlen=max(a.len,b.len);

    for(i=0;i<maxlen;i++){
        c.n[i]=a.n[i]+b.n[i]+jw;
        jw=c.n[i]/10;
        c.n[i]%=10;
    }
    if(jw!=0){              //the top 
        c.n[i]=jw;
        i++;
    }
    c.len=i;
    c.nega=a.nega;
    ans=c;
}

inline int numcmp(num &a,num &b){              //number compare 
                                        //a>b:1 a=b:0 a<b:-1
    if(a.nega&&(!b.nega))return -1;  
    if((!a.nega)&&b.nega)return 1;   

    bool nega=a.nega;

    if(a.len>b.len){
        if(nega)return -1;
        else return 1;
    }
    if(a.len<b.len){
        if(nega)return 1;
        else return -1;
    }
    //if a.len==b.len:
    for(int i=a.len-1;i>=0;i--)         //compare from high to low
        if(nega){
            if(a.n[i]<b.n[i])return 1;
            else if(a.n[i]>b.n[i])return -1;
        }
        else{
            if(a.n[i]<b.n[i])return -1;
            else if(a.n[i]>b.n[i])return 1;
        }
    return 0;
}

inline bool sub(num &a,num &b,num &ans){         //substract  answer>=0:false answer<0:true
    num ta=a,tb=b;
    if(a.nega^b.nega){
        if(b.nega){
            tb.nega^=1;
            add(ta,tb,ans);
            return 0;
        }
        else{
            tb=b;
            tb.nega^=1;
            add(ta,tb,ans);
            return 1;
        }
    }

    num c;
    memset(c.n,0,sizeof(c.n));
    bool ansnega=0;

    if(a.nega){
        ta.nega^=1;
        tb.nega^=1;
    }
    int cmp=numcmp(ta,tb),i,jw=0,maxlen;
    if(cmp==0){                         //==
        c.len=1;
        c.nega=0;
        ans=c;
        return false;
    }
    if(cmp<0){                          //<
        swap(a,b);
        ansnega=true;
    }
    maxlen=max(a.len,b.len);
    for(i=0;i<maxlen;i++){
        c.n[i]=a.n[i]-jw;
        jw=0;
        if(c.n[i]<b.n[i]){
            c.n[i]+=10;
            jw++;
        }
        c.n[i]-=b.n[i];
    }
    c.len=i;
    while(c.n[c.len-1]==0&&c.len>1)c.len--;
    c.nega=ansnega^a.nega;
    ans=c;
    return ansnega;
}

inline void mul(num &a,num &b,num &ans){
    int i,j,jw;
    num c;
    memset(c.n,0,sizeof(c.n));
    bool ansnega=a.nega^b.nega;
    for(i=0;i<a.len;i++){
        jw=0;
        for(j=0;j<b.len;j++){
            c.n[i+j]=a.n[i]*b.n[j]+jw+c.n[i+j];
            jw=c.n[i+j]/10;
            c.n[i+j]%=10;
        }
        c.n[i+j]=jw;
    }

    c.len=a.len+b.len;
    while(c.n[c.len-1]==0&&c.len>1)c.len--;
    c.nega=ansnega;
    ans=c;
}

inline void numcpy(num &a,long long x){
    memset(a.n,0,sizeof(a.n));
    if(x==0){
        a.len=1;
        return;
    }
    a.len=0;
    while(x!=0){
        a.n[a.len]=x%10;
        x/=10;
        a.len++;
    }
}

inline void gnum(num &a){              //get number
    memset(a.n,0,sizeof(a.n));
    char t[MAX];

    scanf("%s",t);
    a.len=strlen(t);
    int st=0;

    if(t[st]=='-'){                     //
        a.nega=true;
        st++;
    }
    else a.nega=false;

    while(t[st]=='0'&&st<a.len-1)st++;

    for(int i=st;i<a.len;i++)
        a.n[a.len-i-1]=t[i]-'0';
    a.len-=st;

    if(a.n[a.len-1]==0)a.nega=false;          //
}

inline void pnum(num &a){              //put number
    if(a.nega)putchar('-');             //
    for(int i=a.len-1;i>=0;i--)
        printf("%d",a.n[i]);
    printf("\n");
}

int main(){
    int n;
    num a,b,c;
    gnum(a);
    gnum(b);

    add(a,b,c);
    pnum(c);

    sub(a,b,c);
    pnum(c);
    mul(a,b,c);
    pnum(c);
    return 0;
}