[题目链接]

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的更多相关文章

  1. CodeForces 1047C Enlarge GCD(数论)题解

    题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k 思路:先求出k,然后每个数除以k.然后找出出现次数最多的质因数即可. 代码: #include<cmath> ...

  2. Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)

    C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...

  3. CodeFroces-- 511div2 C. Enlarge GCD

    题目链接:C. Enlarge GCD 给你一个序列 删除一些数看可以让他们之间的gcd变大如果可以输出删除数量最小的个数 先求出共同 gcd 然后除去 找出出现最多的质数 然后减去就可以了 #inc ...

  4. 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 ...

  5. Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

    传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...

  6. 【Codeforces 1034A】Enlarge GCD

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设原来n个数字的gcd为g 减少某些数字之后 新的gcd肯定是g的倍数 即gx 我们可以枚举这个x值(x>=2) 看看原来的数字里面有多 ...

  7. Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

    题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...

  8. Codeforces Round #511 (Div. 2) C. Enlarge GCD

    题目链接 题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点. 我交了题后,疯狂CE,我以为爆内存,结果是,我对全局数组赋值, 如果直接赋值,会直接在exe内产生内存,否 ...

  9. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

随机推荐

  1. Bash的循环结构(for和while)

    在bash有三中类型的循环结构表达方法:for,while,until.这里介绍常用的两种:for和while. for bash的for循环表达式和python的for循环表达式风格很像: for ...

  2. JS 实现全屏预览 F11功能

    老是不通过,没办法,只能是重新发布了,反正我就是杠上了,大大小小写过很多前端特效,当然也经常在网上copy或者修改人家的代码,我觉得也挺好的,为什么?!因为我想这样,你能怎么办,打我?少废话,直接上代 ...

  3. <struct、union、enum>差异

    关于C++和C的区别 区别最大的是struct,C++中的struct几乎和class一样了,可以有成员函数,而C中的struct只能包含成员变量. enum,union没区别. struct的定义 ...

  4. poj2325 大数除法+贪心

    将输入的大数除以9 无法整除再除以 8,7,6,..2,如果可以整除就将除数记录,将商作为除数继续除9,8,...,3,2. 最后如果商为1 证明可以除尽 将被除过的数从小到大输出即可 #includ ...

  5. AutoMapper的使用在NET core中的使用记录

    关于AutoMapper的用处以及主要便利指出请参考官方文档,本文章仅记录自己使用的过程及代码: 安装 在项目中涉及到的类包括:Account,AccountCondition,AutoMapperC ...

  6. 扫描局域网内所有主机和MAC地址的Shell脚本

    #!/bin/bash #author: InBi #date: 2011-08-16 #website: http://www.itwhy.org/2011/08-20/939.html ##### ...

  7. 序列终结者(bzoj 1521)

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

  8. transaction transaction transaction 最大费用最大流转化到SPFA最长路

    //当时比赛的时候没有想到可以用SPFA做,TLE! Problem Description Kelukin is a businessman. Every day, he travels aroun ...

  9. Binary Tree Preorder Traversal (非递归实现)

    具体思路参见:二叉树的非递归遍历(转) 先序遍历(根左右). 即把每一个节点当做根节点来对待. /** * Definition for binary tree * struct TreeNode { ...

  10. html上传图片类型

    <html>  <head>  <meta charset="utf-8">  <title>上传图片</title> ...