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
*/
 

codeforces364D的更多相关文章

  1. 2018.09.14 codeforces364D(随机化算法)

    传送门 根据国家集训队2014论文集中胡泽聪的随机化算法可以通过这道题. 对于每个数,它有12" role="presentation" style="posi ...

随机推荐

  1. python — 池

    1. 池 池分为:进程池.线程池 池:预先的开启固定个数的进程数/线程数,当任务来临的时候,直接提交给已经开好的进程 / 线程,让这个进程 / 线程去执行就可以了. 池节省了进程.线程的开启.关闭.切 ...

  2. spark调优篇-Spark ON Yarn 内存管理(汇总)

    本文旨在解析 spark on Yarn 的内存管理,使得 spark 调优思路更加清晰 内存相关参数 spark 是基于内存的计算,spark 调优大部分是针对内存的,了解 spark 内存参数有也 ...

  3. VS2013 + Nunit 安装搭建

    Nunit 官方给我提供了Nunit 3的四种安装方式 第一种 通过NuGet进行Full Nunit安装 第二种 通过NuGet进行轻量级 NunitLite 安装 第三种 通过Zip 压缩包下载安 ...

  4. R-corrplot相关性绘图,只有你想不到的

    初步接触数据集,探索性分析后,经常需要做一个相关分析,得到各变量间的相关系数以及显著性水平. 本文介绍一下R-corrplot包进行相关可视化展示. 一 数据准备 载入所需的R包,利用公共数据集mtc ...

  5. wpf 判断项目中的某个窗体是否已经打开或者已经存在

    foreach (Window item in Application.Current.Windows) {     if (item is window1) return; }

  6. C# DataTable根据字段排序

    DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Age");// ...

  7. hbulider 快捷键

    跳转到行        Ctrl + G   页首        Ctrl + Home   页尾        Ctrl + End   下一个选项卡        Ctrl + Tab   上一个 ...

  8. python 获取导入模块的文件路径

    接触到项目上有人写好的模块进行了导入,想查看模块的具体内容是如何实现的,需要找到模块的源文件. 本博文介绍两种查找模块文件路径方法: 方法一: #!/usr/bin/python # -*- codi ...

  9. 利用FastReport直接生成条码

    procedure TForm1.Button1Click(Sender: TObject); var x:TfrxbarCodeView; begin x:=TfrxbarCodeView.Crea ...

  10. 一个用beego写的API项目

    beego-api 一个使用beego写的API 支持Api日志 支持Swagger注解文档 项目地址: https://github.com/eternity-wdd/beego-api 使用说明 ...