题目链接:P1072 [NOIP2009 提高组] Hankson 的趣味题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
input:
2
41 1 96 288
95 1 37 1776
output:
6
2
题解:这道题的关键就在于给定的两个x满足的条件
首先, 条件1没有什么好说的,就是 gcd(x, a0) == a1
条件2是重点!!我们知道x和b0的最小公倍数是b1,可以推出x一定是b1的因数,即 b1%x == 0,由此可以得到x的搜索范围,只需要验证所有 1 <= x <= sqrt(b1),并验证该范围内的所有b1/x(二者都是b1的因子),并注意满足 b1%x == 0
最后,我们易知 x * y == lcm(x, y) * gcd(x, y) => gcd(x, y) == x * y / lcm(x ,y),代入该题,可化为 gcd(x ,b0) == x*b0/b1
循环判断即可,注意判断一下当 x == b1/x 时计数器只加一次
下面贴出代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a0,a1,b0,b1;
int t;
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>a0>>a1>>b0>>b1;
int ans=0;
for(ll x=1;x<=sqrt(b1);x++)
{
if(b1%x!=0)
{
continue;
}
if(__gcd(x,a0)==a1&&__gcd(x,b0)==x*b0/b1)
{
++ans;
}
if(x==b1/x)
{
continue;
}
if(__gcd((b1/x),a0)==a1&&__gcd((b1/x),b0)==(b0/x))
{
++ans;
}
}
cout<<ans<<endl;
}
return 0;
}