压位高精库(加减乘除超全)

目录

前言: 

代码:

用法:

1.读入:

2.输出:

3.比大小:

4.加法:

5.减法:

6.乘法:

7.除法:

结束:


前言: 

我自己写的高精库,挺难写的(只有加减乘除与比较,除法无法处理负数,因为我不会负数取余

某些高精函数之间有依赖关系,请注意! 

未经允许不准转载,转载请注明出处!

代码:

先给代码(记得看用法)

#include<bits/stdc++.h>
#define ll long long
#define mxx 100000000//满此数进一(十的 压的位数 次方)
#define mxw 8//压的位数
#define www 2002//最大位数除以压的位数
using namespace std;
char c;
ll n[www],m[www],ans[www],yu[www];
inline void rd(ll &x);//低读(快读)O(N)
inline void pt(ll x);//低写(快写)O(N)
inline void rrdd(ll rrd[www]);//压位高读 O(N)
inline void pptt(ll ppt[www]);//压位高写 O(N)
inline ll cmpb(ll cm[www],ll cp[www]);//压位高比(0为cm大,1为cp大,2为一样)O(1)~O(N)
inline void adda(ll ada[www],ll adb[www],ll x);//压位高加低(结果,高数,低数)O(N)
inline void addb(ll ads[www],ll adb[www],ll adc[www]);//压位高加高(结果,数1,数2)O(N)
inline void suba(ll su[www],ll sb[www],ll x);//压位高减低(结果,被减数,减数)O(1)~O(N)
inline void subb(ll sub[www],ll su[www],ll sb[www]);//压位高减高(结果,被减数,减数)O(N)
inline void mula(ll mu[www],ll ml[www],ll x);//压位高乘低(结果,高数,低数)O(N)
inline void mulb(ll mu[www],ll ml[www],ll mul[www]);//压位高乘高(结果,数1,数2)O(N^2)
inline void dela(ll de[www],ll &y,ll dl[www],ll x);//压位高除低(结果,余数,被除数,除数)O(N)
inline void delb(ll &dee,ll dll[www],ll de[www],ll dl[www]);//压位同位高除高(商,余数,被除数,除数)(de/dl<mxx)O(log(mxx)*N)
inline void delc(ll dee[www],ll dll[www],ll de[www],ll dl[www]);//压位非同位高除高(商,余数,被除数,除数)O(log(mxx)*N^2)
inline void rd(ll &x){
	x=0;short f=1;char c=getchar();
	while((c<'0'||c>'9')&&c!='-') c=getchar();
	if(c=='-') c=getchar(),f=-1;
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	x*=f;
}
inline void pt(ll x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) pt(x/10);
	putchar((x%10)+'0');
}
inline void rrdd(ll rrd[www]){
	for(ll i=1;i<www;i++)
		rrd[i]=0;
	rrd[0]=1;
	ll t=0,rrrd[www*mxw]={};
	short f=1;char c=getchar();
	if(c=='-') c=getc
当然可以,以下是一个简单的高精加减乘除的 C 代码示例: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 1000 typedef struct { int len; int num[MAX_LEN]; } BigInt; void init(BigInt *a) { memset(a->num, 0, sizeof(a->num)); a->len = 0; } void assign(BigInt *a, char *s) { init(a); int len = strlen(s); for (int i = len - 1, j = 0, k = 1; i >= 0; i--) { a->num[j] += (s[i] - '0') * k; if (k == 10000) { j++; k = 1; } else { k *= 10; } } a->len = len / 4 + 1; while (a->len > 1 && a->num[a->len - 1] == 0) { a->len--; } } void print(BigInt *a) { printf("%d", a->num[a->len - 1]); for (int i = a->len - 2; i >= 0; i--) { printf("%04d", a->num[i]); } printf("\n"); } int compare(BigInt *a, BigInt *b) { if (a->len != b->len) { return a->len - b->len; } for (int i = a->len - 1; i >= 0; i--) { if (a->num[i] != b->num[i]) { return a->num[i] - b->num[i]; } } return 0; } void add(BigInt *a, BigInt *b, BigInt *c) { init(c); int carry = 0; for (int i = 0; i < a->len || i < b->len; i++) { int sum = a->num[i] + b->num[i] + carry; c->num[c->len++] = sum % 10000; carry = sum / 10000; } if (carry > 0) { c->num[c->len++] = carry; } } void sub(BigInt *a, BigInt *b, BigInt *c) { init(c); int borrow = 0; for (int i = 0; i < a->len || i < b->len; i++) { int diff = a->num[i] - b->num[i] - borrow; if (diff < 0) { diff += 10000; borrow = 1; } else { borrow = 0; } c->num[c->len++] = diff; } while (c->len > 1 && c->num[c->len - 1] == 0) { c->len--; } } void mul(BigInt *a, BigInt *b, BigInt *c) { init(c); for (int i = 0; i < a->len; i++) { int carry = 0; for (int j = 0; j < b->len; j++) { int sum = a->num[i] * b->num[j] + c->num[i + j] + carry; c->num[i + j] = sum % 10000; carry = sum / 10000; } if (carry > 0) { c->num[i + b->len] += carry; } } c->len = a->len + b->len; while (c->len > 1 && c->num[c->len - 1] == 0) { c->len--; } } void div(BigInt *a, BigInt *b, BigInt *c, BigInt *d) { init(c); init(d); for (int i = a->len - 1; i >= 0; i--) { d->num[0] = a->num[i]; d->len = 1; int left = 0, right = 9999, mid; while (left <= right) { mid = (left + right) / 2; BigInt tmp, mul; init(&tmp); init(&mul); tmp.num[0] = mid; tmp.len = 1; mul(&tmp, b, &mul); if (compare(&mul, d) <= 0) { left = mid + 1; } else { right = mid - 1; } } c->num[i] = right; BigInt tmp, mul, sub; init(&tmp); init(&mul); init(&sub); tmp.num[0] = right; tmp.len = 1; mul(&tmp, b, &mul); sub(a, &mul, &sub); assign(a, ""); add(&sub, &tmp, a); } while (c->len > 1 && c->num[c->len - 1] == 0) { c->len--; } } int main() { BigInt a, b, c, d; char s1[MAX_LEN], s2[MAX_LEN]; scanf("%s%s", s1, s2); assign(&a, s1); assign(&b, s2); add(&a, &b, &c); sub(&a, &b, &c); mul(&a, &b, &c); div(&a, &b, &c, &d); print(&c); print(&d); return 0; } ``` 这个代码实现了高精加减乘除四个基本运算,可以处理过 long long 范围的整数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值