C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】
题目:
Mr. F has nn 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.
The first line contains an integer nn (2≤n≤3⋅10^5) — the number of integers Mr. F has.
The second line contains nn integers, a1,a2,…,an (1≤ai≤1.5⋅10^7).
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).
3
1 2 4
1
4
6 9 15 30
2
3
1 1 1
-1
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个数,这N个数会有一个最大公约数G,那么需要去掉K个数,使余下的N-K个数的最大公约数变大。求最小K。
我们从gcd的原理分析,这N个数都除以gcd后,余下的数的最大公约数无法变大时因为不存在公因子,所以我们需要对这N个数进行分类,分类的标准就是是否还有共同的公因子,然后找出包含的数目最多的类别,假设这个类别有M个数。
那么N-M就是我们最终的结果。
这里需要注意的技巧是,在进行类别划分的时候,我们用的素数打表的原理。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 3e5+5;
const int MAX = 1.5e7+5;
int A[MAXN], cnt[MAX];
bool visit[MAX]; int gcd(int a, int b)
{
return b==0?a:gcd(b, a%b);
} int Max(const int a, const int b)
{
return a>b?a:b;
} int main()
{
int N;
while(~scanf("%d", &N))
{
int G, ans;
scanf("%d", &A[0]);
G = A[0];
for(int i = 1; i < N; i++)
{
scanf("%d", &A[i]);
G = gcd(G, A[i]);
} memset(cnt, 0, sizeof(cnt));
memset(visit, true, sizeof(visit)); for(int i = 0; i < N; i++)
cnt[A[i]/G]++; visit[0] = visit[1] = false;
ans = 0;
for(int i = 2; i < MAX; i++)
{
int res = cnt[i];
if(visit[i])
{
for(int j = 2*i; j < MAX; j+=i)
{
visit[j] = false;
res += cnt[j];
}
}
ans = Max(ans, res);
}
printf("%d\n", ans==0?-1:N-ans);
}
return 0;
}
C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】的更多相关文章
- Codeforces Round #511 (Div. 2)
Codeforces Round #511 (Div. 2) #include <bits/stdc++.h> using namespace std; int n; int main() ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- 2018.9.21 Codeforces Round #511(Div.2)
只写了AB,甚至还WA了一次A题,暴露了蒟蒻的本质=.= 感觉考的时候有好多正确或和正解有关的思路,但是就想不出具体的解法或者想的不够深(长)(怕不是过于鶸) 话说CF的E题怎么都这么清奇=.= A. ...
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)
题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...
- Codeforces Round #511 (Div. 2) C. Enlarge GCD
题目链接 题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点. 我交了题后,疯狂CE,我以为爆内存,结果是,我对全局数组赋值, 如果直接赋值,会直接在exe内产生内存,否 ...
- Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)
题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...
- Codeforces Round #511 Div.1 A Div.2 C
嗯切一题走人很开心. gzy-50分比我还惨. 题意:有n个数,去掉尽量少的数使得剩下数的gcd变大. 首先把这n个数都除以gcd,就变成了去掉尽量少的数使得gcd不等于1. 可以枚举一个质数,然后统 ...
- B. Cover Points Codeforces Round #511 (Div. 2)【数学】
题目: There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need t ...
随机推荐
- Mybatis和Hibernate比较
作者:乌拉拉链接:http://www.zhihu.com/question/21104468/answer/58579295来源:知乎著作权归作者所有,转载请联系作者获得授权. 1.开发对比开发速度 ...
- 新浪SAE高级开发者认证通过
如题,新浪SAE高级开发者认证通过,申请的方式为提交开源项目地址,用的是如下的项目 http://jqext.sinaapp.com/ 之前该项目是部署在 mopaas 上的,在拿到高级开发者资格后迁 ...
- ps怎么修改gif动图播放速度
ps怎么修改gif动图播放速度 摘自:https://jingyan.baidu.com/article/7e44095302bbdc2fc0e2efad.html photoshop功能很强大,不仅 ...
- Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案
1.配置web.config <system.web> <sessionState mode="StateServer" stateConnectionStrin ...
- Hyper-V和vmware在虚拟机中安装xen总结
1. Hyper-V 在hyper-v中安装了ubuntu13.04,运行很好,使用起来的效果感觉比vmware要舒服.安装变异xen的内核也没有问题,可以正常的安装,update-grub之后也可以 ...
- XE ListBox实现伸缩效果
功能:实现年月日压缩,初始化时item是所有年,点击年展开月,点击月展开天,再点击则收缩. 思路:实际上一开始是将所有item显示,只是将月日的item.height赋值为0, 记录每一行的it ...
- 301 MovedPermanently 重定向
页面永久性移走(301重定向)是一种非常重要的“自动转向”技术. 301重定向可促进搜索引擎优化效果 从搜索引擎优化角度出发,301重定向是网址重定向最为可行的一种办法.当网站的域名发生变更后,搜索引 ...
- 注册 asp.net IIS
C:\Windows\Microsoft.NET\Framework64\v4.0.30319/aspnet_regiis.exe -i
- oracle 中用法dual
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情. dual是一个虚拟表,用来构成select的语法规则,oracle保证d ...
- 建造者(Builder)模式 *
一. 建造者(Builder)模式 建造者模式可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 二. Builder模式的结构: 建造者(Bu ...