求对于我的答案的 hack 及正解(如果我的不是正解的话)
  • 板块学术版
  • 楼主yongqian123
  • 当前回复5
  • 已保存回复5
  • 发布时间2025/7/24 18:17
  • 上次更新2025/7/24 20:27:56
查看原帖
求对于我的答案的 hack 及正解(如果我的不是正解的话)
1036417
yongqian123楼主2025/7/24 18:17

https://www.luogu.com.cn/problem/U579404

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

#define int long long
#define BASE 1000000000
class uints;
ostream& operator << (ostream& _o, const uints& _uints);
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, digit;
		for (int r = _string.size() - 1; r >= 0; r -= 9){
			if (r >= 9) l = r - 8;
			else l = 0;
			digit = 0;
			for (int i = l; i <= r; i++) digit = digit * 10 + _string[i] - '0';
			this->d.push_back(digit);
		}
	}
	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, 0);
		int carry = 0;
		for (int i = 0; i < res.d.size(); i++){
			int sum = carry;
			if (i < this->d.size()) sum += this->d[i];
			if (i < that.d.size()) sum += that.d[i];
			res.d[i] = sum % BASE;
			carry = sum / BASE;
		}res.clear0();
		return res;
	}
	uints operator - (const uints& that) const{
		uints res = *this;
		bool borrow = 0;
		for (int i = 0; i < res.d.size(); i++){
			int diff = res.d[i] - borrow;
			if (i < that.d.size()) diff -= that.d[i];
			if (diff < 0) diff += BASE, borrow = 1;
			else borrow = 0;
			res.d[i] = diff;
		}res.clear0();
		return res;
	}
	uints operator * (const uints& that) const{
		uints res;
		res.d.resize(this->d.size() + that.d.size(), 0);
		for (int i = 0; i < this->d.size(); i++){
			int carry = 0;
			for (int j = 0; j < that.d.size() || carry > 0; j++){
				int product = res.d[i + j] + carry;
				if (j < that.d.size()) product += this->d[i] * that.d[j];
				res.d[i + j] = product % BASE;
				carry = product / BASE;
			}
		}res.clear0();
		return res;
	}
	uints operator / (const uints& that) const{
		if (*this < that) return uints(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 -= (const uints& that){return *this = *this - that;}
	uints& operator -- (){return *this -= 1;}
	bool check(uints a){
		if (a.d.size() * 2 - 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) / 2;
			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;
}
signed main(){
	ios::sync_with_stdio(0), cin.tie(), cout.tie();
	char c;//('u');
	string _s;//(1000,'1');
	uints n;//(_s);
	cin >> c >> n;
//	freopen("d.in", "w", stdout);
//	cout << c << '\n' << n;
	if (c == 'u'){
//		string _s_(888,'9'), _t_(50,'1');
//		_s_='1'+_s_;
		uints k;//(_s_);
		cin >> k;
//		cout << ' ' << k;
//		freopen("d.out", "w", stdout);
		cout << n / k;
	}else{
		uints x;
		x = (n * 2).sqrt() + 1;
		while (x * (x + 1) > n * 2) --x;
//		freopen("d.out", "w", stdout);
		cout << x;
	}return 0;
}
2025/7/24 18:17
加载中...