作者 : Xia Xinyu
日期 : 2021-12-04原题链接
给定一个长度为 n 的整数序列 a1,a2,…,an。
请你从中选出尽可能多的数。
要求满足如下两个条件之一:
仅选择一个数;
选择至少两个数,且所选择的数的最大公约数大于 1;
输出选出数的最大可能数量。
输入格式
第一行包含整数 n。
第二行包含 n 个整数 a1,a2,…,an。
输出格式
一个整数,表示选出数的最大可能数量。
数据范围
前 6 个测试点满足 1≤n≤10。
所有测试点满足 1≤n≤105,1≤ai≤105。
输入样例1:
3
2 3 4
输出样例1:
2
输入样例2:
5
2 3 4 6 7
输出样例2:
3
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final int N = 100010;
int[] cnt = new int[N];
int n = in.nextInt();
for(int i = 0;i < n;i ++){
int x = in.nextInt();
cnt[x] ++;
}
int res = 1;
for(int d = 2;d < N;d ++){
int s = 0;
for(int j = d;j < N;j += d)
s += cnt[j];
res = Math.max(res, s);
}
System.out.println(res);
}
}
时间复杂度
:O(n)
空间复杂度
:O(1)