随机化选讲的例题

John Doe offered his sister Jane Doe find the gcd of some set of numbers a.

Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'.

Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers.

Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'.

Jane coped with the task for two hours. Please try it, too.

Input

The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier.

Output

Print a single integer g — the Ghd of set a.


题目分析

随机化+复杂度分析

首先按照随机化题的处理方式,随机选一个数$a_{pos}$钦定它在答案集合内。再考虑如何找出剩下数中满足条件的最大公因数。由于这个公因数一定是$d=\gcd\{a_{pos},a_i\}$,并且所有$d|d'$的$d'$也都会算在$d$的贡献里。那么这里由复杂度分析的经验得,由于$10^{12}$内因数最多的数约有$7000$个约数,那么我们并不需要对$d$存下所有$d'$的贡献,而是每次暴力地累计答案即可。

话说为什么这个srand(time(0))的随机化正确率这么低……只有选15个数才能擦着时限过去。

 #include<bits/stdc++.h>
typedef long long ll;
const int maxn = ; int n;
ll a[maxn],ans;
std::map<ll, int> mp;
std::map<ll, int>::iterator it,tmp; ll read()
{
char ch = getchar();
ll num = ;
for (; !isdigit(ch); ch=getchar());
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num;
}
ll gcd(ll a, ll b){return !b?a:gcd(b, a%b);}
int main()
{
srand(time()), n = read();
for (int i=; i<=n; i++) a[i] = read();
for (int cse=; cse; --cse)
{
mp.clear();
int pos = 1ll*rand()*rand()%n+;
if (a[pos] <= ans) continue;
for (int i=; i<=n; i++)
{
ll val = gcd(a[pos], a[i]);
if (val > ans) ++mp[val];
}
it = mp.end();
for (int cnt; it!=mp.begin(); )
{
cnt = , --it;
if ((*it).first <= ans) break;
for (tmp=it; tmp!=mp.end(); ++tmp)
if (!((*tmp).first%(*it).first)) cnt += (*tmp).second;
if (cnt >= (n+)/) ans = (*it).first;
}
}
printf("%lld\n",ans);
return ;
}

END

【数学 随机 技巧】cf364D. Ghd的更多相关文章

  1. [CF364D]Ghd

    [CF364D]Ghd 题目大意: 有\(n(n\le10^6)\)个数\(A_{1\sim n}(A_i\le10^{12})\),从中选取\(\lceil\frac n2\rceil\)个数,使得 ...

  2. CF364D Ghd(随机化)

    另一个集合\(s\)的\(ghd\)为\(max\{gcd(s')||s'|>=0.5|s|\}\) 给定序列\(a\),求\(ghd\) 随机化算法.因为\(|s'|\geq 0.5|S|\) ...

  3. Codeforces 1114E(数学+随机算法)

    题面 传送门 分析 通过二分答案,我们显然可以求出数组中最大的数,即等差数列的末项 接着随机取一些数组中的数,对他们两两做差,把得到的差取gcd即为公差 例a={1,5,9,13},我们随机取了1 9 ...

  4. HDU 5312(数学推导+技巧)

    首先说一下.N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示. 对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K: 即随意由k个数组成的解 ...

  5. light OJ 1282 - Leading and Trailing 数学 || double技巧

    http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...

  6. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  7. ( 译、持续更新 ) JavaScript 上分小技巧(二)

    考虑到文章过长,不便于阅读,这里分出第二篇,如有后续,每15个知识点分为一篇... 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第三篇地址:( 译.持续更新 ) Java ...

  8. 装X数学:高雅的数学表示

    采用高雅的数学描述 转自于:研究生之路怎么走?       高雅的数学描述会提高你论文的等级和加强评审人对你基础功底的认可.例如泛函分析.集合.测度.度量空间和拓扑空间.现代代数.微分几何等数学方面的 ...

  9. ZJOI2019一轮游记

    Preface 期待已久的省选终于开始了233,关于之前的一些内容,在ZJOI2019一轮停课刷题记录都可以找到,这里不再赘述 ZJOI2019,Bless All Day -1 今天难得有休息,昨晚 ...

随机推荐

  1. CentOS7.3下Zabbix3.5之微信报警配置

    一.报警脚本放到服务端的 /usr/lib/zabbix/alertscripts/下      属组属主为zabbix zabbix      有执行权限vim weixinbaojing.py # ...

  2. 常见的web性能优化方法

    前言:关于优化问题,随着项目经验不断累积,多方查找资料进行拼接合并,形成如下文章,之后遇到类似好的方法,会不断补充完善. 前端是庞大的,包括 HTML. CSS. Javascript.Image . ...

  3. 红黑树(Red-Black Tree),B树,B-树,B+树,B*树

    (一)红黑树(Red-Black Tree) http://www.cnblogs.com/skywang12345/p/3245399.html#a1 它一种特殊的二叉查找树.红黑树的每个节点上都有 ...

  4. Lecture--9 Sorting

    1/排序算法:冒泡排序bubble sort,插入排序 insertion sort,选择排序 selection sort,快速排序 quick sort,归并排序 merge sort;堆排序 h ...

  5. java里如何实现两个等长度的字符串数组有多少个元素相同(从最左边开始,一旦遇到不同元素则跳出计数)

    不多说,直接上干货! package zhouls.bigdata.DataFeatureSelection.sim; public class test { public static int st ...

  6. CentOS6.5安装配置详解

    1. 环境要求 VMWare软件: CentOS6.5对应的iso镜像文件(位数对应个人计算机位数). 2. 安装步骤 打开VMWare,文件->新建虚拟机(以下几步默认跳过即可) 命名和选择安 ...

  7. 一次Zookeeper 扩展之殇

    一.背景 基于公司发展硬性需求,生产VM服务器要统一迁移到ZStack 虚拟化服务器.检查自己项目使用的服务器,其中zookeeper集群中招,所以需要进行迁移. 二.迁移计划 为了使迁移不对业务产生 ...

  8. PyQt学习笔记

    ---------------个人学习笔记--------------- 1.QtWidgets.QApplication.instance().quit() 方法可退出当前窗体 2.self.Qla ...

  9. ubuntu下apk的反编译

    今天调试一个程序的时候,因为需要上传数据到服务器,但是程序太过久远了,服务器上传的地址就忘记了,但是源码又不在我这里,因为要的急所以就被逼无奈的情况下想到了反编译,我用的是Linux Mint 14. ...

  10. 什么是SpringBoot

    随着动态语言的流行(Ruby,Groovy,Scala,Node.js),Java的开发显得格外的笨重;繁多的配置,低下的开发效率,复杂的部署流程以及第三方技术集成难度大. 在上述环境 下,Sprin ...