class="markdown_views prism-dracula">
这是用来存一个高精度类的板子的blog(写了一下午感觉要死了QAQ).
这个板子主要包含高精加,高精减(不处理负数的情况),高精乘单精,高精乘高精,高精除单精,高精模单精,高精除高精以及高精模单精,还有高精度比大小,高精度赋值,读入和输出等琐碎的操作.
题目:
luogu1932.
代码如下:
#include
using namespace std;
#define Abigail inline void
typedef long long LL;
#define m(s) memset(s,0,sizeof(s))
const int N=20000;
struct bigint{
int v[N+9],len;
bigint(){m(v);len=0;}
bigint(const char *c){
*this=bigint();
len=strlen(c);
for (int i=1;i<=len;++i)
v[len-i+1]=c[i-1]-'0';
while (v[len]==0&&len>1) --len;
}
bigint(const string &s){
*this=bigint();
len=s.size();
for (int i=1;i<=len;++i)
v[len-i+1]=s[i-1]-'0';
while (v[len]==0&&len>1) --len;
}
bigint(int x){
*this=bigint();
for (;x;x/=10) v[++len]=x%10;
while (v[len]==0&&len>1) --len;
}
bigint(LL x){
*this=bigint();
for (;x;x/=10) v[++len]=x%10;
while (v[len==0]&&len>1) --len;
}
friend istream &operator >> (const istream &in,bigint &p){
char c=getchar();
p=bigint();
while (c<'0'||c>'9') c=getchar();
for (;c<='9'&&c>='0';c=getchar()) p.v[++p.len]=c-'0';
for (int i=1;i<=p.len>>1;++i)
swap(p.v[i],p.v[p.len-i+1]);
while (p.v[p.len==0]&&p.len>1) --p.len;
}
friend ostream &operator << (const ostream &out,const bigint &p){
for (int i=p.len;i>=1;--i)
putchar(p.v[i]+'0');
}
bigint operator = (const char *c){*this=bigint(c);}
bigint operator = (const string &s){*this=bigint(s);}
bigint operator = (int x){*this=bigint(x);}
bigint operator = (LL x){*this=bigint(x);}
bool operator < (const bigint &p)const{
if (len^p.len) return len<p.len;
for (int i=len;i>=1;--i)
if (v[i]^p.v[i]) return v[i]<p.v[i];
return false;
}
bool operator >= (const bigint &p)const{return !(*this<p);}
bool operator > (const bigint &p)const{return p<*this;}
bool operator <= (const bigint &p)const{return !(p<*this);}
bool operator == (const bigint &p)const{return *this<=p&&*this>=p;}
bool operator != (const bigint &p)const{return !(*this==p);}
bigint operator + (const bigint &p)const{
bigint ans=bigint();
ans.len=max(len,p.len);
for (int i=1;i<=ans.len;++i){
ans.v[i]+=v[i]+p.v[i];
while (ans.v[i]>=10) ans.v[i]-=10,++ans.v[i+1];
}
for (;ans.v[ans.len+1];++ans.len)
while (ans.v[ans.len]>=10) ans.v[ans.len]-=10,++ans.v[ans.len+1];
return ans;
}
bigint &operator += (const bigint &p){return *this=*this+p;}
bigint operator - (const bigint &p)const{
bigint ans=bigint();
ans.len=len;
for (int i=1;i<=ans.len;++i){
ans.v[i]=ans.v[i]+v[i]-p.v[i];
while (ans.v[i]<0) ans.v[i]+=10,--ans.v[i+1];
}
for (;!ans.v[ans.len]&&ans.len>1;--ans.len);
return ans;
}
bigint &operator -= (const bigint &p){return *this=*this-p;}
bigint operator * (const bigint &p)const{
bigint ans=bigint();
ans.len=len+p.len;
for (int i=1;i<=len;++i)
for (int j=1;j<=p.len;++j){
ans.v[i+j-1]+=v[i]*p.v[j];
if (ans.v[i+j-1]>=10) ans.v[i+j]+=ans.v[i+j-1]/10,ans.v[i+j-1]%=10;
}
while (!ans.v[ans.len]&&ans.len>1) --ans.len;
return ans;
}
bigint &operator *= (const bigint &p){return *this=*this*p;}
bigint operator *(const int &p)const{
bigint ans=bigint();
ans.len=len+10;
for (int i=1;i<=ans.len;++i){
ans.v[i]+=v[i]*p;
if (ans.v[i]>=10) ans.v[i+1]+=ans.v[i]/10,ans.v[i]%=10;
}
while (!ans.v[ans.len]&&ans.len>1) --ans.len;
return ans;
}
bigint &operator *= (const int &p){return *this=*this*p;}
bigint operator / (const int &p)const{
bigint ans=bigint();
int now=0;
ans.len=len;
for (int i=len;i>=1;--i){
now=now*10+v[i];
while (now>=p) now-=p,++ans.v[i];
}
while (!ans.v[ans.len]&&ans.len>1) --ans.len;
return ans;
}
bigint &operator /= (const int &p){return *this=*this/p;}
bigint operator / (const bigint &p)const{
bigint ans=bigint(),now=bigint(0);
ans.len=len;
for (int i=len;i>=1;--i){
now=now*10+v[i];
while (now>=p) now-=p,++ans.v[i];
}
while (!ans.v[ans.len]&&ans.len>1) --ans.len;
return ans;
}
bigint &operator /= (const bigint &p){return *this=*this/p;}
bigint operator % (const int &p)const{
int now=0;
for (int i=len;i>=1;--i){
now=now*10+v[i];
while (now>=p) now-=p;
}
return now;
}
bigint &operator %= (const int &p){return *this=*this%p;}
bigint operator % (const bigint &p)const{
bigint now=bigint(0);
for (int i=len;i>=1;--i){
now=now*10+v[i];
while (now>=p) now-=p;
}
return now;
}
bigint &operator %= (const bigint &p){return *this=*this%p;}
}a,b;
Abigail into(){
cin>>a;cin>>b;
}
Abigail work(){
}
Abigail outo(){
cout<<a+b;puts("");
(a>=b)?cout<<a-b:cout<<'-'<<b-a;puts("");
cout<<a*b;puts("");
cout<<a/b;puts("");
cout<<a%b;puts("");
}
int main(){
into();
work();
outo();
return 0;
}