跳到正文

题解

FRT 搭积木 II 题解

SunShine2012约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

题目分析

高精度模板,这里 99 位一压。

sqrt 函数特别说明一下,可以用二分。

核心代码

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define BASE 1000000000
#define BASEE 9
char c;
class uints{
public:
	vector<int> d;
	void clear0(){
		while (!this->d.empty() && !this->d.back()) this->d.pop_back();
		if (this->d.empty()) this->d.push_back(0);
	}
	uints(): d(1, 0){}
	uints(int _int){
		if (!_int){this->d.push_back(0); return;}
		while (_int > 0) this->d.push_back(_int % BASE), _int /= BASE;
	}
	uints(const string& _string){
		int l, tmp;
		for (int r = _string.size() - 1; r >= 0; r -= BASEE){
			if (r >= BASEE) l = r - BASEE + 1;
			else l = 0;
			tmp = 0;
			for (int i = l; i <= r; i++) tmp = tmp * 10 + _string[i] - '0';
			this->d.push_back(tmp);
		}
	}
	bool operator == (const uints& that) const{
		if (this->d.size() != that.d.size()) return 0;
		for (int i = 0; i < this->d.size(); i++) if (this->d[i] != that.d[i]) return 0;
		return 1;
	}
	bool operator < (const uints& that) const{
		if (this->d.size() != that.d.size()) return this->d.size() < that.d.size();
		for (int i = this->d.size() - 1; i >= 0; i--) if (this->d[i] != that.d[i]) return this->d[i] < that.d[i];
		return 0;
	}
	bool operator > (const uints& that) const{return that < *this;}
	bool operator <= (const uints& that) const{return !(that < *this);}
	bool operator >= (const uints& that) const{return !(*this < that);}
	uints operator + (const uints& that) const{
		uints res;
		res.d.resize(max(this->d.size(), that.d.size()) + 1);
		for (int i = 0; i < res.d.size() - 1; i++){
			if (i < this->d.size()) res.d[i] += this->d[i];
			if (i < that.d.size()) res.d[i] += that.d[i];
			res.d[i + 1] = res.d[i] / BASE, res.d[i] %= BASE;
		}res.clear0();
		return res;
	}
	uints operator - (const uints& that) const{
		uints res = *this;
		bool f = 0;
		for (int i = 0; i < res.d.size(); i++){
			int ans = res.d[i] - f;
			if (i < that.d.size()) ans -= that.d[i];
			if (ans < 0) ans += BASE, f = 1;
			else f = 0;
			res.d[i] = ans;
		}res.clear0();
		return res;
	}
	uints operator * (const uints& that) const{
		uints res;
		res.d.resize(this->d.size() + that.d.size());
		for (int i = 0; i < this->d.size(); i++){
			int tmp = 0;
			for (int j = 0; j < that.d.size() || tmp > 0; j++){
				int ans = res.d[i + j] + tmp;
				if (j < that.d.size()) ans += this->d[i] * that.d[j];
				res.d[i + j] = ans % BASE;
				tmp = ans / BASE;
			}
		}res.clear0();
		return res;
	}
	uints half(){
		if (this->d.size() == 1){
			this->d[0] >>= 1;
			return *this;
		}for (int i = this->d.size() - 1; i >= 0; i--){
			if (this->d[i] & 1) if (i > 0) this->d[i - 1] += BASE;
			this->d[i] >>= 1;
		}this->clear0();
		return *this;
	}
	uints operator / (const uints& that) const{
		if (*this < that) return 0;
		uints res, _res;
		for (int i = this->d.size() - 1; i >= 0; i--){
			_res = _res * BASE + this->d[i];
			int ans = 0, l = 0, r = BASE - 1, mid;
			while (l <= r){
				mid = l + r >> 1;
				if (that * mid <= _res) l = mid + 1, ans = mid;
				else r = mid - 1;
			}res.d.push_back(ans);
			_res = _res - that * ans;
		}reverse(res.d.begin(), res.d.end());
		res.clear0();
		return res;
	}
	uints& operator -- (){
		this->d[0]--;
		if (this->d.size() == 1) return *this;
		int l = 0;
		while (this->d[l] < 0) this->d[l] = BASE - 1, this->d[++l]--;
		this->clear0();
		return *this;
	}
	bool check(uints a){
		if ((a.d.size() << 1) - 1 > this->d.size()) return 0;
		if (*this >= a * a) return 1;
		return 0;
	}
	uints sqrt(){
		uints l, r, mid, ans;
		l = 1, r = *this;
		while (l <= r){
			mid = (l + r).half();
			if (this->check(mid)){
				if (mid >= ans) ans = mid;
				l = mid + 1;
			}else r = mid - 1;
		}return ans;
	}
};
istream& operator >> (istream& _i, uints& _uints){
	string _string;
	_i >> _string;
	_uints = uints(_string);
	return _i;
}
ostream& operator << (ostream& _o, const uints& _uints){
	if (_uints.d.empty()){_o << 0; return _o;}
	_o << _uints.d.back();
	for (int i = _uints.d.size() - 2; i >= 0; i--){
		for (int k = BASE / 10; _uints.d[i] < k; k /= 10) cout << '0';
		if (_uints.d[i]) cout << _uints.d[i];
	}return _o;
}
原文