【数学 随机 技巧】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 今天难得有休息,昨晚 ...
随机推荐
- JOS lab1 part2 分析
lab1的Exercise 2就是让我们熟悉gdb的si操作,并知道BIOS的几条指令在做什么就够了,所以我们也会尽可能的去分析每一行代码. 首先进入到6.8282/lab这个目录下,输入指令make ...
- lifecycle-mapping-metadata.xml
<?xml version="1.0" encoding="UTF-8"?> <lifecycleMappingMetadata> &l ...
- 自动化测试 - Appium + Python史上最全最简环境搭建步骤
一,为什么是Appium借一张图: 1.1 Appium优点 l 开源 l 跨架构:NativeApp.Hybird App.Web App l 跨设备:Android.iOS.Firefox ...
- Jquery 实现表单验证,所有验证通过方可提交
1. [代码]Jquery 实现表单验证,所有验证通过方可提交 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- 028 Implement strStr() 实现 strStr()
实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...
- 与postgis相关的一些常用的sql
create table NODES (ID SERIAL not null,geometry geography(POINTZ, 4326) null); create table EDGES (I ...
- (转)Bash Shell常用快捷键
Bash Shell常用快捷键 原文:https://github.com/hokein/Wiki/wiki/Bash-Shell%E5%B8%B8%E7%94%A8%E5%BF%AB%E6%8D%B ...
- Spark Mllib里的如何对单个数据集用斯皮尔曼计算相关系数
不多说,直接上干货! import org.apache.spark.mllib.stat.Statistics 具体,见 Spark Mllib机器学习实战的第4章 Mllib基本数据类型和Mlli ...
- 其它电脑访问mysql被拒绝
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDE ...
- Docker | 第二章:第一个Docker应用
前言 上一章节,已经简单讲解了Docker相关方面的知识,相信大家已经有个概念了.这章节开始,开始进行实践操作.和学习任何一门语言一样,我们今天也开始从Hello,World开始~ 对Docker不了 ...