传送门

[http://codeforces.com/contest/1047/problem/C]

题意

给你n个数,移除最少的数字使剩下的数字GCD大于初始GCD

思路

需要一点暴力的技巧,先求出初始GCD为g,并统计每个数字的个数这是减少复杂度的关键,令ans=0,我们从i=g+1开始枚举GCD为i的个数,进行统计每次更新ans=min(ans,n-cnt)

需要注意的是某个数的因子依然是那个数倍数的因子,如2 4 8.这样可以避免重复统计。具体看代码

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1.5e7+5;
int a[maxn],num[maxn];
int main(){
int n,i,j,h;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
cin>>n;
int g,m=0,ans=n;
// memset(a,0,sizeof(a));
for(i=1;i<=n;i++){
cin>>h;
if(i==1) g=h;
else g=__gcd(g,h);
num[h]++;
if(h>m) m=h;
}
//cout<<g<<endl;
for(i=g+1;i<=m;i++){
if(a[i]==0){
int cnt=0;
for(j=i;j<=m;j+=i)
a[j]=1,cnt+=num[j];
ans=min(ans,n-cnt);
}
}
if(ans==n) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}

CF 1047 C. Enlarge GCD的更多相关文章

  1. CF 1047 C - Enlarge GCD [素数筛]

    传送门:http://codeforces.com/contest/1047/problem/C 题意:给出n个数字,求最少删除几个数可以使剩下的数字的GCD大于n个数字的GCD 思路:最开始想的是先 ...

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

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

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

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

  4. CodeFroces-- 511div2 C. Enlarge GCD

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

  5. CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

    CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...

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

  7. [Codeforces Round511C] Enlarge GCD

    [题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...

  8. 【Codeforces 1034A】Enlarge GCD

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

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

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

随机推荐

  1. Python基础知识:字典

    1.字典中键-值为一对,keys()返回一个列表,包含字典中所有键,values()返回所有值 favorite_languages ={ 'jack':"python", 'al ...

  2. hadoop系列 第一坑: hdfs JournalNode Sync Status

    今天早上来公司发现cloudera manager出现了hdfs的警告,如下图: 解决的思路是: 1.首先解决简单的问题,查看警告提示的设置的阀值时多少,这样就可以快速定位到问题在哪了,果然Journ ...

  3. WPFの获取任意元素的位置

    如果布局在Grid中: 方法一: //_stackPanel为子元素,_grid为父元素 Point point = _stackPanel.TranslatePoint(new Point(0, 0 ...

  4. read_csv报错Initializing from file failed

    Python版本:Python 3.6 pandas.read_csv() 报错 OSError: Initializing from file failed,一般由两种情况引起:一种是函数参数为路径 ...

  5. linux永久配置ip地址

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 i修改 DEVICE=eth() BOOTPROTO=static ONBOOT=yes IPADDR=192 ...

  6. Qt 编程指南 7 添加背景

    . #include "QT_Text1.h" #include <QtWidgets/QApplication> #include <QBitmap> i ...

  7. 在Debian系中快速有效的安装oracle-java

    jre8下载链接  https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html oracl ...

  8. Spring配置文件和SpringMVC配置文件 web.xml配置文件 保存自用

    话不多说,最近在周末自己抽时间写一些框架做的系统,当所有东西都需要自己配置时候发现自己压根记不住这么多类和路径,所以日常总结就变得尤为重要了 db-config.properties 将配置文件常量提 ...

  9. OpenCV (C++) 颜色跟随

    #include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespace std; ...

  10. PAT A1134 Vertex Cover (25 分)——图遍历

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...