AtCoder题解 —— AtCoder Beginner Contest 182 —— B - Almost GCD

本文介绍了一道算法题——AtCoder Beginner Contest 182 B题,目标是在给定的整数序列中找到拥有最大GCD-ness的整数,并提供了解题思路及AC参考代码。

题目相关

题目链接

AtCoder Beginner Contest 182 B 题,https://atcoder.jp/contests/abc182/tasks/abc182_b

Problem Statement

Given is an integer sequence A: A1,A2,A3,…,AN.
Let the GCD-ness of a positive integer k be the number of elements among A1,A2,A3,…,AN that are divisible by k.
Among the integers greater than or equal to 2, find the integer with the greatest GCD-ness. If there are multiple such integers, you may print any of them.

Input

Input is given from Standard Input in the following format:

N
A1 A2 A3 … AN

Output

Print an integer with the greatest GCD-ness among the integers greater than or equal to 2. If there are multiple such integers, any of them will be accepted.

Samples1

Sample Input 1

3
3 12 7

Sample Output 1

3

Explaination

Among 3, 12, and 7, two of them - 3 and 12 - are divisible by 3, so the GCD-ness of 3 is 2.
No integer greater than or equal to 2 has greater GCD-ness, so 3 is a correct answer.

Samples2

Sample Input 2

5
8 9 18 90 72

Sample Output 2

9

Explaination

In this case, the GCD-ness of 9 is 4.
2 and 3 also have the GCD-ness of 4, so you may also print 2 or 3.

Samples3

Sample Input 3

5
1000 1000 1000 1000 1000

Sample Output 3

1000

Constraints

  • 1≤N≤100
  • 2≤Ai≤1000
  • All values in input are integers.

题解报告

题目翻译

给定一个数列 A:A1, A2, ..., AN。找出这个数列中,数量最多的 k 个数的最大公约数,k\geq 2

样例数据分析

样例 1

根据样例 1,我们知道数列为 {3, 12, 7}。

当 k=2 的时候,{3, 12} 的公约数是 3,{3, 7} 互质没有,{12, 7} 互质没有。

当 k=3 的时候,{3, 12, 7} 互质。

因此样例的答案为 3。

样例 2

根据样例 2,我们知道数列为 {8 9 18 90 72}。

当 k=4 的时候,{8 18 90 72} 的公约数为 2;{9 18 90 72} 的公约数有 3 和 9。因此输出 2、3 或者 9 都是正确的。

样例 3

根据样例 3,我们知道数列为 {1000 1000 1000 1000 1000}。

当 k=5 的时候, {1000 1000 1000 1000 1000} 的公约数有 2、5、10、...、1000,因此输出任何一个都是对的。

题目分析

本题的核心在于理解 “Let the GCD-ness of a positive integer k be the number of elements among A1,A2,A3,…,AN that are divisible by k.”。

由于本题的数据 Ai 的范围是 [2, 1000],因此可以直接使用暴力枚举的方法。思路参考素数筛。基本的思路如下:

1、从 2 到 1000 中枚举每个数 i;

2、找出 i 是数量 A 中的公约数个数最大值。

3、输出这个最大值。

这样时间复杂度为 O(N)。

AC 参考代码

//https://atcoder.jp/contests/abc182/tasks/abc182_b
//B - Almost GCD
#include <bits/stdc++.h>

using namespace std;

const int MAXN=1e2+4;
int nums[MAXN];
int gcds[1002];

int main() {
    int n, maxx=0;
    cin>>n;
    for (int i=1; i<=n; i++) {
        cin>>nums[i];
        maxx=max(nums[i], maxx);
    }

    int zd_gcd=0;
    int ans;
    for (int i=2; i<=maxx; i++) {
        int tot=0;
        for (int j=1; j<=n; j++) {
            if (0==nums[j]%i) {
                tot++;
            }
        }
        if (tot>zd_gcd) {
            zd_gcd=tot;
            ans=i;
        }
    }

    cout<<ans<<"\n";

    return 0;
}

时间复杂度

时间复杂度为 O(N^2)。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值