题目链接:10892 - LCM Cardinality
题意:给定一个数L,求最大公倍数是L的组数。
思路:先把L分解因子,然后答案肯定由两个因子组成,由于因子数量不会很多,暴力枚举即可
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
const int N = 100005;
int n, yin[N], yinn, i, j;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
int main() {
while (~scanf("%d", &n) && n) {
yinn = 0;
for (i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
yin[yinn++] = i;
if (n / i != i)
yin[yinn++] = n / i;
}
}
int ans = 0;
for (i = 0; i < yinn; i++)
for (j = i; j < yinn; j++) {
if (lcm(yin[i], yin[j]) == n)
ans++;
}
printf("%d %d\n", n, ans);
}
return 0;
}