4270: 同源数
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1284 Solved: 224
Description
如果x和y的质因子集合完全相同,那么我们就说他们是同源的。
比如说18 = 2 * 3
2,12 = 3 * 2
2;
Input
本题有多组数据(组数 <= 555555)。
每组数据输入形如:
x y
x, y为整数(1 <= x, y <= 1e18)
Output
输出形入:
ans
如果x, y为同源数,那么ans为”Yes”, 不然为”No”.
Sample Input
18 12 2 3
Sample Output
Yes No
——————————————————————————————
判断两个数是否质因子相同。我们不难想到如果两个数的质因子和他们的最大公约数相同,那么他们肯定是同源数,所以求出GCD不断算公约数即可
#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define inf 0x3f3f3f3f
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
long long x,y;
while(~scanf("%lld%lld",&x,&y))
{
long long t=gcd(x,y);
x/=t;
y/=t;
long long a;
while(x!=1)
{
a=gcd(t,x);
if(a==1) break;
x/=a;
}
while(y!=1)
{
a=gcd(t,y);
if(a==1) break;
y/=a;
}
if(x!=1||y!=1)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}