跳到正文

剪贴板

剪贴板 qk6a1gra

原文
剪贴板信息

剪贴板信息

收录
更新

板子

最大流

C++
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3fll;
const int N=7e4+5;
namespace flow{
	int s,t;
	struct nd{int u,v;ll c;};
	vector<nd>e;
	vector<int>G[N];
	int dis[N],cur[N];
	bool v[N];
	void ade(int f,int t,ll c){e.push_back({f,t,c}),e.push_back({t,f,0}),G[f].push_back(e.size()-2),G[t].push_back(e.size()-1);}
	bool bfs(){memset(v,0,sizeof(v));queue<int>q;q.push(s),v[s]=1,dis[s]=0;while(!q.empty()){int now=q.front();q.pop();for(auto _:G[now]){nd i=e[_];if(!v[i.v]&&i.c>0)dis[i.v]=dis[now]+1,v[i.v]=1,q.push(i.v);}}return v[t];}
	ll dfs(int now,ll mn){if(now==t||!mn)return mn;ll sum=0,f;for(int &_=cur[now];_<G[now].size();_++){nd &i=e[G[now][_]];if(dis[i.v]==dis[now]+1&&(f=dfs(i.v,min(mn,i.c)))){i.c-=f,e[G[now][_]^1].c+=f,sum+=f;if(!(mn-=f))break;}}return sum;}
	ll calc(){ll ans=0;while(bfs())memset(cur,0,sizeof(cur)),ans+=dfs(s,inf);return ans;}
	void init(int _s,int _t){s=_s,t=_t;}
}

平衡树

C++
typedef int VAL_TYPE;
class _My_Set{
	struct node{int l,r,size,dat;VAL_TYPE v;};vector<node> a;
	int root=0;
	void upd(int x){a[x].size=a[a[x].l].size+a[a[x].r].size+1;}
	int newnode(VAL_TYPE val){return a.push_back({0,0,1,rand(),val}),a.size()-1;}
	void split(int p,VAL_TYPE val,int&x,int&y){p?((a[p].v<=val?(x=p,split(a[p].r,val,a[p].r,y)):(y=p,split(a[p].l,val,x,a[p].l))),upd(p),0):x=y=0;}
	int merge(int x,int y){return x&&y?a[y].dat>=a[x].dat?(a[y].l=merge(x,a[y].l),upd(y),y):(a[x].r=merge(a[x].r,y),upd(x),x):x|y;}
	void ist(VAL_TYPE val){int x,y;split(root,val,x,y);root=merge(merge(x,newnode(val)),y);}
	bool ers(VAL_TYPE val){int x,y,z;split(root,val,x,z),split(x,val-1,x,y);bool suc=(a[y].v==val);return root=merge(merge(x,merge(a[y].l,a[y].r)),z),suc;}
	int rank1(VAL_TYPE val){int x,y,rk;split(root,val-1,x,y),rk=a[x].size+1,root=merge(x,y);return rk;}
	int rank2(int x,int rk){return a[a[x].l].size+1==rk?x:a[a[x].l].size+1>rk?rank2(a[x].l,rk):rank2(a[x].r,rk-a[a[x].l].size-1);}
	int pre(VAL_TYPE val){int x,y,an;split(root,val-1,x,y),an=rank2(x,a[x].size),root=merge(x,y);return an;}
	int nxt(VAL_TYPE val){int x,y,an;split(root,val,x,y),an=rank2(y,1),root=merge(x,y);return an;}
	public:
	_My_Set(){srand(time(0));a.push_back({0,0,0,0,0});}
	void insert(VAL_TYPE val){ist(val);}
	bool erase(VAL_TYPE val){return ers(val);}
	int get_rank_by_val(VAL_TYPE val){return rank1(val);}
	VAL_TYPE get_val_by_rank(int rank){return a[rank2(root,rank)].v;}
	VAL_TYPE prev(VAL_TYPE val){return a[pre(val)].v;}
	VAL_TYPE next(VAL_TYPE val){return a[nxt(val)].v;}
};

费用流

