A L C 温馨提示 : 抄代码是不好的习惯 q a q \Huge\color{red} ALC 温馨提示:抄代码是不好的习惯 qaq ALC温馨提示:抄代码是不好的习惯qaq

输入两个正整数 x 0 x _0 x0​, y 0 y _0 y0 ,求出满足下列条件的 P , Q P,Q P,Q 的个数:

  • P , Q P,Q P,Q 是正整数。

  • 要求 P,Q 以 x 0 x _0 x0为最大公约数,以 y 0 y_0 y0为最小公倍数。
    试求:满足条件的所有可能的 P , Q P,Q P,Q 的个数。

话不多说,直接上代码展示

#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
int gcd(int a, int b); // 定义最大公因数函数
int lcm(int a, int b); // 定义最小公倍数函数
int main() {
	int x, y;
	scanf("%d %d", &x, &y); // 输入 x, y
	for(int i = x; i <= y; i++) { // 枚举
		int j = x * y / i;
		if(gcd(i, j) == x && lcm(i, j) == y) {
			cnt++;
		}
	}
	printf("%d", cnt);
	return 0;
}
int gcd(int a, int b) {
	if(b == 0) return a;
	return gcd(b, a % b); // 辗转相除法
}
int lcm(int a, int b) {
	return (a * b / gcd(a, b)); // a × b ÷ gcd(a, b);
}

A C 记录 AC 记录 AC记录

Logo

集算法之大成!助力oier实现梦想!

更多推荐