模板库
自己的模板,放在前台主要是排版好看qwq
读入优化
struct IO{//-std=c++11,with cstdio and cctype
private:
static constexpr int ibufsiz = 1 << 20;
char ibuf[ibufsiz + 1],*inow = ibuf,*ied = ibuf;
static constexpr int obufsiz = 1 << 20;
char obuf[obufsiz],*onow = obuf;
const char *oed = obuf + obufsiz;
public:
inline char getchar(){
#ifndef ONLINE_JUDGE
return ::getchar();
#else
if(inow == ied){
ied = ibuf + sizeof(char) * fread(ibuf,sizeof(char),ibufsiz,stdin);
*ied = '\0';
inow = ibuf;
}
return *inow++;
#endif
}
template<typename T>
inline void read(T &x){
static bool flg;flg = 0;
x = 0;char c = getchar();
while(!isdigit(c))flg = c == '-' ? 1 : flg,c = getchar();
while(isdigit(c))x = x * 10 + c - '0',c = getchar();
if(flg)x = -x;
}
template <typename T,typename ...Y>
inline void read(T &x,Y&... X){read(x);read(X...);}
inline int readi(){static int res;read(res);return res;}
inline long long readll(){static long long res;read(res);return res;}
inline void flush(){
fwrite(obuf,sizeof(char),onow - obuf,stdout);
fflush(stdout);
onow = obuf;
}
inline void putchar(char c){
#ifndef ONLINE_JUDGE
::putchar(c);
#else
*onow++ = c;
if(onow == oed){
fwrite(obuf,sizeof(char),obufsiz,stdout);
onow = obuf;
}
#endif
}
template <typename T>
inline void write(T x,char split = '\0'){
static unsigned char buf[64];
if(x < 0)putchar('-'),x = -x;
int p = 0;
do{
buf[++p] = x % 10;
x /= 10;
}while(x);
for(int i = p;i >= 1;i--)putchar(buf[i] + '0');
if(split != '\0')putchar(split);
}
inline void lf(){putchar('\n');}
~IO(){fwrite(obuf,sizeof(char),onow - obuf,stdout);}
}io;
压位高精
struct BigInt{
typedef long long ll;
static const int base = 1e8;
static const int width = 8;
static const int maxn = 6666;
static inline int max(const int &a,const int &b){return a > b ? a : b;}
int val[maxn],len;
inline void maintain(){
while(!val[len] && len > 1)len--;
}
inline void setzero(){
memset(val,0,sizeof(val));
len = 0;
}
int operator[](int pos)const{
return val[pos];
}
int& operator[](int pos){
return val[pos];
}
BigInt(){setzero();}
BigInt(const BigInt &rhs){
setzero();
len = rhs.len;
for(int i = 1;i <= len;i++)val[i] = rhs[i];
}
BigInt(int x){
setzero();
len = 0;
do{
val[++len] = x % base;
x /= base;
}while(x);
}
BigInt(const char str[]){
setzero();
int siz = strlen(str);
len = (siz - 1) / width + 1;
for(int i = 1;i <= len;i++){
int end = siz - (i - 1) * width,beg = max(0,end - width);
for(;beg < end;beg++)val[i] = val[i] * 10 + str[beg] - '0';
}
}
BigInt operator + (const BigInt &rhs)const{
BigInt res;res.setzero();
res.len = max(len,rhs.len) + 1;
for(int i = 1;i <= res.len;i++)
res[i] += ((ll)val[i] + rhs[i]) % base,res[i + 1] += ((ll)val[i] + rhs[i]) / base,res[i + 1] += res[i] / base,res[i] %= base;
res.maintain();
return res;
}
BigInt operator * (const BigInt &rhs)const{
BigInt res;res.setzero();
res.len = len + rhs.len + 5;
for(int i = 1;i <= len;i++)
for(int j = 1;j <= rhs.len;j++)
res[i + j - 1] += ((ll)val[i] * rhs[j]) % base,res[i + j] += ((ll)val[i] * rhs[j]) / base,res[i + j] += res[i + j - 1] / base,res[i + j - 1] %= base;
res.maintain();
return res;
}
BigInt operator - (const BigInt &rhs)const{
BigInt res;res.setzero();
res.len = len;
for(int i = 1;i <= res.len;i++){
res[i] += val[i] - rhs[i];
if(res[i] < 0)res[i + 1]--,res[i] += base;
}
res.maintain();
return res;
}
BigInt mid()const{
BigInt res(*this);
for(int i = res.len;i >= 1;i--)
res.val[i - 1] += (res.val[i] % 2) * base,res.val[i] /= 2;
res.maintain();
return res;
}
bool operator == (const BigInt &rhs)const{
if(len != rhs.len)return false;
for(int i = 1;i <= len;i++)
if(val[i] != rhs[i])return false;
return true;
}
bool operator < (const BigInt &rhs)const{
if(len != rhs.len)return len < rhs.len;
for(int i = len;i >= 1;i--)
if(val[i] != rhs[i])return val[i] < rhs[i];
return false;
}
bool operator <= (const BigInt &rhs)const{
return (*this < rhs) || (*this == rhs);
}
bool operator > (const BigInt &rhs)const{
return !(*this <= rhs);
}
bool operator >= (const BigInt &rhs)const{
return (*this > rhs) || (*this == rhs);
}
void print(){
printf("%d",val[len]);
for(int i = len - 1;i >= 1;i--)printf("%08d",val[i]);
putchar('\n');
}
};
最大流 Dinic
// need : algorithm queue limits
namespace dinic{
constexpr int maxn = 2e5 + 100;
constexpr int maxm = 2e6 + 100;
using Type = int;
struct Edge{
int v;
Type cap;
}edges[maxm];
int head[maxn],nxt[maxm],tot = 1;
void clear(){
rep(i,2,tot)
head[edges[i].v] = 0,nxt[i] = 0;
tot = 1;
}
void addedge(ci u,ci v,const Type d){
edges[++tot] = {v,d};
nxt[tot] = head[u];
head[u] = tot;
edges[++tot] = {u,0};
nxt[tot] = head[v];
head[v] = tot;
}
int d[maxn];
bool bfs(ci s,ci t){
std::fill_n(d + 1,t,-1);
static std::queue<int> q;
q.push(s);d[s] = 0;
while(!q.empty()){
let u = q.front();q.pop();
for(int i = head[u];i;i = nxt[i]){
let &e = edges[i];
if(e.cap && d[e.v] == -1){
d[e.v] = d[u] + 1;
q.push(e.v);
}
}
}
return d[t] != -1;
}
int cur[maxn];
Type dfs(ci u,Type a,ci t){
if(u == t || !a)return a;
Type res = 0,f;
for(int &i = cur[u];i;i = nxt[i]){
let &e = edges[i];
if(d[u] + 1 == d[e.v] && (f = dfs(e.v,std::min(a,e.cap),t))){
res += f;
edges[i].cap -= f;
edges[i ^ 1].cap += f;
a -= f;
if(!a)break;
}
}
return res;
}
Type maxflow(ci s,ci t){
Type res = 0;
while(bfs(s,t)){
std::copy_n(head + 1,t,cur + 1);
res += dfs(s,std::numeric_limits<Type>::max(),t);
}
return res;
}
}
最小费用最大流
namespace mcmf{
using type = int;
constexpr type inf = 0x3f3f3f3f;
constexpr int maxn = 1e5 + 100,maxm = 1e5 + 100;
struct Edge{
int v,cap;
type cost;
}edges[maxm * 2];
int head[maxn],nxt[maxm * 2],tot = 1;
void clear(ci t){
std::fill_n(head + 1,t,0);
tot = 1;
}
void addedge(ci u,ci v,ci cap,const type cost){
edges[++tot] = Edge{v,cap,cost};
nxt[tot] = head[u];
head[u] = tot;
edges[++tot] = Edge{u,0,-cost};
nxt[tot] = head[v];
head[v] = tot;
}
type h[maxn],dis[maxn];
void spfa(ci s,ci t){
static int q[maxn];
static bool inq[maxn];
std::fill_n(h + 1,t,inf);
int l = 0,r = 0;
q[r++] = s; inq[s] = 1;
h[s] = 0;
while(l != r){
let u = q[l++]; if(l == maxn)l = 0;
inq[u] = 0;
for(int i = head[u];i;i = nxt[i]){
let &e = edges[i];
if(e.cap && h[u] + e.cost < h[e.v]){
h[e.v] = h[u] + e.cost;
if(!inq[e.v]){
q[r++] = e.v; if(r == maxn)r = 0;
inq[e.v] = 1;
}
}
}
}
}
bool dij(ci s,ci t){
struct Node{
int u;
type h;
bool operator < (const Node t)const{
return h > t.h;
}
};
static Node q[maxm * 2];
static bool vis[maxn];
int top = 0;
std::fill_n(dis + 1,t,inf);
std::fill_n(vis + 1,t,false);
dis[s] = 0;
q[top++] = Node{s,dis[s]};
while(top){
let u = q[0].u; std::pop_heap(q,q + top); top--;
if(vis[u])continue;
vis[u] = 1;
for(int i = head[u];i;i = nxt[i]){
let &e = edges[i];
let w = e.cost + h[u] - h[e.v];
if(e.cap && dis[u] + w < dis[e.v]){
dis[e.v] = dis[u] + w;
q[top++] = Node{e.v,dis[e.v]};
std::push_heap(q,q + top);
}
}
}
return dis[t] != inf;
}
bool vis[maxn];
int cur[maxn];
int dfs(ci u,int a,ci t){
if(u == t || !a)return a;
vis[u] = 1;
int res = 0,f;
for(int &i = cur[u];i;i = nxt[i]){
let &e = edges[i];
let w = e.cost + h[u] - h[e.v];
if(e.cap && !vis[e.v] && dis[u] + w == dis[e.v] && (f = dfs(e.v,std::min(a,e.cap),t))){
res += f;
a -= f;
edges[i].cap -= f;
edges[i ^ 1].cap += f;
if(!a)break;
}
}
return res;
}
std::pair<int,type> mcmf(ci s,ci t){
std::pair<int,type> res{0,0};
spfa(s,t);
while(dij(s,t)){
std::copy_n(head + 1,t,cur + 1);
std::fill_n(vis + 1,t,0);
let f = dfs(s,0x7fffffff,t);
res.first += f;
rep(i,1,t)h[i] += dis[i];
res.second += f * h[t];
}
return res;
}
}
Miller-Rabin
namespace Miller_Rabin{
typedef long long ll;
inline ll power_mod(ll a,ll b,ll mod){
ll ret = 1,base = a;
while(b){
if(b & 1)ret = (ret * base) % mod;
base = (base * base) % mod;
b >>= 1;
}
return ret;
}
inline ll witness(ll a,ll n){
ll u = n - 1;
int t = 0;
while(!(u & 1))u >>= 1,t++;
ll x = power_mod(a,u,n),last;
for(int i = 1;i <= t;i++){
last = x;
x = (x * x) % n;
if(x == 1 && last != 1 && last != n - 1)return true;
}
if(x != 1)return true;
return false;
}
inline bool isprime(ll n){
if(n == 1)return false;
const static int prime[] = {2,7,61};
for(int x : prime){
if(x == n)return true;
if(n % x == 0)return false;
if(witness(x,n))return false;
}
return true;
}
}
using Miller_Rabin::isprime;
栈
template <typename T>
struct stack{
struct iterator{
T *p;
iterator(T* x):p(x){}
iterator operator ++(){return iterator(--p);}
bool operator == (const iterator &rhs){return p == rhs.p;}
bool operator != (const iterator &rhs){return !(*this == rhs);}
T operator *(){return *p;}
};
const static int buf_size = 128;
T val[buf_size];
int tot = 0;
inline bool empty(){return !tot;}
inline T top(){return val[tot];}
inline void pop(){tot--;}
inline void push(const T &data){val[++tot] = data;}
inline int size(){return tot;}
inline void clear(){tot = 0;}
inline iterator begin(){return iterator(val + tot);}
inline iterator end(){return iterator(val);}
};
Manacher
namespace manacher{
char s[maxn << 1];
int h[maxn << 1],len;
inline void init(){
s[0] = '#';
int tot = strlen(str + 1);
for(int i = 1;i <= tot;i++)
s[++len] = '#',s[++len] = str[i];
s[++len] = '#';
}
inline void manacher(){
int mx = 0,mid = 0;
for(int i = 1;i <= len;i++){
if(i < mx)h[i] = min(h[2 * mid - i],h[mid] + mid - i);
else h[i] = 1;
while(s[i + h[i]] == s[i - h[i]])h[i]++;
if(h[i] + i > mx)mx = h[i] + i,mid = i;
}
}
}
康托展开
namespace Cantor{
namespace BIT{
int f[maxn],n;
inline int lowbit(int x){return x & (-x);}
inline void init(int x){
memset(f,0,sizeof(f));
n = x;
}
inline void add(int pos){
while(pos <= n){
f[pos] += 1;
pos += lowbit(pos);
}
}
inline int query(int pos){
int res = 0;
while(pos){
res += f[pos];
pos -= lowbit(pos);
}
return res;
}
}
inline void cantor(int n){
BIT::init(n);
for(int i = 1;i <= n;i++){
BIT::add(val[i]);
tmp[i] = val[i] - BIT::query(val[i]);
}
}
}
BSGS
namespace BSGS{
unordered_map<ll,ll> mp;
inline ll qpow(ll a,ll b,ll mod){
ll res = 1,base = a;
while(b){
if(b & 1)res = (res * base) % mod;
base = (base * base) % mod;
b >>= 1;
}
return res;
}
inline ll bsgs(ll y,ll z,ll p){
mp.clear();z %= p;
if(y % p == 0 && z)return -1;
ll m = sqrt(p) + 1;
for(ll i = 0,t = z;i < m;i++,t = t * y % p)mp[t] = i;
for(ll i = 1,tmp = qpow(y,m,p),t = tmp;i <= m + 1;i++,t = t * tmp % p){
if(mp.find(t) != mp.end())return i * m - mp[t];
}
return -1;
}
}
exBSGS
namespace exBSGS{
unordered_map<ll,ll> mp;
inline ll gcd(ll a,ll b){
return b == 0 ? a : gcd(b,a % b);
}
inline ll qpow(ll a,ll b,ll mod){
ll res = 1,base = a;
while(b){
if(b & 1)res = (res * base) % mod;
base = (base * base) % mod;
b >>= 1;
}
return res;
}
inline ll bsgs(ll y,ll z,ll p){
if(y % p == 0 && z)return -1;
z %= p;
ll k = 0,a = 1;
while(true){
ll d = gcd(y,p);if(d == 1)break;
if(z % d)return -1;
z /= d;p /= d;k++;a = a * y / d % p;
if(z == a)return k;
}
mp.clear();
ll m = sqrt(p) + 1;
for(ll i = 0,t = z;i < m;i++,t = t * y % p)mp[t] = i;
for(ll i = 1,tmp = qpow(y,m,p),t = a * tmp % p;i <= m;i++,t = t * tmp % p)
if(mp.find(t) != mp.end())return i * m - mp[t] + k;
return -1;
}
}
Pollard-Rho
namespace Pollard_Rho{
inline ll power_mod(ll a,ll b,ll mod){
a %= mod;
ll res = 1,base = a;
while(b){
if(b & 1)res = res * base % mod;
base = base * base % mod;
b >>= 1;
}
return res;
}
inline ll gcd(ll a,ll b){return !b ? a : gcd(b,a % b);}
inline ll f(ll x,ll c,ll mod){return (x * x + c) % mod;}
inline ll PR(ll x){
ll s = 0,t = 0,c = rand() % (x - 1) + 1,val = 1;
for(ll goal = 1;;goal <<= 1,s = t,val = 1){
for(ll i = 1;i <= goal;i++){
t = f(t,c,x);
val = val * abs(t - s) % x;
if(!(i % 127)){
ll d = gcd(val,x);
if(d > 1)return d;
}
}
ll d = gcd(val,x);
if(d > 1)return d;
}
}
}using Pollard_Rho::PR;