高精度 vector 模板

高精度加法

vector<int> add(vector<int> a,vector<int> b)
{
	vector<int> c;
	if(a.size()<b.size()) swap(a,b);
	int t=0;
	for(int i=0;i<(int)a.size();i++) {
		t+=a[i];
		if(i<(int)b.size()) t+=b[i];
		c.push_back(t%10);
		t/=10;
	}
	if(t) c.push_back(t);
	return c;
}

高精度减法

vector<int> sub(vector<int> a,vector<int> b)
{
	vector<int> c;
	for(int i=0;i<(int)a.size();i++) {
		if(i>=(int)b.size()) {
			if(a[i]<0) a[i]+=10,a[i+1]--;
			c.push_back(a[i]);
		}
		else {
			if(a[i]<b[i]) a[i]+=10,a[i+1]--;
			c.push_back(a[i]-b[i]);
		}
	}
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

高精度乘法

vector<int> mul(vector<int> a,int b)
{
	int t=0;
	vector<int> c;
	for(int i=0;i<(int)a.size();i++) {
		t+=a[i]*b;
		c.push_back(t%10);
		t/=10;
	}
	while(t) c.push_back(t%10),t/=10;
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

高精度除法

vector<int> div(vector<int> a,int b)
{
	int t=0;
	vector<int> c;
	for(int i=a.size()-1;i>=0;i--) {
		t=t*10+a[i];
		c.push_back(t/b);
		t=t%b;
	}
	reverse(c.begin(),c.end());
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

高精度取模

int mod(vector<int> a,int b)
{
	int t=0;
	for(int i=a.size()-1;i>=0;i--) 
		t=(t*10+a[i])%b;
    return t;
}

高精度小于号

bool cmp(vector<int> a,vector<int> b) // <
{
	if(a.size()<b.size()) return true;
	else if(a.size()>b.size()) return false;
	for(int i=a.size()-1;i>=0;i--) {
		if(a[i]<b[i]) return true;
		else if(a[i]>b[i]) return false;
	}
	return false;
}

高精度输入

void input(vector<int> &a)
{
	a.clear();
	string s;
	cin>>s;
	reverse(s.begin(),s.end());
	for(int i=0;i<(int)s.length();i++) 
		a.push_back(s[i]-'0');
}

高精度输出

void output(vector<int> a)
{
	for(int i=a.size()-1;i>=0;i--) 
		putchar(a[i]+'0');
}

Update:12.4 重写

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

vector<int> add(vector<int> a,vector<int> b)
{
	int i,t=0;
	vector<int> c;
	while(b.size()<a.size()) b.push_back(0);
	for(i=0;i<(int)a.size();i++) {
		t+=a[i]+b[i];
		c.push_back(t%10);
		t/=10;
	}
	if(t) c.push_back(t);
	return c;
}

bool cmp(const vector<int> &a,const vector<int> &b) // <
{
	if(a.size()!=b.size()) return (a.size()<b.size());
	for(int i=a.size()-1;i>=0;i--) 
		if(a[i]!=b[i]) return (a[i]<b[i]);
	return false;
}

vector<int> sub(vector<int> a,vector<int> b)
{
	int i;
	vector<int> c;
	while(b.size()<a.size()) b.push_back(0);
	for(i=0;i<(int)a.size();i++) {
		if(a[i]<b[i]) {
			a[i+1]--;
			a[i]+=10;
		}
		c.push_back(a[i]-b[i]);
	}
	while(c.size()>1 && !c.back()) c.pop_back();
	return c;
}

vector<int> mul(vector<int> a,int b)
{
	int i,t=0;
	vector<int> c;
	for(i=0;i<(int)a.size();i++) {
		t+=a[i]*b;
		c.push_back(t%10);
		t/=10;
	}
	for(;t;t/=10) c.push_back(t%10);
	while(c.size()>1 && !c.back()) c.pop_back();
	return c;
}

vector<int> div(vector<int> a,int b)
{
	int i,t=0;
	vector<int> c;
	for(i=a.size()-1;i>=0;i--) {
		t=t*10+a[i];
		c.push_back(t/b);
		t%=b;
	}
	reverse(c.begin(),c.end());
	while(c.size()>1 && !c.back()) c.pop_back();
	return c;
}

int mod(vector<int> a,int b)
{
	int i,t=0;
	for(i=a.size()-1;i>=0;i--) 
		t=(t*10+a[i])%b;
	return t;
}

void input(vector<int> &v)
{
	string s;
	cin>>s;
	reverse(s.begin(),s.end());
	for(int i=0;i<(int)s.length();i++) 
		v.push_back(s[i]-'0');
}

void output(vector<int> v)
{
	for(int i=v.size()-1;i>=0;i--)
		printf("%d",v[i]);
}

高精度 \(vector\) 压位

一定要开 long long,一般压 15 位.

LL base=1e15,width=15;

bool cmp(vector<LL> a,vector<LL> b) 
{
	if(a.size()<b.size()) return true;
	else if(a.size()>b.size()) return false;
	for(LL i=a.size()-1;i>=0;i--) {
		if(a[i]<b[i]) return true;
		else if(a[i]>b[i]) return false;
	}
	return false;
}

vector<LL> sub(vector<LL> a,vector<LL> b)
{
	vector<LL> c;
	for(LL i=0;i<(LL)a.size();i++) {
		if(i>=(LL)b.size()) {
			if(a[i]<0) a[i]+=base,a[i+1]--;
			c.push_back(a[i]);
		}
		else {
			if(a[i]<b[i]) a[i]+=base,a[i+1]--;
			c.push_back(a[i]-b[i]);
		}
	}
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

vector<LL> div(vector<LL> a,LL b)
{
	LL t=0;
	vector<LL> c;
	for(LL i=a.size()-1;i>=0;i--) {
		t=t*base+a[i];
		c.push_back(t/b);
		t=t%b;
	}
	reverse(c.begin(),c.end());
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

void input(vector<LL> &a)
{
	a.clear();
	string s;
	cin>>s;
	LL t;
	reverse(s.begin(),s.end());
	for(LL i=0;i<(LL)s.length();i+=width) {
		t=0;
		for(LL j=min(i+width,(LL)s.length())-1;j>=i;j--) 
			t=t*10+(s[j]-'0');
		a.push_back(t);
	}
}

void output(vector<LL> a)
{
	for(LL i=a.size()-1;i>=0;i--) {
		if(i==(LL)a.size()-1) printf("%lld",a[i]);
		else printf("%015lld",a[i]);
	}
}

vector<LL> mul(vector<LL> a,LL b)
{
	LL t=0;
	vector<LL> c;
	for(LL i=0;i<(LL)a.size();i++) {
		t+=a[i]*b;
		c.push_back(t%base);
		t/=base;
	}
	while(t) c.push_back(t%base),t/=base;
	while(c.back()==0&&c.size()>1) c.pop_back();
	return c;
}

vector<LL> gcd(vector<LL> a,vector<LL> b)
{
	LL t=0;
	vector<LL> c;
	c.push_back(1);
	while(!cmp(b,c)) {
		if(cmp(a,b)) swap(a,b);
		if(cmp(b,c)) break;
		
		if(b[0]%2==0&&a[0]%2==0) a=div(a,2),b=div(b,2),t++;
		else if(a[0]%2==0) a=div(a,2);
		else if(b[0]%2==0) b=div(b,2);
		else a=sub(a,b);
	}
	while(t--) a=mul(a,2);
	return a;
}

两个 \(long\space long\)\(\_\_int128\)

typedef long double LD;

const LL BASE=1e17;
struct HP
{
	LL x,y;
	HP(LL num=0) { x=0,y=num; }
	inline HP operator+(const HP &b) const
	{
		HP c;
		c.x=x+b.x,c.y=y+b.y;
		c.x+=c.y/BASE,c.y%=BASE;
		return c;
	}
	inline void print()
	{
		if(x) printf("%lld%017lld",x,y);
		else printf("%lld",y);
	}
};

inline HP mul(const LL &a,const LL &b)
{
	HP c;
	c.x=(LD)a*b/BASE;
	c.y=((a*b-c.x*BASE)%BASE+BASE)%BASE;
	return c;
}
posted @ 2020-12-05 18:03  cjlworld  阅读(157)  评论(0编辑  收藏  举报