求助 RE

P1303 A*B Problem

wtyqwq @ 2019-08-19 21:02:58

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 1001
#define R register
#define big long long
#define base (big)1e9
using namespace std;
struct bignum
{
    big a[MAXN],length=1;
    bignum(){memset(a,0,sizeof(a));}
    bignum operator=(R const bignum &b)
    {
        length=b.length;
        memcpy(a,b.a,sizeof(b.a));
    }
    inline void clear()
    {
        while(length>1&&!a[length])
            length=length-1;
    }
    inline void input()
    {
        R char s[MAXN+1];
        scanf("%s",s);
        R big k=strlen(s);
        for(R big i=0;i<=k-1;++i)
        {
            R big j=(k-i+8)/9;
            a[j]=(a[j]<<1)+(a[j]<<3)+(s[i]^48);
        }
        length=(k+8)/9;
    }
    inline void print()const
    {
        printf("%lld",a[length]);
        for(R big i=length-1;i>=1;--i)
            printf("%09lld",a[i]);
        printf("\n");
    }
    bignum operator+(R const bignum &b)
    {
        R big x=0;
        R big k=length>b.length?length:b.length;
        for(R big i=1;i<=k+1;++i)
        {
            R big s=a[i]+b.a[i]+x;
            x=s/base;a[i]=s%base;
        }
        length+=b.length;clear();
        return *this;
    }
    bignum operator+=(R const bignum &b)
    {
        *this=*this+b;return *this;
    }
    bignum operator-(R const bignum &b)
    {
        for(R big i=1;i<=length;++i)
        {
            a[i]=a[i]-b.a[i];
            if(a[i]<=0)
            {
                a[i]=a[i]+base;
                --a[i+1];
            }
        }
        return *this;
    }
    bignum operator-=(R const bignum &b)
    {
        *this=*this-b;return *this;
    }
    bignum operator*(R const bignum &b)
    {
        R bignum c;
        for(R big i=1;i<=length;++i)
        {
            R big x=0;
            for(R big j=1;j<=b.length;++j)
            {
                R big s=a[i]*b.a[j]+x;
                c.a[i+j-1]=c.a[i+j-1]+s;
                x=c.a[i+j-1]/base;
                c.a[i+j-1]%=base;
            }
            c.a[i+b.length]=x;
        }
        c.length=length+c.length;
        c.clear();return c;
    }
    bignum operator*=(const bignum &b)
    {
        *this=*this*b;return *this;
    }
};
bignum a,b;
int main()
{
    a.input();
    b.input();
    a*=b;
    a.print();
    return 0;
}

by infinities @ 2019-08-19 21:08:53

您明明过了为何还要写一遍


by guoxinyugz @ 2019-08-19 21:37:09

别以为自己很强


|