首页 > 其他分享 >高精度模板!大众福音!

高精度模板!大众福音!

时间:2024-02-27 20:01:13浏览次数:15  
标签:const 高精度 int len num BigInt return 福音 模板

超级福音!!!高进度模板

两大传送门

  1. 原址传送门(有详细讲解)
  2. 发现地址传送门

废话不多说,直接上代码!

#include <bits/stdc++.h>
struct BigInt {
	static const int maxlength = 1005;
	int num[maxlength], len;
	void clean() {
		memset(num, 0, sizeof(num));
		len = 1;
	}
	BigInt() {
		clean();
	}
	void read() {
		char str[maxlength];
		scanf("%s", str);
		len = strlen(str);
		for (int i = 1; i <= len; i++) {
			num[i] = str[len - i] - '0';
		}
	}
	void write() {
		for (int i = len; i; i--) {
			printf("%d", num[i]);
		}
		putchar('\n');
	}
	void itoBig(int x) {
		clean();
		while (x != 0) {
			num[len++] = x % 10;
			x /= 10;
		}
		if (len != 1) {
			len--;
		}
	}
	bool operator <(const BigInt &cmp) const {
		if (len != cmp.len) {
			return len < cmp.len;
		}
		for (int i = len; i; i--) {
			if (num[i] != cmp.num[i]) {
				return num[i] < cmp.num[i];
			}
		}
		return 0;
	}
	bool operator >(const BigInt &cmp) const {
		return cmp < *this;
	}
	bool operator <=(const BigInt &cmp) const {
		return !(cmp < *this);
	}
	bool operator !=(const BigInt &cmp) const {
		return cmp < *this || *this < cmp;
	}
	bool operator ==(const BigInt &cmp) const {
		return !(cmp < *this || *this < cmp);
	}
	BigInt operator +(const BigInt &A) const {
		BigInt S;
		S.len = max(len, A.len);
		for (int i = 1; i <= S.len; i++) {
			S.num[i] += num[i] + A.num[i];
			if(S.num[i] >= 10) {
				S.num[i] -= 10;
				S.num[i + 1]++;
			}
		}
		while(S.num[S.len + 1]) S.len++;
		return S;
	}
	BigInt operator -(const BigInt &A) const {
		BigInt S;
		S.len = max(len, A.len);
		for (int i = 1; i <= S.len; i++) {
			S.num[i] += num[i] - A.num[i];
			if(S.num[i] < 0) {
				S.num[i] += 10;
				S.num[i + 1]--;
			}
		}
		while (!S.num[S.len] && S.len > 1) {
			S.len--;
		}
		return S;
	}
	BigInt operator *(const BigInt &A) const {
		BigInt S;
		if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) {
			return S;
		}
		S.len = A.len + len - 1;
		for (int i = 1; i <= len; i++) {
			for (int j = 1; j <= A.len; j++) {
				S.num[i + j - 1] += num[i] * A.num[j];
				S.num[i + j] += S.num[i + j - 1] / 10;
				S.num[i + j - 1] %= 10;
			}
		}
		while (S.num[S.len + 1]) {
			S.len++;
		}
		return S;
	}
	BigInt operator /(const BigInt &A) const {
		BigInt S;
		if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) {
			return S;
		}
		BigInt R, N;
		S.len = 0;
		for (int i = len; i; i--) {
			N.itoBig(10);
			R = R * N;
			N.itoBig(num[i]);
			R = R + N;
			int flag = -1;
			for (int j = 1; j <= 10; j++) {
				N.itoBig(j);
				if(N * A > R) {
					flag = j - 1;
					break;
				}
			}
			S.num[++S.len] = flag;
			N.itoBig(flag);
			R = R - N * A;
		}
		for (int i = 1; i <= (S.len >> 1); i++) {
			swap(S.num[i], S.num[len - i + 1]);
		}
		while (!S.num[S.len] && S.len > 1) {
			S.len--;
		}
		return S;
	}
	BigInt operator %(const BigInt &A) const {
		BigInt S;
		BigInt P = *this / A;
		S = *this - P * A;
		return S;
	}
};
int main(){
	
	return 0;
}

