【题解】Ghd

一道概率非酋题?

题目很有意思,要我们选出大于\(\frac{n}{2}\)个数字使得他们的最大公约数最大。

那么我们若随便选择一个数字,他在答案的集合里的概率就大于\(0.5\)了。

我们若连续随机选择\(10\)次,那么我们答案判断失误的概率不就是\(\frac{1}{2^{10}}< \frac{1}{1000}\)吗?

除非你脸黑不然不可能错吧233

那么我们这样,每次随机选择出来的数字,先对所有数取\(gcd\)然后再将出现次数放入\(map\)一一检查答案即可。

时间复杂度\(O(nlogn)\)有一些其他的常数,不算了。

蒯的代码,咕咕咕

#include <cstdio>
#include <ctime>
#include <cstdlib> #include <map> typedef long long LL; inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
} inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
} const int MAXN = 1000005; inline LL gcd(LL a, LL b) {
LL t;
while(b) {
t = a % b;
a = b;
b = t;
}
return a;
} int n;
LL a[MAXN]; int main() {
srand(time(NULL));
n = readint();
for(int i = 1; i <= n; i++) {
a[i] = readint();
}
LL ans = 1;
for(int rep = 1; rep <= 10; rep++) {
int rnd = (rand() * RAND_MAX + rand()) % n + 1;
// 下面我们需要处理出a[rnd]和其他数的gcd,答案可能就是这些gcd中的一个,map里first存gcd,second存该gcd的出现次数
std::map<LL, int> fact;
for(int i = 1; i <= n; i++) {
LL t = gcd(a[i], a[rnd]);
if(!fact.count(t)) fact[t] = 1;
else fact[t]++;
}
// 从大到小来判断每个gcd是否能成为答案
std::map<LL, int>::iterator it = fact.end();
do {
it--;
if((*it).first <= ans) continue;
int cnt = 0;
// 能被当前答案整除的因数对应的原数肯定能够被当前答案整除,统计能被当前答案整除的数字个数
for(std::map<LL, int>::iterator it1 = it; it1 != fact.end() && cnt << 1 < n; it1++) {
if(!((*it1).first % (*it).first)) {
cnt += (*it1).second;
}
}
// 如果能被当前答案整除的数字个数比一半多,当前答案还比已经得到的最优解更优,那么更新最优解
if(cnt << 1 >= n) ans = (*it).first;
} while(it != fact.begin());
}
// CF推荐I64d输出long long
printf("%I64d", ans);
return 0;
}

【题解】[Ghd]的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. eclipse 国际化 $NON-NLS-1$ 含义

    一.$NON-NLS-1$ 含义 Eclipse 如果每行代码里有这个字符串:$NON-NLS-1$ 表示:这一行的第一个字符串是不需要国际化的.同理$NON-NLS-2$,$NON-NLS-3$.. ...

  2. fauxbar.bak

    {"options": { "almostdone":"0", "backup_searchEngines":" ...

  3. Drools环境搭建

    Eclipse3.5安装Drools6.5.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...

  4. 转: 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)

    from:  http://www.cnblogs.com/hdwang/p/5418747.html

  5. GUID概念

     GUID概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是 ...

  6. java查看工具jstack-windows

    Prints Java thread stack traces for a Java process, core file, or remote debug server. This command ...

  7. git 安装方法

    Windows上安装Git示例 在Windows上使用Git,可以从Git官网直接下载安装程序,(网速慢的同学请移步国内镜像),然后按默认选项安装即可. 安装完成后,在开始菜单里找到“Git”-> ...

  8. 【Python】使用scatter()绘制散点图

    绘制简单散点图 要绘制单个点,使用scatter()函数,并向它传递一对x和y坐标,它将在指定位置绘制一个点 import matplotlib.pyplot as plt plt.scatter(2 ...

  9. java集合归纳

    学习自: http://android.blog.51cto.com/268543/400557/ MAP Collection 堆栈队列操作尽可能考虑 linkedlist 多线程同步操作尽可能考虑 ...

  10. Bootstrp--一个导航面板切换的实用例子

    <!--导航区开始--> <ul class="nav nav-tabs nav-stacked" role="tablist"> &l ...