[ 冲刺NOIP2016 ] 高精度模板

包含了+ - * ^ == += *= -=这几个运算,iout用于输出高精度数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define reps(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=105;
struct Int{
	int a[MAXN],len;
	Int(){len=0;memset(a,0,sizeof(a));}
};
void iout(Int a){
	reps(i,a.len,1){
		printf("%d",a.a[i]);
	}
	cout<<endl;
}
//逻辑运算符 
bool operator !=(Int a,Int b){
	if(a.len!=b.len) return true;
	reps(i,a.len,1){
		if(a.a[i]!=b.a[i]) return true;
	}
	return false;
}
bool operator ! (Int a){
	if(a.len!=0) return false;
	return true;
}
bool operator < (Int a,Int b){
	if(a.len!=b.len) return a.len<b.len;
	reps(i,a.len,1){
		if(a.a[i]!=b.a[i]) return a.a[i]<b.a[i];
	}
	return false;
}
bool operator > (Int a,Int b){
	if(a.len!=b.len) return a.len>b.len;
	reps(i,a.len,1){
		if(a.a[i]!=b.a[i]) return a.a[i]>b.a[i];
	}
	return false;
}
bool operator ==(Int a,Int b){
	if(a.len!=b.len) return false;
	reps(i,a.len,1){
		if(a.a[i]!=b.a[i])return false;
	}
	return true;
}
//操作部分 
Int  operator * (Int a,Int b){
	int la=a.len,lb=b.len;
	Int c;
	int maxx=la+lb;
	rep(j,1,lb)
		reps(i,la,1)
			c.a[i+j-1]+=a.a[i]*b.a[j];
	reps(i,maxx,1){
		if(c.a[i]!=0) c.len++;
		if(c.a[i]>=10) c.a[i+1]+=c.a[i]/10,c.a[i]%=10;
	}
	return c;
}
Int  operator + (Int a,Int b){
	Int c;
	int maxx=max(a.len,b.len);
	int tmp=0;
	rep(i,1,maxx){
		c.len++;
		c.a[i]=a.a[i]+b.a[i]+tmp;
		tmp=0;
		if(c.a[i]>=10) tmp=c.a[i]-10,c.a[i]-=10;
	}
	if(tmp!=0) c.a[++c.len]=tmp;
	return c;
}
Int  operator - (Int a,Int b){
	Int c;
	int la=a.len,lb=b.len;
	int minn=lb,maxx=la;
	reps(i,maxx,minn+1){
		c.a[i]=max(a.a[i],b.a[i]);
	}
	rep(i,1,minn)
		if(a.a[i]<b.a[i]) a.a[i+1]--,c.a[i]=a.a[i]+10-b.a[i];
		else c.a[i]=a.a[i]-b.a[i];
	reps(i,maxx,1) 
		if(c.a[i]!=0){
			c.len=i;
			break;
		}
	return c;
}
//组合运算 
Int  operator *= (Int &a,Int b){
	int la=a.len,lb=b.len;
	Int c;
	int maxx=la+lb;
	rep(i,1,la)
		rep(j,1,lb)
			c.a[i+j]=a.a[i]*b.a[j];
	reps(i,maxx,1){
		if(c.a[i]!=0) c.len++;
		if(c.a[i]>=10) c.a[i+1]+=c.a[i]/10,c.a[i]=c.a[i]%10;
	}
	a=c;
}
Int  operator += (Int &a,Int b){
	Int c;
	int maxx=max(a.len,b.len);
	int tmp=0;
	rep(i,1,maxx){
		c.len++;
		c.a[i]=a.a[i]+b.a[i]+tmp;
		tmp=0;
		if(c.a[i]>=10) tmp=c.a[i]-10,c.a[i]-=10;
	}
	if(tmp!=0) c.a[++c.len]=tmp;
	a=c;
}
Int  operator -= (Int &a,Int b){
	Int c,d;
	d=a;
	int la=d.len,lb=b.len;
	int minn=lb,maxx=la;
	reps(i,maxx,minn+1){
		c.a[i]=max(d.a[i],b.a[i]);
	}
	rep(i,1,minn)
		if(d.a[i]<b.a[i]) d.a[i+1]--,c.a[i]=d.a[i]+10-b.a[i];
		else c.a[i]=d.a[i]-b.a[i];
	reps(i,maxx,1) 
		if(c.a[i]!=0){
			c.len=i;
			break;
		}
	a=c;
}
void operator |=(Int &a,int b){
	a=Int();
	while(b>=10){
		a.len++;
		a.a[a.len]=b%10;
		b/=10;
	}
	if(b!=0){
		a.a[++a.len]=b;
	}
}
Int  operator ^ (Int a,int b){
	Int ans;
	ans|=1;
	for(;b;a=a*a,b>>=1)
		if(b&1){
			ans=ans*a;
		}
	
	reps(i,MAXN,1)
		if(ans.a[i]!=0)
			ans.len=i+1;
	return ans;
}

int main(){
	Int po,cd;
	po|=17893;
	cd|=3;
	iout(po-cd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值