跳到正文

题解

P8926 题解

shinzAnmono约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年1月1日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 4 分钟
题目
P8926

版本

  1. v1当前

一看题目,最大公约数,最小公倍数……

一言不合推式子:

令 t=gcd⁡(x,y)t=\gcd(x,y),则 x=t⋅x0,y=t⋅y0,lcm⁡(x,y)=t⋅x0⋅y0x=t\cdot x_0, y=t\cdot y_0, \operatorname{lcm}(x,y) = t\cdot x_0\cdot y_0,显而易见 gcd⁡(x0,y0)=1\gcd(x_0,y_0)=1。

∴\therefore 原式可变形为 t⋅k=t⋅x0⋅y0t\cdot k=t\cdot x_0\cdot y_0

∴k=x0⋅y0\therefore k=x_0\cdot y_0

我们知道,两个数互质的充要条件是两个数没有共同的质因数,对于 kk,我们对其分解质因数。设 kk 有 cntcnt 种质因数,则任意一种质因数必将作为 x0x_0 或 y0y_0 的质因数,根据乘法原理,一共有 2cnt2^{cnt} 种情况。

上面的情况只是当 tt 唯一的时候,所以 t∈[P,Q]t\in[P,Q] 时,一共是 Q−P+1Q-P+1 种取值。所以答案为 2cnt⋅(Q−P+1)2^{cnt}\cdot(Q-P+1)。

我们使用线性筛预处理出 [1,100000000][1, 100000000] 中所有的质数,采用试除法,查找出 kk 的质因数种类个数,然后求的答案即可。

本题的空间可能有一点小,需要使用 bitset 进行优化。

附代码:

C++
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
using ll = long long;
const int mod = 1e9 + 7;
const int sz = 1e8 + 10;
ll qpow(ll base, ll exp) {
    ll ans = 1;
    while (exp) {
        if (exp & 1) ans = ans * base % mod;
        base = base * base % mod, exp >>= 1;
    }
    return ans;
}
std::bitset<sz> vis;
std::vector<ll> primes;
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    int n = 1e8;
    for (int i = 2; i <= n; i++) {
        if (!vis[i])
            primes.push_back(i);
        for (int j = 0; j < primes.size() && i * primes[j] <= n; j++) {
            vis[i * primes[j]] = 1;
            if (i % primes[j] == 0)
                break;
        }
    }
    while (t--) {
        ll k, p, q, ans = 0;
        std::cin >> k >> p >> q;
        if (k == 1) {
            std::cout << q - p + 1 << "\n";
            continue;
        }
        for (ll p: primes) {
            if (p * p > k) break;
            if (k % p == 0) {
                ans++;
                while (k % p == 0) k /= p;
            }
        }
        if (k > 1) ans++;
        std::cout << qpow(2, ans) * (q - p + 1) % mod << "\n";
    }
    return 0;
}
原文