高精度封装

为了保证效率,部分代码拆开CV了一下。

#include<bits/stdc++.h>
using namespace std;
#define rg register
#define I inline
#define gc getchar
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define per(i, a, b) for(int i = a; i >= b; --i)
I int read(){
	rg char ch = gc();
	rg int x = 0, f = 0;
	while(!isdigit(ch)) f |= (ch == '-'), ch = gc();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
	return f ? -x : x; 
}
I void put(int x){
	if(!x){ putchar('\n'); return;}
	putchar((x % 10) ^ 48);
	put(x / 10);
}
const int N = 43;
int n, k;
struct node{
	int len;
	vector<int> c;
	bool flag;
	node(){
		len = 0; flag = false;
		c.clear();
	}
	const bool operator < (const node &rhs){
		if(len < rhs.len) return true;
		if(len > rhs.len) return false;
		per(i, len - 1, 0){
			if(c[i] < rhs.c[i]) return true;
			if(c[i] > rhs.c[i]) return false;
		}
		return false;
	}
	node operator + (const node &rhs){
		if(flag == rhs.flag){
			node v;
			v.flag = flag;
			rg int now, l(0), n = v.len = max(len, rhs.len);
			rep(i, 0, n - 1){
				now = (i < len ? c[i] : 0) + (i < rhs.len ? rhs.c[i] : 0) + l;
				//cout << c[i] << " " << rhs.c[i] << " " << now << " " << l << endl;
				v.c.push_back(now % 10);
				l = now / 10;
			}
			if(l) ++v.len, v.c.push_back(l);
			while(v.len && !(*(v.c.end() - 1))) --v.len, v.c.pop_back();
			return v;
		}else{
			node v;
			rg int now, l(0), n = v.len = max(len, rhs.len);
			if((*this) < rhs){
				v.flag = !flag;
				rep(i, 0, n - 1){
					now = rhs.c[i] - (i < len ? c[i] : 0) + l;
					//cout << c[i] << " " << rhs.c[i] << " " << now << " " << l << endl;
					if(now < 0) now += 10, l = -1;
					else l = now / 10;
					v.c.push_back(now % 10);
				}
				if(l) ++v.len, v.c.push_back(l);
				while(v.len && !(*(v.c.end() - 1))) --v.len, v.c.pop_back();
				return v;
			}else{
				v.flag = flag;
				rep(i, 0, n - 1){
					now = c[i] - (i < rhs.len ? rhs.c[i] : 0) + l;
					//cout << c[i] << " " << rhs.c[i] << " " << now << " " << l << endl;
					if(now < 0) now += 10, l = -1;
					else l = now / 10;
					v.c.push_back(now % 10);
				}
				if(l) ++v.len, v.c.push_back(l);
				while(v.len && !(*(v.c.end() - 1))) --v.len, v.c.pop_back();
				return v;
			}
			//copy -
		}
	}
	node operator - (const node &rhs){
		if(flag == rhs.flag){
			node v;
			rg int now, l(0), n = v.len = max(len, rhs.len);
			if((*this) < rhs){
				v.flag = !flag;
				rep(i, 0, n - 1){
					now = rhs.c[i] - (i < len ? c[i] : 0) + l;
					//cout << c[i] << " " << rhs.c[i] << " " << now << " " << l << endl;
					if(now < 0) now += 10, l = -1;
					else l = now / 10;
					v.c.push_back(now % 10);
				}
				if(l) ++v.len, v.c.push_back(l);
				while(v.len && !(*(v.c.end() - 1))) --v.len, v.c.pop_back();
				return v;
				// copy +
			}else{
				v.flag = flag;
				rep(i, 0, n - 1){
					now = c[i] - (i < rhs.len ? rhs.c[i] : 0) + l;
					//cout << c[i] << " " << rhs.c[i] << " " << now << " " << l << endl;
					if(now < 0) now += 10, l = -1;
					else l = now / 10;
					v.c.push_back(now % 10);
				}
				if(l) ++v.len, v.c.push_back(l);
				while(v.len && !(*(v.c.end() - 1))) --v.len, v.c.pop_back();
				return v;
			}
		}else{
			node t = rhs; t.flag = flag;
			return *this + t;
		}
	}
	node operator * (const int x){
		node v;
		rg int now, l(0);
		rep(i, 0, len - 1){
			now = c[i] * x + l;
			v.c.push_back(now % 10);
			l = now / 10;
		}
		v.len = len;
		if(l) ++v.len, v.c.push_back(l);
	}
	node operator * (const node &rhs) const {
		node v;
		v.len = len + rhs.len + 1;
		v.c.resize(v.len);
		rep(i, 0, len - 1) rep(j, 0, rhs.len - 1) v.c[i + j] += c[i] * rhs.c[j];
		rep(i, 0, v.len - 2){
			v.c[i + 1] += v.c[i] / 10;
			v.c[i] %= 10;
		}
		while(v.len && !(*(v.c.end() - 1))) v.c.pop_back(), --v.len;
		return v;
	}
	I node(int x){
		len = 0; c.clear(); flag = false;
		while(x){
			c.push_back(x % 10);
			x /= 10;
		}
		len = c.size();
	}
	I node(char* s){
		c.clear(); flag = false;
		len = strlen(s + 1);
		per(i, len, 1) c.push_back(s[i] ^ 48);
	}
	I void init(int x){
		c.clear(); flag = false;
		while(x){
			c.push_back(x % 10);
			x /= 10;
		}
		len = c.size();
	}
	I void init(char* s){
		c.clear(); flag = false;
		len = strlen(s + 1);
		per(i, len, 1) c.push_back(s[i] ^ 48);
	}
	I void output(){
		//cout << len << endl;
		if(!len){putchar('0'); return;}
		if(flag) putchar('-');
		per(i, len - 1, 0) putchar(c[i] ^ 48);
		putchar('\n');
	}
}f[N][7];
char s[N];
int main(){
	
	return 0;
}
上一篇:Alyona and towers CodeForces - 739C (线段树)


下一篇:复习类的几个基本函数