跳到正文

题解

短短题解:CF1750D Count GCD

SkyWave约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年2月9日
更新
2026年2月9日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

题目链接

把要求的 gcd⁡(b1,b2,…,bi)=ai\gcd(b_1, b_2, \dots, b_i) = a_i 写成 gcd⁡(gcd⁡(b1,b2,…,bi−1),bi)=ai\gcd(\gcd(b_1, b_2, \dots, b_{i-1}), b_i) = a_i,这等价于要求 gcd⁡(ai−1,bi)=ai\gcd(a_{i - 1}, b_i) = a_i,其中定义 a0a_0 为 gcd⁡\gcd 的单位元;这就已经是充要的了。

对 gcd⁡(ai−1,bi)=ai\gcd(a_{i - 1}, b_i) = a_i 做观察:

  • gcd⁡(ai−1,bi)=ai  ⟹  ai∣ai−1∧ai∣bi\gcd(a_{i-1}, b_i) = a_i \implies a_i \mid a_{i-1} \land a_i \mid b_i
  • gcd⁡(ai−1,bi)=ai  ⟺  gcd⁡(ai−1ai,biai)=1\gcd(a_{i-1}, b_i) = a_i \iff \gcd(\frac{a_{i-1}}{a_i}, \frac{b_i}{a_i}) = 1

因此对于 2≤i≤n2 \leq i \leq n 的每个 ii,必须都有 ai∣ai−1a_{i} \mid a_{i-1},否则不存在任何符合条件的 bb 序列,直接输出 0。

接下来是计数:满足 b1=a1b_1 = a_1,且对于 2≤i≤n2 \leq i \leq n 的每个 ii,有 1≤bi≤m1 \leq b_i \leq m 且 gcd⁡(ai−1ai,biai)=1\gcd(\frac{a_{i-1}}{a_i}, \frac{b_i}{a_i}) = 1 的序列 bb 的个数。

注意到在位置 ii,bib_i 的合法性与其他位置 jj 的 bjb_j 均无关,因此可以使用乘法原理合并每个位置上的答案。

做点简化,设 ai−1ai\frac{a_{i-1}}{a_i} 为 xx,biai\frac{b_i}{a_i} 为 yy。问题即变为统计满足 1≤y⋅ai≤m1 \leq y \cdot a_i \leq m,即 1≤y≤⌊mai⌋1 \leq y \leq \lfloor \frac{m}{a_i} \rfloor 且 gcd⁡(x,y)=1\gcd(x, y) = 1 的 yy 的个数。

这是莫比乌斯反演例题:

∑y=1⌊mai⌋[gcd⁡(x,y)=1]=∑y=1⌊mai⌋∑d∣gcd⁡(x,y)μ(d)=∑y=1⌊mai⌋∑d∣xd∣yμ(d)=∑y=1⌊mai⌋∑d∣xμ(d) [d∣y]=∑d∣xμ(d)∑y=1⌊mai⌋[d∣y]=∑d∣xμ(d)⌊⌊mai⌋d⌋\begin{aligned} \sum_{y=1}^{\left\lfloor \frac{m}{a_i} \right\rfloor} [\gcd(x, y) = 1] &= \sum_{y=1}^{\left\lfloor \frac{m}{a_i} \right\rfloor} \sum_{d \mid \gcd(x, y)} \mu(d) \\[6pt] &= \sum_{y=1}^{\left\lfloor \frac{m}{a_i} \right\rfloor} \sum_{\substack{d \mid x \\ d \mid y}} \mu(d) \\[6pt] &= \sum_{y=1}^{\left\lfloor \frac{m}{a_i} \right\rfloor} \sum_{d \mid x} \mu(d)\,[d \mid y] \\[6pt] &= \sum_{d \mid x} \mu(d) \sum_{y=1}^{\left\lfloor \frac{m}{a_i} \right\rfloor} [d \mid y] \\[6pt] &= \sum_{d \mid x} \mu(d) \left\lfloor \frac{\left\lfloor \frac{m}{a_i} \right\rfloor}{d} \right\rfloor \end{aligned}
C++
#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;
}
原文