【数学 随机 技巧】cf364D. Ghd
随机化选讲的例题
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的更多相关文章
- [CF364D]Ghd
[CF364D]Ghd 题目大意: 有\(n(n\le10^6)\)个数\(A_{1\sim n}(A_i\le10^{12})\),从中选取\(\lceil\frac n2\rceil\)个数,使得 ...
- CF364D Ghd(随机化)
另一个集合\(s\)的\(ghd\)为\(max\{gcd(s')||s'|>=0.5|s|\}\) 给定序列\(a\),求\(ghd\) 随机化算法.因为\(|s'|\geq 0.5|S|\) ...
- Codeforces 1114E(数学+随机算法)
题面 传送门 分析 通过二分答案,我们显然可以求出数组中最大的数,即等差数列的末项 接着随机取一些数组中的数,对他们两两做差,把得到的差取gcd即为公差 例a={1,5,9,13},我们随机取了1 9 ...
- HDU 5312(数学推导+技巧)
首先说一下.N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示. 对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K: 即随意由k个数组成的解 ...
- light OJ 1282 - Leading and Trailing 数学 || double技巧
http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- ( 译、持续更新 ) JavaScript 上分小技巧(二)
考虑到文章过长,不便于阅读,这里分出第二篇,如有后续,每15个知识点分为一篇... 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第三篇地址:( 译.持续更新 ) Java ...
- 装X数学:高雅的数学表示
采用高雅的数学描述 转自于:研究生之路怎么走? 高雅的数学描述会提高你论文的等级和加强评审人对你基础功底的认可.例如泛函分析.集合.测度.度量空间和拓扑空间.现代代数.微分几何等数学方面的 ...
- ZJOI2019一轮游记
Preface 期待已久的省选终于开始了233,关于之前的一些内容,在ZJOI2019一轮停课刷题记录都可以找到,这里不再赘述 ZJOI2019,Bless All Day -1 今天难得有休息,昨晚 ...
随机推荐
- 剑指Offer丑数问题
这是剑指第一次卡死我的题……记录一下 首先看题目: 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数 ...
- new 和 delete 用法
1. 这两个其实是 C++ 语言标准库的库函数,原型分别如下: void *operator new(size_t); //allocate an object void *operator dele ...
- Linux Shell命令系列(4)
16. cat命令 “cat”代表了连结(Concatenation),连接两个或者更多文本文件或者以标准输出形式打印文件的内容. 17. cp 命令 “copy”就是复制.它会从一个地方复制一个文件 ...
- PHP中文手册2
11.异常处理 用户可以用自定义的异常处理类来扩展 PHP 内置的异常处理类.以下的代码说明了在内置的异常处理类中,哪些属性和方法在子类中是可访问和可继承的.译者注:以下这段代码只为说明内置异常处理类 ...
- 4.0.3的mongodb 安装和java使用
一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的github项目地址 https://github.com/247292980/spring- ...
- C#获得字符串首字符字母(大写)
/// <summary> /// 获得字符串首字符字母(大写): /// </summary> /// <param name="cnChar"&g ...
- Vue小贴士
1.去掉空格影响,删除掉此段代码 2.想要同时运行两个Vue项目,修改端口号,黄色框内的内容自己随意改个端口号就行,比如:8082 3.批处理 在项目的根目录中添加a.bat文件,这样就可以在运行的 ...
- Js常见算法实现汇总
/*去重*/ <script> function delRepeat(arr){ var newArray=new Array(); var len=arr.length; for(var ...
- ArrayList,Vector, LinkedList 的存储性能和特性
ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内 ...
- 微信小程序:点击预览图片
在开发微信小程序时,开发人员会参考着小程序api来开发小程序,但有的时候根据情况不同很容易出现bug,以下是我在开发小程序时出现的各种bug,在开发时有需要预览图片. 1.xml <view c ...