跳到正文

个人记录

高精度板子

Uniecho1约 4 分钟
原文
文章信息与版本

文章信息

发布
2019年11月3日
收录
2026年8月3日
更新
2026年11月14日
篇幅
约 4 分钟

版本

  1. v1当前

讲真,不会高精度还是很虚的

为了寻求心理安慰,我结合网上的博客搞了一个阉割版的高精度板子出来

有以下问题

  • 不支持负数(也就是说减法也只能大减小,否则需要特判)

  • 没有高精除高精(不过话说回来,要真考这玩意儿我也就佛系了然后去骂出题人)

  • 没用来切过太多题,正确性有点…咳咳

然而还是有优点,好记,真的好记,而且好打,不遇到毒瘤应该也够用

板子如下

C++
const int base=4;
const int power=1e4;
struct big {
	int A[maxn];
	int & operator [](int pos) {
		return A[pos];
	}
	big() {
		memset(A,0,sizeof(A));
		A[0]=1;
	}
	big(char *S) {
		memset(A,0,sizeof(A));
		int len=strlen(S);
		A[0]=(len+base-1)/base;
		reverse(S,S+len);
		for(int i=0,w,t=0; i<len; i++,w*=10) {
			if(i%base==0) {
				++t;
				w=1;
			}
			A[t]+=w*(S[i]-'0');
		}
	}
	big(int a) {
		memset(A,0,sizeof(A));
		while(a) {
			A[++A[0]]=a%power;
			a/=power;
		}
		A[0]=max(A[0],1);
	}
	friend bool operator <(big a,big b) {
		if(a[0]<b[0])
			return true;
		else if(a[0]>b[0])
			return false;
		else {
			for(int i=a[0]; i>=1; i--)
				if(a[i]!=b[i])
					return a[i]<b[i];
			return false;
		}
	}
	friend big operator +(big a,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];
			c[i+1]+=c[i]/power;
			c[i]%=power;
		}
		if(c[c[0]+1])
			++c[0];
		return c;
	}
	friend big operator -(big a,big b) {
		big c=a;
		for(int i=1; i<=c[0]; i++) {
			c[i]-=b[i];
			if(c[i]<0) {
				c[i]+=power;
				--c[i+1];
			}
		}
		while(c[0]>1&&!c[c[0]])
			--c[0];
		return c;
	}
	friend big operator *(big a,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];
				c[i+j]+=c[i+j-1]/power;
				c[i+j-1]%=power;
			}
		if(c[c[0]+1])
			++c[0];
		return c;
	}
	friend big operator /(big a,int b) {
		big c;
		long long sum=0;
		for(int i=a[0]; i>=1; i--) {
			sum=sum*power+a[i];
			c[i]+=sum/b;
			sum%=b;
			if(c[i])
				c[0]=max(c[0],i);
		}
		return c;
	}
    il void Print(){
		print("%d",A[A[0]]);
		for(int i=A[0]-1;i>=1;i--)
			printf("%04d",A[i]);
	}
};
原文
存档评论2 条抓取于