版本比较
各种模板
正文
增加 79 行删除 79 行
9 行未改动101112131415161718192064 行未改动85868788899091929326 行未改动1201211221231241251261271283 行未改动13213313413513613713813914014114214314414514614 行未改动16116216316416516616716816924 行未改动1941951961971981992002012029 行未改动2122132142152162172182192202212226 行未改动2292302312322332342352362372382392402412422432442452462472482492503 行未改动25425525625725825926026126226326426518 行未改动2842852862872882892902912928 行未改动3013023033043053063073083093103113123133143153163173183192 行未改动32232332432532632732832933033133233333433533611 行未改动34834935035135235335435535635735835936036136236336436536636736836937037110 行未改动3823833843853863873883893906 行未改动3973983994004014024034044054064074084094104114124134144152 行未改动4184194204214224234244254262 行未改动42943043143243343443543643743843944044144244326 行未改动4704714724734744754764774784 行未改动48348448548648748848949049130 行未改动52252352452552652752852953028 行未改动559560561562563564565566567568233 行未改动全部展开8028038048058068078088098108118128138148158168178188198208218228232 行未改动8268278288298308318328338347 行未改动84284384484584684784884985052 行未改动90390490590690790890991091191291311 行未改动92592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199237 行未改动10301031103210331034103510361037103824 行未改动10631064106510661067106810691070107184 行未改动115611571158115911601161116211631164116511661167116811691170117111721173117410 行未改动118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912107 行未改动121812191220122112221223122412251226122712283 行未改动12321233123412351236123712381239124011 行未改动125212531254125512561257125812591260126112621263126412651266126712681269127012711272127313 行未改动1287128812891290129112921293129412951296129712981299130013011302130313041305130613076 行未改动1314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134514 行未改动9 行未改动{char buf[1 << 21], *p1 = buf, *p2 = buf;inline char gc(){#ifdef LOCALreturn getchar();#endifif(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)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(!--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)$。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;}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;}}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;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,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;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;}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);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++)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;}}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;}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 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;}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);}7 行未改动namespace factorization{#define LL long long#define i128 __int128mt19937_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;};LL x = rd() % (n - 1) + 1, y = f(x), t = 1;while(t == 1){13 行未改动using namespace factorization;```# min25`getsum` 里面的 $F$ 必须是**积性函数**,其余地方必须是**完全积性函数**。特别注意:计算的所有内容均是 $2\sim n$,不包含 $1$。数组大小开 $2\sqrt n$。注意 `longlong` 不一定够用,部分题目可能要 `__int128` 之类的东西。```namespace min25{#define LL long longLL 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 行未改动9 行未改动101112131415161718192064 行未改动85868788899091929326 行未改动1201211221231241251261271283 行未改动13213313413513613713813914014114214314414514614 行未改动16116216316416516616716816924 行未改动1941951961971981992002012029 行未改动2122132142152162172182192202212226 行未改动2292302312322332342352362372382392402412422432442452462472482492503 行未改动25425525625725825926026126226326426518 行未改动2842852862872882892902912928 行未改动3013023033043053063073083093103113123133143153163173183192 行未改动32232332432532632732832933033133233333433533611 行未改动34834935035135235335435535635735835936036136236336436536636736836937037110 行未改动3823833843853863873883893906 行未改动3973983994004014024034044054064074084094104114124134144152 行未改动4184194204214224234244254262 行未改动42943043143243343443543643743843944044144244326 行未改动4704714724734744754764774784 行未改动48348448548648748848949049130 行未改动52252352452552652752852953028 行未改动559560561562563564565566567568233 行未改动全部展开8028038048058068078088098108118128138148158168178188198208218228232 行未改动8268278288298308318328338347 行未改动84284384484584684784884985052 行未改动90390490590690790890991091191291311 行未改动92592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199237 行未改动10301031103210331034103510361037103824 行未改动10631064106510661067106810691070107184 行未改动115611571158115911601161116211631164116511661167116811691170117111721173117410 行未改动118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912107 行未改动121812191220122112221223122412251226122712283 行未改动12321233123412351236123712381239124011 行未改动125212531254125512561257125812591260126112621263126412651266126712681269127012711272127313 行未改动1287128812891290129112921293129412951296129712981299130013011302130313041305130613076 行未改动1314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134514 行未改动9 行未改动{char buf[1 << 21], *p1 = buf, *p2 = buf;inline char gc(){#ifdef LOCALreturn getchar();#endifif(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)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(!--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)$。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;}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;}}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;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,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;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;}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);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++)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;}}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;}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 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;}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);}7 行未改动namespace factorization{#define LL long long#define i128 __int128mt19937_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;};LL x = rd() % (n - 1) + 1, y = f(x), t = 1;while(t == 1){13 行未改动using namespace factorization;```# min25`getsum` 里面的 $F$ 必须是**积性函数**,其余地方必须是**完全积性函数**。特别注意:计算的所有内容均是 $2\sim n$,不包含 $1$。数组大小开 $2\sqrt n$。注意 `longlong` 不一定够用,部分题目可能要 `__int128` 之类的东西。```namespace min25{#define LL long longLL 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 行未改动