C++
typedef long long ll;
const int N=100005;
const ll inf=0x3f3f3f3f3f3f3f3fll;
namespace mcf{
	ll s,t,vis[N],dis[N],lstp[N],lste[N],fl[N],mxf,mnc,head[N],ecnt;
	struct edge{int to,nxt;ll f,d;}e[N];
	queue<int>q;
	void aade(int x,int y,ll f,ll d){e[++ecnt]={y,head[x],f,d},head[x]=ecnt;}
	bool spfa(){
		memset(dis,0x3f,sizeof(dis)),memset(fl,0x3f,sizeof(fl)),Cl(vis),vis[s]=1,dis[s]=0,lstp[t]=-1,q.push(s);
		for(int now;!q.empty();){
			now=q.front(),q.pop(),vis[now]=0;
			for(int i=head[now];~i;i=e[i].nxt)if(e[i].f&&dis[e[i].to]>dis[now]+e[i].d){
				dis[e[i].to]=dis[now]+e[i].d,lstp[e[i].to]=now,lste[e[i].to]=i,fl[e[i].to]=min(fl[now],e[i].f);
				if(!vis[e[i].to])vis[e[i].to]=1,q.push(e[i].to);
			}
		}
		return~lstp[t];
	}
	void init(){memset(head,-1,sizeof(head)),ecnt=-1;}
	void ade(int x,int y,ll f,ll d){aade(x,y,f,d),aade(y,x,0,-d);}
	pair<ll,ll> calc(){
		while(spfa()){
			mxf+=fl[t],mnc+=fl[t]*dis[t];
			for(int now=t;now!=s;now=lstp[now])e[lste[now]].f-=fl[t],e[lste[now]^1].f+=fl[t];
		}
		return {mxf,mnc};
	}
}

计算几何

C++
typedef long double db;
const db PI=3.141592653589793238462843383279;
const db inf=1e20;
const db eps=1e-7;
inline bool eq(db a,db b){return fabs(a-b)<=eps;}
inline bool ls(db a,db b){return (a-b)<-eps;}
inline bool gt(db a,db b){return (a-b)>eps;}
inline bool le(db a,db b){return (a-b)<=eps;}
inline bool ge(db a,db b){return (a-b)>=-eps;}
short sig(db x){return x<-eps?-1:x>eps?1:0;}
struct point{
	db x,y;
	point operator+(const point&b)const{return {x+b.x,y+b.y};}
	point operator-(const point&b)const{return {x-b.x,y-b.y};}
	point operator+=(const point&b){return {x=x+b.x,y=y+b.y};}
	point operator-=(const point&b){return {x=x-b.x,y=y-b.y};}
	db operator~()const{return hypot(x,y);}
	point operator*(const db&b)const{return {x*b,y*b};}
	void rot(db ang){db c=cos(ang),s=sin(ang);x=x*c-y*s,y=y*c+x*s;}
	db operator*(const point&b)const{return x*b.x+y*b.y;}
	db operator^(const point&b)const{return x*b.y-y*b.x;}
	bool operator==(const point&b)const{return eq(x,b.x)&&eq(y,b.y);}
	bool operator<(const point&b)const{return eq(y,b.y)?ls(x,b.x):ls(y,b.y);}
	bool operator()()const{return !eq(x,inf);}
	bool operator!()const{return eq(x,inf);};
};
point read(){point p;cin>>p.x>>p.y;return p;}
bool para(point a,point b){return eq(a.x/b.x,a.y/b.y);}
point atop(double ang){return {cos(ang),sin(ang)};}
db dis(point a,point b){return ~(b-a);}
db tri(point a,point b,point c){return (b-a)^(c-b);}
db PtoL(point p,point t1,point t2){return fabs(((t1.y-t2.y)*p.x-(t1.x-t2.x)*p.y+(t1^t2))/dis(t1,t2));}
db PtoS(point p,point t1,point t2){
	point t=t2-t1;
	db c=t*(p-t1)/~t;
	if(ge(c,0)&&le(c,~t))return ~(p-t1-t*(c/~t));
	return min(dis(p,t1),dis(p,t2));
}
point PvertL(point p,point t1,point t2){
	point t=t2-t1;
	db c=t*(p-t1)/~t;
	return t1+t*(c/~t);
}
point LiL(point p1,point p2,point q1,point q2){
	if(para(p2-p1,q2-q1))return {inf,inf};
	point a=p2-p1,b=q2-q1,u=q2-p2;
	return p2-a*((u^b)/(a^b));
}
point SiS(point p1,point p2,point q1,point q2){
	if(para(p2-p1,q2-q1))return {inf,inf};
	db abac=tri(p1,q1,p2),abad=tri(p1,p2,q2),cdca=tri(q1,p1,q2),cdcb=tri(q1,q2,p2);
	if(le(abac*abad,0)&&le(cdca*cdcb,0)){
		db lbd=abac/abad;
		return q1+(q2-q1)*(lbd/(lbd+1));
	}
	return {inf,inf};
}
bool sis(point p1,point p2,point q1,point q2){
	if(para(p2-p1,q2-q1))return 0;
	db abac=tri(p1,q1,p2),abad=tri(p1,p2,q2),cdca=tri(q1,p1,q2),cdcb=tri(q1,q2,p2);
	return (le(abac*abad,0)&&le(cdca*cdcb,0));
}

