B. Divisors of Two Integers
Posted studyshare777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B. Divisors of Two Integers相关的知识,希望对你有一定的参考价值。
Recently you have received two positive integer numbers xx and y. You forgot them, but you remembered a shuffled list containing all divisors of xx (including 11 and xx) and all divisors of y (including 11 and y). If d is a divisor of both numbers xx and y at the same time, there are two occurrences of d in the list.
For example, if x=4x=4 and y=6y=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.
It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers xx and y.
Input
The first line contains one integer n (2≤n≤1282≤n≤128) — the number of divisors of xx 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 xx or divisor of y. If a number is divisor of both numbers xx and y then there are two copies of this number in the list.
Output
Print two positive integer numbers xx and 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
题目描述:
给出一个集合,它是从1~x可以整除x的数集合,和1~y可以整除y的数的集合,合并起来乱序的集合。找出x和y。
分析:
因为它给出了可以包含x和y嘛,那么最大的应该就是y了,关键找到x。如果y不是x的倍数,只要找出最大的不能被y整除的数就是x。而如果x能整除y,那x的除数集合就一定包含在y的集合里面,我们只要找出重复的数中最大的那个就是x了。
代码:
#include <iostream> #include <algorithm> #include <cstring> using namespace std; int main() { int n; cin>>n; int a[n+6]; bool used[n+6]; memset(used,0,sizeof(used)); for(int i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n); int m=a[n-1]; int k=-1; for(int i=0;i<n-1;i++) { if(!used[i]&&m%a[i]==0) { used[i]=true; } else { k=i; } } if(k!=-1) printf("%d %d ",m,a[k]); else { for(int i=n-1;i>=0;i--) { if(a[i]==a[i-1]) { printf("%d %d ",m,a[i]); break; } } } return 0; }
以上是关于B. Divisors of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章
[Project Euler 429] Sum of squares of unitary divisors(数论)
SPOJ:Divisors of factorial (hard) (唯一分解&优化)