Nastya Studies Informatics
题目
Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.
题意
给你4个数l,r,x,y;x,y,分别代表的是最大公约数和最小公倍数题目让你求在l到r之间有多少对满足最大公约数是x最小公倍数是y的数目,(a,b)和(b,a)表示不同对,只有a=b时候才表示一对
题解
我们可以先设两个函数 A和B ,A和B可以分解为 A=xn,B=xm;然后我们根据最小共倍数与最大公约数之间的关系我们可得nm=y/x;然后我们就可以枚举1到根号nm的数字只要她满足其规律即可。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
if(b==0)return a;
else return gcd(b,a%b);
}
int main()
{
int l,r,x,y;
int tem;int ans=0;
cin.tie(0);
ios::sync_with_stdio(0);
cout.tie(0);
cin>>l>>r>>x>>y;
tem=y/x;
if(y%x){cout<<"0"<<endl;return 0;}
for(int i=1;i*i<=tem;i++)
{
int j=tem/i;
if(tem%i==0&&gcd(i,j)==1&&i*x>=l&&i*x<=r&&j*x>=l&&j*x<=r)
ans+=i==j?1:2;
}
cout<<ans<<endl;
}