sgu 499. Greatest Greatest Common Divisor 因数打表

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

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

499. Greatest Greatest Common Divisor
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
input: 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, andWait, 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值