4270: 同源数
Time Limit: 3 Sec Memory Limit:128 MBSubmit: 743 Solved:99
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
YesNo
#include <cstdio> #include <cstdlib> #include <iostream> #include <set> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <cctype> #include <queue> #include <map> using namespace std; typedef long long ll; ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } int main() { ll x,y; while(~scanf("%lld%lld",&x,&y)) { int a=0; ll d=gcd(x,y),g; while(x>1) { g=gcd(x,d); x/=g; if(x==1) {a++;break;} if(g==1) break; } while(y>1) { g=gcd(y,d); y/=g; if(y==1) {a++;break;} if(g==1) break; } if(a==2) printf("Yes\n"); else printf("No\n"); } return 0; } //令 d=gcd(x,y),将x和y不断与d进行约分。 //若此时x=y=1,那么说明其质因子集合相同。