跳到正文

个人记录

bigint模板

Eason_618约 21 分钟
原文
文章信息与版本

文章信息

发布
2025年12月25日
收录
2026年2月8日
更新
2026年11月9日
篇幅
约 21 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
#define _getchar_nolock getchar
#define _putchar_nolock putchar
using namespace std;
inline int read_int(){int res = 0;char c=_getchar_nolock();bool q=0;while(!isdigit(c)){if(c=='-')q=1;c=_getchar_nolock();}res=(c^48);while((isdigit(c=_getchar_nolock())))res=(res<<3)+(res<<1)+(c^48);if(q)res=-res;return res;}
inline double read_double(){double x = 0, y = 1.0;int f = 0, dec = 0;char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-') f = 1;ch = getchar();}while ((ch >= '0' && ch <= '9') || ch == '.') {if (ch == '.') dec = 1;else if (!dec) x = x * 10 + (ch - '0');else x += (y /= 10) * (ch - '0');ch = getchar();}return f ? -x : x;}
inline char read_char(){char res;while((res = getchar()) != EOF && isspace(res));return res;}
inline char* read_chars(){char* s;int sum=0;char ch = getchar();while (isspace(ch)) ch = getchar();while (!isspace(ch) && ch != EOF) {s[++sum]=ch;ch = getchar();}return s;}
inline string read_string(){string s;char ch = getchar();while (isspace(ch)) ch = getchar();while (!isspace(ch) && ch != EOF) {s.push_back(ch);ch = getchar();}return s;}
template<typename T>inline void write_int(T x){if(x < 0)putchar('-'), x = -x;if(x > 9)write_int(x / 10);_putchar_nolock(x % 10 + '0');}
inline void write_chars(const char* s){while(*s)fputc(*s++, stdout);}
void write_double(double x, int precision = 6){if (x < 0) {putchar('-');x = -x;}x += 5 * pow(10, -precision - 1);long long integer = (long long)x;double decimal = x - integer;if (integer == 0) {putchar('0');} else {char buffer[20];int pos = 0;while (integer > 0) {buffer[pos++] = '0' + integer % 10;integer /= 10;}while (--pos >= 0) putchar(buffer[pos]);}if (precision > 0) {putchar('.');while (precision--) {decimal *= 10;int digit = (int)decimal;putchar('0' + digit);decimal -= digit;}}}
#define forr(l, r) for(int i = l; i <= r; i++)
void cin_list(int l, int r, int *a){for(int i = l; i <= r; i++)a[i]=read_int();}
void cout_list(int l, int r, int *a, char sep=' ', char end='\n'){for(int i = l; i <= r; i++)write_int(a[i]),putchar(sep);putchar(end);}
void Merge_Sort(int l, int r, int *a, int *t){if(l>=r)return;int mid=(l+r)/2;Merge_Sort(l, mid, a, t);Merge_Sort(mid+1, r, a, t);int i=l, j=mid+1, s=l;while(i<=mid&&j<=r){if(a[i]<=a[j])t[s++]=a[i++];else t[s++]=a[j++];}while(i<=mid)t[s++]=a[i++];while(j<=r)t[s++]=a[j++];for(int i = l; i <= r; i++)a[i]=t[i];}
void qsort(int l, int r, int *a){int x = a[(l + r) / 2];int i = l, j = r;while(i <= j){while(a[i] < x)i++;while(a[j] > x)j--;if(i <= j){swap(a[i], a[j]);i++;j--;}}if(l < j)qsort(l, j, a);if(i < r)qsort(i, r, a);}
struct bigint{
	vector<int>arr;
	bool f = 0;
    operator int() const {
        int sum = 0;
        for(int i : arr)sum=sum*10+i;
        return f?-sum:sum;
    }
	friend bool operator < (bigint a, bigint b){
		if(a.arr.size()!=b.arr.size())return a.arr.size()<b.arr.size();
		for(int i = 0; i < a.arr.size(); i++){
			if(a.arr[i]!=b.arr[i])return a.arr[i]<b.arr[i];
		}
		return false;
	}
	friend bool operator > (bigint a, bigint b){
		if(a.arr.size()!=b.arr.size())return a.arr.size()>b.arr.size();
		for(int i = 0; i < a.arr.size(); i++){
			if(a.arr[i]!=b.arr[i])return a.arr[i]>b.arr[i];
		}
		return false;
	}
	friend bool operator == (bigint a, bigint b){
		if(a.arr.size()!=b.arr.size())return false;
		for(int i = 0; i < a.arr.size(); i++){
			if(a.arr[i]!=b.arr[i])return false;
		}
		return true;
	}
	friend bool operator <= (bigint a, bigint b){
		return !(a>b);
	}
	friend bool operator >= (bigint a, bigint b){
		return !(a<b);
	}
	friend bool operator != (bigint a, bigint b){
		return !(a==b);
	}
    void operator = (const int& b) {
        arr.clear();
        int t=b;
        if(t==0){
            arr.push_back(0);
        }
        while(t){
            arr.push_back(t % 10);
            t /= 10;
        }
        reverse(arr.begin(), arr.end());
    }
    void operator = (const bigint& b){
        if(this != &b){
            arr=b.arr;
        }
    }
	bigint(int val = 0) {
	    *this = val;
	}
	friend bigint operator + (bigint a, bigint b){
		bigint c;
		int n=max(a.arr.size(), b.arr.size());
		c.arr.resize(n+1);
		for(int i = 0; i < n; i++){
			if(i<a.arr.size())c.arr[n-i]+=a.arr[a.arr.size()-i-1];
			if(i<b.arr.size())c.arr[n-i]+=b.arr[b.arr.size()-i-1];
			c.arr[n-i-1]+=c.arr[n-i]/10;
			c.arr[n-i]%=10;
		}
		while(c.arr.size()>1&&c.arr.front()==0)c.arr.erase(c.arr.begin());
		return c;
	}
	friend bigint operator - (bigint a, bigint b){
		bigint c;
		if(a<b)c.f=1, swap(a, b);
		int n=max(a.arr.size(), b.arr.size());
		c.arr.resize(n+1);
		for(int i = 0; i < n; i++){
			if(i<a.arr.size())c.arr[n-i]+=a.arr[a.arr.size()-i-1];
			if(i<b.arr.size())c.arr[n-i]-=b.arr[b.arr.size()-i-1];
			if(c.arr[n-i]<0)c.arr[n-i-1]--,c.arr[n-i]+=10;
		}
		if(c.f)swap(a, b);
		while(c.arr.size()>1&&c.arr.front()==0)c.arr.erase(c.arr.begin());
		return c;
	}
	friend bigint operator * (bigint a, bigint b){
		bigint c;
		int n=a.arr.size()+b.arr.size(), na=a.arr.size(), nb=b.arr.size();
		c.arr.resize(n+1);
	    for(int i = na-1; i >= 0; i--){
	        for(int j = nb-1; j >= 0; j--){
	            int now=n-(na-i-1)-(nb-j-1);
	            c.arr[now]+=a.arr[i]*b.arr[j];
	            c.arr[now-1]+=c.arr[now]/10;
	            c.arr[now]%=10;
	        }
	    }
		while(c.arr.size()>1&&c.arr.front()==0)c.arr.erase(c.arr.begin());
		return c;
	}
	friend bigint operator / (bigint a, bigint b){
		bigint c, t;
		c.arr.erase(c.arr.begin());
		t.arr.erase(t.arr.begin());
		if(a<b)return 0;
		for(int i = 0; i < a.arr.size(); i++){
			while(t.arr.size()>0&&t.arr.front()==0)t.arr.erase(t.arr.begin());
			t.arr.push_back(a.arr[i]);
			int sum=0;
			while(t>=b){
				t-=b;
				sum++;
			}
			c.arr.push_back(sum);
		}
		while(c.arr.size()>1&&c.arr.front()==0)c.arr.erase(c.arr.begin());
		return c;
	}
	friend bigint operator % (bigint a, bigint b){
		bigint t;
		t.arr.erase(t.arr.begin());
		if(a<b)return a;
		for(int i = 0; i < a.arr.size(); i++){
			while(t.arr.size()>0&&t.arr.front()==0)t.arr.erase(t.arr.begin());
			t.arr.push_back(a.arr[i]);
			int sum=0;
			while(t>=b){
				t-=b;
				sum++;
			}
		}
		return t;
	}
    bigint operator + (int b) {
        bigint temp(b);
        return *this + temp;
    }
    bigint operator - (int b) {
        bigint temp(b);
        return *this - temp;
    }
    bigint operator * (int b) {
        bigint temp(b);
        return *this * temp;
    }
    bigint operator / (int b) {
        bigint temp(b);
        return *this / temp;
    }
	void operator += (bigint b){
		*this=*this+b;
	}
	void operator -= (bigint b){
		*this=*this-b;
	}
	void operator *= (bigint b){
		*this=*this-b;
	}
	void operator /= (bigint b){
		*this=*this-b;
	}
	void operator %= (bigint b){
		*this=*this-b;
	}
	bigint operator ++ (int){
	    bigint temp = *this;
	    *this += 1;
	    return temp;
	}
	bigint& operator ++ (){
	    *this += 1;
	    return *this;
	}
	bigint operator -- (int){
	    bigint temp = *this;
	    *this -= 1;
	    return temp;
	}
	bigint& operator -- (){
	    *this -= 1;
	    return *this;
	}
	bigint in(){
		bigint c;
		char cc=_getchar_nolock();
		while(cc<'0'||cc>'9'){
			if(cc='-')c.f=1;
			cc=_getchar_nolock();
		}
		c.arr.push_back(cc^48);
		while((cc=_getchar_nolock())>='0'&&cc<='9'){
			c.arr.push_back(cc^48);
		}
		while(c.arr.size()>1&&c.arr.front()==0)c.arr.erase(c.arr.begin());
		return c;
	}
	void out(){
		if(f)_putchar_nolock('-');
		for(int i = 0; i < arr.size(); i++)_putchar_nolock(arr[i]+'0');
	}
	void clean(){
		while(!arr.empty())arr.pop_back();
	}
	int to_int(){
		int c = 0;
		for(int i = arr.size()-1; i >= 0; i--)c=(c<<3)+(c<<1)+arr[i];
		return c;
	}
	int to_long_long(){
		long long c = 0;
		for(int i = arr.size()-1; i >= 0; i--)c=(c<<3)+(c<<1)+arr[i];
		return c;
	}
	string to_string(){
		string c = "";
		for(int i = arr.size()-1; i >= 0; i--)c+=arr[i]+'0';
		return c;
	}
}turn;
class inout{
	public:
	    inout& operator>>(int& res) {
	        res = read_int();
	        return *this;
	    }

	    inout& operator>>(unsigned int& res) {
	        res = read_int();
	        return *this;
	    }

	    inout& operator>>(unsigned long& res) {
	        res = read_int();
	        return *this;
	    }

	    inout& operator>>(long long& res) {
	        res = read_int();
	        return *this;
	    }

	    inout& operator>>(unsigned long long& res) {
	        res = read_int();
	        return *this;
	    }

	    inout& operator>>(float& res) {
	        res = read_double();
	        return *this;
	    }

	    inout& operator>>(double& res) {
	        res = read_double();
	        return *this;
	    }

	    inout& operator>>(long double& res) {
	        res = read_double();
	        return *this;
	    }

	    inout& operator>>(char& res) {
	        res = getchar();
	        return *this;
	    }

	    inout& operator>>(char*& res) {
	        res = read_chars();
	        return *this;
	    }

	    inout& operator>>(string& res) {
	        res = read_string();
	        return *this;
	    }

	    inout& operator>>(bigint& a) {
	        a = a.in();
	        return *this;
	    }
	    inout& operator<<(const int& res) {
	        write_int(res);
	        return *this;
	    }

	    inout& operator<<(const unsigned int& res) {
	        write_int(res);
	        return *this;
	    }

	    inout& operator<<(const unsigned long& res) {
	        write_int(res);
	        return *this;
	    }

	    inout& operator<<(const long long& res) {
	        write_int(res);
	        return *this;
	    }

	    inout& operator<<(const unsigned long long& res) {
	        write_int(res);
	        return *this;
	    }

	    inout& operator<<(const float& res) {
	        write_double(res); // 默认六位精度
	        return *this;
	    }

	    inout& operator<<(const double& res) {
	        write_double(res); // 默认六位精度
	        return *this;
	    }

	    inout& operator<<(const long double& res) {
	        write_double(res, 10); // 默认十位精度
	        return *this;
	    }

	    inout& operator<<(const char& res) {
	        putchar(res);
	        return *this;
	    }

	    inout& operator<<(const char*& res) {
	        write_chars(res);
	        return *this;
	    }

	    inout& operator<<(const string& res) {
	        write_chars(res.c_str());
	        return *this;
	    }

	    inout& operator<<(bigint a) {
	        a.out();
	        return *this;
	    }
}cinn, coutt;
#define cin cinn
#define cout coutt
#define int bigint
signed main(){

}
原文