CSP-S 复习 ---- 高精度全家桶

v e c t o r vector vector 实现,高精除高精没有写,口胡
取模的时候重载 < < <,然后 ∗ 10 *10 10 相当于在最前面插一个数字,然后高精减就可以取模
除法更取模插不多,暴力减就可以了
乘法用了 F F T FFT FFT

#include<bits/stdc++.h>
#define cs const
using namespace std;
#define poly vector<int> 
cs int N = 4e5 + 5;
cs double PI = acos(-1.0);
struct node{
	double x, y; 
	node(double _x = 0, double _y = 0){ x = _x; y = _y; }
	node operator + (cs node &a){ return node(x + a.x, y + a.y); }
	node operator - (cs node &a){ return node(x - a.x, y - a.y); }
	node operator * (cs node &a){ return node(x * a.x - y * a.y, x * a.y + y * a.x); }  
}f[N], g[N]; 
int bit, up, rev[N];
void init(int len){ 
	bit = 0; up = 1; while(up <= len) up <<= 1, bit++;
	for(int i = 0; i < up; i++) rev[i] = (rev[i>>1]>>1) | ((i&1) << (bit-1)); 
}
void FFT(node *a, int typ){
	for(int i = 0; i < up; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
	for(int i = 1; i < up; i <<= 1){
		node wn(cos(PI / i), typ * sin(PI / i));
		for(int j = 0; j < up; j += (i<<1)){
			node w(1, 0);
			for(int k = 0; k < i; k++, w = w * wn){
				node x = a[k+j], y = a[k+j+i] * w;
				a[k+j] = x + y; a[k+j+i] = x - y;
			}
		}
	}
}
struct Bignum{
	poly a; int flg; // + -  
	#define pb push_back
	friend Bignum Mul(Bignum A, Bignum B){
		Bignum C;
		int n = A.a.size(), m = B.a.size();
		int len = n + m - 1; init(len);
		for(int i = 0; i < up; i++) f[i] = g[i] = node(0, 0);
		for(int i = 0; i < n; i++) f[i].x = A.a[i];
		for(int i = 0; i < m; i++) g[i].x = B.a[i];
		FFT(f, 1); FFT(g, 1); 
		for(int i = 0; i < up; i++) f[i] = f[i] * g[i];
		FFT(f, -1);
		for(int i = 0; i < up; i++){
			int v = (int)(f[i].x / up + 0.5);
			C.a.pb(v);
		} C.a.resize(len); return C;
	}
	friend Bignum operator + (Bignum A, Bignum B){
		Bignum C; int len = max(A.a.size(), B.a.size());
		A.a.resize(len); B.a.resize(len); C.a.resize(len + 1);
		for(int i = 0; i < len; i++){
			C.a[i] += A.a[i] + B.a[i];
			if(C.a[i] >= 10) C.a[i] -= 10, C.a[i + 1]++;
		} if(!C.a[len]) C.a.resize(len); return C;
	}
	friend bool operator == (Bignum A, Bignum B){
		if(A.a.size() != B.a.size()) return false;
		for(int i = A.a.size() - 1; i >= 0; i--){
			if(A.a[i] != B.a[i]) return false;
		} return true;
	}
	friend bool operator < (Bignum A, Bignum B){
		if(A.a.size() != B.a.size()) return A.a.size() < B.a.size();
		for(int i = A.a.size() - 1; i >= 0; i--){
			if(A.a[i] != B.a[i]) return A.a[i] < B.a[i]; 
		} 
	} 
	friend Bignum operator - (Bignum A, Bignum B){
		Bignum C;
		if(A == B){ C.a.pb(0); return C;}
		if(A < B) swap(A, B), C.flg = -1;
		int len = A.a.size();
		C.a.resize(len); B.a.resize(len);
		for(int i = 0; i < len; i++){
			C.a[i] += A.a[i] - B.a[i];
			if(C.a[i] < 0) C.a[i] += 10, C.a[i + 1]--; 
		} while(!C.a[C.a.size() - 1]) C.a.pop_back(); return C;
	}
	friend Bignum operator * (Bignum A, Bignum B){
		Bignum C; int len = A.a.size() + B.a.size();
		if(len <= 512){
			C.a.resize(len); 
			for(int i = 0; i < A.a.size(); i++){
				for(int j = 0; j < B.a.size(); j++){
					C.a[i + j] += A.a[i] * B.a[j];
				}
			} 
		} 
		else{ C = Mul(A, B); C.a.resize(len); }
		for(int i = 0; i < C.a.size(); i++){
			if(C.a[i] >= 10){
				int k = C.a[i] / 10;
				C.a[i] -= k * 10;
				C.a[i + 1] += k; 
			} 
		} if(!C.a[len - 1]) C.a.pop_back(); return C;
	}
	friend int operator % (Bignum A, int b){
		int nx = 0;
		for(int i = A.a.size() - 1; i >= 0; i--){
			nx = (nx * 10 + A.a[i]) % b;
		} return nx;
	}
	friend Bignum operator / (Bignum A, int b){
		Bignum B; int len = A.a.size(), nx = 0;
		for(int i = len - 1; i >= 0; i--){
			nx = nx * 10 + A.a[i];
			if(nx >= b){
				int k = nx / b; B.a.pb(k); 
				nx -= k * b;
			} else if(B.a.size()) B.a.pb(0);
		} reverse(B.a.begin(), B.a.end()); return B;
	}
	void read(){
		char ch = 0; 
		while(!isdigit(ch)) ch = getchar();
		while(isdigit(ch)) a.pb(ch - '0'), ch = getchar();
		reverse(a.begin(), a.end()); // 小的位在前面  
	}
	void print(){ if(flg == -1) putchar('-'); for(int i = a.size()-1; i >= 0; i--) cout << a[i]; }
}A, B; int x;
int main(){
	A.read(); B.read(); 
	(A + B).print(); puts("");
	(A - B).print(); puts("");
	(A * B).print(); puts("");
	A.read(); scanf("%d", &x);
	int y = A % x, ans = y; 
	while(y) B.a.push_back(y % 10), y /= 10; A = A - B;
	(A / x).print(); puts(""); cout << ans << '\n'; 
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值