Divisors of Two Integers

根据给定的包含两个正整数x和y所有因子(包括1和自身)的混合排序列表,需要恢复这两个原始的正整数。题目保证存在这样的x和y。输入包含因子数量n和n个因子,输出恢复后的x和y。解决方案是找到数列中的最大值y,并通过排序和标记y的因子来找出x。
摘要由CSDN通过智能技术生成

Recently you have received two positive integer numbers x and y. You forgot them, but you remembered a shuffled list containing all divisors of x (including 1 and x) and all divisors of y (including 1 and y). If dis a divisor of both numbers x and yat the same time, there are two occurrences of d
in the list.
For example, if x=4and y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2],[4,6,1,1,2,3,2] or [1,6,3,2,4,1,2].Your problem is to restore suitable positive integer numbers x and y that would yield the same list of divisors (possibly in different order).It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers x and y.
Input
The first line contains one integer n(2≤n≤128) — the number of divisors of x and y.The second line of the input contains n integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1041≤di≤104), where di is either divisor of xor divisor of y

. If a number is divisor of both numbers xxand yythen there are two copies of this number in the list.OutputPrint two positive integer numbers xxand y— such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
Example
Input
10
10 2 8 1 2 4 1 20 4 5
Output
20 8
思路:反向求数列的两个最小公倍数x和y,首先x和y肯定在这个数列中,并且y为其中最大值,那么,先排序,y的因子为能被y除尽并且如果相邻两个数相等,因子为其中一个,对其因子标记,剩下的数列的最大值为x。

代码:

#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
    int n,a[1005],i,j,x,y,b[1005];
    cin>>n;
    memset(b,0,sizeof(b));
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    x=a[n-1];
    y=0;
    for(i=0; i<n-1; i++)
    {
        if(x%a[i]==0&&a[i]!=a[i+1])
        {
            b[i]=1;
        }
    }
    for(i=n-2;i>=0;i--)
    {
        if(b[i]==0)
        {
            y=a[i];
            break;
        }
    }
    cout<<x<<" "<<y<<endl;
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值