struct Polygon{
	vector<point>v;
	void add(point p){v.push_back(p);}
	void add(vector<point>a){for(auto i:a)v.push_back(i);}
	void ers(){v.pop_back();}
	db C(){
		db ans=0;
		for(auto i=v.begin(),j=++v.begin();j!=v.end();i++,j++)ans+=dis(*i,*j);
		ans+=dis(v.back(),v.front());
		return ans;
	}
	db S(){
		db ans=0;
		for(int i=2;i<v.size();i++)ans+=fabs(tri(v[0],v[i-1],v[i]));
		return ans/2;
	}
	bool operator>=(const point&p)const{
		for(auto i:v)if(i==p)return 1;
		for(auto i=v.begin(),j=++v.begin();j!=v.end();i++,j++)if(para(p-*i,*j-*i))return 1;
		point P={1e9-0.03,1e9+0.017};
		bool ans=0;
		for(auto i=v.begin(),j=++v.begin();j!=v.end();i++,j++)ans^=sis(p,P,*i,*j);
		return ans;
	}
	bool operator>(const point&p)const{
		for(auto i:v)if(i==p)return 0;
		for(auto i=v.begin(),j=++v.begin();j!=v.end();i++,j++)if(para(p-*i,*j-*i))return 0;
		point P={1e9-0.03,1e9+0.017};
		bool ans=0;
		for(auto i=v.begin(),j=++v.begin();j!=v.end();i++,j++)ans^=sis(p,P,*i,*j);
		return ans;
	}
	Polygon convexhull(){
		if(v.size()<=3)return *this;
		Polygon stk;int s=0;
		vector<point>V=v;
		sort(V.begin(),V.end());
		stk.add(V[0]),stk.add(V[1]);s=2;
		for(int i=2;i<V.size();i++){
			while(s>1&&tri(stk.v[s-2],stk.v[s-1],V[i])<=0)stk.ers(),s--;
			stk.add(V[i]),s++;
		}
		Polygon ans=stk;ans.ers();
		stk.v.clear();
		stk.add(V[V.size()-1]),stk.add(V[V.size()-2]);s=2;
		for(int i=V.size()-3;~i;i--){
			while(s>1&&tri(stk.v[s-2],stk.v[s-1],V[i])<=0)stk.ers(),s--;
			stk.add(V[i]),s++;
		}
		stk.ers();
		ans.add(stk.v);
		return ans;
	}
};

struct Circle{
	point o;db r;
};

vector<point>CiL(Circle c,point p1,point p2){
	point o=c.o;db r=c.r;
	point p=PvertL(o,p1,p2);
	db d=dis(o,p);
	if(gt(d,r))return {};
	if(eq(d,r))return {p};
	db len=sqrtl(r*r-d*d);
	point t=(p2-p1)*(len/~(p2-p1));
	return {p-t,p+t};
}

vector<point>CiC(Circle c1,Circle c2){
	point o1=c1.o,o2=c2.o,d=o2-o1;
	db r1=c1.r,r2=c2.r;
	db ds=dis(o1,o2);
	if(gt(ds,r1+r2)||ls(ds,fabs(r1-r2)))return{};
	db t=atan2(d.y,d.x),a=acos((r1*r1-r2*r2+ds*ds)/(2*r1*ds));
	if(eq(a,0))return{o1+atop(t+a)*r1};
	return{o1+atop(t+a)*r1,o1+atop(t-a)*r1};
}

高精度

