#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn=500;
using namespace std;
struct BN
{
int e[maxn],len;
int syb;
BN(){memset(e,0,sizeof e);len=1;syb=1;}
BN operator =(const char *a)
{
int j=0;
if(!isdigit(a[0])&&a[0]=='-')
syb=-1,len=strlen(a)-1,j++;
else syb=1,len=strlen(a);
for(int i=1;i<=len;i++,j++)
e[len-i+1]=a[j]-'0';
}
BN operator =(const int& num)
{
char a[maxn];
sprintf(a,"%d",num);
*this=a;
return *this;
}
BN(const int& num){*this = num;}
BN(const char *num){*this = num;}
string str()
{
string res="";
for(int i=1;i<=len;i++)
res=char(e[i]+'0')+res;
if(res=="")res="0";
else if(syb==-1)res="-"+res;
return res;
}
BN operator -()const{BN res=*this;res.syb=0-syb;return res;}
void clean(){while(len>1&&!e[len])len--;}
BN operator +(const BN &a)const
{
BN res;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(syb==1&&a.syb==1);
else if(syb==-1&&a.syb==-1){res=-((-*this)+(-a));return res;}
else if(syb==1&&a.syb==-1){res=*this-(-a);return res;}
else if(syb==-1&&a.syb==1){res=a-(-*this);return res;}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>res.len=0;
int ub=max(len,a.len);
for(int i=1,g=0;g||i<=ub;i++)
{
int x=g;
if(i<=len)x+=e[i];
if(i<=a.len)x+=a.e[i];
res.e[++res.len]=x%10;
g=x/10;
}
return res;
}
BN operator -(const BN &a)const
{
BN res;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(syb==1&&a.syb==1&&*this>a);
else if(syb==-1&&a.syb==1){res=-( (-*this) +a);return res;}
else if(syb==1&&a.syb==-1){res=*this+(-a);return res;}
else if(syb==-1&&a.syb==-1){res=-a-(-*this);return res;}
else if(syb==1&&a.syb==1&&*this<a){res=-(a-*this);return res;}
<span style="white-space:pre"> </span>
res.len=len;
for(int i=1,g=0;i<=len;i++)
{
int x=e[i];
x-=g;
if(i<=a.len)x-=a.e[i];
if(x>=0)g=0;
else x+=10,g=1;
res.e[i]=x;
}
res.clean();
return res;
}
BN operator *(const BN &a)const
{
BN res;
res.len=len+a.len;
for(int i=1;i<=len;i++)
for(int j=1;j<=a.len;j++)
res.e[i+j-1]+=e[i]*a.e[j];
for(int i=1;i<=res.len;i++)
{
res.e[i+1]+=res.e[i]/10;
res.e[i]%=10;
}
res.clean();
if(syb==a.syb)res.syb=1;
else res.syb=-1;
return res;
}
BN operator /(const BN &a)const
{
BN res,f=0;
res.len=len;
for(int i=len;i>=1;i--)
{
f=f*10+e[i];
while(f>=a)
{
f=f-a;
res.e[i]++;
}
}
res.clean();
if(syb==a.syb)res.syb=1;
else res.syb=-1;
return res;
}
BN operator %(const BN &a)const
{
BN res=*this/a;
res=*this-res*a;
return res;
}
bool operator <(const BN &a)const
{
if(syb!=a.syb)return syb<a.syb;
<span style="white-space:pre"> </span>else if(syb==-1) return -*this>(-a);
<span style="white-space:pre"> </span>if(len!=a.len)return len<a.len;
for(int i=len;i>=1;i--)
if(e[i]!=a.e[i])return e[i]<a.e[i];
return 0;
}
bool operator >(const BN &a)const
{
return a < *this;
}
bool operator >=(const BN &a)const
{
return !(*this<a);
}
bool operator <=(const BN &a)const
{
return !(*this>a);
}
};
istream& operator >>(istream &in,BN &a)
{
string s;
in>>s;
a=s.c_str();
return in;
}
ostream& operator <<(ostream &out,BN &a)
{
out<<a.str();
return out;
}
int main()
{
BN a,b;
cin>>a>>b;
BN c=a*b;
cout<<c;
}
高精度模板【高精度加减乘除模带负数判定】
最新推荐文章于 2023-08-05 22:26:53 发布