版本比较
各种模板
正文
增加 454 行删除 237 行
6 行未改动78910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061621112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869702 行未改动7374757673747576777879808182838485868788899091929394959697989910010110210310477787980818283848586878889909192939495969798991001011021031041051061071081091101111127 行未改动1201211221231201241251261271283 行未改动13213313413513213613713813914014113814214314414514614 行未改动16116216316416116516616716816924 行未改动1941951961971941981992002012029 行未改动2122132142152122132142162172182192202212226 行未改动2292302312322292332342352362372382392402412382422432442452422462472482492503 行未改动25425525625725425825926025726126226326426518 行未改动2842852862872842882892902912928 行未改动3013023033043013053063073083093063103113123093133143113153163173183192 行未改动32232332432532232632732832933033132833233333433533611 行未改动34834935035134835235335435535635735835935636036136236336436536636336736836937037110 行未改动3823833843853823863873883893906 行未改动3973983994003974014024034044054064074084094104074114124134144152 行未改动41841942042141842242342042442542642742842943043143242943343443543643743843543944044144244326 行未改动4704714724734704744754764774784 行未改动48348448548648348748848949049130 行未改动5225235245255225265275285295305315325335345355325365375385395365375385395405415405415425435445435445465475455465475485495495505515525515525535545555565575585595605535545555565625575585595605615675685695705625635645655665735745675685695765775785795805815825705715725735745845755765865875885775785795805815825835845855865875905915885895905935945955915925935945955965975975985995996006016026036046056066076086096106116126136146156166176186196206216226236246256266026276286296046306316326336346066356366376386396406416426436446456466476486496506516526536546556566576586086596606616626636646656666676686696706716726736746756766776786796806816826836846856866106116126136146876886896906916926936946956966976986997007017027037047057067077087097107117127137147156167167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577586227597606247617626266276286296307637647657667677687697706327717727736346356366376387747757767777787797807816407827837846426436446456466477857866496507877887897907917927937947957967974 行未改动8028038048056648068078086678098108118128138148158168178186778198208218228232 行未改动8268278288296888308318328338347 行未改动84284384484570484684784884985052 行未改动90390490590676590790876790991091191291311 行未改动92592692792878792993093193293393493579493693793893994094180094294394494594694780694894980895095195295395495595695795881795996096196296396496596682596796896997097197283197397483397597697797897998083998198298398498598698784698898999099199237 行未改动1030103110321033892103410351036103710389 行未改动1048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113025 行未改动115611571158115994311601161116211631164116511661167116811699531170117111721173117410 行未改动118511861187118897211891190119111921193119411959791196119798111981199120012011202120312041205989120612071208120912107 行未改动121812191220122110051006100712221223122412251226122712283 行未改动123212331234123510191236123712381239124011 行未改动125212531254125510391256125712581259126012611262126312641048126512661267126810521269127012711272127313 行未改动12871288128912901074129112921293107712941295129612971298129913001084130113021086130313041305130613076 行未改动131413151316131711011318131913201104132113221323132413251326111013271328132913301114133113321333133413351336133713381339134011241341134213431344134514 行未改动6 行未改动# 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--]); } 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;```
2 行未改动```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(T1 x) { return (x % mod + mod) % mod; } inline int find(T1 key) { int h1 = hash(key); while(dfn[h1] == viti && h[h1] != key) ++h1 == mod && (h1 = 0); if(dfn[h1] == viti && h1 != hash(key)) swap(h[h1], h[hash(key)]), swap(w[h1], w[hash(key)]), h1 = hash(key); return h1; } inline T2& operator[](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(T1 key) { int h1 = find(key); return dfn[h1] == viti; } inline void clear() { ++viti; }}; 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; }};```
# 网络流```7 行未改动 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) {3 行未改动 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(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);14 行未改动 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];24 行未改动 } } return flow[t]; }
int getval() { int ans = 0; while(ssp())9 行未改动
# 多项式各函数的 $n$ 表示 $a$ 的**次数**。没有特殊说明的情况下数组需不同。如果使用了除 NTT 以外的操作(即全家桶的内容),空间翻倍。
Ln:有 $a_0=1,b_0=0$。 Exp:有 $a_0=0,b_0=1$。 Ksm:$a,b$ 两个数组可以相同。设指数为 $k$,那么有 $y=k\bmod p+p\cdot[k\ge p],yy=k\bmod(p-1)$。 Ln:有 $a_0=1,b_0=0$。Exp:有 $a_0=0,b_0=1$。Ksm:$a,b$ 两个数组可以相同。设指数为 $k$,那么有 $y=k\bmod p+p\cdot[k\ge p],yy=k\bmod(p-1)$。Div:$a,r$ 两个数组可以相同。
值得注意的是,多项式的次数必须用变量 `N` 表示。```6 行未改动 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; x = x * x % mod; } return res; }
int bit, tot; int w[N], inv_num[N]; struct Initial {3 行未改动 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; 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++)18 行未改动 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;8 行未改动 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; 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)2 行未改动 t2 = (res1 * b + res2 * a) % mod; res1 = t1, res2 = t2; } y >>= 1; t1 = (1ll * a * a + b * b % mod * ii) % mod, 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;11 行未改动 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; 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;10 行未改动 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++)6 行未改动 { 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; 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[]) {2 行未改动 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] = b[m - i], z[i] = a[n - i]; 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); NTT(x, -1); for(int i = 0; i < m; i++) (r[i] = a[i] - x[i]) < 0 && (r[i] += mod); }}using namespace poly;26 行未改动 { return (Complex){x * a.x - y * a.y, x * a.y + y * a.x}; } };
int bit, tot; Complex w[N]; struct Initial {4 行未改动 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++)30 行未改动 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++) 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;```
## FFT 版本# 集合幂级数```namespace poly{ inline int lg2(int n) { int k = 0; while(1 << k + 1 <= n) k++; return k; } #define LL long long #define u64 unsigned long long
const double PI = acos(-1.0); struct Complex LL ksm(LL x, LL y) { double x, y; Complex operator+(const Complex &a) const LL res = 1; x %= mod; while(y) { return (Complex){x + a.x, y + a.y}; if(y & 1) res = res * x % mod; y >>= 1; x = x * x % mod; } 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}; } }; return res; }
int bit, tot; Complex w[N]; LL inv_num[22]; 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)]; inv_num[1] = 1; for(int i = 2; i <= 21; i++) inv_num[i] = inv_num[mod % i] * (mod - mod / i) % mod; } }initial; void DFT(Complex a[])
void FWT_or(int a[], int inv) { 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; } 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 IDFT(Complex a[]) void FWT_and(int a[], int inv) { 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++) 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++) { Complex x = a[j], y = a[j + len]; a[j] = x + y, a[j + len] = (x - y) * w[l]; 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); } 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; if(inv == -1) { u64 t = ksm(tot, mod - 2); for(int i = 0; i < tot; i++) a[i] = a[i] * t % mod; } } void FFT(Complex a[], int inv) void FWT_or(LL a[], int inv) { inv == 1 ? DFT(a) : IDFT(a); 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; } }
void MTT(int a[], int b[], int c[], const int &mod) template<typename T> void times(T a[], T b[], T c[]) { static Complex x[N], y[N], z[N]; 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[i].x = a[i] >> 15, x[i].y = 0; { 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++) y[i].x = a[i] & (1 << 15) - 1, y[i].y = 0; { 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++) 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); { 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++) c[i] = ((llround(x[i].x) % mod << 30) + ((llround(x[i].y) + llround(y[i].x)) % mod << 15) + llround(y[i].y)) % mod; { 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;```
# FWT# Splay```void FWT_or(LL a[], int n, int inv)struct tree{ for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; 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 < n; i++) a[i] = (a[i] % mod + mod) % mod; int son[2], fa;}a[N];int root, tot;
inline void pushup(int p){
}void FWT_and(LL a[], int n, int inv)
void rotate(int x){ for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; 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 < n; i++) a[i] = (a[i] % mod + mod) % mod; 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 FWT_xor(LL a[], int n, int inv)
void splay(int x, int k){ for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; 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 < n; i++) a[i] = (a[i] % mod + mod) % mod; if(inv == -1) while(a[x].fa != k) { int t = ksm(n, mod - 2); for(int i = 0; i < n; i++) a[i] = a[i] * t % mod; 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;}```
# LCT4 行未改动 { 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;2 行未改动 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;7 行未改动 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);52 行未改动
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]);}```11 行未改动 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; 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;```
# SAM37 行未改动 { 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);9 行未改动}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{25 行未改动 { 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++)10 行未改动 } 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; 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 && 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); }7 行未改动namespace factorization{ #define LL long long #define i128 __int128 mt19937_64 rd(time(0)); i128 ksm(i128 x, LL y, LL mod) mt19937_64 rd(time(0));
i128 ksm(i128 x, LL y, LL mod) { i128 res = 1; x %= mod; while(y)3 行未改动 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;11 行未改动 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; return ((i128)x * x + d) % n; }; LL x = rd() % (n - 1) + 1, y = f(x), t = 1; while(t == 1) {13 行未改动using namespace factorization;```
# min25`getsum` 里面的 $F$ 必须是**积性函数**,其余地方必须是**完全积性函数**。 `getsum` 里面的 $F$ 必须是**积性函数**,其余地方必须是**完全积性函数**。特别注意:计算的所有内容均是 $2\sim n$,不包含 $1$。
数组大小开 $2\sqrt n$。 数组大小开 $2\sqrt n$。注意 `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)6 行未改动 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)14 行未改动