短短题解:CF1750D Count GCD
题目链接
把要求的
对
-
\gcd(a_{i-1}, b_i) = a_i \implies a_i \mid a_{i-1} \land a_i \mid b_i -
\gcd(a_{i-1}, b_i) = a_i \iff \gcd(\frac{a_{i-1}}{a_i}, \frac{b_i}{a_i}) = 1
因此对于 0。
接下来是计数:满足
注意到在位置
做点简化,设
这是莫比乌斯反演例题:
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 200000 + 1, P = 998244353;
int a[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
auto getprms = [](int x) {
vector<int> res;
for (int i = 2; i * i <= x; ++i) if (x % i == 0) {
x /= i;
res.push_back(i);
while (x % i == 0) {
x /= i;
}
}
if (x != 1) {
res.push_back(x);
}
return res;
};
int ans = 1;
for (int i = 2; i <= n; ++i) {
if (a[i - 1] % a[i]) {
cout << "0\n";
return ;
}
vector<int> res = getprms(a[i - 1] / a[i]);
int sm = 0;
auto dfs = [&](auto &&self, int dep, int mul, int mu) {
if (dep == (int)res.size()) {
sm += m / a[i] / mul * mu;
return ;
}
self(self, dep + 1, mul, mu);
self(self, dep + 1, mul * res[dep], -mu);
};
dfs(dfs, 0, 1, 1);
ans = (long long)ans * sm % P;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}