模板
AtCoder Library
快速乘
LL Mul(LL x,LL y,LL p) { return (x*y-LL((long double)x/p*y)*p+p)%p; }
可删堆
struct Heap {
__gnu_pbds::priority_queue<int,greater<>> a,b;
void push(int x) { a.push(x); while(sz(b)&&a.top()==b.top())a.pop(),b.pop(); }
void pop(int x) { b.push(x); while(sz(b)&&a.top()==b.top())a.pop(),b.pop(); }
int top() { return a.top(); }
};
双模哈希
template<const int mod> struct mint {
int x; mint(int x=0):x(x<0?x+mod:x<mod?x:x-mod){}
mint(LL y) { y%=mod, x=y<0?y+mod:y; }
mint& operator += (const mint &a) { x=x+a.x<mod?x+a.x:x+a.x-mod; return *this; }
mint& operator -= (const mint &a) { x=x<a.x?x-a.x+mod:x-a.x; return *this; }
mint& operator *= (const mint &a) { x=1ll*x*a.x%mod; return *this; }
friend mint operator + (mint a,const mint &b) { return a+=b; }
friend mint operator - (mint a,const mint &b) { return a-=b; }
friend mint operator * (mint a,const mint &b) { return a*=b; }
bool operator == (const mint &a) const { return x==a.x; }
};
typedef mint<993244853> mint1;
typedef mint<998244853> mint2;
typedef pair<mint1,mint2> H;
H operator + (const H &a,const H &b) { return {a.fi+b.fi,a.se+b.se}; }
H operator - (const H &a,const H &b) { return {a.fi-b.fi,a.se-b.se}; }
H operator * (const H &a,const H &b) { return {a.fi*b.fi,a.se*b.se}; }
const H bas(131,1331);
自定义哈希函数
struct Hash {
static uint h(uint x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ x>>30) * 0xbf58476d1ce4e5b9;
return x ^ x>>31;
}
size_t operator()(int x) const {
static const uint t = time(0);
return h(x+t);
}
};
分数类
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) ((x)/gcd(x,y)*(y))
struct db {
LL u,d; db(LL u=0,LL d=1):u(u),d(d){}
db inv() const { return db(d,u); }
db operator + (const db &x) const {
LL b = lcm(d,x.d), a = b/d*u + b/x.d*x.u, c = gcd(a,b);
return db(a/c,b/c);
}
db operator - (const db &x) const { return *this + db(-x.u,x.d); }
db operator * (const db &x) const {
LL a = gcd(u,x.d), b = gcd(d,x.u);
return db((u/a)*(x.u/b),(d/b)*(x.d/a));
}
db operator / (const db &x) const { return *this * x.rev(); }
bool operator < (const db &x) const { return u*x.d < d*x.u; }
} O;
乘法取模优化
要求非负环境
struct MOD {
uLL b,m;
void init(int mod) { b = mod, m = (LLL(1)<<64)/b; }
template<typename T>friend T operator % (const T &x,const MOD &mod){
uLL q = (LLL(mod.m)*x)>>64, r = x-q*mod.b;
return r>=mod.b ? r-mod.b : r;
}
template<typename T>friend T operator %= (T &x,const MOD &mod){
uLL q = (LLL(mod.m)*x)>>64, r = x-q*mod.b;
return x = r>=mod.b ? r-mod.b : r;
}
} Mod;
哈希表
struct HT {
static const int N = 2e6+3;
int n,head[N],nxt[N],val[N]; LL key[N];
void ins(LL x,int y) {
int p = x%N;
for(int i = head[p]; i; i = nxt[i]) if( key[i] == x )
return val[i] = y, void();
key[++n] = x, val[n] = y, nxt[n] = head[p], head[p] = n;
}
int at(LL x) {
int p = x%N;
for(int i = head[p]; i; i = nxt[i]) if( key[i] == x ) return val[i];
return -1;
}
} ;
编译优化
#pragma GCC optimize("Ofast","unroll-loops")
#pragma GCC target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,avx2")
基数排序
uint n,a[N],b[N],buc[256];
#define _sort(bg,ed,b,k) {\
memset(buc,0,sizeof buc);\
for(auto *i = bg; i != ed; ++i) ++buc[*i>>k&255];\
for(auto *i = buc+1; i != buc+256; ++i) *i += i[-1];\
for(auto *i = ed; i != bg; --i) (b)[--buc[i[-1]>>k&255]] = i[-1];\
}
_sort(a,a+n,b,0); _sort(b,b+n,a,8); _sort(a,a+n,b,16); _sort(b,b+n,a,24);