传送门:http://codeforces.com/contest/1047/problem/C

题意:

  给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值。

思路:

  自己一开始想把每个数的因子都找出来,找到这些因子中出现次数最多且因子大于n个数的最大公约数的,(n - 次数 )就是答案。但是复杂度是1e9,差那么一点。

自己还是对素数筛理解的不够深。这道题可以枚举素数x,对于每个x,找到所有(a【i】/gcd(all)) 是x倍数的个数,就是一个次数。找这个次数的过程正好与素数筛的过程一致。

  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <bitset>
  8. #include <cctype>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <stack>
  13. #include <cmath>
  14. #include <queue>
  15. #include <list>
  16. #include <map>
  17. #include <set>
  18. #include <cassert>
  19. #include <unordered_map>
  20. using namespace std;
  21. //#pragma GCC optimize(3)
  22. //#pragma comment(linker, "/STACK:102400000,102400000") //c++
  23. // #pragma GCC diagnostic error "-std=c++11"
  24. // #pragma comment(linker, "/stack:200000000")
  25. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  26. // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
  27.  
  28. #define lson (l , mid , rt << 1)
  29. #define rson (mid + 1 , r , rt << 1 | 1)
  30. #define debug(x) cerr << #x << " = " << x << "\n";
  31. #define pb push_back
  32. #define pq priority_queue
  33. #define max3(a,b,c) max(max(a,b),c)
  34.  
  35. typedef long long ll;
  36. typedef unsigned long long ull;
  37.  
  38. typedef pair<ll ,ll > pll;
  39. typedef pair<int ,int > pii;
  40. typedef pair<int,pii> p3;
  41.  
  42. //priority_queue<int> q;//这是一个大根堆q
  43. //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
  44. #define fi first
  45. #define se second
  46. //#define endl '\n'
  47.  
  48. #define OKC ios::sync_with_stdio(false);cin.tie(0)
  49. #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
  50. #define REP(i , j , k) for(int i = j ; i < k ; ++i)
  51. //priority_queue<int ,vector<int>, greater<int> >que;
  52.  
  53. const ll mos = 0x7FFFFFFF; //
  54. const ll nmos = 0x80000000; //-2147483648
  55. const int inf = 0x3f3f3f3f;
  56. const ll inff = 0x3f3f3f3f3f3f3f3f; //
  57. const int mod = ;
  58. const double esp = 1e-;
  59. const double PI=acos(-1.0);
  60.  
  61. template<typename T>
  62. inline T read(T&x){
  63. x=;int f=;char ch=getchar();
  64. while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
  65. while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
  66. return x=f?-x:x;
  67. }
  68.  
  69. /*-----------------------showtime----------------------*/
  70. const int maxn = 3e5+;
  71. const int maxnum = 1.5e7+;
  72. int a[maxn],cnt[maxnum];
  73. int p[maxn];
  74. bool prime[maxnum];
  75. int gcd(int a,int b){
  76. if(b==)return a;
  77. return gcd(b, a%b);
  78. }
  79.  
  80. int main(){
  81. int n,g=;scanf("%d", &n);
  82. int ans = ;
  83. for(int i=; i<=n; i++){
  84. scanf("%d", &a[i]);
  85. g = gcd(g,a[i]);
  86. }
  87. for(int i=; i<=n; i++)cnt[a[i]/g]++;
  88. int tot = ;
  89.  
  90. for(int i=; i<maxnum; i++){
  91. if(!prime[i]){
  92. int x = i;int h = ;
  93. for(int j=i; j<maxnum; j+=i){
  94. prime[j] = ;
  95. h += cnt[j];
  96. }
  97. ans = max(ans, h);
  98. }
  99. }
  100. if(!ans)puts("-1");
  101. else printf("%d\n", n-ans);
  102. return ;
  103. }

CF 1047 c

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

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

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

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

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

  3. Codeforces Round #511 (Div. 2)

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

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

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

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

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

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

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

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

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

  9. Codeforces Round #347 (Div.2)_A. Complicated GCD

    题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...

随机推荐

  1. MYSQL 时间轴数据 获取同一天数据的前3条

    创建表数据 CREATE TABLE `praise_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `pic_id` va ...

  2. 用大白话告诉你 :Java 后端到底是在做什么?

    阅读本文大概需要 6 分钟. 作者:黄小斜 新手程序员通常会走入一个误区,就是认为学习了一门语言,就可以称为是某某语言工程师了.但事实上真的是这样吗?其实并非如此. 今天我们就来聊一聊,Java 开发 ...

  3. vagrant 创建虚拟机时遇到问题

    问题1 :  ceph-node3: Warning: Authentication failure. Retrying.. 问题分析: ssh 认证失败,在向虚拟机拷贝内容时权限不足. 解决办法: ...

  4. 商贸型企业 Java 收货 + 入库 + 生成付款单

    package cn.hybn.erp.modular.system.service.impl; import cn.hybn.erp.core.common.page.LayuiPageFactor ...

  5. 对Java中HashCode方法的深入思考

    前言 最近在学习 Go 语言,Go 语言中有指针对象,一个指针变量指向了一个值的内存地址.学习过 C 语言的猿友应该都知道指针的概念.Go 语言语法与 C 相近,可以说是类 C 的编程语言,所以 Go ...

  6. React Native-安卓环境的搭建

    最近公司做一个项目,项目中使用React Native(简称RN)来写安卓APP,现在我分享下安卓环境搭建的过程. 我参照https://reactnative.cn/docs/getting-sta ...

  7. android ——通知管理

    public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle saved ...

  8. -bash: redis: command not found

    在linux中安装redis,先是拉过去安装,然后通过命令:make   进行编译  编译完成以后通过命令 make install 完成安装:结果在进行启动linux的时候执行           ...

  9. 《机器学习技法》---soft-margin SVM

    1. soft-margin SVM的形式 其中ξn表示每个点允许的犯错程度(偏离margin有多远),但是犯错是有代价的,也就是目标函数里面要最小化的.c控制对犯错的容忍程度. 2. 推导soft ...

  10. 维恩贝特面试JAVA后台开发

    1 自我介绍 2 链表和数组区别(数组空间连续,且有下标,查找快,但是增删数据效率不高,链表的空间不连续,查找起来慢,但是对数据的增删效率高,链表可以随意扩大,数组不能) 3 sort方法的实现 (A ...