标签:const,高精度,int,len,num,BigInt,return,福音,模板
From: https://www.cnblogs.com/iloveoi/p/18037768

相关文章

  • java 实现根据word模板生成word文件 word转pdf
    最近做项目要求生成word文件及PDF文件,生成word文件时其中内容要根据不同公司提供的内容动态替换里面的值。参考了很多之后选择用word模板生成word文件。其中主要参考:https://www.cnblogs.com/suzan/p/10577738.html 简单的word模板:https://files.cnblogs.com/files/blogs/8095......
  • 类:数据结构(模板)、数据类型(反射)、种类(amount)
    1.析构函数:在GC回收资源时,我们可以在析构函数中做事情; 2.也可以不用new关键字进行创建对象: 使用dynamic,可以直接调用name 3.静态构造器只能初始化静态成员 ......
  • 修改VSCODE默认模板(live template)
    1.问题在使用VSCDOE编写html文件时,对于使用的语言这一块,公司统一要求但是VSCODE默认的是,这就需要我们每次都手改一下,非常麻烦,结合IDEA里面使用livetemplate的经历我就在思考能否修改VSCODE的相关配置文件达到同样的效果呢?首先我找到了这个参考:如何修改vscode模板这里要求我......
  • 【模板】交互题
    Codeforces的交互题有点难以调试,写了一个模板方便本地调试。structOracle{private:staticconstintMAXN=2e5+10;intn;lla[MAXN];llquery_cost;#ifdefLOCALstaticconstboolIS_LOCAL=true;#elsestaticconstboolIS_LOCAL......
  • 多项式模板整理
    约定\(F[i]\)表示\(F(x)\)的\(i\)次项系数。多项式乘法基础详情见多项式乘法入门多项式求逆给出\(n\)次多项式\(F(x)\),求一个多项式\(G(x)\),满足\(F(x)G(x)\equiv1\pmod{x^n}\),\(G(x)\)每个系数对\(998244353\)取模。我们逐一递推,假设已知\(G[1...i-1]\),需......
  • P8436 【模板】边双连通分量
    原题链接题解和点双连通分量不同在于点双联通分量:分量内任意两点之间至少有两条独立路径可走,两条路径所经过的点除了起点和终点都不同边双连通分量:分量内任意两点之间至少有两条独立路径可走,两条路径所经过的边都不同(包括重边)用这个图依然可以解释code#include<bits/stdc++......
  • P8435 【模板】点双连通分量
    原题链接题解唯一能解释的图片,黄色代表会执行入栈操作的点code#include<bits/stdc++.h>usingnamespacestd;intvis[500005]={0};intlow[500005]={0};stack<int>q;vector<int>ans[500005];vector<int>G[500005];intlen=0;intcnt=0;voidss(intnow,intf......
  • P3388 【模板】割点(割顶)
    原题链接题解先说结论对单个图做深度搜索树,对于树的根节点,它要能是割点当且仅当她有至少两个不互通的儿子节点对于树的非叶子非根节点,它要能是割点当且仅当存在儿子节点能去的时间戳最小的节点不小于当前节点的深度搜索序对于叶子节点,不可能成为割点code#include<bits/std......
  • JavaScript语法-字符串模板
    [TOC]##JavaScript模板字符串###代码以下是index.js的部分代码:```onShareAppMessage({const{toName,mainText,fromName}=this.data;debugger;return{title:'叮,您收到一张贺卡~',path:'pages/index/index?toname=${toName}&mai......
  • C# 的布尔类型和字符串类型(模板字符串)
    //布尔类型bollboolb=false;b=1==1;//trueboolb1=1>23;//false//值类型:在代码中初始化类型的时候没有赋值但是系统会自动赋值的叫值类型//byteshortint(default0)longfloatdou......