模板:高精度

先把自己以前打的高精度模板放上来吧,凑一篇的样子(原题: 洛谷1932 )。。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef int ll;
struct node{ll a[20001],flag,size;};
inline node del(node t){
	while(t.a[t.size]==0&&t.size>1)t.size--;return t;
}
inline node add(node t){
	while(t.a[t.size+1]>0)t.size++;
	while(t.a[t.size]>9)t.a[t.size+1]+=t.a[t.size]/10,t.a[t.size++]%=10;
	return t;
}
inline node change(ll x){
	node c;memset(c.a,0,sizeof(c.a));
	if(x==0){c.size=c.flag=1;return c;}ll t=x;
	if(t<0)t*=-1,c.flag=-1;else c.flag=1;c.size=0;
	for(;t;t/=10)c.a[++c.size]=t%10;return c;
}
inline bool operator<(node y,node x){
	if(x.flag!=y.flag)return y.flag==-1;
	if(y.flag==-1){
		if(y.size!=x.size)return y.size>x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]>x.a[i];
	}else{
		if(y.size!=x.size)return y.size<x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]<x.a[i];
	}return 0;
}
inline bool operator<=(node y,node x){
	if(x.flag!=y.flag)return y.flag==-1;
	if(y.flag==-1){
		if(y.size!=x.size)return y.size>x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]>x.a[i];
	}else{
		if(y.size!=x.size)return y.size<x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]<x.a[i];
	}return 1;
}
inline bool operator>(node y,node x){
	if(x.flag!=y.flag)return y.flag==1;
	if(y.flag==1){
		if (y.size!=x.size)return y.size>x.size;
		for (ll i=y.size;i>0;i--)if (y.a[i]!=x.a[i])return y.a[i]>x.a[i];
	}else{
		if(y.size!=x.size)return y.size<x.size;
			for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]<x.a[i];
	}return 0;
}
inline bool operator>=(node y,node x){
	if(x.flag!=y.flag)return y.flag==1;
	if(y.flag==1){
		if(y.size!=x.size)return y.size>x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]>x.a[i];
	}else{
		if(y.size!=x.size)return y.size<x.size;
		for(ll i=y.size;i>0;i--)if(y.a[i]!=x.a[i])return y.a[i]<x.a[i];
	}return 1;
}
inline bool operator==(node y,node x){
	if(x.flag!=y.flag)return 0;
	for(ll i=1;i<=y.size;i++)if(y.a[i]!=x.a[i])return 0;
	return 1;
}
inline bool operator!=(node y,node x){
	if(x.flag!=y.flag)return 1;
	for(ll i=1;i<=y.size;i++)if(y.a[i]!=x.a[i])return 1;
	return 0;
}
inline node operator+(node y,node x){
	node c;memset(c.a,0,sizeof(c.a));node t1=y,t2=x;
	if(y.flag!=x.flag){t1.flag=t2.flag=1;
		if(t1==t2)return c.flag=c.size=1,c;
		if(t1<t2)c.flag=x.flag,swap(t1,t2);else c.flag=y.flag;
		for(ll i=1;i<=t1.size;i++){
			c.a[i]+=t1.a[i]-t2.a[i];
			if(c.a[i]<0)c.a[i]+=10,c.a[i+1]--;
		}c.size=t1.size;
	}else{
		if(y<x)swap(t1,t2);c.flag=y.flag;
		for(ll i=1;i<=t1.size;i++){
			c.a[i]+=t1.a[i]+t2.a[i];c.a[i+1]=c.a[i]/10;c.a[i]%=10;
		}c.size=t1.size;
	}return del(add(c));
}
inline node operator+(node y,ll x){
	return y+change(x);
}
inline node operator-(node y,node x){
	x.flag*=-1;return y+x;
}
inline node operator-(node y,ll x){
	return y-change(x);
}
inline node operator*(node y,node x){
	node c;memset(c.a,0,sizeof(c.a));c.flag=y.flag*x.flag;
	for(ll i=1;i<=y.size;i++)for(ll j=1;j<=x.size;j++){
		c.a[i+j-1]+=y.a[i]*x.a[j];c.a[i+j]+=c.a[i+j-1]/10;c.a[i+j-1]%=10;
	}c.size=y.size+x.size-1;return del(add(c));
}
inline node operator*(node y,ll x){
	node t=y;if(x<0)x=-x,t.flag*=-1;for(ll i=1;i<=t.size;i++)t.a[i]*=x;
	for(ll i=1;i<=t.size;i++)t.a[i+1]+=t.a[i]/10,t.a[i]%=10;return del(add(t));
}
inline node operator/(node y,node x){
	node t,c;memset(t.a,0,sizeof(t.a));memset(c.a,0,sizeof(c.a));
	c.flag=y.flag*x.flag;c.size=y.size;t.size=t.flag=x.flag=1;
	for(ll i=y.size;i>0;i--){
		t.a[1]=y.a[i];
		while(x<=t)t=t-x,c.a[i]++;t.size++;
		for(ll j=t.size;j>1;j--)t.a[j]=t.a[j-1];
	}return del(c);
}
inline node operator/(node y,ll x){
	ll k=0;if(x<0)x*=-1,y.flag*=-1;
	for(ll i=y.size;i>0;i--){
		k=k*10+y.a[i];y.a[i]=k/x;k=k%x;
	}return del(y);
}
inline node operator%(node y,node x){
	return y-y/x*x;
}
inline node operator%(node y,ll x){
	return y-y/x*x;
}
inline node read(){
	char s[10001];scanf("%s",s);node t;
	ll i=0;memset(t.a,0,sizeof(t.a));t.size=strlen(s);
	if(s[0]=='-')t.flag=-1,i=1;else t.flag=1;
	for(;i<t.size;i++)t.a[t.size-i]=s[i]-'0';
	if(t.flag==-1)t.size--;t=del(t);
	if(t.size==1&&t.a[1]==0)t.flag=1;return t;
}
inline void write(const node& x){
	if(x.flag==-1)putchar('-');
	for(ll i=x.size;i>0;i--)putchar(x.a[i]+'0');puts("");
}
node a,b;
int main(){
	a=read(),b=read();
	write(a+b),write(a-b),write(a*b),write(a/b),write(a%b);
	return 0;
}
posted @ 2018-07-25 13:12  zhoudong  阅读(171)  评论(0编辑  收藏  举报