这里介绍一种非常魔怔的做法,莫比乌斯反演 + bitset。
首先,莫比乌斯反演是什么呢?不会的可以 点这里 。
令 为 的方案数, 为 的方案数。
则有
根据莫比乌斯反演公式,可得
而 其实就是统计序列中有多少 的因数,将这个个数记为 ,,这里的减一是为了排除空集。
于是,就可以洋洋洒洒写下如下代码:
//code by xuanxuanmeow
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 998244353;
int n;
long long a[N],m,cnt;
inline long long g(long long x){
long long t = 1;
for (int i = 1;i <= n;++i) if (x % a[i] == 0) t = (t << 1) % mod;
return (t - 1 + mod) % mod;
}
inline long long mobius(long long x){
long long f = 1;
for (long long i = 2;i * i <= x;++i){
cnt = 0;
while (x % i == 0) x /= i,++cnt;
if (cnt > 1) return 0;
if (cnt) f = -f;
}
if (x != 1) f = -f;
return f;
}
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 (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;
}
int main(){
cin >> n >> m;
for (int i = 1;i <= n;++i) cin >> a[i];
cout << f(m);
}之后你就会发现,过不去,只能过 个点。
那咋办?换做法。
让我们进行神秘优化吧。
首先,从莫比乌斯函数入手。
- 会在 包含多个质数因数时返回 。
- 否则莫比乌斯函数与质数因数个数有关。
可以考虑先质因数分解 ,获取所有质数因数后,dfs枚举每种因数选或不选,注意,最多只能选一个。
为什么这里可以将dfs枚举因数选不选作为优化手段呢?因为可以发现不同种类的质数个数最多只会有 个,这时复杂度是明显优于一遍遍重新求莫比乌斯函数以及枚举因数。
之后正常做就好了,再补上一点特判,代码如下:
//code by xuanxuanmeow
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 998244353;
int n;
long long a[N],tms,m,cnt,seq[N],c[N],tot,res;
inline long long g(long long x){
long long t = 1;
for (int i = 1;i <= n;++i) if (x % a[i] == 0) t = (t << 1) % mod;
return (t - 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);
}
int main(){
cin >> n >> m;
if (n == 1){
cin >> 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){
cin >> a[i];
if (a[i] == 1) ans = (ans << 1) % mod;
}
cout << ans - 1;
return 0;
}
tms = m;
for (long long i = 2;i * i <= tms;++i){
cnt = 0;
while (tms % i == 0) tms /= i,++cnt;
if (cnt) seq[++tot] = i;
}
if (tms != 1) seq[++tot] = tms;
for (int i = 1;i <= n;++i) cin >> a[i];
dfs(1,1,1);
cout << res;
}但是你会发现还是过不去,只能过 个点,这时候就需要更加神秘的优化了。
思考一下这时候的瓶颈在哪里?发现 函数求解是一个 的过程,如何优化它呢?
考虑对每个 分解质因数,但是请注意,这里并不是暴力分解,而是对着 分解出来的质因数分解,因为如果 包含不在 内的质因数,一定不会被选中,也就可以忽略不计。
分解质因数后如何快速求个数呢?使用bitset就可以了,代码如下:
//code by xuanxuanmeow
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 998244353;
long long n,a[N],tms,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++)
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);
}
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;
if (cnt) seq[++tot] = i,tmper *= seq[tot];
}
if (tms != 1) seq[++tot] = tms,tmper *= seq[tot];
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;
}
}
dfs(1,1,1);
cout << res;
}至此,可以得到满分。