火车头
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,popcnt,tune=native")IO
namespace IO
{
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char gc()
{
#ifdef LOCAL
return getchar();
#endif
if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin);
return p1 == p2 ? EOF : *p1++;
}
template<typename T = int>
inline T read()
{
int f = 1;
T w = 0;
char ch = gc();
while(ch < '0' || '9' < ch)
{
if(ch == '-') f = -1;
ch = gc();
}
while('0' <= ch && ch <= '9')
{
w = (w << 1) + (w << 3) + (ch ^ 48);
ch = gc();
}
return f * w;
}
inline char readc()
{
char ch = gc();
while(ch == ' ' || ch == '\n') ch = gc();
return ch;
}
char obuf[1 << 21], *p3 = obuf;
inline void flush()
{
fwrite(obuf, p3 - obuf, 1, stdout);
}
inline void pc(char c)
{
p3 - obuf <= 1 << 20 ? *p3++ = c : (flush(), p3 = obuf, *p3++ = c);
}
template<typename T>
inline void write(T x)
{
if(x < 0) pc('-'), x = -x;
if(x == 0) pc('0');
static char c[50];
static int tt;
while(x) c[++tt] = x % 10 ^ 48, x /= 10;
while(tt) pc(c[tt--]);
}
inline void ps(string s)
{
for(char &i : s) pc(i); pc('\n');
}
}
using namespace IO;hash_table
对于不能取模的 需自定义 hash 函数;空间较大, 个 <int, int> 类型的空间占用约为 。若需要较多哈希表可以调整模数。
template<typename T1, typename T2>
struct hash_table
{
static const int mod = 2000003;
T1 h[mod];
T2 w[mod];
int dfn[mod], viti = 1;
inline int hash(const T1 &x)
{
return (x % mod + mod) % mod;
}
inline int find(const T1 &key)
{
int h0 = hash(key), h1 = h0;
while(dfn[h1] == viti && h[h1] != key) ++h1 == mod && (h1 = 0);
if(dfn[h1] == viti && h1 != h0)
swap(h[h1], h[h0]), swap(w[h1], w[h0]), h1 = h0;
return h1;
}
inline T2& operator[](const T1 &key)
{
int h1 = find(key);
if(dfn[h1] != viti) h[h1] = key, w[h1] = T2(), dfn[h1] = viti;
return w[h1];
}
inline bool count(const T1 &key)
{
int h1 = find(key);
return dfn[h1] == viti;
}
inline void clear()
{
++viti;
}
};网络流
namespace Flow
{
int h[N], ne[M], e[M], w[M], idx = 1;
void add(int a, int b, int c)
{
e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;
}
void add1(int a, int b, int c)
{
add(a, b, c), add(b, a, 0);
}
int tot, s, t;
int dis[N], cnt[N], cur[N];
int sap(int x, int inflow)
{
if(x == t) return inflow;
int outflow = 0;
for(int &i = cur[x]; i; i = ne[i])
if(w[i] && dis[x] == dis[e[i]] + 1)
{
int flow = sap(e[i], min(inflow - outflow, w[i]));
w[i] -= flow, w[i ^ 1] += flow, outflow += flow;
if(inflow == outflow || dis[s] >= tot) return outflow;
}
if(!--cnt[dis[x]]) dis[s] = tot;
cnt[++dis[x]]++, cur[x] = h[x];
return outflow;
}
int maxflow()
{
int ans = 0;
while(dis[s] < tot) ans += sap(s, INT_MAX);
return ans;
}
}注意是最小费用最大流:
namespace Flow
{
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;
void add(int a, int b, int c, int d)
{
e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;
}
void add1(int a, int b, int c, int d)
{
add(a, b, c, d), add(b, a, 0, -d);
}
int s, t;
int flow[N], dis[N], pre[N];
int q[N], hh, tt;
bool in[N];
bool ssp()
{
memset(flow, 0, sizeof(flow));
memset(dis, 0x3f, sizeof(dis));
memset(in, 0, sizeof(in));
hh = tt = 0;
flow[s] = INT_MAX, dis[s] = 0, in[q[tt++] = s] = 1;
while(hh != tt)
{
int t = q[hh++]; in[t] = 0;
if(hh == N) hh = 0;
for(int i = h[t]; i; i = ne[i])
if(w[i])
{
int j = e[i];
if(dis[j] > dis[t] + w1[i])
{
flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i;
if(!in[j])
{
in[q[tt++] = j] = 1;
if(tt == N) tt = 0;
}
}
}
}
return flow[t];
}
int getval()
{
int ans = 0;
while(ssp())
{
ans += flow[t] * dis[t];
for(int i = t; i != s; i = e[pre[i] ^ 1])
w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t];
}
return ans;
}
}多项式
各函数的 表示 的次数。没有特殊说明的情况下数组需不同。如果使用了除 NTT 以外的操作(即全家桶的内容),空间翻倍。
Ln:有 。 Exp:有 。 Ksm: 两个数组可以相同。设指数为 ,那么有 。 Div: 两个数组可以相同。
值得注意的是,多项式的次数必须用变量 N 表示。
namespace poly
{
#define LL long long
inline int lg2(int n)
{
int k = 0;
while(1 << k + 1 <= n) k++;
return k;
}
LL ksm(LL x, LL y)
{
LL res = 1;
x %= mod;
while(y)
{
if(y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
int bit, tot;
int w[N], inv_num[N];
struct Initial
{
Initial()
{
bit = lg2(N), tot = 1 << bit;
w[tot >> 1] = ksm(3, mod - 1 >> bit + 1), w[0] = 1;
for(int i = bit - 1; i; i--) w[1 << i - 1] = 1ll * w[1 << i] * w[1 << i] % mod;
for (int i = 1; i < tot; i++) w[i] = 1ll * w[i & -i] * w[i ^ (i & -i)] % mod;
inv_num[1] = 1;
for(int i = 2; i <= tot; i++) inv_num[i] = 1ll * (mod - mod / i) * inv_num[mod % i] % mod;
}
}initial;
void DFT(int a[])
{
for(int len = tot >> 1; len; len >>= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
int x = a[j], y = 1ll * a[j + len] * w[l] % mod;
(a[j] = x + y) >= mod && (a[j] -= mod), (a[j + len] = x - y) < 0 && (a[j + len] += mod);
}
}
void IDFT(int a[])
{
for(int len = 1; len < tot; len <<= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
int x = a[j], y = a[j + len];
(a[j] = x + y) >= mod && (a[j] -= mod), a[j + len] = 1ll * (x - y + mod) * w[l] % mod;
}
for(int i = 1; i < tot >> 1; i++) swap(a[i], a[tot - i]);
for(int i = 0; i < tot; i++) a[i] = 1ll * a[i] * inv_num[tot] % mod;
}
void NTT(int a[], int inv)
{
inv == 1 ? DFT(a) : IDFT(a);
}
void Inv(int a[], int n, int b[])
{
static int c[N];
int m = 1 << lg2(n) + 1;
for(int i = n + 1; i < m; i++) a[i] = 0;
b[0] = ksm(a[0], mod - 2);
for(int i = 1; 1 << i <= m; i++)
{
bit = i + 1, tot = 1 << i + 1;
for(int j = tot >> 2; j < tot; j++) b[j] = 0;
for(int j = 0; j < tot; j++) c[j] = j < tot >> 1 ? a[j] : 0;
NTT(b, 1), NTT(c, 1);
for(int j = 0; j < tot; j++) b[j] = (2 - 1ll * c[j] * b[j] % mod + mod) * b[j] % mod;
NTT(b, -1);
}
}
int cipolla(int n)
{
if(!n) return 0;
if(ksm(n, mod - 1 >> 1) == mod - 1) return -1;
mt19937 rd(time(0));
int a = rd() % (mod - 1) + 1;
while(ksm(1ll * a * a - n + mod, mod - 1 >> 1) == 1) a = rd() % (mod - 1) + 1;
int ii = (1ll * a * a - n + mod) % mod;
LL res1 = 1, res2 = 0, b = 1, y = mod + 1 >> 1, t1, t2;
while(y)
{
if(y & 1)
{
t1 = (res1 * a + res2 * b % mod * ii) % mod,
t2 = (res1 * b + res2 * a) % mod;
res1 = t1, res2 = t2;
}
y >>= 1;
t1 = (1ll * a * a + b * b % mod * ii) % mod,
t2 = 2 * a * b % mod;
a = t1, b = t2;
}
return res1;
}
void Sqrt(int a[], int n, int b[])
{
static int c[N], d[N];
int m = 1 << lg2(n) + 1;
for(int i = n + 1; i < m; i++) a[i] = 0;
b[0] = cipolla(a[0]), b[0] = min(b[0], (mod - b[0]) % mod);
static int inv2 = ksm(2, mod - 2);
for(int i = 1; 1 << i <= m; i++)
{
bit = i + 1, tot = 1 << i + 1;
for(int j = tot >> 2; j < tot; j++) b[j] = 0;
for(int j = 0; j < tot; j++) c[j] = j < tot >> 1 ? a[j] : 0;
Inv(b, (tot >> 1) - 1, d);
for(int j = tot >> 1; j < tot; j++) d[j] = 0;
NTT(b, 1), NTT(c, 1), NTT(d, 1);
for(int j = 0; j < tot; j++) b[j] = (c[j] + 1ll * b[j] * b[j] % mod) * d[j] % mod * inv2 % mod;
NTT(b, -1);
}
}
void Ln(int a[], int n, int b[])
{
static int c[N];
Inv(a, n, b);
for(int i = 1; i <= n; i++) c[i - 1] = 1ll * a[i] * i % mod;
bit = lg2(2 * n - 1) + 1, tot = 1 << bit;
for(int i = n; i < tot; i++) c[i] = 0;
for(int i = n + 1; i < tot; i++) b[i] = 0;
NTT(c, 1), NTT(b, 1);
for(int i = 0; i < tot; i++) b[i] = 1ll * c[i] * b[i] % mod;
NTT(b, -1);
for(int i = n; i; i--) b[i] = 1ll * b[i - 1] * inv_num[i] % mod;
b[0] = 0;
}
void Exp(int a[], int n, int b[])
{
static int c[N], d[N];
int m = 1 << lg2(n) + 1;
for(int i = n + 1; i < m; i++) a[i] = 0;
b[0] = 1;
for(int i = 1; 1 << i <= m; i++)
{
bit = i + 1, tot = 1 << i + 1;
for(int j = tot >> 2; j < tot; j++) b[j] = 0;
for(int j = 0; j < tot; j++) c[j] = j < tot >> 1 ? a[j] : 0;
Ln(b, (tot >> 1) - 1, d);
for(int j = tot >> 1; j < tot; j++) d[j] = 0;
NTT(b, 1), NTT(c, 1), NTT(d, 1);
for(int j = 0; j < tot; j++) b[j] = (1ll - d[j] + c[j] + mod) * b[j] % mod;
NTT(b, -1);
}
}
void Ksm(int a[], int n, int b[], LL y, LL yy)
{
int t = n;
for(int i = 0; i <= n; i++)
if(a[i])
{
t = i;
break;
}
if(y && n / y < t)
{
for(int i = 0; i <= n; i++) b[i] = 0;
return;
}
static int c[N];
for(int i = 0; i <= n - y * t; i++) b[i] = a[i + t];
n -= y * t;
int b0 = ksm(b[0], yy), inv = ksm(b[0], mod - 2);
for(int i = 0; i <= n; i++) b[i] = 1ll * b[i] * inv % mod;
Ln(b, n, c);
for(int i = 0; i <= n; i++) c[i] = 1ll * c[i] * y % mod;
Exp(c, n, b);
for(int i = n; ~i; i--) b[i + y * t] = 1ll * b[i] * b0 % mod;
for(int i = 0; i < y * t; i++) b[i] = 0;
}
void Div(int a[], int n, int b[], int m, int c[], int r[])
{
if(n < m)
{
for(int i = 0; i <= n; i++) r[i] = a[i];
for(int i = n + 1; i < m; i++) r[i] = 0;
return;
}
static int x[N], y[N], z[N];
for(int i = 0; i <= n - m; i++) x[i] = m >= i ? b[m - i] : 0, z[i] = a[n - i];
Inv(x, n - m, y);
bit = lg2(n - m << 1) + 1, tot = 1 << bit;
for(int i = n - m + 1; i < tot; i++) y[i] = z[i] = 0;
NTT(y, 1), NTT(z, 1);
for(int i = 0; i < tot; i++) y[i] = 1ll * y[i] * z[i] % mod;
NTT(y, -1);
for(int i = 0; i <= n - m; i++) c[i] = y[n - m - i];
for(int i = n - m + 1; i < m; i++) c[i] = 0;
for(int i = 0; i < m; i++) y[i] = b[i], z[i] = c[i];
bit = lg2(m - 1 << 1) + 1, tot = 1 << bit;
for(int i = m; i < tot; i++) y[i] = z[i] = 0;
NTT(y, 1), NTT(z, 1);
for(int i = 0; i < tot; i++) x[i] = 1ll * y[i] * z[i] % mod;
NTT(x, -1);
for(int i = 0; i < m; i++) (r[i] = a[i] - x[i]) < 0 && (r[i] += mod);
}
}
using namespace poly;浮点数版:
namespace poly
{
inline int lg2(int n)
{
int k = 0;
while(1 << k + 1 <= n) k++;
return k;
}
const double PI = acos(-1.0);
struct Complex
{
double x, y;
Complex operator+(const Complex &a) const
{
return (Complex){x + a.x, y + a.y};
}
Complex operator-(const Complex &a) const
{
return (Complex){x - a.x, y - a.y};
}
Complex operator*(const Complex &a) const
{
return (Complex){x * a.x - y * a.y, x * a.y + y * a.x};
}
};
int bit, tot;
Complex w[N];
struct Initial
{
Initial()
{
bit = lg2(N), tot = 1 << bit;
w[0].x = 1;
for(int i = 0; i < bit; i++) w[1 << i] = (Complex){cos(PI / (1 << i + 1)), sin(PI / (1 << i + 1))};
for (int i = 1; i < tot; i++) w[i] = w[i & -i] * w[i ^ (i & -i)];
}
}initial;
void DFT(Complex a[])
{
for(int len = tot >> 1; len; len >>= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
Complex x = a[j], y = a[j + len] * w[l];
a[j] = x + y, a[j + len] = x - y;
}
}
void IDFT(Complex a[])
{
for(int len = 1; len < tot; len <<= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
Complex x = a[j], y = a[j + len];
a[j] = x + y, a[j + len] = (x - y) * w[l];
}
for(int i = 1; i < tot >> 1; i++) swap(a[i], a[tot - i]);
double t = 1.0 / tot;
for(int i = 0; i < tot; i++) a[i].x *= t, a[i].y *= t;
}
void FFT(Complex a[], int inv)
{
inv == 1 ? DFT(a) : IDFT(a);
}
void MTT(int a[], int b[], int c[], const int &mod)
{
static Complex x[N], y[N], z[N];
for(int i = 0; i < tot; i++)
x[i].x = a[i] >> 15, x[i].y = 0;
for(int i = 0; i < tot; i++)
y[i].x = a[i] & (1 << 15) - 1, y[i].y = 0;
for(int i = 0; i < tot; i++)
z[i].x = b[i] >> 15, z[i].y = b[i] & (1 << 15) - 1;
FFT(x, 1), FFT(y, 1), FFT(z, 1);
for(int i = 0; i < tot; i++)
x[i] = x[i] * z[i], y[i] = y[i] * z[i];
FFT(x, -1), FFT(y, -1);
for(int i = 0; i < tot; i++)
c[i] = ((llround(x[i].x) % mod << 30) + ((llround(x[i].y) + llround(y[i].x)) % mod << 15) + llround(y[i].y)) % mod;
}
}
using namespace poly;集合幂级数
namespace poly
{
#define LL long long
#define u64 unsigned long long
LL ksm(LL x, LL y)
{
LL res = 1;
x %= mod;
while(y)
{
if(y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
int bit, tot;
LL inv_num[22];
struct Initial
{
Initial()
{
inv_num[1] = 1;
for(int i = 2; i <= 21; i++)
inv_num[i] = inv_num[mod % i] * (mod - mod / i) % mod;
}
}initial;
void FWT_or(int a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
~inv ? ((a[k + i] += a[k]) >= mod && (a[k + i] -= mod)) : ((a[k + i] -= a[k]) < 0 && (a[k + i] += mod));
}
void FWT_and(int a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
~inv ? ((a[k] += a[k + i]) >= mod && (a[k] -= mod)) : ((a[k] -= a[k + i]) < 0 && (a[k] += mod));
}
void FWT_xor(int a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
{
int x = a[k], y = a[k + i];
(a[k] = x + y) >= mod && (a[k] -= mod), (a[k + i] = x - y) < 0 && (a[k + i] += mod);
}
if(inv == -1)
{
u64 t = ksm(tot, mod - 2);
for(int i = 0; i < tot; i++) a[i] = a[i] * t % mod;
}
}
void FWT_or(LL a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
~inv ? (a[k + i] += a[k]) : (a[k + i] -= a[k]);
for(int i = 0; i < tot; i++) a[i] = (a[i] % mod + mod) % mod;
}
void FWT_and(LL a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
~inv ? (a[k] += a[k + i]) : (a[k] -= a[k + i]);
for(int i = 0; i < tot; i++) a[i] = (a[i] % mod + mod) % mod;
}
void FWT_xor(LL a[], int inv)
{
for(int i = 1; i < tot; i <<= 1)
for(int j = 0; j < tot; j += i << 1)
for(int k = j; k < j + i; k++)
a[k] += a[k + i], a[k + i] = a[k] - (a[k + i] << 1);
for(int i = 0; i < tot; i++) a[i] = (a[i] % mod + mod) % mod;
if(inv == -1)
{
u64 t = ksm(tot, mod - 2);
for(int i = 0; i < tot; i++) a[i] = a[i] * t % mod;
}
}
template<typename T>
void times(T a[], T b[], T c[])
{
static int x[22][N], y[22][N], z[22][N];
for(int i = 0; i <= bit; i++)
for(int j = 0; j < tot; j++)
x[i][j] = y[i][j] = z[i][j] = 0;
for(int i = 0; i < tot; i++)
{
x[__builtin_popcount(i)][i] = a[i];
y[__builtin_popcount(i)][i] = b[i];
}
for(int i = 0; i <= bit; i++) FWT_or(x[i], 1), FWT_or(y[i], 1);
for(int i = 0; i <= bit; i++)
for(int j = 0; j <= i; j++)
for(int k = 0; k < tot; k++)
z[i][k] = (z[i][k] + 1ull * x[j][k] * y[i - j][k]) % mod;
for(int i = 0; i <= bit; i++) FWT_or(z[i], -1);
for(int i = 0; i < tot; i++) c[i] = z[__builtin_popcount(i)][i];
}
template<typename T>
void Inv(T a[], T b[])
{
static LL c[22][N], d[22][N];
for(int i = 1; i <= bit; i++)
for(int j = 0; j < tot; j++)
c[i][j] = 0;
u64 t = ksm(a[0], mod - 2);
for(int i = 1; i < tot; i++) c[__builtin_popcount(i)][i] = mod - a[i] * t % mod;
for(int i = 1; i <= bit; i++) FWT_or(c[i], 1);
for(int i = 0; i < tot; i++)
{
d[0][i] = 1;
for(int j = 1; j <= bit; j++)
{
u64 sum = 0;
for(int k = 1; k <= j; k++)
{
sum += c[k][i] * d[j - k][i];
if(k == 16) sum %= mod;
}
d[j][i] = sum % mod;
}
}
for(int i = 1; i <= bit; i++) FWT_or(d[i], -1);
b[0] = t;
for(int i = 1; i < tot; i++) b[i] = d[__builtin_popcount(i)][i] * t % mod;
}
template<typename T>
void Ln(T a[], T b[])
{
static LL c[22][N], d[22][N];
for(int i = 1; i <= bit; i++)
for(int j = 0; j < tot; j++)
c[i][j] = 0;
for(int i = 1; i < tot; i++) c[__builtin_popcount(i)][i] = a[i];
for(int i = 1; i <= bit; i++) FWT_or(c[i], 1);
for(int i = 0; i < tot; i++)
{
d[0][i] = 0;
for(int j = 1; j <= bit; j++)
{
u64 sum = 0;
for(int k = 1; k < j; k++)
{
sum += d[k][i] * c[j - k][i];
if(k == 16) sum %= mod;
}
d[j][i] = (c[j][i] * j - sum % mod + mod) % mod;
}
for(int j = 1; j <= bit; j++) d[j][i] = d[j][i] * inv_num[j] % mod;
}
for(int i = 1; i <= bit; i++) FWT_or(d[i], -1);
b[0] = 0;
for(int i = 1; i < tot; i++) b[i] = d[__builtin_popcount(i)][i];
}
template<typename T>
void Exp(T a[], T b[])
{
static LL c[22][N], d[22][N];
for(int i = 1; i <= bit; i++)
for(int j = 0; j < tot; j++)
c[i][j] = 0;
for(int i = 1; i < tot; i++) c[__builtin_popcount(i)][i] = a[i];
for(int i = 1; i <= bit; i++) FWT_or(c[i], 1);
for(int i = 0; i < tot; i++)
{
d[0][i] = 1;
for(int j = 1; j <= bit; j++) c[j][i] = c[j][i] * j % mod;
for(int j = 1; j <= bit; j++)
{
u64 sum = 0;
for(int k = 0; k < j; k++)
{
sum += d[k][i] * c[j - k][i];
if(k == 16) sum %= mod;
}
d[j][i] = sum % mod * inv_num[j] % mod;
}
}
for(int i = 1; i <= bit; i++) FWT_or(d[i], -1);
b[0] = 1;
for(int i = 1; i < tot; i++) b[i] = d[__builtin_popcount(i)][i];
}
template<typename T>
void comp(T a[], T b[], T c[])
{
int n = bit;
static int f[22][N], g[N], h[N];
for(int i = 0, fac = 1; i <= n; i++, fac = 1ll * fac * i % mod)
f[i][0] = 1ll * b[i] * fac % mod;
for(int i = 0; i < n; i++)
{
bit = i, tot = 1 << i;
for(int s = 0; s < tot; s++) g[s] = a[s | 1 << i];
for(int j = 1; j <= n - i; j++)
{
times(f[j], g, h);
for(int s = 0; s < tot; s++)
f[j - 1][s | 1 << i] = h[s];
}
}
for(int i = 0; i < 1 << n; i++) c[i] = f[0][i];
}
}
using namespace poly;Splay
struct tree
{
int son[2], fa;
}a[N];
int root, tot;
inline void pushup(int p)
{
}
void rotate(int x)
{
int y = a[x].fa, z = a[y].fa, k = a[y].son[1] == x;
if(z) a[z].son[a[z].son[1] == y] = x;
a[x].fa = z;
a[y].son[k] = a[x].son[k ^ 1];
if(a[x].son[k ^ 1]) a[a[x].son[k ^ 1]].fa = y;
a[x].son[k ^ 1] = y, a[y].fa = x;
pushup(y), pushup(x);
}
void splay(int x, int k)
{
while(a[x].fa != k)
{
int y = a[x].fa, z = a[y].fa;
if(z != k)
if(a[y].son[1] == x ^ a[z].son[1] == y) rotate(x);
else rotate(y);
rotate(x);
}
if(!k) root = x;
}LCT
struct LCT
{
struct tree
{
int son[2], fa;
bool lazy;
}a[];
inline void pushup(int p)
{
}
inline void pushdown(int p)
{
if(a[p].lazy)
{
swap(a[p].son[0], a[p].son[1]);
a[a[p].son[0]].lazy ^= 1, a[a[p].son[1]].lazy ^= 1, a[p].lazy = 0;
}
}
void rotate(int x)
{
int y = a[x].fa, z = a[y].fa, k = a[y].son[1] == x;
if(a[z].son[0] == y || a[z].son[1] == y) a[z].son[a[z].son[1] == y] = x;
a[x].fa = z;
a[y].son[k] = a[x].son[k ^ 1];
if(a[x].son[k ^ 1]) a[a[x].son[k ^ 1]].fa = y;
a[x].son[k ^ 1] = y, a[y].fa = x;
pushup(y), pushup(x);
}
void splay(int x)
{
static int q[N], tt;
q[++tt] = x;
for(int i = x; a[a[i].fa].son[0] == i || a[a[i].fa].son[1] == i; i = a[i].fa) q[++tt] = a[i].fa;
while(tt) pushdown(q[tt--]);
while(a[a[x].fa].son[0] == x || a[a[x].fa].son[1] == x)
{
int y = a[x].fa, z = a[y].fa;
if(a[z].son[0] == y || a[z].son[1] == y)
if(a[z].son[0] == y ^ a[y].son[0] == x) rotate(x);
else rotate(y);
rotate(x);
}
}
void access(int x)
{
for(int y = 0; x; y = x, x = a[x].fa)
splay(x), a[x].son[1] = y, pushup(x);
}
void makeroot(int x)
{
access(x), splay(x), a[x].lazy ^= 1;
}
int findroot(int x)
{
access(x), splay(x);
while(a[x].son[0]) x = a[x].son[0];
splay(x);
return x;
}
void split(int x, int y)
{
makeroot(x), access(y), splay(y);
}
void link(int x, int y)
{
makeroot(x);
if(findroot(y) != x) a[x].fa = y;
}
void cut(int x, int y)
{
makeroot(x);
if(findroot(y) == x && a[y].fa == x && !a[y].son[0])
a[y].fa = a[x].son[1] = 0, pushup(x);
}
};静态点分治
bool vis[N];
int sz[N];
void getsize(int x, int fa)
{
sz[x] = 1;
for(int i = h[x]; i; i = ne[i])
if(e[i] != fa && !vis[e[i]])
getsize(e[i], x), sz[x] += sz[e[i]];
}
int root, maxsize;
void getroot(int x, int fa, int cnt)
{
int maxn = 0;
for(int i = h[x]; i; i = ne[i])
if(e[i] != fa && !vis[e[i]])
getroot(e[i], x, cnt), maxn = max(maxn, sz[e[i]]);
maxn = max(maxn, cnt - sz[x]);
if(maxn < maxsize) root = x, maxsize = maxn;
}
void dfs(int x)
{
getsize(x, 0), getroot(x, 0, maxsize = sz[x]), vis[root] = 1;
...
for(int i = h[root]; i; i = ne[i])
if(!vis[e[i]]) dfs(e[i]);
}SA
值域是 。
namespace SA
{
int len, sa[N], rk[N], x[N], y[N], cnt[N], maxn = 255;
int height[N], minn[N][20];
template<typename T>
void init(T c[], int n)
{
len = n;
for(int i = 1; i <= n; i++) cnt[x[i] = c[i]]++;
for(int i = 1; i <= maxn; i++) cnt[i] += cnt[i - 1];
for(int i = n; i; i--) sa[cnt[x[i]]--] = i;
for(int j = 1; j <= n; j <<= 1)
{
int val = 0;
for(int i = n - j + 1; i <= n; i++) y[++val] = i;
for(int i = 1; i <= n; i++)
if(sa[i] > j) y[++val] = sa[i] - j;
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= n; i++) cnt[x[i]]++;
for(int i = 1; i <= maxn; i++) cnt[i] += cnt[i - 1];
for(int i = n; i; i--) sa[cnt[x[y[i]]]--] = y[i], y[i] = 0;
swap(x, y);
x[sa[1]] = val = 1;
for(int i = 2; i <= n; i++)
x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j] ? val : ++val;
if((maxn = val) == n) break;
}
for(int i = 1; i <= n; i++) rk[sa[i]] = i;
for(int i = 1, j = 0; i <= n; i++)
{
if(rk[i] == 1) continue;
if(j) j--;
int k = sa[rk[i] - 1];
while(i + j <= n && k + j <= n && c[i + j] == c[k + j]) j++;
height[rk[i]] = j;
}
for(int i = 2; i <= n; i++)
{
minn[i][0] = height[i];
for(int j = 1; 1 << j <= i; j++)
minn[i][j] = min(minn[i][j - 1], minn[i - (1 << j - 1)][j - 1]);
}
}
void clear()
{
memset(y, 0, sizeof(y));
memset(cnt, 0, sizeof(cnt));
memset(height, 0, sizeof(height));
maxn = 255;
}
inline int getmin(int l, int r)
{
int k = __lg(r - l + 1);
return min(minn[l + (1 << k) - 1][k], minn[r][k]);
}
inline int lcp(int a, int b)
{
if(a == b) return len - a + 1;
if(rk[a] > rk[b]) swap(a, b);
return getmin(rk[a] + 1, rk[b]);
}
}
using SA::lcp;SAM
空间开 倍,to 数组的 是字符集大小。
namespace SAM
{
int tot = 1, last = 1, to[N][30], fail[N], len[N];
void extend(int c)
{
int now = ++tot, p = last, q;
len[last = now] = len[p] + 1;
while(p && !to[p][c]) to[p][c] = now, p = fail[p];
if(!p)
{
fail[now] = 1;
return;
}
if(len[q = to[p][c]] == len[p] + 1)
{
fail[now] = q;
return;
}
len[++tot] = len[p] + 1, fail[tot] = fail[q], fail[q] = fail[now] = tot;
memcpy(to[tot], to[q], sizeof(to[tot]));
while(p && to[p][c] == q) to[p][c] = tot, p = fail[p];
}
}
using namespace SAM;PAM
传小写字母串, 是字符集大小。
namespace PAM
{
int tot = 1, last = 1, to[N][30], fail[N], len[N];
void build(char c[], int n)
{
auto getfail = [&](int x, int now)
{
while(c[now - len[x] - 1] != c[now]) x = fail[x];
return x;
};
fail[0] = 1, len[1] = -1;
for(int i = 1; i <= n; i++)
{
last = getfail(last, i);
if(!to[last][c[i] - 'a'])
{
fail[++tot] = to[getfail(fail[last], i)][c[i] - 'a'];
to[last][c[i] - 'a'] = tot;
len[tot] = len[last] + 2;
}
last = to[last][c[i] - 'a'];
}
}
}
using namespace PAM;分数类
struct frac
{
LL x, y;
frac() {}
frac(LL x1, LL y1 = 1)
{
LL d = __gcd(abs(x1), abs(y1));
if(y1 < 0) d = -d;
x = x1 / d, y = y1 / d;
}
friend frac operator+(frac a, frac b) {return frac(a.x * b.y + a.y * b.x, a.y * b.y);}
friend frac operator-(frac a, frac b) {return frac(a.x * b.y - a.y * b.x, a.y * b.y);}
friend frac operator*(frac a, frac b) {return frac(a.x * b.x, a.y * b.y);}
friend frac operator/(frac a, frac b) {return frac(a.x * b.y, a.y * b.x);}
friend bool operator<(frac a, frac b) {return a.x * b.y < b.x * a.y;}
friend bool operator>(frac a, frac b) {return a.x * b.y > b.x * a.y;}
friend bool operator<=(frac a, frac b) {return a.x * b.y <= b.x * a.y;}
friend bool operator>=(frac a, frac b) {return a.x * b.y >= b.x * a.y;}
friend bool operator==(frac a, frac b) {return a.x * b.y == b.x * a.y;}
};凸包
namespace Hull
{
#define LL long long
#define PLL pair<LL, LL>
void merge(vector<PLL> &a, vector<PLL> &b, vector<PLL> &c)//上凸包,传参可相同
{
static PLL q1[N];
int tt1 = 0;
int i = 0, j = 0;
while(i < a.size() && j < b.size())
{
if(a[i].first == b[j].first)
{
q1[++tt1] = {a[i].first, max(a[i].second, b[j].second)};
i++, j++;
}
else if(a[i].first < b[j].first) q1[++tt1] = a[i++];
else q1[++tt1] = b[j++];
}
while(i < a.size()) q1[++tt1] = a[i++];
while(j < b.size()) q1[++tt1] = b[j++];
static PLL q2[N];
int tt2 = 0;
for(int i = 1; i <= tt1; i++)
{
while(tt2 > 1 && (q1[i].second - q2[tt2].second) * (q1[i].first - q2[tt2 - 1].first) >= (q1[i].second - q2[tt2 - 1].second) * (q1[i].first - q2[tt2].first)) tt2--;
q2[++tt2] = q1[i];
}
c.resize(tt2);
for(int i = 1; i <= tt2; i++) c[i - 1] = q2[i];
}
void minkowski(vector<PLL> &a, vector<PLL> &b, vector<PLL> &c)
{
c.resize(a.size() + b.size() - 1);
c[0] = {a[0].first + b[0].first, a[0].second + b[0].second};
for(int i = 1, j = 0, k = 0; j + 1 < a.size() || k + 1 < b.size(); i++)
{
if(j + 1 < a.size() && (k + 1 == b.size() || (a[j + 1].second - a[j].second) * (b[k + 1].first - b[k].first) > (b[k + 1].second - b[k].second) * (a[j + 1].first - a[j].first))) j++;
else k++;
c[i] = {a[j].first + b[k].first, a[j].second + b[k].second};
}
}
}
using namespace Hull;计算几何
namespace geometry
{
#define LL long long
struct point
{
LL x, y;
point operator+(const point &t) const
{
return {x + t.x, y + t.y};
}
point operator-(const point &t) const
{
return {x - t.x, y - t.y};
}
LL operator*(const point &t) const
{
return x * t.y - y * t.x;
}
LL operator/(const point &t) const
{
return x * t.x + y * t.y;
}
LL len()
{
return x * x + y * y;
}
bool operator==(const point &t) const
{
return x == t.x && y == t.y;
}
};
bool intersection(point a1, point a2, point b1, point b2)
{
if(min(a1.x, a2.x) <= max(b1.x, b2.x) && min(b1.x, b2.x) <= max(a1.x, a2.x) && min(a1.y, a2.y) <= max(b1.y, b2.y) && min(b1.y, b2.y) <= max(a1.y, a2.y))
{
LL c1 = (a2 - a1) * (b1 - a1), c2 = (a2 - a1) * (b2 - a1), c3 = (b2 - b1) * (a2 - b1), c4 = (b2 - b1) * (a1 - b1);
return (c1 > 0 ? 1 : c1 < 0 ? -1 : 0) * (c2 > 0 ? 1 : c2 < 0 ? -1 : 0) <= 0 && (c3 > 0 ? 1 : c3 < 0 ? -1 : 0) * (c4 > 0 ? 1 : c4 < 0 ? -1 : 0) <= 0;
}
else return false;
}
void convex(point a[], int &n)
{
static int q[N], tt;
for(int i = 2; i <= n; i++)
if(a[i].x != a[1].x ? a[i].x < a[1].x : a[i].y < a[1].y)
swap(a[i], a[1]);
point t = a[1];
for(int i = 1; i <= n; i++) a[i] = a[i] - t;
sort(a + 2, a + n + 1, [](point a, point b){return a * b ? a * b > 0 : a.len() < b.len();});
tt = 0;
for(int i = 1; i <= n; i++)
{
while(tt > 1 && (a[i] - a[q[tt - 1]]) * (a[q[tt]] - a[q[tt - 1]]) >= 0) tt--;
q[++tt] = i;
}
for(int i = 1; i <= tt; i++) a[i] = a[q[i]] + t;
n = tt, a[n + 1] = a[1];
}
bool in(point a[], int n, point x)
{
point t = a[1];
if((x - t) * (a[2] - t) > 0 || (x - t) * (a[2] - t) == 0 && (x - t).len() > (a[2] - t).len()) return 0;
if((a[n] - t) * (x - t) > 0 || (a[n] - t) * (x - t) == 0 && (x - t).len() > (a[n] - t).len()) return 0;
int p = lower_bound(a + 2, a + n + 1, x, [&](point a, point b){return (a - t) * (b - t) ? (a - t) * (b - t) > 0 : (a - t).len() < (b - t).len();}) - a - 1;
return (a[p % n + 1] - a[p]) * (x - a[p]) >= 0;
}
void minkowski(point a[], int n, point b[], int m, point c[], int &tot)
{
static point a1[N], b1[N];
for(int i = 1; i <= n; i++) a1[i] = a[i + 1] - a[i];
for(int i = 1; i <= m; i++) b1[i] = b[i + 1] - b[i];
int i = 1, j = 1;
c[tot = 1] = a[1] + b[1];
while(i <= n && j <= m) tot++, c[tot] = c[tot - 1] + (a1[i] * b1[j] >= 0 ? a1[i++] : b1[j++]);
while(i <= n) tot++, c[tot] = c[tot - 1] + a1[i++];
while(j <= m) tot++, c[tot] = c[tot - 1] + b1[j++];
convex(c, tot);
}
}
using namespace geometry;miller-rabin & pollard-pho
pollard_pho 需要传一个空的存质因数的 vector。返回的 vector 中,每个质因数的出现次数是他的指数,没有去重。
namespace factorization
{
#define LL long long
#define i128 __int128
mt19937_64 rd(time(0));
i128 ksm(i128 x, LL y, LL mod)
{
i128 res = 1;
x %= mod;
while(y)
{
if(y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
bool miller_rabin(LL n)
{
if(n <= 1 || n > 2 && ~n & 1) return false;
LL m = n - 1, k = 0;
while(~m & 1) m >>= 1, k++;
vector<int> s = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
for(int a : s)
{
if(a >= n) break;
i128 x = ksm(a, m, n), y;
for(int i = 1; i <= k; i++, x = y)
{
y = x * x % n;
if(y == 1 && x != 1 && x != n - 1) return false;
}
if(x != 1) return false;
}
return true;
}
void pollard_rho(LL n, vector<LL> &p)
{
if(n <= 1) return;
if(miller_rabin(n))
{
p.push_back(n);
return;
}
LL d = rd() % (n - 1) + 1;
auto f = [&](LL x)
{
return ((i128)x * x + d) % n;
};
LL x = rd() % (n - 1) + 1, y = f(x), t = 1;
while(t == 1)
{
for(int i = 1; i <= 32; i++)
{
while(x == y)
d = rd() % (n - 1) + 1, x = rd() % (n - 1) + 1, y = f(x);
if((i128)t * abs(x - y) % n == 0) break;
t = (i128)t * abs(x - y) % n;
x = f(x), y = f(f(y));
}
t = __gcd(n, t);
}
pollard_rho(t, p), pollard_rho(n / t, p);
}
}
using namespace factorization;min25
getsum 里面的 必须是积性函数,其余地方必须是完全积性函数。
特别注意:计算的所有内容均是 ,不包含 。
数组大小开 。
注意 longlong 不一定够用,部分题目可能要 __int128 之类的东西。
namespace min25
{
#define LL long long
LL n, sqr, val[N], f[N];
int prime[N], cnt;
bool isprime[N];
LL sum[N];
void getprime(int n)
{
for(int i = 2; i <= n; i++)
{
if(!isprime[i]) prime[++cnt] = i;
for(int j = 1; i * prime[j] <= n; j++)
{
isprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
sum[i] = /*F(prime[1~i])*/
}
int id1[N], id2[N], tot;
inline int get(LL x)
{
return x <= sqr ? id1[x] : id2[n / x];
}
int getsum(LL n, int x)
{
if(n <= prime[x]) return 0;
LL sum = f[get(n)] - sum[x];
for(int i = x + 1; i <= cnt && prime[i] <= n / prime[i]; i++)
{
LL pw = prime[i];
for(int j = 1; pw <= n; j++, pw *= prime[i])
sum = (sum + /*F(pw)*/ * (getsum(n / pw, i) + (j > 1))) % mod;
}
return sum;
}
void init(LL n1)
{
getprime(sqr = sqrt(n = n1));
for(LL l = 1, r; l <= n; l = r + 1)
{
r = n / (n / l);
LL t = n / l;
if(t <= sqr) id1[t] = ++tot;
else id2[n / t] = ++tot;
val[tot] = t;
f[tot] = /*F(2~t)*/
}
for(int i = 1; i <= cnt; i++)
for(int j = 1; j <= tot && val[j] >= 1ll * prime[i] * prime[i]; j++)
f[j] -= /*F(prime[i])*/ * (f[get(val[j] / prime[i])] - sum[i - 1]);
}
}