C. Enlarge GCD

题目链接:https://codeforces.com/contest/1047/problem/C

题意:

给出n个数,然后你可以移除一些数。现在要求你移除最少的数,让剩下数的gcd变大。

题解:

首先可以先让所有数都除以他们的gcd,让他们互质,好让问题简单化。

由唯一分解定理,题目中的问题可以转化为:找出最多数都共有的质因子,假设其数目为mx,答案就是n-mx。

上面的想法也是基于贪心,具体做法还是有点技巧,就是在筛素数的时候就进行判断,具体见代码吧:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+,M = 2e7+;
int n;
int a[N];
int cnt[M],vis[M];
int main(){
scanf("%d",&n);
int d=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
d=__gcd(d,a[i]);
}
for(int i=;i<=n;i++) cnt[a[i]/d]++;
int mx=;
for(int i=;i<=2e7;i++){
int tot=;
if(!vis[i]){
vis[i]=;
for(int j=i;j<=2e7;j+=i){
vis[j]=;
tot+=cnt[j];
}
}
mx=max(mx,tot);
}
int ans=n-mx;
if(ans==n) ans=-;
cout<<ans;
return ;
}

Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)的更多相关文章

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

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

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

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

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

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

  4. Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心

    C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #691 (Div. 2) C. Row GCD (数学)

    题意:给你两个数组\(a\)和\(b\),对于\(j=1,...,m\),找出\(a_1+b_j,...,a_n+b_j\)的\(gcd\). 题解:我们很容易的得出\(gcd\)的一个性质:\(gc ...

  6. Codeforces Round #511 (Div. 2)

    Codeforces Round #511 (Div. 2) #include <bits/stdc++.h> using namespace std; int n; int main() ...

  7. 2018.9.21 Codeforces Round #511(Div.2)

    只写了AB,甚至还WA了一次A题,暴露了蒟蒻的本质=.= 感觉考的时候有好多正确或和正解有关的思路,但是就想不出具体的解法或者想的不够深(长)(怕不是过于鶸) 话说CF的E题怎么都这么清奇=.= A. ...

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

  9. Codeforces Round #554 (Div. 2)-C(gcd应用)

    题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...

随机推荐

  1. vuex -- vue的状态管理模式

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 状态管理模式.集中式存储管理 一听就很高大 ...

  2. leetcode-电话号码的字母组合

    电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" ...

  3. HTMLTestRunner带饼图

    # -*- coding: utf-8 -*- """ A TestRunner for use with the Python unit testing framewo ...

  4. python 文件编译成exe可执行文件。

    pyinstaller打包方法: pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://sourceforge.net/ ...

  5. Java进阶知识点:更优雅地关闭资源 - try-with-resource

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  6. FPGA学习-PS2接口

    选自http://m.elecfans.com/article/774143.html

  7. Paper Reading - Learning to Evaluate Image Captioning ( CVPR 2018 ) ★

    Link of the Paper: https://arxiv.org/abs/1806.06422 Innovations: The authors propose a novel learnin ...

  8. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...

  9. Python—字典(当索引不好用时)

    一.定义与概念 1.字典是针对非序列集合而提供的一种数据类型 举例:检索学生信息. “<键><值>对”. 键(即身份证号码) 值(即学生信息). “键值对”例子 姓名和电话号码 ...

  10. Notes of the scrum meeting(12.11)

    meeting time:19:30~20:30p.m.,December 11th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...