题目情境#
题目链接:https://www.luogu.com.cn/problem/P1029 ↗
题目描述
输入两个正整数 ,求出满足下列条件的 的个数:
-
是正整数。
-
要求 以 为最大公约数,以 为最小公倍数。
试求:满足条件的所有可能的 的个数。
输入格式
一行两个正整数 。
输出格式
一行一个数,表示求出满足条件的 的个数。
输入 #1
3 60
plaintext输出 #1
4
plaintext对于 的数据,。
算法解析:#
利用性质 :令 ,枚举 满足 且 ,则 为一组解,对称计数加 2; 时去掉重复的一次。复杂度 。
Python代码实现#
# P1029 [NOIP 2001 普及组] 最大公约数和最小公倍数问题
import math
x, y = map(int, input().split())
t = x * y
ans = 0
for i in range(1, int(t ** 0.5) + 1):
if t % i == 0 and math.gcd(t // i, i) == x:
ans += 2
if x == y:
ans -= 1
print(ans)pythonC++代码实现#
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL x,y,ans;
LL gcd(LL a, LL b){
return b==0 ? a : gcd(b,a%b);
}
int main(){
cin>>x>>y;
LL t = x*y;
for(LL i=1; i*i<=t; i++)
if(t%i==0 && gcd(i,t/i)==x)
ans += 2;
if(x==y) ans--;
cout << ans;
return 0;
}c