科技板子
大部分是我手打,极少部分是摘录
位模乘(这个算法用到了汇编,在NOI系列比赛不让用)
#define ull unsigned long long
ull modmul(ull a,ull b,ull p){
ull ans;
__asm__("mulq %2;divq %3":"=d"(ans):"a"(a),"m"(b),"m"(p));
return ans;
}//a*b mod p位模乘#2(这个算法用到了浮点数除法,可能较慢,但比log的快速乘快)
#define ull unsigned long long
#define ll long long
ll modmul(ll a,ll b,ll m) {
ull c=(ull)a*b-(ull)((long double)a/m*b+0.5L)*m;
return c>=0?c:c+m;
}//a*b mod m扩栈(这个代码用到了汇编,在NOI系列比赛不让用)
int main(){
int SIZE=512<<20;
char*p=(char*)malloc(SIZE)+SIZE;
__asm__("movq %0, %%rsp\n" :: "r"(p));
exit(0);//必要的
}//SIZE bytes快速除
struct fastmod{
#define ll unsigned long long
#define lll __uint128_t
int f, l; ll m, d;
fastmod(ll d):d(d){
l=64-__builtin_clzll(d - 1);
lll M=((lll(1)<<(64+l))+(lll(1)<<l))/d;
if(M<(lll(1)<<64))f=1,m=M;
else f=0,m=M-(lll(1)<<64);
}
friend ll operator/(ll n,const fastmod&m){
if(m.f)return lll(n)*m.m>>64>>m.l;
else{ll t=lll(n)*m.m>>64;return(((n-t)>>1)+t)>>(m.l-1);}
}
friend ll operator%(ll n,const fastmod&m){return n-n/m*m.d;}
};用法:使用模数初始化 ,然后取模和除就可以用这个 结构体即可
#define reg register
struct control{
int ct,val;
control(int Ct,int Val=-1):ct(Ct),val(Val){}
inline control operator()(int Val){return control(ct,Val);}
}Endl(0),_prs(1),_setprecision(2);
struct FastIO{
#define IOSIZE 1048576
char in[IOSIZE],*p,*pp,out[IOSIZE],*q,*qq,ch[20],*t,b,K,prs;
FastIO():p(in),pp(in),q(out),qq(out+IOSIZE),t(ch),b(1),K(6){}
~FastIO(){fwrite(out,1,q-out,stdout);}
inline char getch(){return p==pp&&(pp=(p=in)+fread(in,1,IOSIZE,stdin),p==pp)?b=0,EOF:*p++;}
inline void putch(char x){q==qq&&(fwrite(out,1,q-out,stdout),q=out),*q++=x;}
inline void puts(const char str[]){fwrite(out,1,q-out,stdout),fwrite(str,1,strlen(str),stdout),q=out;}
inline void getline(string&s){
s.clear();
for(reg char ch;(ch=getch())!='\n'&&b;)s+=ch;
}
#define indef(T) inline FastIO&operator>>(T&x){\
x=0;reg char f=0,ch;\
while(!isdigit(ch=getch())&&b)f|=ch=='-';\
while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getch();\
return x=f?-x:x,*this;\
}
indef(int)
indef(long long)
inline FastIO&operator>>(char&ch){return ch=getch(),*this;}
inline FastIO&operator>>(string&s){
s="";reg char ch;
while(isspace(ch=getch())&&b);
while(!isspace(ch)&&b)s+=ch,ch=getch();
return *this;
}
#define indef_f(T)inline FastIO&operator>>(T&x){\
x=0;reg char f=0,ch;\
T d=0.1;\
while(!isdigit(ch=getch())&&b)f|=(ch=='-');\
while(isdigit(ch))x=x*10+(ch^48),ch=getch();\
if(ch=='.')while(isdigit(ch=getch()))x+=d*(ch^48),d*=0.1;\
return x=f?-x:x,*this;\
}
indef_f(double)
indef_f(float)
indef_f(long double)
indef_f(__float128)
#define outdef(_T) inline FastIO&operator<<(_T x){\
!x&&(putch('0'),0),x<0&&(putch('-'),x=-x);\
while(x)*t++=x%10+48,x/=10;\
while(t!=ch)putch(*--t);\
return *this;\
}
outdef(int)
outdef(long long)
outdef(__int128)
inline FastIO&operator<<(char ch){return putch(ch),*this;}
inline FastIO&operator<<(const char str[]){return puts(str),*this;}
inline FastIO&operator<<(const string&s){return puts(s.c_str()),*this;}
#define outdef_f(_T) inline FastIO&operator<<(_T x){\
reg int k=0;\
this->operator<<(__int128(x));\
putch('.');\
x-=__int128(x);\
prs&&(x+=5*pow(10,-K-1));\
while(k<K)putch(int(x*=10)^48),x-=int(x),++k;\
return *this;\
}
outdef_f(float)
outdef_f(double)
outdef_f(long double)
outdef_f(__float128)
inline FastIO&operator<<(const control&cl){
switch(cl.ct){
case 0:putch('\n');break;
case 1:prs=cl.val;break;
case 2:K=cl.val;break;
}
return*this;
}
inline operator bool(){return b;}
}io;使用方法:
io>>x;
io<<x<<Endl;//其他定义自己研究(不是)精简板
#define ll long long
ll gcd(ll a,ll b){if(!b)return a;while(b^=a^=b^=a%=b);return a;}
ll GCD(ll a,ll b){if(b)while((a%=b)&&(b%=a));return a+b;}科技板
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll gcd(ll a,ll b){
ll r(a|b);
if(!a||!b)return r;
b>>=__builtin_ctz(b);
if(b==1)return r&-r;
for(;;){
a>>=__builtin_ctz(a);
if(a==1)return r&-r;
if(a==b)return a<<__builtin_ctz(r);
if(a<b)swap(a,b);
a-=b;
}
}科技
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
pair<ll,ll>fib(ll n){
if(n==0)return{0,1};
pair<ll,ll>p=fib(n>>1);
const ll c(p.first*(((p.second<<1)-p.first+mod)%mod)%mod);
const ll d(p.first*p.first%mod+p.second*p.second%mod);
if(n&1)return{d,(c+d)%mod};
else return{c,d};
}
int main(){
ll x;cin>>x;
cout<<fib(x).first<<'\n';
return 0;
}//fib(x).first:斐波那契数列第 x 项,second:第x+1项科技逆元
#define ll long long
#define mod 998244353
ll inv(ll x){return x==1?1:(mod-mod/x)*inv(mod%x)%mod;}黑魔法数组访问(以 举例)
#include<bits/stdc++.h>
using namespace std;
int a[2];
int main(){
cin>>0[a]>>1[a];
1[a]+=0[a];
cout<<1[a]<<'\n';
return 0;
}极速 +代码提速器(测试得可以提速 0.4s ,只要添加即可提速)
int seed((unsigned long long)new char);
int rd(){return(seed^=seed<<17,seed^=seed>>13,seed^=seed<<3,seed<0?-seed:seed);}临时数组
举例(随机输出 YES 和 NO):
#include<bits/stdc++.h>
using namespace std;
int main(){
mt19937 rd((unsigned long long)new char);
while(1)cout<<(string[]){"YES","NO"}[rd()&1]<<'\n';
return 0;
}Linux没有权限怎么配置node等
find . -type f -name '*' | xargs perl -pi -e 's|/bin/env node|/bin/env /home/user/<username>/node/bin/node|g'然后在 ~/.bashrc 里面添加alias
手写
template<class T>
struct vec{
T*v;size_t sz,cap;
vec(){v=new T[1],sz=0,cap=1;}
vec(int _sz){resize(_sz);}
vec(int _sz,const T&t){resize(_sz,t);}
void reserve(size_t nwcap){
if(nwcap<=cap)return;
T*nw=new T[nwcap];
memcpy(nw,v,sizeof(T)*sz);
delete[]v;
cap=nwcap;
v=nw;
}
void push_back(const T&val){
if(sz>=cap)reserve(cap<<1);
v[sz++]=val;
}
void resize(size_t siz){
reserve(siz+sz);
memset(v+sz,0,sizeof(T)*siz);
sz+=siz;
}
void resize(size_t siz,const T&t){
reserve(siz+sz);
fill(v+sz,v+sz+siz,t);
sz+=siz;
}
void pop_back(){sz&&(--sz);}
T&operator[](size_t id){return v[id];}
T operator[](size_t id)const{return v[id];}
size_t size(){return sz;}
bool empty(){return sz>0;}
void clear(){sz=0;}
T*begin(){return v;}
T*end(){return v+sz;}
void insert(T*it,const T&val){
size_t delta=it-v;
if(sz>=cap)reserve(cap<<2);
it=v+delta;
memmove(it+1,it,sizeof(T)*((int)sz-(it-v)));
*it=val,++sz;
}
void erase(T*st,T*ed){memmove(st,ed,sizeof(T)*((int)sz-(ed-v))),sz-=ed-st;}
void erase(T*it){erase(it,it+1);}
};手写 rope
#include<bits/stdc++.h>
using namespace std;
template<typename T>class rop;
template<typename T>class node{
friend class rop<T>;
private:
node<T>()=default;
node<T>(T t);
node<T>(vector<T>&s);
int Height=0,Size=0,Begin=0;
node<T>*Lson=nullptr;
node<T>*Rson=nullptr;
vector<T>*Data=nullptr;
};
template<typename T>class rop{
public:
void clear();
node<T>*set(vector<T>&v);
T Fetch(node<T>*R,int Index);
node<T>*change(node<T>*R,int Index,T t);
inline int height(node<T>*R);
inline int size(node<T>*R);
node<T>*connect(node<T>*Left,node<T>*Right);
node<T>*uncheckconnect(node<T>*Left,node<T>*Right);
node<T>*substring(node<T>*R,int Start,int Len);
vector<node<T>*>MemoryPool;
};
template<typename T>
node<T>::node(T t):Size(1){Data=new vector<T>{t};}
template<typename T>
node<T>::node(vector<T>&s):Size(s.size()){Data=new vector<T>(s);}
template<typename T>
void rop<T>::clear(){
for(auto i:MemoryPool){
if(i!=nullptr)delete i->Data;
delete i;
}
MemoryPool.clear();
}
template<typename T>
node<T>*rop<T>::set(vector<T>&v){
clear();
node<T>*R=new node<T>(v);
MemoryPool.push_back(R);
return R;
}
template<typename T>
inline int rop<T>::height(node<T>*R){return R?R->Height:-1;}
template<typename T>
inline int rop<T>::size(node<T>*R){return R?R->Size:0;}
template<typename T>
node<T>*rop<T>::connect(node<T>*Left,node<T>*Right){
if(Left==nullptr)return Right;
if(Right==nullptr)return Left;
if(height(Left->Lson)>height(Left->Rson)&&height(Left->Lson)>height(Right))return uncheckconnect(Left->Lson,uncheckconnect(Left->Rson,Right));
if(height(Right->Rson)>height(Right->Lson)&&height(Right->Rson)>height(Left))return uncheckconnect(uncheckconnect(Left,Right->Lson),Right->Rson);
return uncheckconnect(Left,Right);
}
template<typename T>
T rop<T>::Fetch(node<T>*R,int Index){
if(R->Data!=nullptr)return(*(R->Data))[R->Begin+Index];
if(Index>=size(R->Lson))return Fetch(R->Rson,Index-size(R->Lson));
else return Fetch(R->Lson,Index);
}
template<typename T>
node<T>*rop<T>::change(node<T>*R,int Index,T t){
if(Index<0||Index>=size(R))return R;
node<T>*Left=nullptr,*Right=nullptr;
if(Index>0)Left=substring(R,0,Index);
if(Index<size(R)-1)Right=substring(R,Index+1,size(R)-Index-1);
node<T>*MidRope=new node<T>(t);
MemoryPool.push_back(MidRope);
return connect(Left,connect(MidRope,Right));
}
template<typename T>
node<T>*rop<T>::uncheckconnect(node<T>*Left,node<T>*Right){
node<T>*ConnectNode=new node<T>();
ConnectNode->Lson=Left;
ConnectNode->Rson=Right;
ConnectNode->Height=max(height(Left),height(Right))+ 1;
ConnectNode->Size=size(Left)+ size(Right);
MemoryPool.push_back(ConnectNode);
return ConnectNode;
}
template<typename T>
node<T>*rop<T>::substring(node<T>*R,int Start,int Len){
if(R==nullptr)return R;
if(Start<=0&&Len>=size(R))return R;
if(R->Data!=nullptr){
node<T>*SubNode=new node<T>(*R);
SubNode->Begin=R->Begin +(Start>0?Start:0);
SubNode->Size=min(R->Size,Len);
return SubNode;
}
node<T>*Left,*Right;
if(Start+Len<=size(R->Lson))return substring(R->Lson,Start,Len);
if(Start>=size(R->Lson))return substring(R->Rson,Start-size(R->Lson),Len);
Left=substring(R->Lson,Start,size(R->Lson)-Start);
Right=substring(R->Rson,0,Len-(size(R->Lson)-Start));
return connect(Left,Right);
}手写 LOG
#define LOG2(x) (31^(__builtin_clz(x)))多项式的高精板子
#ifndef BIGINTEGER_H
#define BIGINTEGER_H
#include <cmath>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <algorithm>
class BigInteger {
protected:
using digit_t = long long;
static const int WIDTH = 9;
static const digit_t BASE = 1e9;
static const long long FFT_LIMIT = 10000;
digit_t* digits;
long capacity, size;
bool flag;
inline void push(const digit_t&);
inline void pop();
inline int compare(const BigInteger&) const;
static inline BigInteger fft_mul(const BigInteger&, const BigInteger&);
inline BigInteger move(const long long&);
static inline BigInteger inverse(const BigInteger&);
public:
inline void reserve(const long&);
protected:
inline void resize(const long&);
public:
BigInteger() : digits(nullptr), flag(true) {*this = 0;}
BigInteger(const BigInteger& x) : digits(nullptr) {*this = x;}
BigInteger(const long long& x) : digits(nullptr) {*this = x;}
BigInteger(const std::string& s) : digits(nullptr) {*this = s;}
BigInteger& operator= (const BigInteger&);
BigInteger& operator= (const long long&);
BigInteger& operator= (const std::string&);
~BigInteger() {
if (digits != nullptr) delete[] digits, digits = nullptr;
}
friend std::ostream& operator << (std::ostream& out, const BigInteger& 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, BigInteger& x) {
std::string s; in >> s; x = s;
return in;
}
std::string to_string() const;
BigInteger operator- () const;
BigInteger abs() const;
bool operator< (const BigInteger&) const;
bool operator> (const BigInteger&) const;
bool operator== (const BigInteger&) const;
bool operator!= (const BigInteger&) const;
bool operator<= (const BigInteger&) const;
bool operator>= (const BigInteger&) const;
BigInteger operator+ (const BigInteger&) const;
BigInteger operator- (const BigInteger&) const;
BigInteger operator* (const BigInteger&) const;
BigInteger operator/ (const long long&) const;
BigInteger operator/ (const BigInteger&) const;
BigInteger operator% (const long long&) const;
BigInteger operator% (const BigInteger&) const;
BigInteger pow(const long long&) const;
BigInteger pow(const long long&, const BigInteger&) const;
BigInteger sqrt(const long long& = 2) const;
BigInteger& operator+= (const BigInteger&);
BigInteger& operator-= (const BigInteger&);
BigInteger& operator*= (const BigInteger&);
BigInteger& operator/= (const long long&);
BigInteger& operator/= (const BigInteger&);
BigInteger& operator%= (const long long&);
BigInteger& operator%= (const BigInteger&);
};
inline void BigInteger::push(const digit_t& val) {
if (capacity == size) {
long new_capacity = 0;
if (capacity < 1000) new_capacity = capacity << 1;
else new_capacity = (new_capacity >> 1) * 3;
digit_t* new_digits = new digit_t[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 BigInteger::pop() {digits[size--] = 0;}
inline int BigInteger::compare(const BigInteger& 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 BigInteger::reserve(const long& sz) {
if (digits != nullptr) delete[] digits;
capacity = sz, size = 0;
digits = new digit_t[sz + 1];
std::memset(digits, 0, sizeof(digit_t) * (sz + 1));
}
inline void BigInteger::resize(const long& sz) {
reserve(sz), size = sz;
}
BigInteger& BigInteger::operator= (const BigInteger& x) {
reserve(x.size + 1);
flag = x.flag, size = x.size;
std::memcpy(digits, x.digits, sizeof(digit_t) * (x.size + 1));
return *this;
}
BigInteger& BigInteger::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;
}
BigInteger& BigInteger::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 BigInteger::to_string() const {
std::stringstream ss; ss << *this;
return ss.str();
}
BigInteger BigInteger::operator- () const {
BigInteger res = *this;
res.flag = !flag;
return res;
}
BigInteger BigInteger::abs() const {
BigInteger res = *this;
res.flag = true;
return res;
}
bool BigInteger::operator< (const BigInteger& x) const {return compare(x) < 0;}
bool BigInteger::operator> (const BigInteger& x) const {return compare(x) > 0;}
bool BigInteger::operator== (const BigInteger& x) const {return compare(x) == 0;}
bool BigInteger::operator!= (const BigInteger& x) const {return compare(x) != 0;}
bool BigInteger::operator<= (const BigInteger& x) const {return compare(x) <= 0;}
bool BigInteger::operator>= (const BigInteger& x) const {return compare(x) >= 0;}
BigInteger BigInteger::operator+ (const BigInteger& x) const {
if (!x.flag) return *this - x.abs();
if (!flag) return x - abs();
BigInteger res;
res.flag = !(flag ^ x.flag);
int n = std::max(size, x.size) + 1;
res.reserve(n);
digit_t carry = 0;
for (int i = 1; i <= n; i++) {
digit_t 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;
}
BigInteger BigInteger::operator- (const BigInteger& x) const {
if (!x.flag) return *this + x.abs();
if (!flag) return -(abs() + x);
BigInteger res;
if (*this < x) res.flag = false;
digit_t carry = 0;
int n = std::max(size, x.size);
res.reserve(n);
for (int i = 1; i <= n; i++) {
digit_t 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 {
constexpr 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;
}
}
}
}
}
BigInteger BigInteger::fft_mul(const BigInteger& a, const BigInteger& 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);
BigInteger res;
res.resize(a.size + b.size + 1);
digit_t carry = 0;
for (int i = 0; i <= a.size + b.size; i++) {
carry += (digit_t)(arr[i * 3].imag * -0.5 / lim + 0.5);
carry += (digit_t)(arr[i * 3 + 1].imag * -0.5 / lim + 0.5) * 1000LL;
carry += (digit_t)(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;
}
BigInteger BigInteger::operator* (const BigInteger& x) const {
BigInteger zero = 0;
if (*this == zero || x == zero) return zero;
int n = size, m = x.size;
if (1LL * n * m >= 1) {
BigInteger res = fft_mul(*this, x);
res.flag = !(flag ^ x.flag);
return res;
}
BigInteger 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;
}
BigInteger BigInteger::operator/ (const long long& x) const {
if (x == 0) throw -1;
if (*this == 0) return 0;
BigInteger res;
res.flag = !(flag ^ (x >= 0));
digit_t 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;
}
BigInteger BigInteger::operator/ (const BigInteger& x) const {
static std::vector<BigInteger> pow2 = {1};
static std::vector<BigInteger> estimate;
estimate.clear();
if (*this < x) return 0;
BigInteger 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 BigInteger(pow2.back());
}
BigInteger 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;
}
BigInteger BigInteger::operator% (const long long& x) const {
if (x == 2) return digits[1] & 1;
return *this - (*this / x * x);
}
BigInteger BigInteger::operator% (const BigInteger& x) const {
return *this - (*this / x * x);
}
BigInteger BigInteger::pow(const long long& x) const {
BigInteger res = 1, a = *this;
for (long long t = x; t != 0; t >>= 1) {
if (t & 1) res *= a;
a *= a;
}
return res;
}
BigInteger BigInteger::pow(const long long& x, const BigInteger& p) const {
BigInteger res = 1, a = *this % p;
for (long long t = x; t != 0; t >>= 1) {
if (t & 1) res = res * a % p;
a = a * a % p;
}
return res;
}
BigInteger BigInteger::sqrt(const long long& x) const {
BigInteger l = 0, r = 1;
while (r.pow(x) <= *this) l = r, r += r;
while (l + 1 < r) {
BigInteger mid = (l + r) / 2;
if (mid.pow(x) <= *this) l = mid;
else r = mid;
}
return l.pow(x) <= *this ? l : r;
}
BigInteger& BigInteger::operator+= (const BigInteger& x) {return *this = *this + x;}
BigInteger& BigInteger::operator-= (const BigInteger& x) {return *this = *this - x;}
BigInteger& BigInteger::operator*= (const BigInteger& x) {return *this = *this * x;}
BigInteger& BigInteger::operator/= (const long long& x) {return *this = *this / x;}
BigInteger& BigInteger::operator/= (const BigInteger& x) {return *this = *this / x;}
BigInteger& BigInteger::operator%= (const long long& x) {return *this = *this / x;}
BigInteger& BigInteger::operator%= (const BigInteger& x) {return *this = *this % x;}
#endif //BIGINTEGER_H
计时器(基本不耗时间,卡常用)
long long Timer[100];
chrono::high_resolution_clock::time_point nwT[10];
#define rev(x) nwT[x]=chrono::high_resolution_clock::now()
#define oti(x) Timer[x]+=(chrono::high_resolution_clock::now()-nwT[x]).count()
struct TIMER{
~TIMER(){for(int i=0;i<10;++i)cerr<<Timer[i]/1e6<<"ms ";cerr<<endl;}
}TIMER_;