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. nginx日志模块、事件模块

    日志模块 1.access_log指令 语法: access_log path [format [buffer=size [flush=time]]]; access_log logs/access. ...

  2. C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否

    原文:C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否 叶子 1 微信扫一扫 新浪微博 Twitter Facebook C# Redis分布式锁的应用 c#redis分 ...

  3. javaScript节流与防抖

    一.节流(throttle) 用来实现阻止在短时间内重复多次触发同一个函数.主要用途:防止使用脚本循环触发网络请求的函数的恶意行为,确保请求的真实性(当然也包括其他阻止高频触发行为的应用): 实现原理 ...

  4. 小程序wxs是作用

    wxs weixin script,小程序的脚本语言:可以结合wxml构建页面结构: 说白了 就是在小程序里面写函数表达式的地方: wxml里面直接使用wxs,有错误再次刷新就能解决 <wxs ...

  5. vuex数据传递的流程

    当组件修改数据的时候必须通过store.dispacth来调用actions中的方法. 当actions中的方法被触发的时候通过调用commit的方法来触发mutations里面的方法 mutatio ...

  6. 2.1 使用JAXP 对 xml文档进行DOM解析

    //使用 jaxp 对xml文档进行dom解析 public class Demo2 { //必要步骤 @Test public void test() throws Exception { //1. ...

  7. [转]TCP的三次握手与四次挥手

    TCP的概述 TCP把连接作为最基本的对象,每一条TCP连接都有两个端点,这种断点我们叫作套接字(socket),它的定义为端口号拼接到IP地址即构成了套接字,例如,若IP地址为192.3.4.16 ...

  8. 【51nod2026】Gcd and Lcm(杜教筛)

    题目传送门:51nod 我们可以先观察一下这个$f(x)=\sum_{d|x}\mu(d) \cdot d$. 首先它是个积性函数,并且$f(p^k)=1-p \ (k>0)$,这说明函数$f( ...

  9. Delphi 特性限定符

  10. Centos7.4安装RabbitMQ

    1.1 安装RabbitMQ 1.1.1 系统环境 [root@rabbitmq ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 ( ...