zstu-4270同源数

来源:互联网 发布:cf显示网络异常win7 编辑:程序博客网 时间:2024/05/17 04:55

4270: 同源数

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 1377  Solved: 261

Description

如果x和y的质因子集合完全相同,那么我们就说他们是同源的。
比如说18 = 2 * 32,12 = 3 * 22

Input

本题有多组数据(组数 <= 555555)。
每组数据输入形如:
x y
x, y为整数(1 <= x, y <= 1e18)

Output

输出形入:
ans
如果x, y为同源数,那么ans为”Yes”, 不然为”No”.

Sample Input

18 122 3

Sample Output

YesNo

HINT

Source


解题思路:求出两个数的gcd,两个数除以gcd后得到的数,不断地和gcd求gcd,并除以求得gcd,看能否变为1


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <bitset>#include <stack>#include <map>#include <climits>#include <functional> using namespace std; #define LL long longconst int INF=0x3f3f3f3f; LL gcd(LL a,LL b){    if(a>b) swap(a,b);    while(b%a)    {        LL k=b%a;        b=a;        a=k;    }    return a;} int main(){    LL x,y;    while(~scanf("%lld%lld",&x,&y))    {        LL k=gcd(x,y);        x/=k,y/=k;        int flag=1;        while(x!=1)        {            int kk=gcd(x,k);            x/=kk;            if(kk==1) break;        }        while(y!=1)        {            int kk=gcd(y,k);            y/=kk;            if(kk==1) break;        }        if(x!=1||y!=1) printf("No\n");        else printf("Yes\n");    }    return 0;}

0 0
原创粉丝点击