您的位置:首页 > 其它

压位高精模板(支持加法、高精乘低精、高精除低精)

2017-09-05 16:11 246 查看
这里的写法是针对低精乘数小于1e8的情况

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
#include<ctime>
using namespace std;
const int maxn=10005;
const int maxl=205;
const int carry=1e8;
const int PP=8;

struct bign{
int z[maxl],len;
bign(){
memset(z,0,sizeof(z));
len=0;
}
bign(int x) { *this=x; }
bign operator = (int x)
{
bign re;
re.len=1;
re.z[0]=x;
*this=re;
}
bign operator = (char *c)
{
int len=strlen(c);
char tmp[PP+5];
bign now;
memset(tmp,0,sizeof(tmp));
while(len>=PP)
{
strncpy(tmp,c+len-PP,PP);
now.z[now.len++]=atoi(tmp);
len-=PP;
}
if(len)
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,c,len);
now.z[now.len++]=atoi(tmp);
}
*this=now;
}
friend bign operator + (bign x,bign y)
{
bign re;
re.len=max(x.len,y.len)+1;
for(int i=0;i<re.len;i++)
{
re.z[i]+=x.z[i]+y.z[i];
if(re.z[i]>=carry) re.z[i]-=carry,re.z[i+1]++;
}
while(re.len>1 && re.z[re.len-1]==0) re.len--;
return re;
}
friend bign operator += (bign &x,bign y)
{
x=x+y;
return x;
}
friend bign operator * (bign x,int y)
{
LL tmp;
bign re;
re.len=x.len+1;
for(int i=0;i<x.len;i++)
{
tmp=(LL)x.z[i]*y;
if(tmp+re.z[i]>=carry) re.z[i+1]=(re.z[i]+tmp)/carry,re.z[i]=(re.z[i]+tmp)%carry;
else re.z[i]+=tmp;
}
while(re.len>1 && re.z[re.len-1]==0) re.len--;
return re;
}
friend bign operator / (bign x,int y)
{
LL tmp=0;
bign re;
int pos=x.len-1;
while(pos>=0)
{
tmp*=carry;
re.z[pos]+=(x.z[pos]+tmp)/y;
tmp=(x.z[pos]+tmp)%y;
pos--;
}
re.len=x.len;
while(re.len>1 && re.z[re.len-1]==0) re.len--;
return re;
}
friend bool operator < (bign x,bign y)
{
if(x.len!=y.len) return x.len<y.len;
for(int i=x.len-1;i>=0;i--)
if(x.z[i]!=y.z[i]) return x.z[i]<y.z[i];
return 0;
}
void out()
{
printf("%d",z[len-1]);
for(int i=len-2;i>=0;i--)
printf("%0*d",PP,z[i]);
}
};

int main()
{
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: