题目来源:https://www.luogu.org/problem/show?pid=1072
这题暴力枚举就可以过。
由(x,a0)=a1,[x,b0]=b1
推出a1|x,x|b1,(x/a1,a0/a1)=1,(b1/x,b1/b0)=1。
枚举x。
代码:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
int _;
scanf("%d", &_);
for (int k = 1; k <= _; k++) {
int a0, a1, b0, b1;
scanf("%d%d%d%d", &a0, &a1, &b0, &b1);
if (a0 % a1 != 0 || b1 % b0 != 0) {
printf("0\n");
continue;
}
int tot = 0;
for (int x = 1; x * x <= b1; x++) {
if (b1 % x == 0) {
if ((x % a1 == 0 && gcd(x / a1, a0 / a1) == 1) && (gcd(b1 / x, b1 / b0) == 1))tot++;
if (x * x == b1)continue;
int t = b1 / x;
if (t % a1 == 0 && (gcd(t / a1, a0 / a1) == 1) && (gcd(b1 / t, b1 / b0) == 1))tot++;
}
}
printf("%d\n", tot);
}
return 0;
}