int_1024(高精度模板)

int_1024

很多题目啊 __int128不够 需要高精(纯纯恶心人) 而我们又懒得打 遂有int_1024

整合高精

Elaina's code
#include<bits/stdc++.h>
using namespace std;
class DividedByZeroException {};
class int_1024{
   	private:
       	vector<char> digits;
       	bool sign;
       	void trim();
   	public:
       	int_1024(int);
       	int_1024(string&) ;
       	int_1024();
       	int_1024(const int_1024&);
       	int_1024 operator=(const int_1024& op2);
       	int_1024 abs() const;
       	int_1024 pow(int a);
       	friend int_1024 operator+=(int_1024&, const int_1024&);
       	friend int_1024 operator-=(int_1024&, const int_1024&);
       	friend int_1024 operator*=(int_1024&, const int_1024&);
       	friend int_1024 operator/=(int_1024&, const int_1024&) throw(DividedByZeroException);
       	friend int_1024 operator%=(int_1024&, const int_1024&) throw(DividedByZeroException);
       	friend int_1024 operator+(const int_1024&, const int_1024&);
       	friend int_1024 operator-(const int_1024&, const int_1024&);
       	friend int_1024 operator*(const int_1024&, const int_1024&);
       	friend int_1024 operator/(const int_1024&, const int_1024&) throw(DividedByZeroException);
       	friend int_1024 operator%(const int_1024&, const int_1024&) throw(DividedByZeroException);
       	friend int_1024 operator-(const int_1024&);
      	friend int_1024 operator++(int_1024&);
      	friend int_1024 operator++(int_1024&, int);
       	friend int_1024 operator--(int_1024&);
       	friend int_1024 operator--(int_1024&, int);
       	friend bool operator>(const int_1024&, const int_1024&);
       	friend bool operator<(const int_1024&, const int_1024&);
       	friend bool operator==(const int_1024&, const int_1024&);
       	friend bool operator!=(const int_1024&, const int_1024&);
       	friend bool operator>=(const int_1024&, const int_1024&);
       	friend bool operator<=(const int_1024&, const int_1024&);
       	friend ostream& operator<<(ostream&, const int_1024&);
       	friend istream& operator>>(istream&, int_1024&);
   	public:
       	static const int_1024 ZERO;
       	static const int_1024 ONE;
       	static const int_1024 TEN;
};
const int_1024 int_1024::ZERO=int_1024(0);
const int_1024 int_1024::ONE=int_1024(1);
const int_1024 int_1024::TEN=int_1024(10);
int_1024::int_1024(){
   	sign=true;
}
int_1024::int_1024(int val){
   	if (val>=0){
       	sign=true;
   	}
   	else{
       	sign=false;
       	val*=(-1);
   	}
   	do{
       	digits.push_back((char)(val%10));
       	val/=10;
   	}while(val!=0);
}
int_1024::int_1024(string& def){
   	sign=true;
   	for(string::reverse_iterator iter=def.rbegin();iter<def.rend();iter++){
      	char ch=(*iter);
       	if(iter==def.rend()-1){
           	if(ch=='+'){
               	break;
           	}
           	if(ch=='-'){
               	sign=false;
               	break;
           	}
       	}
       	digits.push_back((char)((*iter)-'0'));
   	}
   	trim();
}
void int_1024::trim(){
   	vector<char>::reverse_iterator iter=digits.rbegin();
   	while(!digits.empty()&&(*iter)==0){
       	digits.pop_back();
       	iter=digits.rbegin();
   	}
   	if(digits.size()==0){
       	sign = true;
       	digits.push_back(0);
   	}
}
int_1024::int_1024(const int_1024& op2){
   	sign=op2.sign;
   	digits=op2.digits;
}
int_1024 int_1024::operator=(const int_1024& op2){
   	digits=op2.digits;
   	sign=op2.sign;
   	return (*this);
}
int_1024 int_1024::abs() const{
   	if(sign){
       	return *this;
   	}
   	else{
       	return -(*this);
   	}
}
int_1024 int_1024::pow(int a){
   	int_1024 res(1);
   	for(int i=0;i<a;i++){
       	res*=(*this);
   	}
   	return res;
}
int_1024 operator+=(int_1024& op1, const int_1024& op2){
   	if (op1.sign == op2.sign){
       	vector<char>::iterator iter1;
       	vector<char>::const_iterator iter2;
       	iter1=op1.digits.begin();
       	iter2=op2.digits.begin();
       	char to_add=0;
       	while (iter1!=op1.digits.end()&&iter2!=op2.digits.end()){
           	(*iter1)=(*iter1)+(*iter2)+to_add;
           	to_add=((*iter1)>9);
           	(*iter1)=(*iter1)%10;
           	iter1++;
           	iter2++;
       	}
       	while(iter1!=op1.digits.end()){
           	(*iter1)=(*iter1)+to_add;
           	to_add=((*iter1)>9);
           	(*iter1)%=10;
           	iter1++;
       	}
       	while(iter2!=op2.digits.end()){
           	char val=(*iter2)+to_add;
           	to_add=(val > 9);
           	val%=10;
           	op1.digits.push_back(val);
           	iter2++;
       	}
       	if(to_add!=0){
           	op1.digits.push_back(to_add);
       	}
       	return op1;
   	}
   	else{
       	if(op1.sign){
           	return op1-=(-op2);
       	}
       	else{
           	return op1=op2-(-op1);
       	}
   	}
}
int_1024 operator-=(int_1024& op1, const int_1024& op2){
   	if(op1.sign == op2.sign){
       	if(op1.sign){
           	if(op1<op2){
               	return op1=-(op2-op1);
           	}
       	}
       	else{
           	if(-op1 > -op2){
              	return op1=-((-op1)-(-op2));
           	}
           	else{
               	return op1=(-op2)-(-op1);
           	}
       	}
       	vector<char>::iterator iter1;
       	vector<char>::const_iterator iter2;
       	iter1=op1.digits.begin();
       	iter2=op2.digits.begin();
       	char to_substract=0;
       	while(iter1!=op1.digits.end()&&iter2!=op2.digits.end()){
           	(*iter1)=(*iter1)-(*iter2)-to_substract;
           	to_substract=0;
           	if((*iter1)<0){
               	to_substract=1;
               	(*iter1)+=10;
           	}
	        iter1++;
	        iter2++;
       	}
       	while(iter1!=op1.digits.end()){
           	(*iter1)=(*iter1)-to_substract;
           	to_substract=0;
           	if((*iter1)<0){
               	to_substract=1;
               	(*iter1)+=10;
           	}
           	else{
               	break;
           	}
           	iter1++;
       	}
       	op1.trim();
       	return op1;
   	}
   	else{
       	if(op1>int_1024::ZERO){
           	return op1+=(-op2);
       	}
       	else{
           	return op1=-(op2+(-op1));
       	}
   	}
}
int_1024 operator*=(int_1024& op1, const int_1024& op2){
   	int_1024 result(0);
   	if(op1==int_1024::ZERO||op2==int_1024::ZERO){
       	result=int_1024::ZERO;
   	}
   	else{
       	vector<char>::const_iterator iter2=op2.digits.begin();
       	while(iter2!=op2.digits.end()){
           	if(*iter2!=0){
               	deque<char> temp(op1.digits.begin(), op1.digits.end());
               	char to_add=0;
               	deque<char>::iterator iter1=temp.begin();
               	while (iter1 != temp.end()) {
                   	(*iter1) *= (*iter2);
                   	(*iter1) += to_add;
                   	to_add = (*iter1) / 10;
                   	(*iter1) %= 10;
                   	iter1++;
               	}
               	if (to_add != 0) {
                   	temp.push_back(to_add);
               	}
               	int num_of_zeros = iter2 - op2.digits.begin();
               	while (num_of_zeros--) {
                   	temp.push_front(0);
               	}
               	int_1024 temp2;
               	temp2.digits.insert(temp2.digits.end(), temp.begin(), temp.end());
               	temp2.trim();
               	result = result + temp2;
           	}
           	iter2++;
       	}
       	result.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
   	}
   	op1 = result;
   	return op1;
}
int_1024 operator/=(int_1024& op1, const int_1024& op2) throw(DividedByZeroException) {
   	if (op2 == int_1024::ZERO) {
       	throw DividedByZeroException();
   	}
   	int_1024 t1 = op1.abs(), t2 = op2.abs();
   	if (t1 < t2) {
       	op1 = int_1024::ZERO;
       	return op1;
   	}
   	deque<char> temp;
   	vector<char>::reverse_iterator iter = t1.digits.rbegin();
   	int_1024 temp2(0);
   	while (iter != t1.digits.rend()) {
       	temp2 = temp2 * int_1024::TEN + int_1024((int)(*iter));
       	char s = 0;
       	while (temp2 >= t2) {
           	temp2 = temp2 - t2;
           	s = s + 1;
       	}
       	temp.push_front(s);
       	iter++;
   	}
   	op1.digits.clear();
   	op1.digits.insert(op1.digits.end(), temp.begin(), temp.end());
   	op1.trim();
   	op1.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
   	return op1;
}
int_1024 operator%=(int_1024& op1, const int_1024& op2) throw(DividedByZeroException) {
   	return op1 -= ((op1 / op2) * op2);
}
int_1024 operator+(const int_1024& op1, const int_1024& op2) {
   	int_1024 temp(op1);
   	temp += op2;
   	return temp;
}
int_1024 operator-(const int_1024& op1, const int_1024& op2) {
   	int_1024 temp(op1);
   	temp -= op2;
   	return temp;
}
int_1024 operator*(const int_1024& op1, const int_1024& op2) {
   	int_1024 temp(op1);
   	temp *= op2;
   	return temp;
}
int_1024 operator/(const int_1024& op1, const int_1024& op2) throw(DividedByZeroException) {
   	int_1024 temp(op1);
   	temp /= op2;
   	return temp;
}
int_1024 operator%(const int_1024& op1, const int_1024& op2) throw(DividedByZeroException) {
   	int_1024 temp(op1);
   	temp %= op2;
   	return temp;
}
int_1024 operator-(const int_1024& op) {
   	int_1024 temp = int_1024(op);
   	temp.sign = !temp.sign;
   	return temp;
}
int_1024 operator++(int_1024& op) {
   	op += int_1024::ONE;
   	return op;
}
int_1024 operator++(int_1024& op, int x) {
   	int_1024 temp(op);
   	++op;
   	return temp;
}
int_1024 operator--(int_1024& op) {
   	op -=  int_1024::ONE;
   	return op;
}
int_1024 operator--(int_1024& op, int x) {
   	int_1024 temp(op);
   	--op;
   	return temp;
}
bool operator<(const int_1024& op1, const int_1024& op2) {
   	if (op1.sign != op2.sign) {
       	return !op1.sign;
   	}
   	else {
       	if (op1.digits.size() != op2.digits.size())
           	return (op1.sign && op1.digits.size() < op2.digits.size())
               	|| (!op1.sign && op1.digits.size() > op2.digits.size());
       	vector<char>::const_reverse_iterator iter1, iter2;
       	iter1 = op1.digits.rbegin();
       	iter2 = op2.digits.rbegin();
       	while (iter1 != op1.digits.rend()) {
           	if (op1.sign &&  *iter1 < *iter2) {
               	return true;
           	}
           	if (op1.sign &&  *iter1 > *iter2) {
               	return false;
           	}
           	if (!op1.sign &&  *iter1 > *iter2) {
               	return true;
           	}
           	if (!op1.sign &&  *iter1 < *iter2) {
               	return false;
           	}
           	iter1++;
           	iter2++;
       	}
       	return false;
   	}
}
bool operator==(const int_1024& op1, const int_1024& op2) {
   	if (op1.sign != op2.sign  || op1.digits.size() != op2.digits.size()) {
       	return false;
   	}
   	vector<char>::const_iterator iter1, iter2;
   	iter1 = op1.digits.begin();
   	iter2 = op2.digits.begin();
   	while (iter1 != op1.digits.end()) {
       	if (*iter1 != *iter2) {
           	return false;
       	}
       	iter1++;
       	iter2++;
   	}
   	return true;
}
bool operator!=(const int_1024& op1, const int_1024& op2) {
   	return !(op1 == op2);
}
bool operator>=(const int_1024& op1, const int_1024& op2) {
   	return (op1 > op2) || (op1 == op2);
}
bool operator<=(const int_1024& op1, const int_1024& op2) {
   	return (op1 < op2) || (op1 == op2);
}
bool operator>(const int_1024& op1, const int_1024& op2) {
   	return !(op1 <= op2);
}
ostream& operator<<(ostream& stream, const int_1024& val) {
   	if (!val.sign) {
       	stream << "-";
   	}
   	for (vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++) {
       	stream << (char)((*iter) + '0');
   	}
   	return stream;
}
istream& operator>>(istream& stream, int_1024& val) {
   	string str;
   	stream >> str; 
   	val = int_1024(str);
   	return stream;
}

signed main(){
	int_1024 a,b;
	cin>>a>>b;
	cout<<a+b;
}


使用说明

某些楞憨竟然不会用
所以在这附上使用说明:

使用说明 定义:与int一样 直接int_1024 a;

赋初值:a=(数字);

输入输出:cin>>a;cout<<a;

加减乘除模同正常int的用法(== != < > <= >=也同)

  1. 本高精模板使用大量STL
    可能会稍微有亿点点慢

  2. 数据范围:
    由于本高精函数基于STL,所以只要有内存就能无限开

  3. 输入仅支持cin,cout

  4. 不支持位运算

  5. 支持开数组、绝对值、幂运算

  6. 记得把#define int long long 注释掉

特别注意

\[\LARGE \color{red}{请看评论} \]

posted @ 2024-06-18 10:00  Elaina_0  阅读(76)  评论(2编辑  收藏  举报
浏览器标题切换
浏览器标题切换end