Content

Monday, December 7, 2020

My Template

Debug Function : 

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << '\n'; }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << " "; err(++it, args...); }
view raw debug.cpp hosted with ❤ by GitHub
Bit Check : 

inline int Set(ll N, ll pos){return N=N | (1LL<<pos);}
inline int Reset(ll N, ll pos){return N=N & ~(1LL<<pos);}
inline bool Check(ll N, ll pos){return (bool)(N & (1LL<<pos));}
view raw bit_check.cpp hosted with ❤ by GitHub
Check two fractional number to skip precision error : 

inline bool Equal(ld x, ld y) { return fabs(x-y) < eps; } ///x==y
inline bool Greater(ld x, ld y) { return (x - eps) > y; } /// x>y
inline bool Lesser(ld x, ld y) { return (x + eps) < y; } /// x<y
view raw dc.cpp hosted with ❤ by GitHub
Modular Arithmetic :

inline void normal(ll &a) { a %= mod; (a < 0) && (a += mod); }
inline ll modMul(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; }
inline ll modAdd(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; }
inline ll modSub(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1LL) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; }
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
view raw ma.cpp hosted with ❤ by GitHub
Directional Arrays : 

int dx[] = {+0+0-1+1}; ///Up-down, Left-Right
int dy[] = {+1-1+0+0};
int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
int dx[] = {-2-2-1-1,  1,  1,  2,  2}; ///Knight's Move
int dy[] = {-1,  1-2,  2-2,  2-1,  1}; 

Compiler Optimization : 

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")

No comments:

Post a Comment