top1编程
← 返回题目
题解

[NOIP 2009 提高组] Hankson 的趣味题

1 条题解

  • 0
    @ 2026-10-21 1:37:16

    1. 解题思路

    由最小公倍数为b1可知x必须是b1因子,枚举因子对并分别检查gcd(x,a0)=a1以及lcm(x,b0)=b1。

    2. 坑点和注意事项

    输入组数≤2000、数≤2×10^9。枚举只需sqrt(b1),平方根因子不要重复统计;最小公倍数先除gcd再乘并用long long。

    3. 复杂度

    • 时间复杂度:O(t sqrt(B)+t D log B),D为约数数。
    • 空间复杂度:O(1)。
    /*
    思路:由最小公倍数为b1可知x必须是b1因子,枚举因子对并分别检查gcd(x,a0)=a1以及lcm(x,b0)=b1。
    关键规则与坑点:输入组数≤2000、数≤2×10^9。枚举只需sqrt(b1),平方根因子不要重复统计;最小公倍数先除gcd再乘并用long long。
    时间复杂度:O(t sqrt(B)+t D log B),D为约数数。空间复杂度:O(1)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    bool ok(long long x, long long a0, long long a1, long long b0, long long b1)
    {
        return gcd(x, a0) == a1 && x / gcd(x, b0) * b0 == b1;
    }
    int main()
    {
        int n;
        cin >> n;
        while (n--)
        {
            long long a0, a1, b0, b1;
            cin >> a0 >> a1 >> b0 >> b1;
            int ans = 0;
            for (long long d = 1; d * d <= b1; d++)
                if (b1 % d == 0)
                {
                    ans += ok(d, a0, a1, b0, b1);
                    if (d * d != b1)
                        ans += ok(b1 / d, a0, a1, b0, b1);
                }
            cout << ans << endl;
        }
    }
    
    • 1