C++
class Lint{
protected:
	using ll = long long;
	static const int WIDTH = 9;
	static const ll BASE = 1e9;
	static const long long FFT_LIMIT = 10000;
	ll* digits;
	long capacity, size;
	bool flag;
	inline void push(const ll&);
	inline void pop();
	inline int compare(const Lint&) const;
	static inline Lint fft_mul(const Lint&, const Lint&);
	inline Lint move(const long long&);
	static inline Lint inverse(const Lint&);
public:
	inline void reserve(const long&);
protected:
	inline void resize(const long&);
public:
	Lint() : digits(nullptr), flag(true) {*this = 0;}
	Lint(const Lint& x) : digits(nullptr) {*this = x;}
	Lint(const long long& x) : digits(nullptr) {*this = x;}
	Lint(const std::string& s) : digits(nullptr) {*this = s;}
	Lint& operator= (const Lint&);
	Lint& operator= (const long long&);
	Lint& operator= (const std::string&);
	~Lint() {if (digits != nullptr) delete[] digits, digits = nullptr;}
	friend std::ostream& operator << (std::ostream& out, const Lint& x) {
		if (!x.flag) out << '-';
		out << (long long) x.digits[x.size];
		for (int i = x.size - 1; i >= 1; i--) out << std::setw(WIDTH) << std::setfill('0') << (long long) x.digits[i];
		return out;
	}
	friend std::istream& operator >> (std::istream& in, Lint& x) {std::string s; in >> s; x = s; return in;}
	std::string to_string() const;
	Lint operator- () const;
	Lint abs() const;
	bool operator< (const Lint&) const;
	bool operator> (const Lint&) const;
	bool operator== (const Lint&) const;
	bool operator!= (const Lint&) const;
	bool operator<= (const Lint&) const;
	bool operator>= (const Lint&) const;
	Lint operator+ (const Lint&) const;
	Lint operator- (const Lint&) const;
	Lint operator* (const Lint&) const;
	Lint operator/ (const long long&) const;
	Lint operator/ (const Lint&) const;
	Lint operator% (const long long&) const;
	Lint operator% (const Lint&) const;
	Lint pow(const long long&) const;
	Lint pow(const long long&, const Lint&) const;
	Lint sqrt(const long long& = 2) const;
	Lint& operator+= (const Lint&);
	Lint& operator-= (const Lint&);
	Lint& operator*= (const Lint&);
	Lint& operator/= (const long long&);
	Lint& operator/= (const Lint&);
	Lint& operator%= (const long long&);
	Lint& operator%= (const Lint&);
};
inline void Lint::push(const ll& val) {
	if (capacity == size) {
		long new_capacity = 0;
		if (capacity < 1000) new_capacity = capacity << 1;
		else new_capacity = (new_capacity >> 1) * 3;
		ll* new_digits = new ll[new_capacity + 1];
		std::memcpy(new_digits, digits, sizeof(long long) * (capacity + 1));
		delete[] digits;
		digits = new_digits, capacity = new_capacity, ++size;
		return;
	}
	digits[++size] = val;
}
inline void Lint::pop() {digits[size--] = 0;}
inline int Lint::compare(const Lint& x) const {
	if (flag && !x.flag) return 1;
	if (!flag && x.flag) return -1;
	int sgn = (flag && x.flag ? 1 : -1);
	if (size > x.size) return sgn;
	if (size < x.size) return -sgn;
	for (int i = size; i >= 1; i--) {
		if (digits[i] > x.digits[i]) return sgn;
		if (digits[i] < x.digits[i]) return -sgn;
	}
	return 0;
}
inline void Lint::reserve(const long& sz) {
	if (digits != nullptr) delete[] digits;
	capacity = sz, size = 0;
	digits = new ll[sz + 1];
	std::memset(digits, 0, sizeof(ll) * (sz + 1));
}
inline void Lint::resize(const long& sz) {reserve(sz), size = sz;}
Lint& Lint::operator= (const Lint& x) {
	reserve(x.size + 1);
	flag = x.flag, size = x.size;
	std::memcpy(digits, x.digits, sizeof(ll) * (x.size + 1));
	return *this;
}
Lint& Lint::operator= (const long long& x) {
	flag = x >= 0;
	reserve(12);
	if (x == 0) {
		digits[1] = 0;
		return *this;
	}
	if (x == LLONG_MIN) return *this = "-9223372036854775808";
	long long n = std::abs(x);
	do {
		push(n % BASE);
		n /= BASE;
	} while (n);
	return *this;
}
Lint& Lint::operator= (const std::string& s) {
	flag = true, reserve(s.size() / WIDTH + 1);
	if (s.empty() || s == "-") return *this = 0;
	int i = 0;
	if (s[0] == '-') flag = false, i++;
	for (int j = s.size() - 1; j >= i; j -= WIDTH) {
		int start = std::max(i, j - WIDTH + 1), len = j - start + 1;
		push(std::stoi(s.substr(start, len)));
	}
	return *this;
}
std::string Lint::to_string() const {
	std::stringstream ss; ss << *this;
	return ss.str();
}
Lint Lint::operator- () const {
	Lint res = *this;
	res.flag = !flag;
	return res;
}
Lint Lint::abs() const {
	Lint res = *this;
	res.flag = true;
	return res;
}
bool Lint::operator< (const Lint& x) const {return compare(x) < 0;}
bool Lint::operator> (const Lint& x) const {return compare(x) > 0;}
bool Lint::operator== (const Lint& x) const {return compare(x) == 0;}
bool Lint::operator!= (const Lint& x) const {return compare(x) != 0;}
bool Lint::operator<= (const Lint& x) const {return compare(x) <= 0;}
bool Lint::operator>= (const Lint& x) const {return compare(x) >= 0;}
Lint Lint::operator+ (const Lint& x) const {
	if (!x.flag) return *this - x.abs();
	if (!flag) return x - abs();
	Lint res;
	res.flag = !(flag ^ x.flag);
	int n = std::max(size, x.size) + 1;
	res.reserve(n);
	ll carry = 0;
	for (int i = 1; i <= n; i++) {
		ll d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
		res.push(d1 + d2 + carry);
		carry = res.digits[i] / BASE;
		res.digits[i] %= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
Lint Lint::operator- (const Lint& x) const {
	if (!x.flag) return *this + x.abs();
	if (!flag) return -(abs() + x);
	Lint res;
	if (*this < x) res.flag = false;
	ll carry = 0;
	int n = std::max(size, x.size);
	res.reserve(n);
	for (int i = 1; i <= n; i++) {
		ll d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
		if (res.flag) res.push(d1 - d2 - carry);
		else res.push(d2 - d1 - carry);
		if (res.digits[i] < 0) res.digits[i] += BASE, carry = 1;
		else carry = 0;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
namespace BigInteger_FFT {
	const long double PI = std::acos(-1.0L);
	struct complex {
		long double real, imag;
		complex(long double x = 0.0, long double y = 0.0) : real(x), imag(y) {}
		complex operator + (const complex& other) const {return complex(real + other.real, imag + other.imag);}
		complex operator - (const complex& other) const {return complex(real - other.real, imag - other.imag);}
		complex operator * (const complex& other) const {return complex(real * other.real - imag * other.imag, real * other.imag + other.real * imag);}
		complex conj() const {return complex(real, -imag);}
	};
	complex* arr = nullptr;
	inline void init(int n) {
		if (arr != nullptr) delete[] arr, arr = nullptr;
		arr = new complex[n + 1];
		for (int i = 0; i <= n; i++) arr[i] = complex(0.0, 0.0);
	}
	inline void fft(complex* a, long len) {
		for (long i = len; i >= 2; i >>= 1) {
			complex wn(std::cos(2.0L * PI / i), std::sin(2.0L * PI / i));
			for (long j = 0; j < len; j += i) {
				complex w(1.0, 0.0);
				for (long k = j; k < j + (i >> 1); k++, w = w * wn) {
					complex u = a[k], t = a[k + (i >> 1)];
					a[k] = u + t, a[k + (i >> 1)] = w * (u - t);
				}
			}
		}
	}
	inline void idft(complex* a, long len) {
		for (long i = 2; i <= len; i <<= 1) {
			complex wn(std::cos(2.0L * PI / i), std::sin(2.0L * PI / i));
			for (long j = 0; j < len; j += i) {
				complex w(1.0, 0.0);
				for (long k = j; k < j + (i >> 1); k++, w = w * wn) {
					complex u = a[k], t = w * a[k + (i >> 1)];
					a[k] = u + t, a[k + (i >> 1)] = u - t;
				}
			}
		}
	}
}
Lint Lint::fft_mul(const Lint& a, const Lint& b) {
	long lim = 1;
	while (lim <= 3 * (a.size + b.size)) lim <<= 1;
	BigInteger_FFT::init(lim);
	using BigInteger_FFT::arr;
	for (int i = 0; i < a.size; i++) {
		arr[i * 3].real = a.digits[i + 1] % 1000;
		arr[i * 3 + 1].real = a.digits[i + 1] / 1000 % 1000;
		arr[i * 3 + 2].real = a.digits[i + 1] / (1000 * 1000);
	}
	for (int i = 0; i < b.size; i++) {
		arr[i * 3].imag = b.digits[i + 1] % 1000;
		arr[i * 3 + 1].imag = b.digits[i + 1] / 1000 % 1000;
		arr[i * 3 + 2].imag = b.digits[i + 1] / (1000 * 1000);
	}
	BigInteger_FFT::fft(arr, lim);
	for (int i = 0; i < lim; i++) arr[i] = (arr[i] * arr[i]).conj();
	BigInteger_FFT::idft(arr, lim);
	Lint res;
	res.resize(a.size + b.size + 1);
	ll carry = 0;
	for (int i = 0; i <= a.size + b.size; i++) {
		carry += (ll)(arr[i * 3].imag * -0.5 / lim + 0.5);
		carry += (ll)(arr[i * 3 + 1].imag * -0.5 / lim + 0.5) * 1000LL;
		carry += (ll)(arr[i * 3 + 2].imag * -0.5 / lim + 0.5) * 1000LL * 1000LL;
		res.digits[i + 1] += carry % BASE, carry /= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
Lint Lint::operator* (const Lint& x) const {
	Lint zero = 0;
	if (*this == zero || x == zero) return zero;
	int n = size, m = x.size;
	if (1LL * n * m >= 1) {
		Lint res = fft_mul(*this, x);
		res.flag = !(flag ^ x.flag);
		return res;
	}
	Lint res;
	res.flag = !(flag ^ x.flag);
	res.resize(n + m + 2);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			res.digits[i + j - 1] += digits[i] * x.digits[j];
			res.digits[i + j] += res.digits[i + j - 1] / BASE;
			res.digits[i + j - 1] %= BASE;
		}
	}
	for (int i = 1; i <= n + m + 1; i++) {
		res.digits[i + 1] += res.digits[i] / BASE;
		res.digits[i] %= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
Lint Lint::operator/ (const long long& x) const {
	if (x == 0) throw -1;
	if (*this == 0) return 0;
	Lint res;
	res.flag = !(flag ^ (x >= 0));
	ll cur = 0, div = std::abs(x);
	res.resize(size);
	for (int i = size; i >= 1; i--) {
		cur = cur * BASE + digits[i];
		res.digits[i] = res.flag ? (cur / div) : (-cur / -div);
		cur %= div;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
Lint Lint::operator/ (const Lint& x) const {
	static std::vector<Lint> pow2 = {1};
	static std::vector<Lint> estimate;
	estimate.clear();
	if (*this < x) return 0;
	Lint cur = x;
	while (cur <= *this) {estimate.emplace_back(cur), cur += cur;if (estimate.size() > pow2.size()) pow2.emplace_back(pow2.back() + pow2.back());}
	if (cur == *this) {pow2.back().flag = !(flag ^ x.flag);return Lint(pow2.back());}
	Lint res = pow2[estimate.size() - 1];
	cur = estimate.back();
	for (long i = estimate.size() - 1; i >= 0; i--) if (cur + estimate[i] <= *this) cur += estimate[i], res += pow2[i];
	res.flag = !(flag ^ x.flag);
	return res;
}
Lint Lint::operator% (const long long& x) const {if (x == 2) return digits[1] & 1;return *this - (*this / x * x);}
Lint Lint::operator% (const Lint& x) const {return *this - (*this / x * x);}
Lint Lint::pow(const long long& x) const {
	Lint res = 1, a = *this;
	for (long long t = x; t != 0; t >>= 1, a *= a) if (t & 1) res *= a;
	return res;
}
Lint Lint::pow(const long long& x, const Lint& p) const {
	Lint res = 1, a = *this % p;
	for (long long t = x; t != 0; t >>= 1, a = a * a % p) if (t & 1) res = res * a % p;
	return res;
}
Lint Lint::sqrt(const long long& x) const {
	Lint l = 0, r = 1;
	while (r.pow(x) <= *this) l = r, r += r;
	while (l + 1 < r) {
		Lint mid = (l + r) / 2;
		if (mid.pow(x) <= *this) l = mid;
		else r = mid;
	}
	return l.pow(x) <= *this ? l : r;
}
Lint& Lint::operator+= (const Lint& x) {return *this = *this + x;}
Lint& Lint::operator-= (const Lint& x) {return *this = *this - x;}
Lint& Lint::operator*= (const Lint& x) {return *this = *this * x;}
Lint& Lint::operator/= (const long long& x) {return *this = *this / x;}
Lint& Lint::operator/= (const Lint& x) {return *this = *this / x;}
Lint& Lint::operator%= (const long long& x) {return *this = *this / x;}
Lint& Lint::operator%= (const Lint& x) {return *this = *this % x;}