大整数乘法-分治

用分治法实现2个大整数的乘法。EOJ—1070。

#include <iostream>
#include <vector>
#include <stdio.h>
#include <cmath>
using namespace std;

vector<int> add(vector<int> &a,vector<int> &b);
vector<int> sub(vector<int> &a,vector<int> &b);
vector<int> multi(vector<int> &n,vector<int> &m);

int main()
{
    int n;
    cin>>n;
    getchar();

    for(int i=0; i<n; i++)
    {
        vector<int> a,b,t;
        char temp;
        temp=getchar();
        while(temp!=' ')
        {
            a.push_back( temp-'0' );
            temp=getchar();
        }

        temp=getchar();
        while(temp!='\n')
        {
            b.push_back( temp-'0' );
            temp=getchar();
        }


        t=multi(a,b);

        for(unsigned int j=0; j<t.size(); j++)
            cout<<t[j];
        cout<<endl;
    }
    return 0;
}

vector<int> add(vector<int> &x,vector<int> &y)
{
    vector<int> a(x),b(y);
    int test=a.size()-b.size();
    vector<int> dip(abs(test),0);
    if(test>0)
        b.insert(b.begin(),dip.begin(),dip.end());
    if(test<0)
        a.insert(a.begin(),dip.begin(),dip.end());
    int len=a.size();
    vector<int> t(len,0),fin(1,0);
    fin[0]=1;
    int carry=0;
    for(int i=len-1; i>=0; i--)
    {
        t[i]=(a[i]+b[i]+carry)%10;
        carry=(a[i]+b[i]+carry)/10;
    }
    if(carry)
        t.insert(t.begin(),fin.begin(),fin.end());
    vector<int>::iterator it=t.begin();
    while(*it==0&&it!=t.end()-1)
        t.erase(it);
    return t;
}
vector<int> sub(vector<int> &x,vector<int> &y)
{
    vector<int> a(x),b(y);
    int test=a.size()-b.size();
    vector<int> dip(abs(test),0);
    if(test>0)
        b.insert(b.begin(),dip.begin(),dip.end());
    if(test<0)
        a.insert(a.begin(),dip.begin(),dip.end());
    int len=a.size();
    vector<int> t(len,0);
    int carry=0;
    for(int i=len-1; i>=0; i--)
    {
        if(a[i]+carry<b[i])
        {
            a[i]+=10;
            t[i]=a[i]-b[i]+carry;
            carry=-1;
        }
        else
        {
            t[i]=a[i]-b[i]+carry;
            carry=0;
        }
    }
    vector<int>::iterator it=t.begin();
    while(*it==0&&it!=t.end()-1)
        t.erase(it);
    return t;

}

vector<int> multi(vector<int> &n,vector<int> &m)
{

    int test=n.size()-m.size();
    vector<int> dip(abs(test),0);
    if(test>0)
        m.insert(m.begin(),dip.begin(),dip.end());
    if(test<0)
        n.insert(n.begin(),dip.begin(),dip.end());
    if(n.size()==1)
    {
        vector<int> t(1,0),biu(1,0);

        t[0]=(n[0]*m[0])%10;
        if((n[0]*m[0])/10)
        {
            biu[0]=(n[0]*m[0])/10;
            t.insert(t.begin(),biu.begin(),biu.end());
        }
        return t;
    }
    vector<int>::iterator iter1=n.begin(),iter2=m.begin();
    for(unsigned int i=0; i<n.size()/2; i++)
    {
        ++iter1;
        ++iter2;
    }
    vector<int> a,b,c,d;
    a.insert(a.begin(),n.begin(),iter1);
    b.insert(b.begin(),iter1,n.end());
    c.insert(c.begin(),m.begin(),iter2);
    d.insert(d.begin(),iter2,m.end());
    vector<int> t1=multi(a,c),t2=multi(b,d);
    vector<int> t3=add(t1,t2),new1((n.size()/2+n.size()%2)*2,0),new2(n.size()/2+n.size()%2,0);
    vector<int> h1=add(a,b),h2=add(c,d);
    vector<int> t4=multi(h1,h2);
    vector<int> h3=sub(t4,t3);
    if(h3[0])h3.insert(h3.end(),new2.begin(),new2.end());
    if(t1[0])
    {
        t1.insert(t1.end(),new1.begin(),new1.end());
        t3=add(t1,h3);
    }

    else t3=h3;
    return add(t3,t2);
}

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