最大公因子(辗转相除法)递归版

该问题要求通过两个老鼠的ID确定它们是否有共同祖先。ID是基于其父母ID乘积生成的,具有最小质因数。编写一个程序,根据输入的两个ID判断它们是否共享一个祖先,这可以通过计算最大公因子实现。如果公因子不为1,则表示有共同祖先,输出'Sim',否则输出'Nao'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

♬ Caloventor tiene miedo… ♬
Benedetto, Nathan
As is well known by any cultured person, rats are the smartest beings on earth. Followed directly by dolphins.

MaratonIME knows about the species hierarchy and uses this knowledge in it’s regard. Usually, when they need some resource, they know it’s always useful to have a smart rat available. Unfortunately, rats are not very fond of us, primates, and will only help us if they owe us some favour.

With that in mind, MaratonIME decided to help a little rat called Pablito. Pablito is studying rat’s genealogy, to help with cloning and genetic mapping. luckily, the way rats identify themselves make the job much easier.

The rat society is, historically, matriarchal. At first, there were little families, each of which had it’s own leading matriarch. At that time, it was decided that rats would organize themselves according to the following rules:

Each martiarch had an id number greater than one.
Each of these ids were chosen in a way such that they would have the least amount of divisors possible.
Each family member had the same id as the matriarch.
The id of any newborn rat would be the product of its parents id’s.
For instance, the offspring of a rat with id 6 and another with id 7 is 42.

Pablito needs to know if two given rats have a common ancestor, but his only tool is the id number of each of the two rats, which is always a positive integer greater than 1 with no more than 16 digits. Can you help him?

Create a program that decides if a pair of rats have some common ancestor.

Input
The input begins with a positive integer t ≤ 105, the number of test cases.

After that, follows t lines, each with two integers ai e bi identifying two rats.

Every rat’s id is a positive integer greater than 1 and with no more than 16 digits.

Output
For each test case, print “Sim” if the rats ai and bi share a common ancestor and “Nao” otherwise.

Example
Input
2
2 4
3 5
Output
Sim
Nao
思路:题意是说有公因子就是有公共祖先,直接求最大公因子就可以,没有的话这个算法返回的是1;大于10^5的整形数据要用Long long int

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
long long int f(long long int a,long long int b)
{
    if(a%b==0)return b;
    else return f(b,a%b);
}
int main()
{
   long long  int t;
    scanf("%lld",&t);
    while(t--)
    {
       long long int a,b;
        scanf("%lld%lld",&a,&b);
       long long int m=f(a,b);
        if(m==1)printf("Nao\n");
        else printf("Sim\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值