P1029题解

· · 题解

思路

因为两个数的积等于它们最大公约数和它们最小公倍数的积,即:

P \times Q = x_0 \times y_0 \gcd(P,Q) = x_0 \operatorname{lcm}(P,Q) = y_0

1\sqrt[]{x_0 \times y_0} 枚举 P,逐个判断有没有满足第一个条件的 Q,再带着 PQ 判断一下能不能满足后面的两个条件。(注意:对于 PQ 两个数不同的情况,交换这两个数也一定能得到另一组解)

注:

solution

#include<bits/stdc++.h>
using namespace std;
int x,y,ans;
int main(){
    cin>>x>>y;
    if(x==y) ans--;
    y*=x;
    for(int i=1;i<=sqrt(y);i++){
        if(y%i==0&&__gcd(i,y/i)==x) ans+=2;
    }
    cout<<ans;
    return 0;
}