BigInt类

持续维护中,根据需求更新。

struct BigInt {
	int l, a[23333], base;
	BigInt() {
		l = 0, base = 10;
		memset(a, 0, sizeof(a));
	}
	BigInt Trans(int x) {
		BigInt y;
		while (x)
			y.a[++y.l] = x % y.base, x /= y.base;
		return y;
	}
	friend BigInt operator +(BigInt x, BigInt y) {
		BigInt z;
		z.l = std::max(x.l, y.l);
		for (int i = 1; i <= z.l; i++)
			z.a[i] += x.a[i] + y.a[i], z.a[i + 1] += z.a[i] / x.base, z.a[i] %= x.base;
		if (z.a[z.l + 1]) z.l++;
		return z;
	}
	friend BigInt operator +(BigInt x, int y) {
		BigInt tmp = tmp.Trans(y);
		return x + tmp;
	}
	friend BigInt operator -(BigInt x, BigInt y) {
		BigInt z;
		z.l = std::max(x.l, y.l);
		for (int i = 1; i <= z.l; i++) {
			if (x.a[i] < y.a[i]) x.a[i] += x.base, x.a[i + 1]--;
			z.a[i] = x.a[i] - y.a[i];
		}
		while (!z.a[z.l] && z.l) z.l--;
		if (z.l == 0) z.a[1] = 1, z.l = 1;
		return z;
	}
	friend BigInt operator *(BigInt x, BigInt y) {
		BigInt z;
		z.l = x.l + y.l;
		if ((x.l == 1 && x.a[1] == 0) || (y.l == 1 && y.a[1] == 0)) {
			z.l = 1;
			return z;
		}
		for (int i = 1; i <= x.l; i++)
			for (int j = 1; j <= y.l; j++)
				z.a[i + j - 1] += x.a[i] * y.a[j], z.a[i + j] += z.a[i + j - 1] / x.base, z.a[i + j - 1] %= x.base;
		while (!z.a[z.l] && z.l) z.l--;
		if (!z.l) {z.l = 1, z.a[1] = 0;}
		return z;
	}
	friend BigInt operator *(BigInt x, int y) {
		BigInt z; int l = x.l;
		for (int i = 1; i <= l; i++)
			z.a[i] += x.a[i] * y, z.a[i + 1] += z.a[i] / x.base, z.a[i] %= x.base;
		while (z.a[l + 1])
			l++, z.a[l + 1] += z.a[l] / x.base, z.a[l] %= x.base;
		z.l = l;
		while (!z.a[z.l]) z.l--;
		return z;
	}
	friend BigInt operator /(BigInt x, int y) {
		BigInt z; z.l = x.l;
		int t = 0;
		for (int i = x.l; i >= 1; i--)
			t = t * 10 + x.a[i], z.a[i] = t / y, t %= y;
		while (!z.a[z.l]) z.l--;
		return z;
	}
	void print() {
		for (int i = l; i >= 1; i--)
			printf("%d", a[i]);
		printf("
");
	}
};
原文地址:https://www.cnblogs.com/gekoo/p/11222781.html