高精度模板

1.压位(比较简单)

const int up = 10;
const int M = 1000;
struct High {
    int c[up + 5];
    void out () {
        int poi = up;
        while (poi >= 1 && c[poi] == 0) poi--;
        write (c[poi--]);
        while (poi >= 1) printf ("%03d", c[poi--]);
    }
    High () {
        memset (c, 0, sizeof c);
    }
    High (int x) {
        int poi = 1;
        while (x) {
            c[poi++] = x % M;
            x /= M;
        }
        while (poi <= up)
            c[poi++] = 0;
    }
};
bool operator < (High x, High y) {
    for (int i = up; i >= 1; i--) {
        if (x.c[i] < y.c[i]) return 1;
        if (x.c[i] > y.c[i]) return 0;
    }
    return 0;
}
bool operator > (High x, High y) {
    for (int i = up; i >= 1; i--) {
        if (x.c[i] > y.c[i]) return 1;
        if (x.c[i] < y.c[i]) return 0;
    }
    return 0;
}
bool operator == (High x, High y) {
    for (int i = up; i >= 1; i--)
        if (x.c[i] != y.c[i]) return 0;
    return 1;
}
High operator + (High x, High y) {
    High res;
    for (int i = 1; i <= up; i++) {
        res.c[i] += x.c[i] + y.c[i];
        res.c[i + 1] += res.c[i] / M;
        res.c[i] %= M;
    }
    return res;
}
High operator * (High x, High y) {
    High res;
    for (int i = 1; i <= up; i++) {
        for (int j = 1; j <= up && i + j - 1 <= up; j++) {
            res.c[i + j - 1] += x.c[i] * y.c[j];
            res.c[i + j] += res.c[i + j - 1] / M;
            res.c[i + j - 1] %= M;
        }
    }
    return res;
}
High operator - (High x, High y) {
    High res;
    for (int i = 1; i <= up; i++) {
        res.c[i] = x.c[i] - y.c[i];
        if (res.c[i] < 0) {
            res.c[i] += M;
            res.c[i + 1] -= 1;
        }
    }
    return res;
}

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; }
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);
}

3.压位

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值