namespace Templates {
struct Bitset {
#define For(i, l, r) for (int i = (l); i <= (r); i ++)
#define IV inline void
#define IB inline bool
#define IS inline Bitset
typedef unsigned int uint;
typedef unsigned long long ull;
const int INF = 1e9;
vector<ull> bit; int len;
Bitset(int x = 0) {bit.resize(x); len = x;}
IV resize(int x) {bit.resize((x >> 6) + 1); len = (x >> 6) + 1;}
IV set1(int x) {bit[x>>6] |= (1ull<<(x&63));}
IV set0(int x) {bit[x>>6] &= (~(1ull<<(x&63)));}
IV flip(int x) {bit[x>>6] ^= (1ull<<(x&63));}
IB operator [] (int x) {return (bit[x>>6] >> (x&63)) & 1;}
IB any() {For(i, 0, len-1) if(bit[i]) return 1;return 0;}
IS operator ~ () const {Bitset res(len);For(i, 0, len-1) res.bit[i] = ~bit[i];return res;}
IS operator | (const Bitset &b) const {Bitset res(len); For(i, 0, len-1) res.bit[i] = bit[i] | b.bit[i];return res;}
IS operator & (const Bitset &b) const {Bitset res(len); For(i, 0, len-1) res.bit[i] = bit[i] & b.bit[i];return res;}
IS operator ^ (const Bitset &b) const {Bitset res(len); For(i, 0, len-1) res.bit[i] = bit[i] ^ b.bit[i];return res;}
IV operator &= (const Bitset &b) {For(i, 0, len-1) bit[i] &= b.bit[i];}
IV operator |= (const Bitset &b) {For(i, 0, len-1) bit[i] |= b.bit[i];}
IV operator ^= (const Bitset &b) {For(i, 0, len-1) bit[i] ^= b.bit[i];}
uint count() {int res(0); For(i, 0, len-1) res += __builtin_popcountll(bit[i]);return res;}
Bitset operator << (const int t) const {
Bitset res(len); int high = t >> 6, low = t & 63; ull lst = 0;
for(int i = 0; i + high < len; i++)
{res.bit[i + high] = (lst | (bit[i] << low)); if(low) lst = (bit[i] >> (64 - low));}
return res;
}
Bitset operator >> (const int t) const {
Bitset res(len); int high = t >> 6, low = t & 63; ull lst = 0;
for(int i = len - 1; i >= high; i--)
{res.bit[i - high] = (lst | (bit[i] >> low)); if(low) lst = (bit[i] << (64 - low));}
return res;
}
void operator <<= (const int t) {
int high = t >> 6, low = t & 63;
for(int i = len - high - 1; ~i; i--)
{bit[i + high] = (bit[i] << low); if(low && i) bit[i + high] |= (bit[i - 1] >> (64 - low));}
for(int i = 0; i < min(high, len - 1); i++) bit[i] = 0;
}
void operator >>= (const int t) {
int high = t >> 6, low = t & 63;
for(int i = high; i < len; i++)
{bit[i - high] = (bit[i] >> low); if(low && i != len) bit[i - high] |= (bit[i + 1] << (64 - low));}
for(int i = max(len - high, 0); i < len; i++) bit[i] = 0;
}
int findnext(int t) {
int high = t >> 6, low = t & 63; ull tmp = bit[high] & ~((1ull << (low + 1)) - 1);
if(low != 63 && tmp) return (high << 6) + __builtin_ctzll(tmp);
for(int i = high + 1; i < len; i++) if(bit[i]) return (i << 6) + __builtin_ctzll(bit[i]);
return INF;
}
int findprev(int t) {
int high = t >> 6, low = t & 63; ull tmp = bit[high] & ((1ull << low) - 1);
if(tmp) return (high << 6) + 63 - __builtin_clzll(tmp);
for(int i = high - 1; i >= 0; i--) if(bit[i]) return (i << 6) + 63 - __builtin_clzll(bit[i]);
return -INF;
}
#undef IV
#undef IB
#undef IS
#undef For
};
#define cst128 const int128
#define INT128_MAX int128{9223372036854775807,-1}
#define INT128_MIN int128{9223372036854775808u,0}
static const char HEXDIGITS[]="0123456789abcdefghijklmnopqrstuvwxyz";
static const char HEXDIGITSUPPER[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct int128;
void tostring128(int128,const int,char*,unsigned long long);
void tostring128_upper(int128,const int,char*,unsigned long long);
struct int128{
uint64_t up=0,low=0;
int128(){}
template<typename _Tp>
int128(const _Tp x){up=x<0?-1:0;low=x;}
int128(const unsigned __int64 x){low=x;}
int128(std::initializer_list<uint64_t>initlist){
up=*initlist.begin();
low=*(initlist.begin()+1);
}
int128 operator+=(cst128 x);
int128 operator-=(cst128 x);
int128 operator*=(cst128 x);
int128 operator/=(cst128 x);
int128 operator%=(cst128 x);
int128 operator<<=(const int x);
int128 operator>>=(const int x);
int128 operator&=(cst128 x);
int128 operator|=(cst128 x);
int128 operator^=(cst128 x);
operator unsigned long long(){return low;}
operator bool(){return up&&low;}
const char* c_str(const int base=10,const int upper=0){
static char Str[129];
(upper?tostring128_upper:tostring128)(*this,base,Str,129);
return Str;
}
};
int128 operator ""_128(unsigned long long int x){return int128(x);}
int128 operator&(cst128 n1,cst128 n2){return{n1.up&n2.up,n1.low&n2.low};}
int128 operator|(cst128 n1,cst128 n2){return{n1.up|n2.up,n1.low|n2.low};}
int128 operator~(cst128 num){return{~num.up,~num.low};}
int128 operator^(cst128 n1,cst128 n2){return{n1.up^n2.up,n1.low^n2.low};}
bool zero128(cst128 num){return !num.up&&!num.low;}
int128 operator<<(cst128 num,const int x){return x>=128?0:x==64?int128{num.low,0}:!x?num:x<64?int128{(num.up<<x)+(num.low>>(64-x)),num.low<<x}:128>x&&x>64?int128{num.low<<(x-64),0}:(int128)0;}
int128 operator>>(cst128 num,const int x){return x>=128?0:x==64?int128{0,num.up}:!x?num:x<64?int128{num.up>>x,(num.up<<(64-x))+(num.low>>x)}:128>x&&x>64?int128{0,num.up>>(x-64)}:(int128)0;}
int bits128(cst128 num){
int result=0;
if(num.up){
result=64;
for(uint64_t up=num.up;up;++result)
up>>=1;
}else
for(uint64_t low=num.low;low;++result)
low>>=1;
return result;
}
bool operator==(cst128 n1,cst128 n2){return n1.up==n2.up&&n1.low==n2.low;}
bool operator!=(cst128 n1,cst128 n2){return!(n1==n2);}
bool operator>(cst128 n1,cst128 n2){return (n1.up&0x8000000000000000)^(n2.up&0x8000000000000000)?bool(n2.up&0x8000000000000000):n1.up==n2.up?n1.low>n2.low:n1.up>n2.up;}
bool operator>=(cst128 n1,cst128 n2){return n1>n2||n1==n2;}
bool operator<(cst128 n1,cst128 n2){return!(n1>=n2);}
bool operator<=(cst128 n1,cst128 n2){return!(n1>n2);}
//bool operator&&(cst128 n1,cst128 n2){return n1&&n2;}
//bool operator||(cst128 n1,cst128 n2){return n1||n2;}
bool operator!(cst128 num){return!(num.up||num.low);}
int128 operator+(cst128 n1,cst128 n2){return{n1.up+n2.up+(n1.low+n2.low<n1.low),n1.low+n2.low};}
int128 operator-(cst128 n1,cst128 n2){return{n1.up-n2.up-(n1.low-n2.low>n1.low),n1.low-n2.low};}
int128 operator-(cst128 num){return{~num.up,-num.low};}
int128 operator*(cst128 n1,cst128 n2){
int128 target,tmp,tmp2;
uint64_t
top[4]={n1.up>>32,n1.up&0xffffffff,n1.low>>32,n1.low&0xffffffff},
bottom[4]={n2.up>>32,n2.up&0xffffffff,n2.low>>32,n2.low&0xffffffff},products[4][4];
for(signed char y=3;y>=0;--y)
for(signed char x=3;x>=0;--x)
products[3-x][y]=top[x]*bottom[y];
uint64_t fourth32=products[0][3]&0xffffffff;
uint64_t third32=(products[0][2]&0xffffffff)+(products[0][3]>>32)+(products[1][3]&0xffffffff);
uint64_t second32=(products[0][1]&0xffffffff)+(products[0][2]>>32)+(products[1][2]&0xffffffff)+(products[1][3]>>32)+(products[2][3]&0xffffffff);
uint64_t first32=(products[0][0]&0xffffffff)+(products[0][1]>>32)+(products[1][1]&0xffffffff)+(products[1][2]>>32)+(products[2][2]&0xffffffff)+(products[2][3]>>32)+(products[3][3]&0xffffffff);
return(int128)fourth32+int128{second32,0}+int128{first32<<32,0}+int128{third32>>32,third32<<32};
}
void divmod128(const int128 &l,const int128 &r,int128 &retDiv,int128 &retMod){
int128 copyd,adder,resDiv=0,resMod=l;
const int diffBits=bits128(l)-bits128(r);
if(r>l){
retMod=l;
retDiv=0;
}else{
copyd=r<<diffBits;
adder=int128(1)<<diffBits;
if(copyd>resMod){
copyd=copyd>>1;
adder=adder>>1;
}
while(resMod>=r){
if(resMod>=copyd){
resMod=resMod-copyd;
resDiv=resDiv|adder;
}
copyd=copyd>>1;
adder=adder>>1;
}
retDiv=resDiv;
retMod=resMod;
}
}
int128 operator/(cst128 l,cst128 r){
int128 copyd,adder,resDiv=0,resMod=l,retDiv;
const int diffBits=bits128(l)-bits128(r);
if(r>l)retDiv=0;
else{
copyd=r<<diffBits;
adder=int128(1)<<diffBits;
if(copyd>resMod){
copyd=copyd>>1;
adder=adder>>1;
}
while(resMod>=r){
if(resMod>=copyd){
resMod=resMod-copyd;
resDiv=resDiv|adder;
}
copyd=copyd>>1;
adder=adder>>1;
}
retDiv=resDiv;
}return retDiv;
}
int128 operator%(cst128 l,cst128 r){return l-l/r*r;}
int128 int128::operator+=(cst128 r){return *this=*this+r;}
int128 int128::operator-=(cst128 r){return *this=*this-r;}
int128 int128::operator*=(cst128 r){return *this=*this*r;}
int128 int128::operator/=(cst128 r){return *this=*this/r;}
int128 int128::operator%=(cst128 r){return *this=*this%r;}
int128 int128::operator<<=(const int r){return *this=*this<<r;}
int128 int128::operator>>=(const int r){return *this=*this>>r;}
int128 int128::operator&=(cst128 r){return *this=*this&r;}
int128 int128::operator|=(cst128 r){return *this=*this&r;}
static void reverseString(char* str,const unsigned long long length){
for(unsigned long long i=0,j=length-1;i<j;++i,--j)
str[i]^=str[j]^=str[i]^=str[j];
}
void tostring128(int128 num,const int base,char *out,unsigned long long outLength){
if(num==INT128_MIN&&base==10)return(void)memcpy(out,"-170141183460469231731687303715884105728",40);
bool neg=num<0_128;
if(neg)num=-num;
int128 rDiv=num,rMod=0;
unsigned long long pos=0;
if(base<2||base>36)return;
do{
if(pos>outLength-1)return;
divmod128(rDiv,base,rDiv,rMod);
out[pos++]=HEXDIGITS[rMod.low];
}while(!zero128(rDiv));
out[pos]=neg?45:0;
if(neg)out[++pos]=0;
reverseString(out,pos);
}
void tostring128_upper(int128 num,const int base,char *out,unsigned long long outLength){
if(num==INT128_MIN&&base==10)return(void)memcpy(out,"-170141183460469231731687303715884105728",40);
bool neg=num<0_128;
if(neg)num=-num;
int128 rDiv=num,rMod=0;
unsigned long long pos=0;
if(base<2||base>36)return;
do{
if(pos>outLength-1)return;
divmod128(rDiv,base,rDiv,rMod);
out[pos++]=HEXDIGITSUPPER[rMod.low];
}while(!zero128(rDiv));
out[pos]=neg?45:0;
if(neg)out[++pos]=0;
reverseString(out,pos);
}
void tostring128(int128 num,const int base,char *out,unsigned long long outLength,const char* BaseMap){
bool neg=num<0_128;
if(neg)num=-num;
int128 rDiv=num,rMod=0;
unsigned long long pos=0;
do{
if(pos>outLength-1)return;
divmod128(rDiv,base,rDiv,rMod);
out[pos++]=BaseMap[rMod.low];
}while(!zero128(rDiv));
out[pos]=neg?45:0;
if(neg)out[++pos]=0;
reverseString(out,pos);
}
}剪贴板
剪贴板 b41byn3m
剪贴板信息
剪贴板信息
- 收录
- 更新