【题解】[Ghd]
【题解】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]的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- Codeforces 535B Tavas and SaDDas 水题一枚
题目链接:Tavas and SaDDas Once again Tavas started eating coffee mix without water! Keione told him that ...
- Tiny4412 支持 adb reboot-bootloader
硬件版本: Tiny4412ADK + S700 4GB u-boot 版本: u-boot-2010-12 linux版本: Linux-3.0.8 版本一 支持 adb re ...
- ios界面笔记(一)
基于一个简单视图的view解析
- Usage of API documented as @since1.6+
Usage of API documented as @since1.6+ File ->Project Structure->Project Settings -> Modules ...
- Zookeeper demo增删改查
Zookeeper 的增删改查demo代码 public class SimpleZkClient { private static final String connectString = &quo ...
- android 仿ios开关控件
ios一些控件还是挺美丽的,可是对android程序猿来说可能比較苦逼,由于ios一些看起来简单的效果对android来说可能就没那么简单了,可是没办法非常多产品都是拿ios的一些控件叫android ...
- 微信小程序 - 单个题目
后端传过来的数据,如果通过wx:for遍历出来那就是一个页面全部排下来.... 我的想法就是,页面初始化时设置一个默认值 1/50 就是 index+1 / 50(后端传过来的数组长度),通过控制in ...
- 【Excle数据透视表】如何隐藏数据透视表中行字段的”+/-”按钮
如下图:新建的数据透视表中有存在"+/-"符号,导致数据透视图不太美观,那么怎么处理呢? 解决方案 单击"显示"组中的"+/-"按钮显示或隐 ...
- 1M网速等于多少K
http://zhidao.baidu.com/question/157400316.html&__bd_tkn__=65ac453b343794385019e962bfb06bb8c710d ...
- 深入了解Struts1的执行机理
要说Struts1的工作流程.就必需要说一下Model1和Model2了.由于这个框架是踏着他们的尸骨一步一步的发展起来的. Model1开发模式,想想我们刚刚開始接触Java的时候,我们用的就是这样 ...