【Codeforces 364D】Ghd
给你nnn个数,求一个尽量大的数,使得数列中有超过n2\frac{n}{2}2n个数能被该数整除。
数ai≤1012a_i \leq 10^{12}ai≤1012
发现每一个数出现在能被整除的集合中的概率均≥12≥\frac{1}{2}≥21,于是考虑随机。每一次猜测一个数在这个集合中,然后计算与其它数的gcd。最后查找一个最大的出现次数超过n2\frac{n}{2}2n的gcd即可。(若res[i]∣res[j]res[i]|res[j]res[i]∣res[j]则sum[i]+=sum[j]sum[i]+=sum[j]sum[i]+=sum[j])
我们分析一下这样做的正确率。每一次选对的概率≥12≥\frac{1}{2}≥21,所以说一次选错的概率≤12\leq \frac{1}{2}≤21,即随机xxx次的成功率为1−12x\Large 1-\frac{1}{2^x}1−2x1。当xxx取约141414时,其成功率可以接受。
#include<bits/stdc++.h>#define reg register#define ll long longusing namespace std;const int mn = 1000005;ll a[mn], res[mn];int sum[mn];inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}int main(){srand(time(NULL));int n;scanf("%d", &n);for(int i = 1; i <= n; i++)scanf("%I64d", &a[i]);ll ans = 1;for(int T = 0; T < 13; T++){int pos = ((rand() << 15) + rand()) % n + 1;if(a[pos] <= ans) continue;ll maxs = 1;for(reg int i = 1; i <= n; i++)res[i] = gcd(a[i], a[pos]), maxs = max(res[i], maxs);if(maxs <= ans) continue;sort(res + 1, res + 1 + n); int cnt = 0;for(reg int i = 1; i <= n; i++){if(res[i] != res[i - 1])res[++cnt] = res[i], sum[cnt] = 0;++sum[cnt];}for(reg int i = cnt; i; i--){if(ans >= res[i]) break;int tmp = 0;for(int j = i; j <= cnt; j++)if(res[j] % res[i] == 0) tmp += sum[j];if((tmp << 1) >= n) {ans = res[i]; break;}}}printf("%I64d\n", ans);}
【Codeforces 364D】Ghd相关推荐
- 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)
题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...
- 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)
题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...
- 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)
题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...
- 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)
题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...
- 【CodeForces 997C】Sky Full of Stars(组合计数)
题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- 【codeforces 807C】Success Rate
[题目链接]:http://codeforces.com/contest/807/problem/C [题意] 给你4个数字 x y p q 要求让你求最小的非负整数b; 使得 (x+a)/(y+b) ...
- 【codeforces 766E】Mahmoud and a xor trip
[题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...
最新文章
- ItemsControl 解析
- spark MapOutputTrackerMaster
- ajax就收data的参数
- 算法 - 排序稳定性总结
- 一文带你彻底搞懂C++中一些常见指针(形如*p)的用法
- android sd卡 格式化 rom,Android开发之获取SD卡及手机ROM容量的方法
- Android开发笔记(一百一十四)发布工具
- OC之集合的创建及应用
- 25. Element nextSibling 属性
- Flutter 绝对定位 轮播图背景色
- 国内最知名的PHP开源网站管理系统,国内最强十款开源网站管理系统
- 思科FTP服务器如何传输文件,与FXP配置示例的ASA文件传输
- 转载 注解@PostConstruct与@PreDestroy讲解及实例 - 云淡风轻、仅此一抹 - 博客频道 - CSDN.NET http://blog.csdn.net/yaerfeng/art
- windows安装paddlepaddle踩坑教程
- PMBOK2004版44个过程的工具和技术的总结
- css里的deg,CSS角度单位:deg、grad、rad、turn
- 关于科傻软件的使用感受
- 前端页面与form表单提交:代码分享
- PDF复制乱码 -- 原因及解决方案
- SVM入门(八)松弛变量 1
热门文章
- 超全Python图像处理讲解!花五天才整理的!
- 吾爱破解网站访问出错
- readonly属性
- php正则判断是否为图片格式,JS 获取文件后缀,判断文件类型(比如是否为图片格式)...
- 设备日常检查(巡检)
- Fujitsu Diagnostic(硬盘坏道检测工具)v6.8绿色版
- 夜深人静写算法(十四)- 基数估计 (Cardinality Estimation)
- Java 中文按拼音进行排序
- 南京融时代 php,河西南,回不去的3万5时代
- html 文字阴影 一重投影,鼠标移入文字添加阴影 溢出的文字 添加多重颜色