Ghd

CodeForces - 364D

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.

Examples

Input
66 2 3 4 5 6

Output
3

Input
55 5 6 10 15

Output
5

题意:n个数中取一半,使得gcd最大

sol:似乎是鬼畜的随机化算法,每次随机取一个,然后计算所有数字与它的gcd,再随便判断一下个数是否满足一半就好了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{ll s=0; bool f=0; char ch=' ';while(!isdigit(ch))    {f|=(ch=='-'); ch=getchar();}while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{if(x<0) {putchar('-'); x=-x;}if(x<10) {putchar(x+'0'); return;}write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=1000005;
int n,m;
ll a[N],cnt=0,b[N],ans=0;
struct Node{ll num; int cnt;}c[N];
inline ll gcd(ll a,ll b)
{return (!b)?(a):(gcd(b,a%b));
}
inline void Solve()
{ll tmp=a[rand()%n+1];
//    cout<<"tmp="<<tmp<<endl;int i,j;for(i=1;i<=n;i++) b[i]=gcd(a[i],tmp);sort(b+1,b+n+1);cnt=0; c[++cnt].num=b[1]; c[cnt].cnt=1;for(i=2;i<=n;i++){if(b[i]!=b[i-1]){c[++cnt].num=b[i]; c[cnt].cnt=0;} c[cnt].cnt++;}for(i=1;i<=cnt;i++){int sum=0;for(j=1;j<=cnt;j++) if(c[j].num%c[i].num==0) sum+=c[j].cnt;if(sum>=m) ans=max(ans,c[i].num);}
}
int main()
{srand(20030310);int i;R(n);for(i=1;i<=n;i++) R(a[i]);m=(n+1)/2;for(i=1;i<=10;i++) Solve();Wl(ans);return 0;
}
/*
input
6
6 2 3 4 5 6
output
3input
5
5 5 6 10 15
output
5
*/

View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/11221736.html

codeforces364D相关推荐

最新文章

  1. uni-app编译配置
  2. android 打开系统相机,
  3. 学教育成空中楼阁 巨额学费背后却是失业风险
  4. 孪生神经网络_轩辕实验室:数字孪生:基于机器学习的汽车数字孪生模型
  5. linux7如何进入紧急模式,CentOS7开机进入紧急模式EmergencyMode的解决办法
  6. 第二个冲刺周期第二天
  7. 访问修饰符 c# 1613703072
  8. 剑指Offer:二进制中1的个数
  9. MTK 驱动(38)---MTK 待机问题分析
  10. redis 系列19 客户端
  11. Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D
  12. ZREVRANK key member
  13. highlighting v5.0插件使用 一(持续更新)
  14. 现代城市生态与环境学
  15. 计算机考研828是什么意思,浙江大学828计算机程序设计基础考研复习经验
  16. Delft3d GRD及ENC问题
  17. HTML5、css3、js实现3D相册
  18. 八、服务器【Ubuntu】GPU-TeslaP100部署
  19. linux管道的使用
  20. AWK详解 什么是AWK

热门文章

  1. 第 6 章 Harddisk IO
  2. 将用户添加至sudoers列表
  3. openGL 坐标系的互相转换
  4. java script 技巧
  5. 【知识强化】第二章 进程管理 2.1 进程与线程
  6. 常用的第三方模块 Pillow url
  7. 2.0 pomelo-treasure官方demo的使用
  8. C#_Math函数总结
  9. centos 7 yum 安装 python3
  10. linux 打包压缩工具