高精度

1.普通

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
struct high_precision {
	vector <int> c;
	
	high_precision () {
		c.clear ();
	}
	void colour (int len) {
		while (c.size () < len) c.push_back (0);
	}
	void operator = (const string x) {
		c.resize (x.size ());
		for (int i = 0; i < x.size (); i++) {
			c[x.size () - i - 1] = x[i] - '0';
		}
	}
	void operator = (int tem) {
		string x; x.clear ();
		while (tem) {
			x += tem % 10 + '0';
			tem /= 10;
		}
		reverse (x.begin (), x.end ());
		c.resize (x.size ());
		for (int i = 0; i < x.size (); i++) {
			c[x.size () - i - 1] = x[i] - '0';
		}
	}
	
	void operator = (LL tem) {
		string x; x.clear ();
		while (tem) {
			x += tem % 10 + '0';
			tem /= 10;
		}
		reverse (x.begin (), x.end ());
		c.resize (x.size ());
		for (int i = 0; i < x.size (); i++) {
			c[x.size () - i - 1] = x[i] - '0';
		}
	}
	void inout () {
		if (c.size () == 0)  {
			putchar ('0');
		}
		else {
			if (c[c.size () - 1] < 0) {
				putchar ('-');
				for (int i = c.size () - 1; i >= 0; i--) {
					printf ("%d", Abs (c[i]));
				}
			}
			else {
				for (int i = c.size () - 1; i >= 0; i--) {
					printf ("%d", c[i]);
				}
			}
		}
	}
};
template <typename T>
high_precision Change (T x) {
	string tem; tem.clear ();
	high_precision ans;
	while (x) {
		tem += x % 10 + '0';
		x /= 10;
	}
	reverse (tem.begin (), tem.end ()); 
	ans = tem;
	return ans;
}
bool operator < (high_precision x, high_precision y) {
	int lenx = x.c.size (), leny = y.c.size ();
	if (x.c[lenx - 1] < 0 && y.c[leny - 1] < 0) {
		if (lenx != leny) return lenx > leny;
		else {
			for (int i = lenx - 1; i >= 0; i--) {
				if (x.c[i] != y.c[i]) {
					return x.c[i] < y.c[i];
				}
			}
			return 0;
		}
	}
	else if (x.c[lenx - 1] < 0 && y.c[leny - 1] > 0) {
		return 1;
	}
	else if (x.c[lenx - 1] > 0 && y.c[leny - 1] < 0) {
		return 0;
	}
	else {
		if (lenx != leny) return lenx < leny;
		else {
			for (int i = lenx - 1; i >= 0; i--) {
				if (x.c[i] != y.c[i]) {
					return x.c[i] < y.c[i];
				}
			}
			return 0;
		}
	}
}
bool operator > (high_precision x, high_precision y) {
	return y < x;
}
bool operator == (high_precision x, high_precision y) {
	if (x.c.size () != y.c.size ()) return 0;
	for (int i = x.c.size () - 1; i >= 0; i--) {
		if (x.c[i] != y.c[i]) return 0;
	}
	return 1;
}
bool operator >= (high_precision x, high_precision y) {
	if (x > y) return 1;
	else if (x == y) return 1;
	else return 0;
}
bool operator <= (high_precision x, high_precision y) {
	if (x < y) return 1;
	else if (x == y) return 1;
	else return 0;
}
high_precision operator * (high_precision x, high_precision y) {
	high_precision ans;
	ans.colour (x.c.size () + y.c.size ());
	for (int i = 0; i < x.c.size (); i++) {
		for (int j = 0; j < y.c.size (); j++) {
			ans.c[i + j] += x.c[i] * y.c[j];
			ans.c[i + j + 1] += ans.c[i + j] / 10;
			ans.c[i + j] %= 10;
		}
	}
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	return ans;
}
template <typename T>
high_precision operator * (high_precision a, T y) {
	return a * Change (y);
}
high_precision operator + (high_precision x, high_precision y) {
	high_precision ans;
	int len = Max (x.c.size (), y.c.size ());
	ans.colour (len + 1);
	x.colour (len);
	y.colour(len);
	
	for (int i = 0; i < len; i++) {
		ans.c[i] += x.c[i] + y.c[i];
		ans.c[i + 1] += ans.c[i] / 10;
		ans.c[i] %= 10;
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans;
}
template <typename T>
high_precision operator + (high_precision a, T y) {
	return a + Change (y);
}
high_precision operator - (high_precision x, high_precision y) {
	high_precision ans;
	
	int len = Max (x.c.size (), y.c.size ()) + 3;
	x.colour (len); y.colour (len); ans.colour (len);
	
	bool check = 0;
	if (x < y) swap (x, y), check = 1;
	
	int flag = 0;
	for (int i = 0; i < ans.c.size (); i++) {
		ans.c[i] = x.c[i] - y.c[i] + flag;
		if (ans.c[i] < 0) {
			ans.c[i] += 10;
			flag = -1;
		}
		else {
			flag = 0;
		}
	}
	
	if (check) {
		for (int i = 0; i < ans.c.size (); i++) {
			ans.c[i] = -ans.c[i];
		}
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans; 
}
template <typename T>
high_precision operator - (high_precision a, T y) {
	return a - Change (y);
}
high_precision operator / (high_precision x, high_precision y) {
	high_precision ans, flag;
	ans.colour (x.c.size ());
	for (int i = x.c.size () - 1; i >= 0; i--) {
		flag.c.insert (flag.c.begin (), x.c[i]);
		int Index;
		for (int j = 1; j <= 10; j++) {
			high_precision res;
			res = y * j;
			Index = j;
			if (res > flag) break;
		}
		high_precision res;
		res = y * (Index - 1);
		flag = flag - res;
		ans.c[i] = Index - 1;
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans;
}
template <typename T>
high_precision operator / (high_precision x, T y) {
	return x / Change (y);
}
high_precision operator % (high_precision x, high_precision y) {
	high_precision res = x / y;
	return x - res * y;
}
template <typename T>
high_precision operator % (high_precision a, T y) {
	return a % Change (y);
}

2.压位

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Mod = 10000;
struct high_precision {
	vector <int> c;
	
	high_precision () {
		c.clear ();
	}
	void colour (int len) {
		while (c.size () < len) c.push_back (0);
	}
	void operator = (const string x) {
		c.resize (x.length ());
		for (int i = 0, j = 0; i < x.length (); i += 4, j++) {
			for (int k = 0; k < 4; k++) {
				c[j] = c[j] * 10 + x[k + j];
			}
		}
	}
	void operator = (int tem) {
		vector <int> x; x.clear ();
		while (tem) {
			x.push_back (tem % Mod);
			tem /= Mod;
		}
		reverse (x.begin (), x.end ());
		c.resize (x.size ());
		for (int i = 0; i < x.size (); i++) {
			c[x.size () - i - 1] = x[i];
		}
	}
	void operator = (LL tem) {
		string x; x.clear ();
		while (tem) {
			x += tem % Mod + '0';
			tem /= Mod;
		}
		reverse (x.begin (), x.end ());
		c.resize (x.size ());
		for (int i = 0; i < x.size (); i++) {
			c[x.size () - i - 1] = x[i] - '0';
		}
	}
	void inout () {
		if (c.size () == 0)  {
			putchar ('0');
		}
		else {
			if (c[c.size () - 1] < 0) {
				putchar ('-');
				for (int i = c.size () - 1; i >= 0; i--) {
					if (i != c.size () - 1) printf ("%04d", Abs (c[i]));
					else printf ("%d", Abs (c[i]));
				}
			}
			else {
				for (int i = c.size () - 1; i >= 0; i--) {
					if (i != c.size () - 1) printf ("%04d", Abs (c[i]));
					else printf ("%d", Abs (c[i]));
				}
			}
		}
	}
};
template <typename T>
high_precision Change (T x) {
	string tem; tem.clear ();
	high_precision ans;
	while (x) {
		tem += x % Mod + '0';
		x /= Mod;
	}
	reverse (tem.begin (), tem.end ()); 
	ans = tem;
	return ans;
}
bool operator < (high_precision x, high_precision y) {
	int lenx = x.c.size (), leny = y.c.size ();
	if (x.c[lenx - 1] < 0 && y.c[leny - 1] < 0) {
		if (lenx != leny) return lenx > leny;
		else {
			for (int i = lenx - 1; i >= 0; i--) {
				if (x.c[i] != y.c[i]) {
					return x.c[i] < y.c[i];
				}
			}
			return 0;
		}
	}
	else if (x.c[lenx - 1] < 0 && y.c[leny - 1] > 0) {
		return 1;
	}
	else if (x.c[lenx - 1] > 0 && y.c[leny - 1] < 0) {
		return 0;
	}
	else {
		if (lenx != leny) return lenx < leny;
		else {
			for (int i = lenx - 1; i >= 0; i--) {
				if (x.c[i] != y.c[i]) {
					return x.c[i] < y.c[i];
				}
			}
			return 0;
		}
	}
}
bool operator > (high_precision x, high_precision y) {
	return y < x;
}
bool operator == (high_precision x, high_precision y) {
	if (x.c.size () != y.c.size ()) return 0;
	for (int i = x.c.size () - 1; i >= 0; i--) {
		if (x.c[i] != y.c[i]) return 0;
	}
	return 1;
}
bool operator >= (high_precision x, high_precision y) {
	if (x > y) return 1;
	else if (x == y) return 1;
	else return 0;
}
bool operator <= (high_precision x, high_precision y) {
	if (x < y) return 1;
	else if (x == y) return 1;
	else return 0;
}
high_precision operator * (high_precision x, high_precision y) {
//	printf ("x: "); x.inout (); printf ("\ny: "); y.inout (); printf ("\n");
	
	high_precision ans;
	ans.colour (x.c.size () + y.c.size ());
	for (int i = 0; i < x.c.size (); i++) {
		for (int j = 0; j < y.c.size (); j++) {
			ans.c[i + j] += x.c[i] * y.c[j];
			ans.c[i + j + 1] += ans.c[i + j] / Mod;
			ans.c[i + j] %= Mod;
		}
	}
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
//	printf ("ans: "); ans.inout (); printf ("\n");
	return ans;
}
template <typename T>
high_precision operator * (high_precision a, T y) {
	return a * Change (y);
}
high_precision operator + (high_precision x, high_precision y) {
	high_precision ans;
	int len = Max (x.c.size (), y.c.size ());
	ans.colour (len + 1);
	x.colour (len);
	y.colour(len);
	
	for (int i = 0; i < len; i++) {
		ans.c[i] += x.c[i] + y.c[i];
		ans.c[i + 1] += ans.c[i] / Mod;
		ans.c[i] %= Mod;
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans;
}
template <typename T>
high_precision operator + (high_precision a, T y) {
	return a + Change (y);
}
high_precision operator - (high_precision x, high_precision y) {
	high_precision ans;
	
	int len = Max (x.c.size (), y.c.size ()) + 3;
	x.colour (len); y.colour (len); ans.colour (len);
	
	bool check = 0;
	if (x < y) swap (x, y), check = 1;
	
	int flag = 0;
	for (int i = 0; i < ans.c.size (); i++) {
		ans.c[i] = x.c[i] - y.c[i] + flag;
		if (ans.c[i] < 0) {
			ans.c[i] += Mod;
			flag = -1;
		}
		else {
			flag = 0;
		}
	}
	
	if (check) {
		for (int i = 0; i < ans.c.size (); i++) {
			ans.c[i] = -ans.c[i];
		}
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans; 
}
template <typename T>
high_precision operator - (high_precision a, T y) {
	return a - Change (y);
}
high_precision operator / (high_precision x, high_precision y) {
	high_precision ans, flag;
	ans.colour (x.c.size ());
	for (int i = x.c.size () - 1; i >= 0; i--) {
		flag.c.insert (flag.c.begin (), x.c[i]);
		int Index;
		for (int j = 1; j < Mod; j++) {
			high_precision res;
			res = y * j;
			Index = j;
			if (res > flag) break;
		}
		high_precision res;
		res = y * (Index - 1);
		flag = flag - res;
		ans.c[i] = Index - 1;
	}
	
	while (ans.c.size () >= 1 && ans.c[ans.c.size () - 1] == 0) ans.c.pop_back ();
	
	return ans;
}
template <typename T>
high_precision operator / (high_precision x, T y) {
	return x / Change (y);
}
high_precision operator % (high_precision x, high_precision y) {
	high_precision res = x / y;
	return x - res * y;
}
template <typename T>
high_precision operator % (high_precision a, T y) {
	return a % Change (y);
}
posted @ 2020-12-08 20:58  C2022lihan  阅读(72)  评论(0编辑  收藏  举报