ZSTUOJ 4270: 同源数

4270: 同源数

Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 1356 Solved: 249
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 12
2 3
Sample Output
Yes
No

题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4270

题解:

令k=gcd(x, y),让x不断除以k且不断计算k==1?,如果k==1且a==1时,表明x和y的质因子相同,是同源数,否则输出No。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<sstream>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b){
    return b == 0 ? a:gcd(b, a%b);
}

bool fun(ll a, ll b){
    ll k = gcd(a, b);
    while(1){
        if(k == 1){
            if(a == 1) return true;
            else return false;
        }
        a /= k;
        k = gcd(a, b);
    }
    return false;
}

int main(){
    ll x, y;
    while(scanf("%lld%lld", &x, &y) == 2){
        if(fun(x, y)) printf("Yes\n");
        else printf("No\n");
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值