【高精度】

新版本

#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
std :: string read_in_LLint;
template<typename Tec,const long long int maxbit>
class LLINT {
	private : 
		std :: vector<Tec> num;
		int m_b_l;
		int the_signal;
	public : 
		//初始化與再初始化部分;
		LLINT () {
			num.push_back(0);
			m_b_l = log10(maxbit); 
			the_signal = 0;
		}
		LLINT (int the_size) {
			num.push_back(0);
			m_b_l = log10(maxbit); 
			the_signal = 0; 
			num.resize(the_size);
		}
		void init(Tec new_value) {
			if(new_value < 0) {
				the_signal = 1;
				new_value = -new_value;
			}
			num.clear();
			num.push_back(new_value);
			while(num.back() >= maxbit) {
				num.push_back(num.back()/maxbit);
				num[num.size()-2] %= maxbit;
			}
		}
		void resize(int new_size) { 
			num.resize(new_size);
		}
		void clear() {
			num.clear();
		}
		//訪問部分;
		Tec at(int place) {
			if(place >= num.size()) {
				std :: cerr << "LLINT >> at(int place) >> unknown space." << std :: endl;
				return -1;
			} else 
				return num[place];
		}
		Tec& operator [] (int place) {
			return num[place];
		}
		Tec& back() {
			return num.back();
		}
		int& LSIGN() {
			return the_signal;
		}
		std :: vector<Tec>& LNUM() {
			return num;
		}
		//其它;
		void push(Tec tar) {
			num.push_back(tar);
		}
		void emplace(Tec tar) {
			num.emplace_back(tar);
		}
		void pop() {
			if(num.empty()) 
				return;
			num.pop_back();
		}
		int size() {
			return num.size();
		}
		bool empty() {
			return num.empty();
		}
		//輸入與輸出部分;
		void scan() {
			std :: cin >> read_in_LLint;
			if(read_in_LLint[0] == '-') {
				the_signal = 1;
				read_in_LLint.erase(read_in_LLint.begin());
			}
			resize(ceil(read_in_LLint.length()/(double)m_b_l));
			for(register int i = 0;i < size();++i) 
				for(register int j = (int)read_in_LLint.length()-(i+1)*m_b_l;j < (int)read_in_LLint.length()-i*m_b_l;++j) {
					if(!isdigit(read_in_LLint[j])||j < 0) 
						continue;
					num[i] = (num[i]<<1)+(num[i]<<3)+(read_in_LLint[j]^48);
				}
		}
		void print(char end_char) {
			if(the_signal) 
				std :: cout << '-';
			std :: cout << num.back();
			for(register int i = num.size()-2;i >= 0;--i) 
				std :: cout << std :: setw(m_b_l) << std :: setfill('0') << num[i];
			std :: cout << end_char;
		}
		//大小判斷部分;
		template<typename Tecin,const long long int maxbitin>
		friend bool operator > (LLINT<Tecin,maxbitin>,LLINT<Tecin,maxbitin>);
		template<typename Tecin,const long long int maxbitin>
		friend bool operator < (LLINT<Tecin,maxbitin>,LLINT<Tecin,maxbitin>);
		template<typename Tecin,const long long int maxbitin>
		friend bool operator == (LLINT<Tecin,maxbitin>,LLINT<Tecin,maxbitin>);
		template<typename Tecin,const long long int maxbitin>
		friend bool operator >= (LLINT<Tecin,maxbitin>,LLINT<Tecin,maxbitin>);
		template<typename Tecin,const long long int maxbitin>
		friend bool operator <= (LLINT<Tecin,maxbitin>,LLINT<Tecin,maxbitin>);
		template<typename Tecin,const long long int maxbitin>
		friend bool operator > (LLINT<Tecin,maxbitin> ob,LLINT<Tecin,maxbitin> sub) {
			if(ob.the_signal^sub.the_signal) {
				return ob.the_signal == 1;
			} else {
				if(ob.the_signal == 1) {
					if(ob.size()^sub.size()) 
						return ob.size() < sub.size();
					return std::lexicographical_compare(ob.num.rbegin(),ob.num.rend(),sub.num.rbegin(),sub.num.rend());
				} else {
					if(ob.size()^sub.size()) 
						return ob.size() > sub.size();
					return std::lexicographical_compare(sub.num.rbegin(),sub.num.rend(),ob.num.rbegin(),ob.num.rend());
				}
			}
		}
		template<typename Tecin,const long long int maxbitin>
		friend bool operator < (LLINT<Tecin,maxbitin> ob,LLINT<Tecin,maxbitin> sub) {
			if(ob.the_signal^sub.the_signal) {
				return ob.the_signal == 0;
			} else {
				if(ob.the_signal == 1) {
					if(ob.size()^sub.size()) 
						return ob.size() > sub.size();
					return std::lexicographical_compare(sub.num.rbegin(),sub.num.rend(),ob.num.rbegin(),ob.num.rend());
				} else {
					if(ob.size()^sub.size()) 
						return ob.size() < sub.size();
					return std::lexicographical_compare(ob.num.rbegin(),ob.num.rend(),sub.num.rbegin(),sub.num.rend());
				}
			}
		}
		template<typename Tecin,const long long int maxbitin>
		friend bool operator == (LLINT<Tecin,maxbitin> ob,LLINT<Tecin,maxbitin> sub) {
			return (ob.the_signal == sub.the_signal)&&(ob.num == sub.num);
		}
		template<typename Tecin,const long long int maxbitin>
		friend bool operator >= (LLINT<Tecin,maxbitin> ob,LLINT<Tecin,maxbitin> sub) {
			return !(ob < sub);
		}
		template<typename Tecin,const long long int maxbitin>
		friend bool operator <= (LLINT<Tecin,maxbitin> ob,LLINT<Tecin,maxbitin> sub) {
			return !(ob > sub);
		}
};
//重載運算符部分;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator + (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator + (LLINT<Tec,maxbit>,Tec);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator += (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator += (LLINT<Tec,maxbit>,Tec);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator * (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator * (LLINT<Tec,maxbit>,Tec);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator *= (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit>,Tec);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator / (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator /= (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator % (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator %= (LLINT<Tec,maxbit>,LLINT<Tec,maxbit>);
//基礎型運算部分;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LABS(LLINT<Tec,maxbit> ob) {
	ob.LSIGN() = 0;
	return ob;
}//返回一個LLINT的絕對值;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LMAKE(Tec ob) {
	LLINT<Tec,maxbit> res;
	res.init(ob);
	return res;
}//返回一個LLINT的絕對值;
template<typename Tec1,const long long int maxbit1,typename Tec2,const long long int maxbit2>
LLINT<Tec1,maxbit1> LTURN(LLINT<Tec2,maxbit2> &ob) {
	if(maxbit1 == maxbit2) {
		LLINT<Tec1,maxbit1> res;
		for(register auto i : ob.LNUM()) 
			res.emplace(i);
	} else if(maxbit2 > maxbit1) {
		LLINT<Tec1,maxbit1> res(ob.size());
		for(register auto i : ob.LNUM()) 
			res.emplace(i);
		for(register int i = 0;i < res.size()-1;++i) 
			if(res[i] >= maxbit1) {
				res[i+1] += res[i]/maxbit1;
				res[i] %= maxbit1;
			}
		while(res.back() >= maxbit1) {
			res.emplace(res.back()/maxbit1);
			res[res.size()-2] %= maxbit1;
		}
	}
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LADD (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	if(ob.size() > sub.size()) {
		LLINT<Tec,maxbit> res = ob;
		res.emplace(0);
		for(register auto i = 0;i < sub.size();++i) {
			res[i] += sub[i];
			if(res[i] >= maxbit) {
				res[i] -= maxbit;
				++res[i+1];
			}
		}
		for(register int i = sub.size();res[i] >= maxbit;++i) {
			res[i] -= maxbit;
			++res[i+1];
		}
		if(res.size() > 1&&!res.back()) 
			res.pop();
		return res;
	} else {
		LLINT<Tec,maxbit> res = sub;
		res.emplace(0);
		for(register auto i = 0;i < ob.size();++i) {
			res[i] += ob[i];
			if(res[i] >= maxbit) {
				res[i] -= maxbit;
				++res[i+1];
			}
		}
		for(register int i = ob.size();res[i] >= maxbit;++i) {
			res[i] -= maxbit;
			++res[i+1];
		}
		if(res.size() > 1&&!res.back()) 
			res.pop();
		return res;
	}
}//同符號加法;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LADD (LLINT<Tec,maxbit> ob,Tec &sub) {
	LLINT<Tec,maxbit> res = ob;
	res.emplace(0);
	res[0] += sub;
	for(register int i = 0;i < res.size()-1&&res[i] >= maxbit;++i) {
		res[i+1] += res[i]/maxbit;
		res[i] %= maxbit;
	}
	while(res.back() >= maxbit) {
		res.emplace(res.back()/maxbit);
		res[res.size()-2] %= maxbit;
	}
	if(res.size() > 1&&!res.back()) 
		res.pop();
	return res;
}//同符號加法2;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LMINUS (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	for(register int i = 0;i < sub.size();++i) {
		ob[i] -= sub[i];
		if(ob[i] < 0) {
			--ob[i+1];
			ob[i] += maxbit;
		}
	}
	for(register int i = sub.size();i < ob.size()&&ob[i] < 0;++i) {
		--ob[i+1];
		ob[i] += maxbit;
	}
	while(ob.size() > 1&&!ob.back()) 
		ob.pop();
	return ob;
}//同符號減法,要求ob大於等於sub;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LMULTI (LLINT<Tec,maxbit> ob,const Tec &sub,const int rbit) {
	LLINT<Tec,maxbit> res;
	res.clear();
	register __uint128_t multi_helper = 0;
	for(register int i = -rbit;i < ob.size();++i) {
		if(i < 0) 
			res.emplace(0);
		else {
			multi_helper += (__uint128_t)ob[i]*sub;
			if(multi_helper >= maxbit) {
				res.emplace(multi_helper%maxbit);
				multi_helper /= maxbit;
			} else {
				res.emplace(multi_helper);
				multi_helper = 0;
			}
		}
	}
	while(multi_helper) {
		if(multi_helper >= maxbit) {
			res.emplace(multi_helper%maxbit);
			multi_helper /= maxbit;
		} else {
			res.emplace(multi_helper);
			multi_helper = 0;
		}
	}
	while(res.size() > 1&&!res.back()) 
		res.pop();
	return res;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> LDEVIDE (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub,bool the_choice) {
	//the_choice == 0 for / ;
	//the_choice == 1 for % ;
	LLINT<Tec,maxbit> res;
	if(!sub.back()) {
		std :: cerr << "LLINT >> Lfunction >> LDEVIDE >> unexpected value 0 found in "
				<< (the_choice ? "modulo" : "divisor") << std :: endl;
		return the_choice ? ob : res;
	}
	res.LSIGN() = ob.LSIGN()^sub.LSIGN();
	ob = LABS(ob);
	sub = LABS(sub);
	if(ob < sub) 
		return the_choice ? ob : res;
	res.resize(ob.size()-sub.size()+1);
	register int dvd_L, dvd_R, dvd_M;
	for(register int i = ob.size()-sub.size();~i;--i) {
		dvd_L = 0, dvd_R = maxbit;
		while(dvd_L <= dvd_R) {
			dvd_M = (dvd_L+dvd_R)>>1;
			if(LMULTI(sub,dvd_M,i) <= ob) {
				dvd_L= dvd_M+1;
				res[i] = dvd_M;
			} else 
				dvd_R = dvd_M-1;
		}
		ob = LMINUS(ob,LMULTI(sub,res[i],i));
	}
	while(res.size() > 1&&!res.back()) 
		res.pop();
	return the_choice ? ob : res;
}
//重載運算符部分;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator + (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	if(ob.LSIGN() == sub.LSIGN()) {
		return LADD(ob,sub);
	} else {
		if(LABS(ob) > LABS(sub)) 
			return LMINUS(ob,-sub);
		else 
			return LMINUS(sub,-ob);
	}
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator + (LLINT<Tec,maxbit> ob,Tec sub) {
	return ob+LMAKE<Tec,maxbit>(sub);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator += (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return ob = ob+sub;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator += (LLINT<Tec,maxbit> ob,Tec sub) {
	return ob = ob+sub;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator * (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	LLINT<__uint128_t,maxbit> res(ob.size()+sub.size());
	res.LSIGN() = ob.LSIGN()^sub.LSIGN();
	res.emplace(0);
	for(register int i = 0;i < ob.size();++i) {
		if(!ob[i]) 
			continue;
		for(register int j = 0;j < sub.size();++j) {
			if(!sub[j]) 
				continue;
			res[i+j] += ob[i]*sub[j];
		}
	}
	for(register int i = 0;i < res.size()-1;++i) 
		if(res[i] >= maxbit) {
			res[i+1] += res[i]/maxbit;
			res[i] %= maxbit;
		}
	while(res.back() >= maxbit) {
		res.emplace(res.back()/maxbit);
		res[res.size()-2] %= maxbit;
	}
	if(res.size() > 1&&!res.back()) 
		res.pop();
	return LTURN<Tec,maxbit,__uint128_t,maxbit>(res);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator * (LLINT<Tec,maxbit> ob,Tec sub) {
	LLINT<Tec,maxbit> res(ob.size());
	if(sub < 0) 
		res.LSIGN() ^= 1;
	res.emplace(0);
	register __uint128_t multi_helper;
	for(register int i = 0;i < ob.size();++i) {
		multi_helper = (__uint128_t)ob[i]*sub;
		if(multi_helper >= maxbit) {
			res[i+1] += multi_helper/maxbit;
			res[i] += multi_helper%maxbit;
		}
	}
	while(res.back() >= maxbit) {
		res.emplace(res.back()/maxbit);
		res[res.size()-2] %= maxbit;
	}
	if(res.size() > 1&&!res.back()) 
		res.pop();
	return res;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator *= (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return ob = ob*sub;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator *= (LLINT<Tec,maxbit> ob,Tec sub) {
	return ob = ob*sub;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit> ob) {
	ob.LSIGN() ^= 1;
	return ob;
}//取負號;
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	if(ob.LSIGN()^sub.LSIGN()) {
		return LADD(ob,-sub);
	} else {
		if(LABS(ob) < LABS(sub)) 
			return -LMINUS(sub,ob);
		else 
			return LMINUS(ob,sub);
	}
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator - (LLINT<Tec,maxbit> ob,Tec &sub) {
	return ob-LMAKE<Tec,maxbit>(sub);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator -= (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return ob = ob-sub;
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator / (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return LDEVIDE(ob,sub,false);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator /= (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return ob = LDEVIDE(ob,sub,false);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator % (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return LDEVIDE(ob,sub,true);
}
template<typename Tec,const long long int maxbit>
LLINT<Tec,maxbit> operator %= (LLINT<Tec,maxbit> ob,LLINT<Tec,maxbit> sub) {
	return ob = LDEVIDE(ob,sub,true);
}
int main() {
	//to do;
	return 0;
}

歷史版本

#include <stdio.h>
enum {
	sysofnum = 10000
};
template<int Hsize>
class HQ {
	private : 
		int maxbit;
		int Hnum[Hsize];
	public : 
		bool friend operator < (const HQ &_x,const HQ &_y) {
			if(_x.maxbit^_y.maxbit) 
				return _x.maxbit < _y.maxbit;
			for(int i = _x.maxbit;i >= 1;--i) 
				if(_x.Hnum[i]^_y.Hnum[i]) 
					return _x.Hnum[i] < _y.Hnum[i];
			return false;
		}
		bool friend operator < (const HQ &_x,const HQ &_y) {
			if(_x.maxbit^_y.maxbit) 
				return _x.maxbit > _y.maxbit;
			for(int i = _x.maxbit;i >= 1;--i) 
				if(_x.Hnum[i]^_y.Hnum[i]) 
					return _x.Hnum[i] > _y.Hnum[i];
			return false;
		}
		bool friend operator == (const HQ &_x,const HQ &_y) {
			if(_x.maxbit^_y.maxbit) 
				return false;
			for(int i = _x.maxbit;i >= 1;--i) 
				if(_x.Hnum[i]^_y.Hnum[i]) 
					return false;
			return true;
		}
		HQ friend operator + (const HQ &_x,const HQ &_y) {
			#ifdef _x.maxbit > _y.maxbit
				#define _h _x
				#define _l _y
			#else 
				#define _h _y
				#define _l _x 
			#endif
			HQ _res = _h;
			for(int i = 1;i < _res.maxbit;++i) {
				_res.Hnum[i] += _l.Hnum[i];
				if(_res.Hnum[i] >= sysofnum) {
					_res.Hnum[i] -= sysofnum;
					++_res.Hnum[i+1];
				}
			}
			_res.Hnum[_res.maxbit] += _l.Hnum[_res.maxbit];
			while(_res.Hnum[_res.maxbit] > sysofnum) {
				_res.Hnum[_res.maxbit] -= sysofnum;
				++_res.Hnum[++_res.maxbit];
			}
			#undef _h
			#undef _l
			return _res;
		}
		HQ friend operator += (HQ &_x,const HQ &_y) {
			_x.maxbit = _x.maxbit > _y.maxbit ? _x.maxbit : _y.maxbit;
			for(int i = 1;i <= _x.maxbit;++i) {
				_x.Hnum[i] += _y.Hnum[i];
				if(_x.Hnum[i] >= sysofnum) {
					_x.Hnum[i] -= sysofnum;
					++_x.Hnum[i+1];
				}
			}
			while(_x.Hnum[_x.maxbit] > sysofnum) {
				_x.Hnum[_x.maxbit] -= sysofnum;
				++_x.Hnum[++_x.maxbit];
			}
			return _x;
		}
		HQ friend operator - (const HQ &_x,const HQ &_y) {
			HQ _res = _x;
		}
};
posted @ 2022-08-05 22:03  bikuhiku  阅读(40)  评论(1编辑  收藏  举报