高精压位

#include<bits/stdc++.h>
#define re return
#define inc(i,l,r) for(int i=l;i<=r;++i)
#define dec(i,l,r) for(int i=l;i>=r;--i)

const int power=4,base=10000,maxn=10005;
//power为4的原因是因为10e4*10e4=10e9,接近int范围(也就是说可以long long);
 

using namespace std;

struct num
{
	int a[maxn];
	
	//构造函数二选一
	//无参,有参,你更喜欢那个? 
	num(){memset(a,0,sizeof(a));}
	num(char*s)
	{
		memset(a,0,sizeof(a));
		int len=strlen(s);
		a[0]=(len+power-1)/power;
		for(int i=0,t=0,w;i<len;w*=10,++i)
		{
			if(i%power==0)w=1,++t;
			a[t]+=w*(s[i]^48);
		}
	}
	
	
	
	inline void rev(){reverse(a+1,a+a[0]+1);}
	inline void print()
	{
		printf("%d",a[a[0]]);
		dec(i,a[0]-1,1)
		printf("%04d",a[i]);
	}
	
	bool operator<(const num &q)
	{
		if(a[0]<q.a[0])re 1;
		if(a[0]>q.a[0])re 0;
		dec(i,a[0],1)//从高到低
		if(a[i]!=q.a[i])re a[i]<q.a[i];
		re 0;
	}
	
	num operator+ (const num&q)
	{
		num c;
		c.a[0]=max(a[0],q.a[0]);
		inc(i,1,c.a[0])
		{
			c.a[i]+=a[i]+q.a[i];
			c.a[i+1]+=c.a[i]/base;
			c.a[i]%=base;
		}
		if(c.a[c.a[0]+1])++c.a[0];
		re c;
	} 
	num operator-(const num&q)
	{
		num c;
		inc(i,1,a[0])
		{
			c.a[i]+=a[i]-q.a[i];
			if(c.a[i]<0)
			{
				--c.a[i+1];
				c.a[i]+=base; 
			}
		}
		while(!c.a[a[0]]&&a[0]>0)--a[0];
		c.a[0]=a[0];
		re c;
	}
	
	num operator*(const num&q)
	{
		num c;
		c.a[0]=a[0]+q.a[0]-1;
		inc(i,1,a[0])inc(j,1,q.a[0])
		{
			c.a[i+j-1]+=a[i]*q.a[j];
			//+=    一直再改变 
			if(c.a[i+j-1]>base)
			{
				c.a[i+j]+=c.a[i+j-1]/base;
				c.a[i+j-1]%=base;
			}
		}
		if(c.a[c.a[0]+1])++c.a[0];
		re c;
	}
	
	inline void add(int k){if(k||a[0])a[++a[0]]=k;}
	inline void rev(){reverse(a+1,a+a[0]+1);}
	
	num operator/(const num&q)
	{
		num x,y;
		x.a[0]=a[0];
		dec(i,a[0],1)
		{
			y.add(a[i]);
			y.rev();
			while(!(y<q))
			{
				y=y-q;
				++x.a[i];
			}
			y.rev();
		}
		while(x.a[0]&&x.a[x.a[0]]==0)--x.a[0];
		re x;
	}
	
}p,q,ans;

char a[maxn],aa[maxn];

int main()
{
	scanf("%s%s",a,aa);
	 reverse(a,a+strlen(a));
     reverse(aa,aa+strlen(aa));
     //不可以先读入,后反转,因为num.a[]存了四个数
	  
	p=num(a);q=num(aa);

	ans=p+q;
	加法 
	
	
	if(p<q){putchar('-');ans=q-p;}
	else ans=p-q;
	//减法
	
	ans=p*q; 
	//乘法 (高精乘以高精)
	
	ans=p/q;
	//(高精除以高精)
	//(高精除以单精详见国王游戏)
	ans.print();
	re 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值