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
6
6 2 3 4 5 6
Output
3
Input
5
5 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=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m;
ll a[N],cnt=,b[N],ans=;
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+];
// cout<<"tmp="<<tmp<<endl;
int i,j;
for(i=;i<=n;i++) b[i]=gcd(a[i],tmp);
sort(b+,b+n+);
cnt=; c[++cnt].num=b[]; c[cnt].cnt=;
for(i=;i<=n;i++)
{
if(b[i]!=b[i-]){c[++cnt].num=b[i]; c[cnt].cnt=;} c[cnt].cnt++;
}
for(i=;i<=cnt;i++)
{
int sum=;
for(j=;j<=cnt;j++) if(c[j].num%c[i].num==) sum+=c[j].cnt;
if(sum>=m) ans=max(ans,c[i].num);
}
}
int main()
{
srand();
int i;
R(n);
for(i=;i<=n;i++) R(a[i]);
m=(n+)/;
for(i=;i<=;i++) Solve();
Wl(ans);
return ;
}
/*
input
6
6 2 3 4 5 6
output
3 input
5
5 5 6 10 15
output
5
*/
 

最新文章

  1. 如何比较两个SQL数据库的字段差别。
  2. CSS中!important的作用
  3. 洛谷P1889 士兵站队
  4. BlueTooth: 蓝牙基础知识进阶——链路控制操作
  5. VS2013 当前不会命中断点,还没有为该文档加载任何符号
  6. SU suacor命令学习
  7. sap mm_1
  8. MySQL 使用方法简单教程
  9. 采用Service实现本地推送通知
  10. (六)Struts2 国际化
  11. string的一些更改发
  12. ACM1174_爆头解题思路_空间三维坐标求点到直线的距离
  13. 恭喜我开通了CSDN博客
  14. DOM生成XML文档
  15. 利用jTessBoxEditor工具进行Tesseract-OCR样本训练
  16. ActiveSupport::Concern 和 gem &#39;name_of_person&#39;(300✨) 的内部运行机制分析
  17. java中的byte
  18. 跟着刚哥学Redis
  19. android 手机不能断点
  20. 【转】Luajit-2.1.0-beta1的发布和生成arm64用bytecode的解脱

热门文章

  1. MySQL8.0哪些新特性你最期待
  2. 【Trie】Nikitosh 和异或
  3. Spring集成kafka,消费者运行时内存占用会一直增长
  4. MyBatis 示例-动态 SQL
  5. 泛型约束:接口约束、new()构造函数约束、组合约束(即多种约束合并)
  6. Ocelot + Consul的demo
  7. 关于如何查看 MySQL 信息、查看Oracle 版本
  8. TouchGFX版本
  9. javascript typeof instanceof
  10. 【收藏】linux快速查找文件的技巧