499. Greatest Greatest Common Divisor
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
Memory limit: 262144 kilobytes
input: standard
output: standard
output: standard
Andrew has just made a breakthrough in sociology: he realized how to predict whether two persons will be good friends or not. It turns out that each person has an inner friendship number (a positive integer). And the quality of friendship between two persons is equal to the greatest common divisor of their friendship number. That means there are prime people (with a prime friendship number) who just can't find a good friend, and
Wait, this is irrelevant to this problem. You are given a list of friendship numbers for several people. Find the highest possible quality of friendship among all pairs of given people. Input
The first line of the input file contains an integer n (
) — the number of people to process. The next n lines contain one integer each, between 1 and
(inclusive), the friendship numbers of the given people. All given friendship numbers are distinct. Output
Output one integer — the highest possible quality of friendship. In other words, output the greatest greatest common divisor among all pairs of given friendship numbers. Example(s)
sample input | sample output |
4 9 15 25 16 | 5 |
#include <bits/stdc++.h>
using namespace std;
int v[1000005];
int ans=1;
void f(int x){
if(v[x]>=2){
ans=ans>x?ans:x;
}
}
void solve(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int a;
scanf("%d",&a);
for(int i=1;i*i<=a;i++){
if(i*i==a){
v[i]++;
f(i);
}
else{
if(a%i==0){
v[a/i]++;
v[i]++;
f(i);
f(a/i);
}
}
}
}
printf("%d",ans);
return;
}
int main(){
solve();
return 0;
}

本文介绍了一个计算问题,即如何找出一组正整数中任意两个数的最大公约数的最大值。通过输入一系列人的友谊编号,利用质因数分解和哈希表记录每个质因数出现的次数来解决此问题。

495

被折叠的 条评论
为什么被折叠?



