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

Input
The first line of the input file contains an integer
n
(


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;
}