da shu mo ban

时间:2024-10-13 16:06:14
#include<bits/stdc++.h>
using namespace std;
const int maxn=;/*精度位数,自行调整*/
//1.如果需要控制输出位数的话,在str()里面把len调成需要的位数
//2.很大的位数是会re的,所以如果是幂运算的话,如 计算x^p的位数n, n=p*log(10)x+1;(注意要加一)
//3.还可以加上qmul,取模的过程也就是str(),c_str()再搞一次
class bign
{
//io*2 bign*5*2 bool*6
friend istream& operator>>(istream&,bign&);
friend ostream& operator<<(ostream&,const bign&);
friend bign operator+(const bign&,const bign&);
friend bign operator+(const bign&,int&);
friend bign operator*(const bign&,const bign&);
friend bign operator*(const bign&,int&);
friend bign operator-(const bign&,const bign&);
friend bign operator-(const bign&,int&);
friend bign operator/(const bign&,const bign&);
friend bign operator/(const bign&,int&);
friend bign operator%(const bign&,const bign&);
friend bign operator%(const bign&,int&);
friend bool operator<(const bign&,const bign&);
friend bool operator>(const bign&,const bign&);
friend bool operator<=(const bign&,const bign&);
friend bool operator>=(const bign&,const bign&);
friend bool operator==(const bign&,const bign&);
friend bool operator!=(const bign&,const bign&); private://如果想访问len,改成public
int len,s[maxn];
public:
bign()
{
memset(s,,sizeof(s));
len=;
}
bign operator=(const char* num)
{
int i=,ol;
ol=len=strlen(num);
while(num[i++]==''&&len>)
len--;
memset(s,,sizeof(s));
for(i=; i<len; i++)
s[i]=num[ol-i-]-'';
return *this;
}
bign operator=(int num)
{
char s[maxn];
sprintf(s,"%d",num);
*this=s;
return *this;
}
bign(int num)
{
*this=num;
}
bign(const char* num)
{
*this=num;
}
string str() const
{
string res="";
for(int i=; i<len; i++)
res=char(s[i]+'')+res;
if(res=="")
res="";
return res;
}
};
bool operator<(const bign& a,const bign& b)
{
int i;
if(a.len!=b.len)
return a.len<b.len;
for(i=a.len-; i>=; i--)
if(a.s[i]!=b.s[i])
return a.s[i]<b.s[i];
return false;
}
bool operator>(const bign& a,const bign& b)
{
return b<a;
}
bool operator<=(const bign& a,const bign& b)
{
return !(a>b);
}
bool operator>=(const bign& a,const bign& b)
{
return !(a<b);
}
bool operator!=(const bign& a,const bign& b)
{
return a<b||a>b;
}
bool operator==(const bign& a,const bign& b)
{
return !(a<b||a>b);
}
bign operator+(const bign& a,const bign& b)
{
int up=max(a.len,b.len);
bign sum;
sum.len=;
for(int i=,t=;t||i<up; i++)
{
if(i<a.len)
t+=a.s[i];
if(i<b.len)
t+=b.s[i];
sum.s[sum.len++]=t%;
t/=;
}
return sum;
}
bign operator+(const bign& a,int& b)
{
bign c=b;
return a+c;
}
bign operator*(const bign& a,const bign& b)
{
bign res;
for(int i=; i<a.len; i++)
{
for(int j=; j<b.len; j++)
{
res.s[i+j]+=(a.s[i]*b.s[j]);
res.s[i+j+]+=res.s[i+j]/;
res.s[i+j]%=;
}
}
res.len=a.len+b.len;
while(res.s[res.len-]==&&res.len>)
res.len--;
if(res.s[res.len])
res.len++;
return res;
}
bign operator*(const bign& a,int& b)
{
bign c=b;
return a*c;
}
//只支持大数减小数
bign operator-(const bign& a,const bign& b)
{
bign res;
int len=a.len;
for(int i=; i<len; i++)
{
res.s[i]+=a.s[i]-b.s[i];
if(res.s[i]<)
{
res.s[i]+=;
res.s[i+]--;
}
}
while(res.s[len-]==&&len>)
len--;
res.len=len;
return res;
}
bign operator-(const bign& a,int& b)
{
bign c=b;
return a-c;
}
bign operator/(const bign& a,const bign& b)
{
int i,len=a.len;
bign res,f;
for(i=len-; i>=; i--)
{
f=f*;
f.s[]=a.s[i];
while(f>=b)
{
f=f-b;
res.s[i]++;
}
}
while(res.s[len-]==&&len>)
len--;
res.len=len;
return res;
}
bign operator/(const bign& a,int& b)
{
bign c=b;
return a/c;
}
bign operator%(const bign& a,const bign& b)
{
int len=a.len;
bign f;
for(int i=len-; i>=; i--)
{
f=f*;
f.s[]=a.s[i];
while(f>=b)
f=f-b;
}
return f;
}
bign operator%(const bign& a,int& b)
{
bign c=b;
return a%c;
}
bign& operator+=(bign& a,const bign& b)
{
a=a+b;
return a;
}
bign& operator-=(bign& a,const bign& b)
{
a=a-b;
return a;
}
bign& operator*=(bign& a,const bign& b)
{
a=a*b;
return a;
}
bign& operator/=(bign& a,const bign& b)
{
a=a/b;
return a;
}
bign& operator++(bign& a)
{
a=a+;
return a;
}
bign& operator++(bign& a,int)
{
bign t=a;
a=a+;
return t;
}
bign& operator--(bign& a)
{
a=a-;
return a;
}
bign& operator--(bign& a,int)
{
bign t=a;
a=a-;
return t;
}
istream& operator>>(istream &in,bign& x)
{
string s;
in>>s;
x=s.c_str();
return in;
}
ostream& operator<<(ostream &out,const bign& x)
{
out<<x.str();
return out;
}
int main()
{
bign;
return ;
}

相关文章