随机化选讲的例题

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. Spring @CacheEvict 无效。。。。

    使用 此 注解 时...需要 配置 它 的 key .... @CacheEvict(value = { "adminFunc", "groupFunc" }, ...

  2. linux高级编程基础系列:线程间通信

    linux高级编程基础系列:线程间通信 转载:原文地址http://blog.163.com/jimking_2010/blog/static/1716015352013102510748824/ 线 ...

  3. Java面向对象_常用类库api——二分查找算法

    概念:又称为折半查找,优点是比较次数少,查找速度快,平均性能好:缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 例: public class Bi ...

  4. OpenStack git cmd

    文件流转的三个工作区域:Git 的工作目录,暂存区域,以及本地仓库. 基本的 Git 工作流程如下: 在工作目录中修改某些文件. 对修改后的文件进行快照,然后保存到暂存区域. 提交更新,将保存在暂存区 ...

  5. JAVA多线程之Semaphore

    Semaphore:动态增减信号量,用于控制对某资源访问的同一时间的并发量.类似于令牌,谁拿到令牌(acquire)就可以去执行了,如果没有令牌则需要等待. [如何获取]:semaphore.tryA ...

  6. VMware下linux与window文件夹共享

    这里说的是在虚拟机下来实现在windows下共享一个文件夹. 下面来说明一下是如何实现的: 1.  安装VMware.Workstation. 2.  安装Redhat Linux 9.0,在虚拟机下 ...

  7. MongoDB Linux 安装配置 后台运行

    介绍安装的文档很多,可以参考这篇: http://www.mkyong.com/mongodb/how-to-install-mongodb-on-mac-os-x/ 安装完后你可能会碰到的2个问题. ...

  8. drools的error:Jboss rule 6.4.0 Cannot find a default StatelessKieSession

    drools的kmodule.xml文件,如果是默认加载必须放在META-INF的文件夹下,如下图 官网大佬原话 Have you defined kieSession and KieBase nam ...

  9. 现代java开发指南系列

    [翻译]现代java开发指南系列 [翻译]现代java开发指南 第一部分 [翻译]现代java开发指南 第二部分 [翻译]现代java开发指南 第三部分

  10. 用简单的方法学习ES6

    ES6 简要概览 这里是ES6 简要概览.本文大量参考了ES6特性代码仓库,请允许我感谢其作者@Luke Hoban的卓越贡献,也感谢@Axel Rauschmayer所作的[优秀书籍]//explo ...