版本比较
各种模板
正文
增加 79 行删除 79 行
9 行未改动1011121314141516161718192064 行未改动8586878889899091929326 行未改动1201211221231241241251261271283 行未改动13213313413513613613713813914014114214214314414514614 行未改动16116216316416516516616716816924 行未改动1941951961971981981992002012029 行未改动2122132142152162172182162172182192202212226 行未改动2292302312322332332342352362372382392402412422422432442452462462472482492503 行未改动25425525625725825825926026126126226326426518 行未改动2842852862872882882892902912928 行未改动3013023033043053053063073083093103103113123133133143153153163173183192 行未改动32232332432532632632732832933033133233233333433533611 行未改动34834935035135235235335435535635735835936036036136236336436536636736736836937037110 行未改动3823833843853863863873883893906 行未改动3973983994004014014024034044054064074084094104114114124134144152 行未改动4184194204214224224234244254262 行未改动42943043143243343343443543643743843943944044144244326 行未改动4704714724734744744754764774784 行未改动48348448548648748748848949049130 行未改动52252352452552652652752852953028 行未改动559560561562563564563564565566567568233 行未改动全部展开8028038048058068068078088098098108118128138148158168178188198198208218228232 行未改动8268278288298308308318328338347 行未改动84284384484584684684784884985052 行未改动90390490590690790790890990991091191291311 行未改动92592692792892992993093193293393493593693693793893994094194294294394494594694794894894995095095195295395495595695795895995996096196296396496596696796796896997097197297397397497597597697797897998098198198298398498598698798898898999099199237 行未改动103010311032103310341034103510361037103824 行未改动106310641065106610671067106810691070107184 行未改动11561157115811591160116011611162116311641165116611671168116911701170117111721173117410 行未改动1185118611871188118911891190119111921193119411951196119611971198119811991200120112021203120412051206120612071208120912107 行未改动121812191220122112221223122412221223122412251226122712283 行未改动123212331234123512361236123712381239124011 行未改动125212531254125512561256125712581259126012611262126312641265126512661267126812691269127012711272127313 行未改动12871288128912901291129112921293129412941295129612971298129913001301130113021303130313041305130613076 行未改动131413151316131713181318131913201321132113221323132413251326132713271328132913301331133113321333133413351336133713381339134013411341134213431344134514 行未改动9 行未改动{ char buf[1 << 21], *p1 = buf, *p2 = buf; inline char gc() { #ifdef LOCAL #ifdef LOCAL return getchar(); #endif #endif if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin); return p1 == p2 ? EOF : *p1++; } template<typename T = int>64 行未改动 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) 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)26 行未改动 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] = 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;2 行未改动 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;28 行未改动 { Initial() { inv_num[1] = 1; for(int i = 2; i <= 21; i++) inv_num[i] = inv_num[mod % i] * (mod - mod / i) % mod; 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)233 行未改动全部展开 { 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);24 行未改动 } 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 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;}84 行未改动 { 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 行未改动