C. Enlarge GCD -codeforces1047

C. Enlarge GCD -codeforces1047

Mr. F has n positive integers, a1,a2,…,an.

He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.

But this problem is too simple for him, so he does not want to do it by himself. If you help him, he will give you some scores in reward.

Your task is to calculate the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.

Input
The first line contains an integer n(2≤n≤3⋅105) — the number of integers Mr. F has.

The second line contains n integers, a1,a2,…,an (i≤ai≤1.5⋅107).

Output
Print an integer — the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.

You should not remove all of the integers.

If there is no solution, print “1” (without quotes).

Examples
input
3
1 2 4
output
1
input
4
6 9 15 30
output
2
input
3
1 1 1
output
1
Note
In the first example, the greatest common divisor is 1 in the beginning. You can remove 1 so that the greatest common divisor is enlarged to 2. The answer is 1.

In the second example, the greatest common divisor is 3 in the beginning. You can remove 6 and 9 so that the greatest common divisor is enlarged to 15. There is no solution which removes only one integer. So the answer is 2.

In the third example, there is no solution to enlarge the greatest common divisor. So the answer is 1.

  • 题意:给你长度为n的数组,这个数组的gcd为m,输出能够使m变大的情况下至少要删除多少个数,如果全部删除的话,就为没有答案,输出-1

  • 题解:看完别人的代码才会的(O(∩_∩)O哈哈~)。一种是直接素数筛暴力枚举,很不可思议,开的是1.5*10710^7107方的数组,首先把所有的数都除以m,然后在数组s1中记录下这个位置的值有多少个。最后开始从2开始筛出那些是这些数倍数的共有几(t)个,找出最大的那个。答案便为n-t。

#include<bits/stdc++.h>
using namespace std;
const int inf=1.5e7+10,INF = 3e5+10;
int s1[inf],s2[inf],sn[INF],n,gkd;
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",sn+i),gkd=__gcd(sn[i],gkd);
    for(int i=1;i<=n;i++)s1[sn[i]/gkd]++;
    int t=0;
    for(int i=2;i<inf;i++){
        if(!s2[i]){
            int h=0;
            for(int j=i;j<inf;j+=i)
                h+=s1[j],s2[j]=1;
            t=max(t,h);
        }
    }
    printf("%d\n",t?n-t:-1);
}

posted @ 2018-09-22 18:04 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值