template<typename Tec>
Tec max(const Tec&,const Tec&);
template<typename Tec>
Tec min(const Tec&,const Tec&);
template<typename Tec>
void makemax(Tec&,const Tec&);
template<typename Tec>
void makemin(Tec&,const Tec&);
template<typename Tec>
void swap(Tec,Tec);
template<typename Tec>
Tec lowbit(const Tec&);
template<typename Tec>
Tec highbit(const Tec&);
template<typename Tec>
int bitcount(const Tec&);
template<typename Tec>
Tec XOR(const Tec&);
template<typename Tec>
Tec AND(const Tec&,const Tec&);
template<typename Tec>
Tec OR(const Tec&,const Tec&);
template<typename Tec>
Tec light_multi(Tec,Tec,const Tec&);
template<typename Tec>
Tec turtle_multi(Tec,Tec,const Tec&);
template<typename Tec>
Tec quick_pow(Tec,Tec,const Tec&);
template<typename Tec>
Tec defended_pow(Tec,Tec,const Tec&);
template<typename Tec>
Tec turtle_pow(Tec,Tec,const Tec&);
float InvSqrt(float);
float Sqrt(const float&);
template<typename Tec>
Tec max(const Tec &__x,const Tec __y) {
return __x < __y ? __y : __x;
}
template<typename Tec>
Tec min(const Tec &__x,const Tec &__y) {
return __x < __y ? __x : __y;
}
template<typename Tec>
void makemax(Tec &__x,const Tec &__y) {
__x = max(__x,__y);
}
template<typename Tec>
void makemin(Tec &__x,const Tec &__y) {
__x = min(__x,__y);
}
template<typename Tec>
void swap(Tec &__x,Tec &__y) {
Tec __tmp = __x;
__x = __y;
__y = __tmp;
}
template<typename Tec>
Tec lowbit(const Tec &__x) {
return __x&(-__x);
}
template<typename Tec>
Tec highbit(const Tec &__x) {
return (Tec)1<<((sizeof(Tec)<<3)-__builtin_clz(__x)-1);
}
template<typename Tec>
int bitcount(const Tec &__x) {
return __builtin_popcount(__x);
}
template<typename Tec>
Tec XOR(const Tec &__x) {
if(__x&2)
if(__x&1)
return 0;
else
return __x+1;
else
if(__x&1)
return 1;
else
return __x;
}
template<typename Tec>
Tec AND(const Tec &__b,const Tec &__e) {
return (~((highbit(__b^__e)<<1)-1))&__b&__e;
}
template<typename Tec>
Tec OR(const Tec &__b,const Tec &__e) {
return ~AND(~__b,~__e);
}
template<typename Tec>
Tec turtle_multi(Tec _a,Tec _n,Tec _p) {
_a %= _p, _n %= _p;
Tec _res = 0;
while(_n) {
if(_n&1) {
_res = _res+_a;
if(_res >= _p)
_res -= _p;
}
_a <<= 1;
if(_a >= _p)
_a -= _p;
_n >>= 1;
}
return _res;
}
template<typename Tec>
Tec light_multi(Tec _a,Tec _b,const Tec &_p) {
_a %= _p, _b %= _p;
Tec _c = (long double)_a*_b/_p;
Tec _x = _a*_b, _y = _c*_p;
Tec _res = (Tec)(_x%_p)-(Tec)(_y%_p);
if(_res < 0) _res += _p;
return _res;
}
template<typename Tec>
Tec quick_pow(Tec _a,Tec _n,const Tec &_p) {
Tec _res = 1;
while(_n) {
if(_n) _res = _res*_a%_p;
_a = _a*_a%_p;
_n >>= 1;
}
return _res;
}
template<typename Tec>
Tec defended_pow(Tec _a,Tec _n,const Tec &_p) {
Tec _res = 1;
while(_n) {
if(_n&1) _res = light_multi(_res,_a,_p);
_a = light_multi(_a,_a,_p);
_n >>= 1;
}
return _res;
}
template<typename Tec>
Tec turtle_pow(Tec _a,Tec _n,const Tec &_p) {
Tec _res = 1;
while(_n) {
if(_n) _res = turtle_multi(_res,_a,_p);
_a = turtle_multi(_a,_a,_p);
_n >>= 1;
}
return _res;
}
float InvSqrt(float __x) {
float __xhalf = 0.5f*__x;
int _i_ = *(int*)&__x;
_i_ = 0x5f375a86-(_i_>>1);
__x = *(float*)&_i_;
__x = __x*(1.5f-__xhalf*__x*__x);
__x = __x*(1.5f-__xhalf*__x*__x);
__x = __x*(1.5f-__xhalf*__x*__x);
return __x;
}
float Sqrt(const float &__x) {
return 1.0/InvSqrt(__x);
}//精度可达1e-6;
namespace INUMBER {
class Inum {
private :
double _apu, _ipu;
public :
Inum () {_apu = _ipu = 0;}
template<typename Tec>
Inum (const Tec &__a,const Tec &__b) {_apu = __a, _ipu == __b;}
Inum operator + (const double&);
Inum friend operator + (const Inum&,const Inum&);
Inum operator - (const double&);
Inum friend operator - (const Inum&,const Inum&);
Inum operator * (const double&);
Inum friend operator * (const Inum&,const Inum&);
Inum operator / (const double&);
Inum friend operator / (const Inum&,const Inum&);
static Inum inv(const Inum &__in) {
return Inum{__in._apu,-__in._ipu};
}
Inum operator + (double &_x) {
return Inum{_apu+_x,_ipu};
}
Inum friend operator + (const Inum &_x,const Inum &_y) {
return Inum{_x._apu+_y._apu,_x._ipu+_y._ipu};
}
Inum operator - (double &_x) {
return Inum{_apu-_x,_ipu};
}
Inum friend operator - (const Inum &_x,const Inum &_y) {
return Inum{_x._apu+_y._apu,_x._ipu+_y._ipu};
}
Inum operator * (double &_x) {
return Inum{_apu*_x,_ipu*_x};
}
Inum friend operator * (const Inum &_x,const Inum &_y) {
return Inum{_x._apu*_y._apu-_x._ipu*_y._ipu,
_x._ipu*_y._apu+_x._apu*_y._ipu};
}
Inum operator / (double &_x) {
return Inum{_apu/_x,_ipu/_x};
}
Inum friend operator / (const Inum &_x,const Inum &_y) {
return (_x*inv(_y))/(_y._apu*_y._apu+_y._ipu*_y._ipu);
}
};
} using namespace INUMBER;