A. Enlarge GCD
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Mr. F has nn positive integers, a1,a2,…,ana1,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 nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of integers Mr. F has.
The second line contains nn integers, a1,a2,…,ana1,a2,…,an (1≤ai≤1.5⋅1071≤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
Copy
3
1 2 4
output
Copy
1
input
Copy
4
6 9 15 30
output
Copy
2
input
Copy
3
1 1 1
output
Copy
-1
Note
In the first example, the greatest common divisor is 11 in the beginning. You can remove 11 so that the greatest common divisor is enlarged to 22. The answer is 11.
In the second example, the greatest common divisor is 33 in the beginning. You can remove 66 and 99 so that the greatest common divisor is enlarged to 1515. There is no solution which removes only one integer. So the answer is 22.
In the third example, there is no solution to enlarge the greatest common divisor. So the answer is −1
本来的想法是通过把每个数的因子都拆掉这样的话找出因子出现次数第二多的数T,那么答案就是Ans=n-t
然而虽然只有300000个数但是数的大小却有 1.5*10的7次方 要是每个数都很大那就绝对爆炸啊
真正的题解是:找出n个数的gcd 然后只要找大于这个gcd的ans_gcd能被n个数整除次数最多的就好了(想到这我一开始绝对可能会TLE因为O(n*n)复杂度太高 但是关键是 比如找能被2整除的那么 一个数如果因子有 4 或者 6 或者 8那么他的因子只要规定是2就好了)
#include <stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
using namespace std;
int a[20000001];
int need[20000001];
int main(int argc, char *argv[])
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
int i;
int min1=INF;
int maxn=-1;
for(i=0;i<n;i++)
{
int t;
scanf("%d",&t);
a[t]++;
maxn=max(maxn,t);
if(i==0)
{
min1=t;
}
else
{
min1=__gcd(min1,t);
}
}
//printf("%d\n",min1);
memset(need,0,sizeof(need));
int start=min1+1;
int ans=INF;
for(i=start;i<=maxn;i++)
{
if(!need[i])
{
int tot=0;
int j;
for(j=i;j<=maxn;j+=i)
{
need[j]=1;
//printf("%d\n",j);
tot+=a[j];
}
//printf("%d %d\n",tot,i);
ans=min(ans,(n-tot));
}
}
if(ans==INF||ans==n)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}