CodeForces - 1047CEnlarge GCD(这题很难,快来看题解,超级详细,骗浏览量)
C. Enlarge GCD
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mr. F has n positive integers, a1,a2,…,an.
He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.
But this problem is too simple for him, so he does not want to do it by himself. If you help him, he will give you some scores in reward.
Your task is to calculate the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.
Input
The first line contains an integer n (2≤n≤3⋅105) — the number of integers Mr. F has.
The second line contains n integers, a1,a2,…,an (1≤ai≤1.5⋅107).
Output
Print an integer — the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.
You should not remove all of the integers.
If there is no solution, print «-1» (without quotes).
Examples
inputCopy
3
1 2 4
outputCopy
1
inputCopy
4
6 9 15 30
outputCopy
2
inputCopy
3
1 1 1
outputCopy
-1
Note
In the first example, the greatest common divisor is 1 in the beginning. You can remove 1 so that the greatest common divisor is enlarged to 2. The answer is 1.
In the second example, the greatest common divisor is 3 in the beginning. You can remove 6 and 9 so that the greatest common divisor is enlarged to 15. There is no solution which removes only one integer. So the answer is 2.
In the third example, there is no solution to enlarge the greatest common divisor. So the answer is −1.
这道题是说删最少的数字使得,数列的最大公约增大。这道题要明白第一点,输出-1的情况,当输入数字都是一样的时候,其他情况全都有解,至少有一个N-1的解。
先对数列求整体的最小公倍数。假设第一个与第二个的小公倍数a=gcd(x1,x2)则第二个与第三个的是b=gcd(a,x3),即x1,x2,x3的最小公倍数。以此类推即可求出所有数的最大公约数,本以为会超时,没想到922ms过了。 那么要想变大,就要从比最大公约数大一个的开始枚举,好在如果先枚举过2之后4,6,8,就都不用枚举了,实际上是没举素数,但是埃氏筛法也很费时间,加上就超时,这里就用了埃氏筛法的思想。我是用桶的方法存的数据,不然重复元素不好处理,这样根据下标就能判断这个数在哪里,有几个。每次枚举出的都是按最大公约数枚举,这样刚好能处理。
ps:
2 4 8 18 34 44
最大公约数是2
从3开始枚举
cnt+=a[3]=0;
cnt+=a[6]=0;
…
cnt+=a[18]=1;
…
ans=5;
然后是4;
cnt=3;
ans=4;
然后是5
cnt=0;
ans=6;
布拉布拉,就写就是了。
上界肯定是比第一大数小的数,但是小多少,不清楚,直接枚举到最大数即可。刚刚有优化了一下,发现很多地方都可以减少时间消耗。跑出来了700ms的好成绩,哈哈哈哈。
#include <bits/stdc++.h>
using namespace std;
const int MAX =1.5e7 + 10;
int jishu[MAX], a[MAX];
int flag=0,maxn=0 ,n, d = 0, ob;
int main()
{
scanf("%d",&n);
for (int i=0; i<n; ++i)
{
scanf("%d", &ob);
maxn=max(maxn,ob);
a[ob]++;
if (i==0)d = ob;
d = __gcd(d,ob);//艾玛,省老多事了。
}
if(a[ob]==n)
{
printf("-1\n");
return 0;
}
int ans = n;
for (int i=d + 1;i<=maxn; ++i)
if (jishu[i]==0)
{
int ct = 0;
for (int j = i; j <= maxn; j += i)
jishu[j] = 1, ct += a[j];
ans = min(ans,n-ct);
}
printf("%d\n",ans);
return 0;
}
CodeForces - 1047CEnlarge GCD(这题很难,快来看题解,超级详细,骗浏览量)的更多相关文章
- 30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- Codeforces 1291 Round #616 (Div. 2) C. Mind Control(超级详细)
C. Mind Control You and your n−1 friends have found an array of integers a1,a2,-,an. You have decide ...
- CodeForces 610B-Vika and Squares,有坑点,不是很难~~
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网
函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网 函数式编程很难,这正是你要学习它的原因 156 次分享 新浪微博 腾讯微博 Tweet 人人网 QQ空间 很奇怪不是,很少有人每天都使用函数 ...
- CodeForces - 468A ——(思维题)
Little X used to play a card game called "24 Game", but recently he has found it too easy. ...
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- 听说 JVM 性能优化很难?今天我小试了一把!
文章首发于公众号「陈树义」及个人博客 shuyi.tech,欢迎关注访问. 对于 Java 开发的同学来说,JVM 性能优化可以说是比较难掌握的知识点.这不仅因为 JVM 性能优化需要掌握晦涩难懂的 ...
- java 基础题 很基础, 很有趣
都是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我一样参加各大IT校园招聘的同学们,纯考Java基础功底, 老手们就不用进来了,免得笑话我们这些未出校门的孩纸们, ...
- 转:Eric Lippert:阅读代码真的很难
转自:http://blog.jobbole.com/438/ 相关文章 微软资深软件工程师:阅读代码真的很难(第2篇) 阅读优秀代码是提高开发人员修为的一种捷径 学会阅读源代码 如何阅读大型代码库? ...
随机推荐
- MTK Android 耳机线控的实现方法
android 耳机线控的实现方法 keycodeonkeydownkeyevent 耳机线控的功能 耳机线控是一种很好用,并且能提升用户体验的功能.可以用来实现一些常用和基本的功能.比如:实现音乐播 ...
- 2015蓝桥杯分机号(C++C组)
标题:分机号X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位.比如:751,520,321 都满足要求,而,766,918,201 就不符合要求.现 ...
- 中阶d03.4 JDBC_DAO
1.环境准备(单项目下用,在大jdbc项目下只用配置一次) jdbc的驱动(mysqlxxjdbc.jar).util工具(包装释放资源.建立连接.访问properties文件等方法) 2.dao的概 ...
- matplotlib locators
2020-03-23 17:59:59 -- Edit by yangray The Locator class is the base class for all tick locators. Th ...
- Java基础】并发 - 多线程
Java基础]并发 - 多线程 分类: Java2014-05-03 23:56 275人阅读 评论(0) 收藏 举报 Java 目录(?)[+] 介绍 Java多线程 多线程任务执行 大多数 ...
- Java课程设计之——爬虫篇
主要使用的技术 Httplcient Jsoup 多线程 dao模式 网络爬虫简介 网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取 ...
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...
- SpringCloud入门(十): Config 统一配置中心
SpringCloud Config 简介 在分布式系统中,由于服务组件过多,为了方便争对不通的环境下的服务配置文件统一管理,实时更新,所以出现了分布式配置中心组件.市面上开源的配置中心有很多,360 ...
- 学习Salesforce | Platform Developer Ⅰ 平台初级开发认证考试指南及备考资源
一.平台开发人员考试计划 Salesforce平台开发人员初级认证面向具有在Lightning平台上构建自定义应用程序的知识.技能和经验的个人. 该认证考核Lightning平台的基本编程能力,并会使 ...
- App的数据如何用python抓取
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. App中的数据可以用网络爬虫抓取么 答案是完全肯定的:凡是可以看到的APP数 ...