大整数分治乘法

#include <iostream>

#include <cstring>

#include <string>

#include <cstdio>

using namespace std;

//500 digits at most

struct Num{

    int num[1000],len;

    Num(){

        memset(num,0,sizeof(num));

        len=1;

    }

    Num(const string &s){

        len=s.size();

        memset(num,0,sizeof(num));

        for(int i=0;i<len;++i){

            num[i]=s[len-1-i]-‘0’;

        }

    }

    Num& operator=(const Num& right){

        copy(right.num,right.num+right.len,this->num);

        len=right.len;

        return *this;

    }

    friend ostream& operator<<(ostream &os,const Num &output){

        for(int i=output.len-1;i>=0;–i)

            os<<output.num[i];

        return os;

    }

    friend Num operator+(Num &x,Num &y){

        Num ans;

        ans.len=max(x.len,y.len);

        for(int i=0;i<ans.len;++i){

            ans.num[i]=x.num[i]+y.num[i];

        }

        for(int i=0;i<ans.len;++i){

            ans.num[i+1]+=ans.num[i]/10;

            ans.num[i]%=10;

        }

        if(ans.num[ans.len]) ++ans.len;

        return ans;

    }

    friend Num operator-(const Num &left,const Num& right){

        Num ans;

        ans.len=max(left.len,right.len);

        for(int i=0;i<ans.len;++i)

            ans.num[i]=left.num[i]-right.num[i];

        for(int i=0;i<ans.len;++i){

            if(ans.num[i]<0){

                –ans.num[i+1];

                ans.num[i]+=10;

            }

        }

        while(ans.len>1 && !ans.num[ans.len-1]) –ans.len;

        return ans;

    }

    friend Num mul_digit(int x,Num y){

        Num ans=y;

        for(int i=0;i<ans.len;++i){

            ans.num[i]*=x;

        }

        for(int i=0;i<ans.len;++i){

            ans.num[i+1]+=ans.num[i]/10;

            ans.num[i]%=10;

        }

        if(ans.num[ans.len]) ++ans.len;

        return ans;

    }

    friend Num mul(const Num &x,const Num &y){

        if(x.len==1 || y.len==1){

            if(x.len==1) return mul_digit(x.num[0],y);

            else return mul_digit(y.num[0],x);

        }

        Num a,b,c,d,ans;

        int len1=x.len>>1,len2=y.len>>1;

//        cout<<“len  “<<len1<<” “<<len2<<endl;

        copy(x.num,x.num+x.len-len1,b.num);

        b.len=x.len-len1;

        copy(x.num+x.len-len1,x.num+x.len,a.num);

        a.len=len1;

        copy(y.num,y.num+y.len-len2,d.num);

        d.len=y.len-len2;

        copy(y.num+y.len-len2,y.num+y.len,c.num);

        c.len=len2;

        Num tem_ac=mul(a,c),tem_bd=mul(b,d),ac,bd,fin_cdab;

        Num cdab;

        if(x.len-len1>=y.len-len2){

            Num aa,new_ac;

            copy(a.num,a.num+a.len,aa.num+x.len-len1-y.len+len2);

            aa.len=a.len+x.len-len1-y.len+len2;

            copy(tem_ac.num,tem_ac.num+tem_ac.len,new_ac.num+x.len-len1-y.len+len2);

            new_ac.len=tem_ac.len+x.len-len1-y.len+len2;

            cdab=mul(c+d,aa+b)-new_ac-tem_bd;

        }

        else{

            Num bb,new_bd;

            copy(b.num,b.num+b.len,bb.num+y.len-len2-x.len+len1);

            bb.len=b.len+y.len-len2-x.len+len1;

            copy(tem_bd.num,tem_bd.num+tem_bd.len,new_bd.num+y.len-len2-x.len+len1);

            new_bd.len=tem_bd.len+y.len-len2-x.len+len1;

            cdab=mul(c+d,a+bb)-tem_ac-new_bd;

        }

//        cout<<“临时cdab “<<cdab<<” “;

        copy(tem_ac.num,tem_ac.num+tem_ac.len,ac.num+x.len-len1+y.len-len2);

        ac.len=tem_ac.len+y.len-len2+x.len-len1;

        int minlen=min(x.len-len1,y.len-len2);

        copy(cdab.num,cdab.num+cdab.len,fin_cdab.num+minlen);

        fin_cdab.len=cdab.len+minlen;

        ans=ac+fin_cdab;

        ans=ans+tem_bd;

        while(ans.len>1 && !ans.num[ans.len-1]) –ans.len;

//        cout<<“debug:”<<“a=”<<a<<” b=”<<b<<” c=”<<c<<” d=”<<d<<” ac=”<<ac<<” cdab=”<<fin_cdab<<” bd=”<<tem_bd<<endl;

        return ans;

    }

    friend Num operator*(const Num &x,const Num &y){

        return mul(x,y);

    }

};

int main()

{

    string a,b;

    while(cin>>a>>b){

        Num x=a,y=b;

        cout<<“***************************************************”<<endl;

        cout<<a<<endl<<“*”<<endl<<b<<endl<<“=”<<endl<<x*y<<endl;

        cout<<“***************************************************”<<endl;

    }

    return 0;

}

    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/FANGPINLEI/article/details/45030423
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