跳到正文

个人记录

模板高精度

WangNONO约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Apa{
	inline string add(string a, string b){
	    string res;
	    int i = a.size() - 1, j = b.size() - 1;
	    int carry = 0;
	    while (i >= 0 || j >= 0 || carry) {
	        int num = carry;
	        if (i >= 0) num += a[i--] - '0';
	        if (j >= 0) num += b[j--] - '0';
	        carry = num / 10;
	        res.push_back(num % 10 + '0');
	    }
	    reverse(res.begin(), res.end());
	    return res;
	}
	inline string sub(string a, string b){
	    string res;
	    int i = a.size() - 1, j = b.size() - 1;
	    int borrow = 0;
	    while(i >= 0){
	        int num = (a[i--] - '0') - borrow;
	        if(j >= 0) num -= (b[j--] - '0');
	        if(num < 0){
	            borrow = 1;
	            num += 10;
	        }else borrow = 0;
	        res.push_back(num + '0');
	    }
	    reverse(res.begin(), res.end());
	    int pos = 0;
	    while(res[pos] == '0') ++pos;
	    return res.substr(pos);
	}
	inline string mul(string a, int b){
	    string res;
	    int carry = 0;
	    for(int i = a.size() - 1; i >= 0 || carry; i--){
	        int num = carry;
	        if (i >= 0) num += (a[i] - '0') * b;
	        carry = num / 10;
	        res.push_back(num % 10 + '0');
	    }
	    reverse(res.begin(), res.end());
	    int pos = 0;
	    while(res[pos] == '0') ++pos;
	    return res.substr(pos);
	}
	inline string mul1(string a, string b) {
	    int m = a.size(), n = b.size();
	    vector<int> res(m + n, 0);
	    for(int i = m - 1; i >= 0; i--)
	        for (int j = n - 1; j >= 0; j--){
	            int mul = (a[i] - '0') * (b[j] - '0');
	            int sum = mul + res[i + j + 1];
	            res[i + j + 1] = sum % 10;
	            res[i + j] += sum / 10;
	        }
	    string ans;
	    for(int num : res) if(!(ans.empty() && num == 0)) ans.push_back(num + '0');
	    return ans.empty() ? "0" : ans;
	}
	inline string div(string a, int b){
	    string res;
	    int rem = 0;
	    for(char c : a){
	        rem = rem * 10 + (c - '0');
	        res.push_back(rem / b + '0');
	        rem %= b;
	    }
	    int pos = 0;
	    while(res[pos] == '0') pos++;
	    return res.substr(pos);
	}
	inline int mod(string a, int b){
	    int rem = 0;
	    for(char c : a)rem = (rem * 10 + (c - '0')) % b;
	    return rem;
	}
}hp;
inline string operator*(string s, string s1){return hp.mul1(s, s1);}
inline string operator*(string s, int s1){return hp.mul(s, s1);}
inline string operator*(int s1, string s){return hp.mul(s, s1);}
inline int operator%(string s, int s1){return hp.mod(s, s1);}
inline string operator/(string s, int s1){return hp.div(s, s1);}

int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);

	return 0;
}
原文