版本比较
AT_abc349_f [ABC349F] Subsequence LCM
正文
增加 146 行删除 26 行
123455677891011111213131415161723 行未改动414243444545464748493 行未改动535455565757585960616263646565666768686970707172737447 行未改动12212312412512612612712812913010 行未改动14114214314414514514614714814911 行未改动16116216316416516616716816917017117217317417517617717817916516616716816 行未改动185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328这里介绍一种非常魔怔的做法,莫比乌斯反演 + `bitset`。
首先,莫比乌斯反演是什么呢?不会的可以 [点这里](https://oi-wiki.org/math/number-theory/mobius/) 。
令 $f(x)$ 为 $\operatorname{lcm} = x$ 的方案数,$g(x)$ 为 $\operatorname{lcm} \mid x$ 的方案数。令 $f(x)$ 为 $\operatorname{lcm} = x$ 的选取方案数,$g(x)$ 为 $\operatorname{lcm} \mid x$ 的选取方案数。
则有则有:$$g(x)=\sum_{d \mid x} f(d)$$根据莫比乌斯反演公式,可得根据莫比乌斯反演公式,可得:$$f(x)=\sum_{d \mid x} 𝜇(\frac{x}{d}) \times g(d)f(x)=\sum_{d \mid x} \mu(\frac{x}{d}) \times g(d)$$而 $g(x)$ 其实就是统计序列中有多少 $x$ 的因数,将这个个数记为 $p$,$g(x)=2^p-1$,这里的减一是为了排除空集。
于是,就可以洋洋洒洒写下如下代码:23 行未改动}inline long long f(long long x){ long long res = 0; for (long long i = 1;i * i <= x;++i){ if (x % i == 0) if (x % i == 0) if (i != x / i) res = (res + ((((mobius(i) * g(x / i) + mod) % mod) + ((mobius(x / i) * g(i) + mod) % mod) + mod) % mod)) % mod; else res = (res + ((mobius(i) * g(x / i) + mod) % mod)) % mod; } return res;3 行未改动 for (int i = 1;i <= n;++i) cin >> a[i]; cout << f(m);}```之后你就会发现,过不去,只能过 $25$ 个点。之后你就会发现过不去,只能过 $25$ 个点。
那咋办?~~换做法。~~
让我们进行神秘优化吧。
首先,从莫比乌斯函数入手。
- $𝜇(x)$ 会在 $x$ 包含多个质数因数时返回 $0$。- $\mu(x)$ 会在 $x$ 的质因数分解中包含多个相同质数相乘时返回 $0$。- 否则莫比乌斯函数与质数因数个数有关。
可以考虑先质因数分解 $m$,获取所有质数因数后,`dfs`枚举**每种**因数选或不选,注意,最多只能选一个。可以考虑先质因数分解 $m$,获取所有质数因数后,`dfs` 枚举**每种**质数选或不选。
为什么这里可以将`dfs`枚举因数选不选作为优化手段呢?因为可以发现不同种类的质数个数最多只会有 $13$ 个,这时复杂度是明显优于一遍遍重新求莫比乌斯函数以及枚举因数。为什么这里可以将 `dfs` 枚举质数选不选作为优化手段呢?因为可以发现不同种类的质数个数最多只会有 $13$ 个,而此时的复杂度是 $O(2^k)$,这时复杂度是明显优于一遍遍重新求莫比乌斯函数以及枚举因数。
之后正常做就好了,再补上一点特判,代码如下:
```cpp47 行未改动思考一下这时候的瓶颈在哪里?发现 $g(x)$ 函数求解是一个 $O(n)$ 的过程,如何优化它呢?
考虑对每个 $a_i$ 分解质因数,但是请注意,这里并不是暴力分解,而是对着 $m$ 分解出来的质因数分解,因为如果 $a_i$ 包含不在 $m$ 内的质因数,一定不会被选中,也就可以忽略不计。
分解质因数后如何快速求个数呢?使用`bitset`就可以了,代码如下:分解质因数后如何快速求个数呢?使用 `bitset` 就可以了,代码如下:```cpp//code by xuanxuanmeow#include <bits/stdc++.h>using namespace std;10 行未改动}inline long long fastpow(long long a,long long b){ long long num = 1,t = a; while (b){ if (b & 1) num = num * t % mod; if (b & 1) num = num * t % mod; t = t * t % mod,b >>= 1; } return num;}11 行未改动 for (int i = x;i <= tot;++i) dfs(i + 1,num * seq[i],-f);}int main(){ read(n),read(m); if (n == 1){ read(a[1]); if (a[1] == m) cout << 1; else cout << 0; return 0; } if (m == 1){ long long ans = 1; for (int i = 1;i <= n;++i){ read(a[i]); if (a[i] == 1) ans = (ans << 1) % mod; } cout << ans - 1; return 0; } tms = m,tmper = 1; for (long long i = 2;i * i <= tms;++i){ cnt = 0; while (tms % i == 0) tms /= i,++cnt;16 行未改动 cnt = 0; while (a[tor] % seq[j] == 0) a[tor] /= seq[j],c[j - 1][++cnt][tor] = true; } } if (!tor){ cout << 0; return 0; } dfs(1,1,1); cout << res;}```至此,可以得到满分。
但这跑的还是太慢了,如何优化?
观察到现在的瓶颈其实是在对 $m$ 的质因数分解上。
使用 `Pollard-Rho` 在 $O(m^{\frac{1}{4}})$ 的时间复杂度分解质因数。
代码如下:```cpp//code by xuanxuanmeow#include <bits/stdc++.h>using namespace std;#define f(x) (((((x) % n) * ((x) % n) % n) + c) % n)#define abs(x) ((x) > 0?(x):-(x))mt19937 rnd(random_device{}());const int N = 2e5 + 5;const int mod = 998244353;long long n,a[N],m,cnt,seq[N],tag,tot,res,tor,tmper,pr,ps;bitset <N> c[16][56],banned;#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)#define lcm(x,y) (x / __gcd(x,y) * y)char *p1,*p2,buf[100000],ch;inline void read(long long &x){ ch = nc(),x = 0; while (ch < '0' || ch > '9') ch = nc(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48),ch = nc();}inline long long fastpow(long long a,long long b){ long long num = 1,t = a; while (b){ if (b & 1) num = num * t % mod; t = t * t % mod,b >>= 1; } return num;}inline long long g(long long x){ banned.reset(); for (int i = 1;i <= tot;++i){ cnt = 0; while (x % seq[i] == 0) x /= seq[i],++cnt; banned |= c[i - 1][cnt + 1]; } return (fastpow(2,tor - banned.count()) - 1 + mod) % mod;}inline void dfs(int x,long long num,long long f){ res = (res + ((f * g(m / num) + mod) % mod)) % mod; for (int i = x;i <= tot;++i) dfs(i + 1,num * seq[i],-f);}inline __int128 random(__int128 l,__int128 r){ return rnd() % (r - l + 1) + l;}inline __int128 fastpows(__int128 a,__int128 b,__int128 moder){ __int128 num = 1,t = a; while (b){ if (b & 1) num = num * t % moder; t = t * t % moder,b >>= 1; } return num;}inline bool checker(long long n){ if (n < 3 || !(n & 1)) return n == 2; if (n % 3 == 0) return n == 3; __int128 u = n - 1,t = 0; while (!(u & 1)) u >>= 1,++t; for (int i = 1;i <= 10;++i){ __int128 a = random(2,n - 2),v = fastpows(a,u,n); if (v == 1) continue; int s; for (s = 0;s < t;++s){ if (v == n - 1) break; v = 1ll * v * v % n; } if (s == t) return false; } return true;}inline long long prs(__int128 n){ if (n == 4) return 2; __int128 x = random(0,n - 1),y = x,c = random(3,n - 1),d = 1,cnt,tmp; x = f(x),y = f(f(y)); for (int lim = 1;x != y;lim = min(128,lim << 1)){ cnt = 1; for (int i = 1;i <= lim;++i){ tmp = cnt * abs(x - y) % n; if (!tmp) break; cnt = tmp; x = f(x),y = f(f(y)); } d = __gcd(cnt,n); if (d != 1) return d; } return n;}inline void primes(long long m){ if (m == 1) return; if (checker(m)){ seq[++tot] = m,tmper *= m; return; } long long x = prs(m),y = m / x; while ((ps = __gcd(x,y)) != 1) y /= ps; primes(x),primes(y);}int main(){ read(n),read(m); tmper = 1,primes(m); for (int i = 1;i <= n;++i){ read(a[++tor]); if (m % a[tor] != 0){ --tor; continue; } tag = 0,pr = a[tor]; while ((ps = __gcd(tmper,pr)) != 1) pr /= ps; if (pr != 1){ --tor; continue; } for (int j = 1;j <= tot;++j){ cnt = 0; while (a[tor] % seq[j] == 0) a[tor] /= seq[j],c[j - 1][++cnt][tor] = true; } } if (!tor){ cout << 0; return 0; } dfs(1,1,1); cout << res;}```