CF 1047 C. Enlarge GCD
传送门
[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的更多相关文章
- CF 1047 C - Enlarge GCD [素数筛]
传送门:http://codeforces.com/contest/1047/problem/C 题意:给出n个数字,求最少删除几个数可以使剩下的数字的GCD大于n个数字的GCD 思路:最开始想的是先 ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- CodeFroces-- 511div2 C. Enlarge GCD
题目链接:C. Enlarge GCD 给你一个序列 删除一些数看可以让他们之间的gcd变大如果可以输出删除数量最小的个数 先求出共同 gcd 然后除去 找出出现最多的质数 然后减去就可以了 #inc ...
- 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, ...
- 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 Round511C] Enlarge GCD
[题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...
- 【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,就输出要删的数的个 ...
随机推荐
- Head First Android --- Enable USB debugging on your device
1. Enable USB debugging on your device On your device, open “Developer options” (in Android 4.0 o ...
- xunit-ICollectionFixture
https://github.com/dmetzgar/dotnetcoreinaction
- Python基础知识:字符串
1.Python中大写字母命名的变量,默认为常量,不可修改:列如:MYSQL_CONNECTION = '192.168.1.1' 2.字符串换行输入格式:换行用\隔开,两行分别用引号,制表符\t,换 ...
- 【PAT】B1002 写出这个数
思路: 1.以字符串形式输入数据,计算结果 2.使用sprintf将结果数字转换为字符串(将数字每一位分开) 3.分别输出字符每一位 1,笨方法,用的ifelse来判断输出 #include<s ...
- WPFの操作文件浏览框几种方式
方式1: 使用win32控件OpenFileDialog Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog ...
- nodejs中async使用
waterfall , parallel , series , eachSeries //var async = require('async'); /*** *① * 串行有关联 执行每个函数 ...
- BZOJ1024:[SCOI2009]生日快乐(DFS)
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕. 现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的 ...
- laravel orm进行增删改查
https://laravelacademy.org/post/9699.html 建议用DB门面直接操作数据库,因为ORM性能低.数据查询上面,ORM不会比DB差的,就比如with,是用了sql最基 ...
- Luogu P3372 【模板】线段树 1
qwq #include<cstdio> using namespace std; ; int n,m,x,y,flag; *maxn],r[*maxn]; *maxn],sum[*max ...
- rook 排错记录 + Orphaned pod found kube-controller-manager的日志输出
1.查看rook-agent(重要)和mysql-wordpress 的日志,如下: MountVolume.SetUp failed for volume "pvc-f002e1fe-46 ...