【题解】【Hankson 的趣味题】

【题解】【Hankson 的趣味题】

Hankson 的趣味题

既然b1是x的倍数,可以考虑求出b1的所有正约数,并对于每个约数检验gcd和lcm,求正约数复杂度是O(sqrt(b1))的,还有一个优化是在求gcd和lcm之前先判断x是否是a1的倍数

Ps:这道题踩的坑是自己一开始在循环中直接让x=b,每轮循环x+=b,但这样会漏掉(b1/x)的解

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read()
{
	register int x=0,w=1;
	register char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-') {ch=getchar();w=-1;	}
	while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();	}
	return x*w;
}
int n,a,b,c,d;
inline int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}
inline int lcm(int x,int y)
{
	return x/gcd(x,y)*y;
}
bool check(int x)
{
	if(gcd(x,a)!=b) return 0;
	if(lcm(x,c)!=d) return 0;
	return 1;
}
signed main()
{
    n=read();
    while(n--)
    {
    	int ans=0;
    	a=read();b=read();c=read();
    	d=read();
    	for(int x=1;x*x<=d;x++)
    	{
     		if(d%x) continue;	
    		if(check(x)) ans++;
    		if(x!=d/x&&d/x%b==0&&check(d/x)) ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2021-08-17 07:57  glq_C  阅读(76)  评论(0)    收藏  举报