ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

高精度

2022-08-13 21:04:01  阅读:134  来源: 互联网

标签:const 高精度 int big operator return data


适用于OI的高精度模板

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct big{
	typedef pair<big, big> pbb;
	static const int L = 1e3, MOD = 1e4, B = 4;
	ll data[L];
	void clear(){
		 memset(data, 0, sizeof(data));
	}
	big(){
		clear();
	}
	big(const char str[]){
		clear();
		int len = strlen(str + 1);
		data[0] = ceil(1.0 * len / B);
		for(int i = 1; i <= len; ++i){
			int j = ceil (1.0 * (len - i + 1) / B);
			data[j] = data[j] * 10 + str[i] -'0';
		}
		while(data[0] > 1 && !data[data[0]]) --data[0];
	}
	big(ll x){
		clear();
		while(x){
			data[++data[0]] = x % MOD;
			x /= MOD;
		}
		data[0] = max(data[0], 1ll);
	}
	big(int x){
		clear();
		while(x){
			data[++data[0]] = x % MOD;
			x /= MOD;
		}
		data[0] = max(data[0], 1ll);

	}
	big operator =(const big & b){
		memcpy(this, b.data, sizeof(*this));
		return *this;
	}
	friend big operator + (const big& a, const big& b){
		big c;
		c[0] = max(a[0], b[0]);
		for(int i = 1; i <= c[0]; ++i){
			c[i] = a[i] + b[i];
		}
		for(int i = 1; i <= c[0]; ++i){
			c[i + 1] += c[i] / MOD;
			c[i] %= MOD;
		}
		if(c[c[0] + 1]) ++c[0];
		return c;
	}
	friend big operator -(const big& a, const big& b){
		assert(cmp(a, b) != -1);
		big c;
		c[0] = a[0];
		for(int i = 1; i <= a[0]; ++i){
			c[i] = a[i] - b[i];
		}
		for(int i = 1; i <=  a[0]; ++i){
			if(c[i] < 0){
				c[i] += MOD;
				c[i + 1] --;
			}
		}
		while(c[0] > 1 && !c[c[0]]) --c[0];
		return c;
	}
	friend big operator *(const big& a, const big& b){
		big c;
		c[0] = a[0] + b[0] - 1;
		for(int i = 1; i <= a[0]; ++i){
			for(int j = 1; j <= b[0]; ++j){
				c[i + j - 1] += a[i] * b[j];
			}
		}
		for(int i = 1; i <= c[0]; ++i){
			c[i + 1] += c[i] /MOD;
			c[i] %= MOD;
		} 
		if(c[c[0] + 1]) ++c[0];
		return c;
	}
	friend big qpow(big a ,ll k){
		big ret = 1;
		for(;k;k>>=1){
			if(k & 1) ret = ret * a;
			a = a * a;
		}
		return ret;
	}
	friend pbb modres(big a, const big & b){
		big c;
		c[0] = a[0] - b[0] + 1;
		for(int i = c[0] * B; i >= 1; --i){
			big tmp = b * qpow(big(10), i-1);
			int j = ceil(1.0 *i/ B);
			int cnt = 0;
			while(cmp(a, tmp) >= 0) a = a - tmp, ++cnt;
			c[j] = c[j] * 10 + cnt;
		}
		while(c[0] > 1 && !c[c[0]]) --c[0];
		return {c, a};
	}
	friend big operator /(const big& a, const big& b){
		return modres(a, b).first;
	}
	friend big operator %(const big& a, const big& b){
		return modres(a, b).second;
	}
	friend int cmp(const big& a, const big& b){
		if(a[0] != b[0]) return a[0] > b[0] ? 1 : -1;
		for(int i = a[0]; i >= 1; --i){
			if(a[i] != b[i]) return a[i] > b[i] ? 1 : -1;
		}
		return 0;
	}
	ll operator [](int x)const{
		return data[x];
	}
	ll& operator [](int x){
		return data[x];
	}
	string to_str()const{
		stringstream x;
		x << data[data[0]];
		for(int i = data[0] - 1; i >= 1; --i){
			x << setfill('0') << setw(B) << data[i];
		}
		return x.str();
	}
	friend istream& operator >>(istream& in, big& a){
		char tmp[L];
		in >> (tmp + 1);
		a = tmp;
		return in;
	}
	friend ostream& operator <<(ostream& out, big a){
		out << a.to_str();
		return out;
	}
	friend big gcd(big a, big b){
		if(cmp(a, b) == -1) swap(a, b);
		return	cmp(b, 0) == 0? a : gcd(b, a % b);
	}
	friend big sqrt(big a){
		big x = a;
		for(int i = 1; i <= 100; ++i){
			x = (x + a/x) / 2;
		}
		return x;
	}
};
int main(){
	big a, b;
	cin >> a >> b;
	cout << a*b << endl;
	return 0;
}

标签:const,高精度,int,big,operator,return,data
来源: https://www.cnblogs.com/cdsidi/p/16583979.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有