相比之前通过函数的形式实现,结构体重载运算符显得更简便,尤其是在主函数运算的时候,形式与平时无异。
其中mod为数组中每一位的模,若mod=10,则数组中每一个数对应的就是该数的每一位,则数组的长度len就是该数的位数,该模板采用的是四位压缩的方法,每四位存一个数。(mod的的大小决定运算的快慢和输出的方式)
模板如下:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <cstdio>
#define maxn 10000
#define mod 10000
using namespace std;
int n,m,a[maxn];
struct BI{
int l[5000],len;
void print(){
printf("%d",l[len]);
for(int q=len-1;q>0;q--){
if(l[q]==0){
printf("0000");
continue;
}
for(int w=10;w*l[q]<mod;w*=10){
printf("0");
}
printf("%d",l[q]);
}}}p;
BI operator +(const BI &a,const BI &b)
{
BI c;
c.len=max(a.len,b.len);
int x=0;
for(int q=1; q<=c.len; q++)
{
c.l[q]=a.l[q]+b.l[q]+x;
x=c.l[q]/mod;
c.l[q]%=mod;
}
if(x>0)
{
c.l[++c.len]=x;
}
return c;
}
BI operator *(const BI &a,const BI &b)
{
BI c;
c.len=max(a.len,b.len);
int x;
for(int q=1; q<=a.len; q++)
{
x=0;
for(int w=1; w<=b.len; w++)
{
c.l[q+w-1]+=a.l[q]*b.l[w]+x;
x=c.l[q+w-1]/mod;
c.l[q+w-1]%=mod;
}
c.l[q+b.len]=x;
}
c.len=a.len+b.len;
while((c.l[c.len]==0)&&(c.len>1))
{
c.len--;
}
return c;
}
BI operator -(const BI &a,const BI &b)
{
BI x=a,y=b,c;
c.len=max(a.len,b.len);
for(int q=1; q<=c.len; q++)
{
if(x.l[q]<y.l[q])
{
x.l[q+1]--;
x.l[q]+=mod;
}
c.l[q]+=(x.l[q]-y.l[q]);
}
while((c.l[c.len]==0)&&(c.len!=1))
{
c.len--;
}
return c;
}
int main()
{
return 0;
}