[Codeforces Round511C] Enlarge GCD
[题目链接]
https://codeforces.com/contest/1047/problem/C
[算法]
首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数中选出一个数集 , 使得这个数集中的数最大公约数不为1 , 最大化数集大小
预处理Ai范围内的质数 , 然后对于每个数分解质因数即可
时间复杂度 : O(NlogN + N log V)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + ;
const int MAXP = 1.5e7 + ; int n,tot;
int a[MAXN],prime[MAXP],f[MAXP],cnt[MAXP]; template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline int gcd(int x,int y)
{
if (y == ) return x;
else return gcd(y,x % y);
} int main()
{ read(n);
for (int i = ; i <= n; i++) read(a[i]);
int g = a[];
for (int i = ; i <= n; i++) g = gcd(g,a[i]);
for (int i = ; i <= n; i++) a[i] /= g;
for (int i = ; i < MAXP; i++)
{
if (!f[i])
{
f[i] = i;
prime[++tot] = i;
}
for (int j = ; j <= tot; j++)
{
int tmp = i * prime[j];
if (tmp > MAXP) break;
f[tmp] = prime[j];
if (prime[j] == f[i]) break;
}
}
int ans = -;
for (int i = ; i <= n; i++)
{
int pre = - , tmp = a[i];
while (tmp != )
{
if (f[tmp] == pre)
{
tmp /= f[tmp];
continue;
}
cnt[f[tmp]]++;
if (cnt[f[tmp]] > ans) ans = cnt[f[tmp]];
pre = f[tmp];
tmp /= f[tmp];
}
}
if (ans == -) printf("-1\n");
else printf("%d\n",n - ans); return ; }
[Codeforces Round511C] Enlarge GCD的更多相关文章
- CodeForces 1047C Enlarge GCD(数论)题解
题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k 思路:先求出k,然后每个数除以k.然后找出出现次数最多的质因数即可. 代码: #include<cmath> ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- CodeFroces-- 511div2 C. Enlarge GCD
题目链接:C. Enlarge GCD 给你一个序列 删除一些数看可以让他们之间的gcd变大如果可以输出删除数量最小的个数 先求出共同 gcd 然后除去 找出出现最多的质数 然后减去就可以了 #inc ...
- 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 integ ...
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- 【Codeforces 1034A】Enlarge GCD
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设原来n个数字的gcd为g 减少某些数字之后 新的gcd肯定是g的倍数 即gx 我们可以枚举这个x值(x>=2) 看看原来的数字里面有多 ...
- 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 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
随机推荐
- 利用端口转发来访问virtualbox虚拟机centos6的jupyter notebook
1.除了在virtualbox中设置常规的端口转发外,还需要在windows上打开cmd,输入ssh -N -f -L localhost:8888:localhost:8889 -p 22 root ...
- 把wav文件等时长切割
ffmpeg -i somefile.mp3 -f segment -segment_time 1800 -c copy out%03d.mp3 segment_time 是切割时长,单位秒
- win7 x64安装glpk
下载glpk,下载地址:http://ftp.gnu.org/gnu/glpk/
- Windows系列原版系统镜像下载
原版系统镜像下载 Windows 10 系统 Windows 10 企业版 1511版 (64位) Windows 10 Enterprise, Version 1511 (x64) – DVD (C ...
- 关于Python构建微服务的思考(一)
一:什么是微服务? 微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成. 系统中的各个微服务可被独立部署,各个微服务之间是松耦合的. 每个微服务仅关注于完成一件任务并很好地完成该任务. ...
- js 列表几种循环的比较
数组 遍历 普通遍历 最简单的一种,也是使用频率最高的一种. let arr = ['a', 'b', 'c', 'd', 'e'] for (let i = 0; i < arr.length ...
- java 反射---学习笔记
一.java的动态性 反射机制 动态编译 动态执行JavaScript代码 动态字节码操作 二.动态语言 动态语言 程序运行时,可以改变程序结构或变量类型,典型的语言:python.ruby.java ...
- 标准sqlserver连接语句
sqlserver左右全内连接 原始链接http://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html 连接条件可在FR ...
- bzoj3304[Shoi2005]带限制的最长公共子序列 DP
题意:给出三个序列,求出前两个的公共子序列,且包含第三个序列,要求长度最长. 这道题目怎么做呢,f[i][j]表示a串1-i,b串1-j的最长,g[i][j]表示a串i-n,b串j-m最长, 那么只需 ...
- Jackson 字符串转List<Map>
String a = "[{\"id\":27,\"text\":\"网络\"},{\"id\":32,\&q ...